1+ #include < ArduinoJson.h>
2+ #include < Http.h>
3+
4+ unsigned long lastRunTime = 0 ;
5+ unsigned long waitForRunTime = 0 ;
6+
7+ unsigned int RX_PIN = 7 ;
8+ unsigned int TX_PIN = 8 ;
9+ unsigned int RST_PIN = 12 ;
10+ HTTP http (9600 , RX_PIN, TX_PIN, RST_PIN);
11+ /*
12+ * the setup routine runs once when you press reset:
13+ */
14+ void setup () {
15+ Serial.begin (9600 );
16+ while (!Serial);
17+ Serial.println (" Starting!" );
18+ }
19+
20+ /*
21+ * the loop routine runs over and over again forever:
22+ */
23+ void loop () {
24+ if (shouldTrackTimeEntry ()){
25+ http.wakeUp ();
26+ trackTimeEntry ();
27+ }
28+ else {
29+ http.sleep ();
30+ }
31+ }
32+
33+ /*
34+ * functions
35+ */
36+ void print (const __FlashStringHelper *message, int code = -1 ){
37+ if (code != -1 ){
38+ Serial.print (message);
39+ Serial.println (code);
40+ }
41+ else {
42+ Serial.println (message);
43+ }
44+ }
45+
46+ bool shouldTrackTimeEntry (){
47+ /*
48+ * This calculation uses the max value the unsigned long can store as key. Remember when a negative number
49+ * is assigned or the maximun is exceeded, then the module is applied to that value.
50+ */
51+ unsigned long elapsedTime = millis () - lastRunTime;
52+ print (F (" Elapsed time: " ), elapsedTime);
53+ return elapsedTime >= waitForRunTime;
54+ }
55+
56+ void readVoltage (char *voltageBuffer){
57+ /*
58+ * We are using a voltage divider, so the voltage in each resistence is calculated by:
59+ * Vn = (Rn / R1 + .. + Rn) * Vt. In our example: V = (270/270 + 270) * 5V = 2.5v
60+ */
61+ int input = analogRead (0 ); // Read the input pin. Possible values are betwen 0 (0v) to 1023 (5v)
62+ float maximumBatterVoltage = 5.0 ;
63+ float maximumAnalogicIput = 1023.0 ;
64+ float dividedVoltage = maximumBatterVoltage * input / maximumAnalogicIput;
65+
66+ float voltage = dividedVoltage * 2 ; // We use 2 because of our two resistences are equal
67+ /*
68+ * As the Arduino implementaion of sprintf does not support float values we need to convert it to char
69+ */
70+ dtostrf (voltage, 4 , 2 , voltageBuffer);
71+ }
72+
73+ void trackTimeEntry (){
74+
75+ char response[32 ];
76+ char body[90 ];
77+ Result result;
78+
79+ print (F (" Cofigure bearer: " ), http.configureBearer (" movistar.es" ));
80+ result = http.connect ();
81+ print (F (" HTTP connect: " ), result);
82+
83+ unsigned int moisture = random (1023 );
84+ char voltage[6 ];
85+ readVoltage (voltage);
86+
87+ sprintf (body, " [{\" weatherEntries\" :[{\" moisture\" : %d, \" currentVoltage\" : %s}], \" name\" : \" Arduino\" }]" , moisture, voltage);
88+ Serial.println (body);
89+ result = http.post (" smartgarden.herokuapp.com/api/devices" , body, response);
90+ print (F (" HTTP POST: " ), result);
91+ if (result == SUCCESS) {
92+ Serial.println (response);
93+ StaticJsonBuffer<32 > jsonBuffer;
94+ JsonObject& root = jsonBuffer.parseObject (response);
95+ lastRunTime = millis ();
96+ waitForRunTime = root[" waitForRunTime" ];
97+
98+ print (F (" Last run time: " ), lastRunTime);
99+ print (F (" Next post in: " ), waitForRunTime);
100+ }
101+
102+ print (F (" HTTP disconnect: " ), http.disconnect ());
103+ }
0 commit comments