33#include < Http.h>
44#include < DHT.h>
55#include < Battery.h>
6- // #include <NewPing.h>
7-
8- // #define TRIGGER_PIN 5
9- // #define ECHO_PIN 6
10- // #define MAX_DISTANCE 100
11- #define RX_PIN 11
12- #define TX_PIN 12
13- #define RST_PIN 10
14- #define MOISTURE_PIN 7
15- #define TEMPERATURE_HUMIDITY_PIN 13
6+ #include < LowPower.h>
7+ #include < NewPing.h>
8+
9+ #define TRIGGER_PIN 7
10+ #define ECHO_PIN 8
11+ #define MAX_DISTANCE 100
12+ #define RST_PIN 11
13+ #define RX_PIN 10
14+ #define TX_PIN 9
15+ #define MOISTURE_PIN 1
16+ #define TEMPERATURE_HUMIDITY_PIN 6
1617#define DHTTYPE DHT11
1718#define OPEN_VALVE_PIN 5
18- #define LIPO_BATTERY_PIN 5
19- #define LITIO_BATTERY_PIN 6
20- #define ENDPOINT " https://your.api"
21- #define BODY_FORMAT " {\" w\" :{\" m\" : %d, \" t\" : %d, \" h\" : %d, \" mv\" : %d, \" sv\" : %d}}"
22- // #define BEARER "gprs-service.com"
19+ #define OPEN_VALVE_VIN_PIN 4
20+ #define LITIO_BATTERY_PIN 0
2321// #define BEARER "movistar.es"
2422#define BEARER " gprs-service.com"
23+ #define MAX_VOLTAGE 4200
24+ #define MIN_VOLTAGE 3500
2525#define DEBUG TRUE
2626
27- unsigned long waitForRunTime = 1 ;
28- const HTTP http (9600 , RX_PIN, TX_PIN, RST_PIN, DEBUG);
29- const DHT dht (TEMPERATURE_HUMIDITY_PIN, DHTTYPE);
30- Battery liPoBattery (3300 , 3800 , LIPO_BATTERY_PIN);
31- Battery litioBattery (3300 , 3800 , LITIO_BATTERY_PIN);
32- // const NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
27+ const char ENDPOINT[] = {" https://your.api" };
28+ const char BODY_FORMAT[] PROGMEM = {" {\" w\" :{\" m\" : %d, \" t\" : %d, \" h\" : %d, \" mv\" : %d, \" sv\" : %d, \" v\" : %d}}" };
29+
30+ const PROGMEM HTTP http (9600 , RX_PIN, TX_PIN, RST_PIN, DEBUG);
31+ const PROGMEM DHT dht (TEMPERATURE_HUMIDITY_PIN, DHTTYPE);
32+ const PROGMEM Battery litioBattery (MIN_VOLTAGE, MAX_VOLTAGE, LITIO_BATTERY_PIN);
33+ const PROGMEM NewPing sonar (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
34+
35+ unsigned long timeToSleep = 0 ;
36+ unsigned long starToSleepTime = 0 ;
3337
3438/*
3539 * functions
3640 */
41+ void openValve (){
42+ Serial.print (F (" Open valve" ));
43+
44+ pinMode (OPEN_VALVE_VIN_PIN, OUTPUT);
45+ pinMode (OPEN_VALVE_PIN, OUTPUT);
46+ digitalWrite (OPEN_VALVE_PIN, HIGH);
47+ delay (15000 );
48+ pinMode (OPEN_VALVE_VIN_PIN, INPUT);
49+ pinMode (OPEN_VALVE_PIN, INPUT);
50+ }
3751
38- void openValveFor (unsigned long milliseconds){
39- Serial.print (F (" Open valve for: " ));
40- Serial.println (milliseconds);
41- digitalWrite (OPEN_VALVE_PIN, LOW);
42- delay (10000 + milliseconds);
52+ void closeValve (){
4353 Serial.println (F (" Close Valve" ));
44- digitalWrite (OPEN_VALVE_PIN, HIGH);
54+
55+ pinMode (OPEN_VALVE_VIN_PIN, OUTPUT);
56+ pinMode (OPEN_VALVE_PIN, OUTPUT);
57+ digitalWrite (OPEN_VALVE_PIN, LOW);
58+ delay (15000 );
59+ pinMode (OPEN_VALVE_VIN_PIN, INPUT);
60+ pinMode (OPEN_VALVE_PIN, INPUT);
61+ }
62+
63+ void openValveFor (unsigned long milliseconds){
64+ openValve ();
65+ delay (milliseconds);
66+ closeValve ();
4567}
4668
4769unsigned int readMoisture () {
@@ -52,79 +74,118 @@ unsigned int readMoisture() {
5274 return total/100 ;
5375}
5476
55- void manageGarden (){
56- int humidity = dht.readHumidity ();
77+ bool rightVoltage (unsigned int voltage){
78+ return voltage >= MIN_VOLTAGE;
79+ }
80+
81+ bool shouldSleep (){
82+ return millis () - starToSleepTime <= timeToSleep;
83+ }
84+
85+ // int availableMemory()
86+ // {
87+ // int size = 1024;
88+ // byte *buf;
89+ //
90+ // while ((buf = (byte *) malloc(--size)) == NULL)
91+ // ;
92+ //
93+ // free(buf);
94+ //
95+ // return size;
96+ // }
97+
98+ void manageGarden (){
99+ unsigned int humidity = dht.readHumidity ();
57100 Serial.print (F (" Humidity % " ));
58101 Serial.println (humidity);
59- int temperature = dht.readTemperature ();
60- Serial.print (F (" Temperature " ));
102+
103+ unsigned int temperature = dht.readTemperature ();
104+ Serial.print (F (" Temperature Cº " ));
61105 Serial.println (temperature);
106+
62107 unsigned int moisture = readMoisture ();
63108 Serial.print (F (" Moisture " ));
64109 Serial.println (moisture);
110+
65111 unsigned int litioBatteryVoltage = litioBattery.voltage ();
66112 Serial.print (F (" Litio voltage " ));
67113 Serial.println (litioBatteryVoltage);
68- unsigned int liPoBatteryVoltage = liPoBattery.voltage ();
114+
115+ Serial.print (F (" Water tank distance cm: " ));
116+ unsigned int distance = sonar.ping_cm ();
117+ Serial.println (distance);
118+
119+ http.wakeUp ();
120+ unsigned int liPoBatteryVoltage = http.readVoltage ();
69121 Serial.print (F (" LiPo voltage " ));
70122 Serial.println (liPoBatteryVoltage);
71- // Serial.print(F("Water tank distance cm: "));
72- // float distance = sonar.ping_cm();
73- // Serial.println(distance);
74-
75- char response[32 ];
76- char body[90 ];
77- Result result;
78- sprintf (body, BODY_FORMAT, moisture, temperature, humidity, litioBatteryVoltage, liPoBatteryVoltage);
79- Serial.println (body);
80123
124+ if (rightVoltage (liPoBatteryVoltage)){
125+ char response[32 ];
126+ char body[70 ];
127+ Result result;
128+ sprintf_P (body, BODY_FORMAT, moisture, temperature, humidity, litioBatteryVoltage, liPoBatteryVoltage, distance);
129+ Serial.println (body);
81130
82- http.configureBearer (BEARER);
83- result = http.connect ();
84- result = http.post (ENDPOINT, body, response);
85- http.disconnect ();
86-
87- if (result == SUCCESS) {
88- Serial.println (response);
89- StaticJsonBuffer<32 > jsonBuffer;
90- JsonObject& root = jsonBuffer.parseObject (response);
131+ http.configureBearer (BEARER);
132+ result = http.connect ();
133+ result = http.post (ENDPOINT, body, response);
134+ http.disconnect ();
135+ http.sleep ();
91136
92- if (strcmp (root[" action" ], " open-valve" ) == 0 ){
93- openValveFor (root[" value" ]);
94- // Delay 1 minute to overpass the irrigation time
95- delay (60000 );
96- }
97- else {
98- delay (root[" value" ]);
137+ if (result == SUCCESS) {
138+ Serial.println (response);
139+ StaticJsonBuffer<32 > jsonBuffer;
140+ JsonObject& root = jsonBuffer.parseObject (response);
141+
142+ if (strcmp (root[" action" ], " open-valve" ) == 0 ){
143+ // Delay 1 minute to overpass the irrigation time
144+ unsigned long sleepTime = root[" value" ];
145+ openValveFor (sleepTime + 60000 );
146+ }
147+ else {
148+ timeToSleep = root[" value" ];
149+ // 26848 is the offset the arduino millis have per hour
150+ timeToSleep -= 26848 * timeToSleep/(60 *60 *1000 );
151+ starToSleepTime = millis ();
152+ }
99153 }
100154 }
155+ else {
156+ http.sleep ();
157+ Serial.println (F (" Low voltage detected. I can not work property, charge me!" ));
158+ delay (10000 );
159+ }
101160}
102161
103- /*
104- * the setup routine runs once when you press reset:
105- */
106- void setup () {
162+ void initialize (){
107163 if (DEBUG){
108164 Serial.begin (9600 );
109165 while (!Serial);
110166 Serial.println (F (" Starting!" ));
111167 }
112168
113- pinMode (MOISTURE_PIN, INPUT );
114- pinMode (OPEN_VALVE_PIN, OUTPUT);
115- pinMode (OPEN_VALVE_PIN, HIGH );
169+ delay ( 1000 );
170+
171+ openValveFor ( 0 );
116172
117- openValveFor (1000 );
118173 dht.begin ();
119- liPoBattery.begin (5000 , 1.0 );
120- litioBattery.begin (5000 , 1.0 );
174+ litioBattery.begin (5000 , 1.02 );
175+ }
176+
177+ /*
178+ * the setup routine runs once when you press reset:
179+ */
180+ void setup () {
181+ initialize ();
121182}
122183
123184/*
124185 * the loop routine runs over and over again forever:
125186 */
126- void loop () {
127- http. wakeUp ();
128- manageGarden ();
129- http. sleep ();
187+ void loop (){
188+ if (! shouldSleep ()){
189+ manageGarden ();
190+ }
130191}
0 commit comments