|
| 1 | +#include <LowPower.h> |
1 | 2 | #include <ArduinoJson.h> |
2 | 3 | #include <Http.h> |
3 | 4 |
|
4 | 5 | unsigned long lastRunTime = 0; |
5 | 6 | unsigned long waitForRunTime = 0; |
| 7 | +unsigned long millisOffset = 0; |
| 8 | +unsigned int RX_PIN = 11; |
| 9 | +unsigned int TX_PIN = 12; |
| 10 | +unsigned int RST_PIN = 10; |
| 11 | +HTTP http(9600, RX_PIN, TX_PIN, RST_PIN, true); |
6 | 12 |
|
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 | 13 | /* |
12 | 14 | * the setup routine runs once when you press reset: |
13 | 15 | */ |
@@ -43,56 +45,51 @@ void print(const __FlashStringHelper *message, int code = -1){ |
43 | 45 | } |
44 | 46 | } |
45 | 47 |
|
| 48 | +unsigned long currentMillis(){ |
| 49 | + return millis() + millisOffset; |
| 50 | +} |
| 51 | + |
| 52 | +void sleepEightSeconds(){ |
| 53 | + LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); |
| 54 | + /* The 8000 is 8 seconds, the time the clock has been sleeping */ |
| 55 | + millisOffset += 8000; |
| 56 | +} |
| 57 | + |
46 | 58 | bool shouldTrackTimeEntry(){ |
47 | 59 | /* |
48 | 60 | * This calculation uses the max value the unsigned long can store as key. Remember when a negative number |
49 | 61 | * is assigned or the maximun is exceeded, then the module is applied to that value. |
50 | | - */ |
51 | | - unsigned long elapsedTime = millis() - lastRunTime; |
| 62 | + */ |
| 63 | + sleepEightSeconds(); |
| 64 | + unsigned long elapsedTime = currentMillis() - lastRunTime; |
52 | 65 | print(F("Elapsed time: "), elapsedTime); |
53 | 66 | return elapsedTime >= waitForRunTime; |
54 | 67 | } |
55 | 68 |
|
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 | 69 | void trackTimeEntry(){ |
74 | 70 |
|
75 | 71 | char response[32]; |
76 | 72 | char body[90]; |
77 | 73 | Result result; |
78 | 74 |
|
79 | | - print(F("Cofigure bearer: "), http.configureBearer("movistar.es")); |
| 75 | + print(F("Cofigure bearer: "), http.configureBearer("your.apn")); |
80 | 76 | result = http.connect(); |
81 | 77 | print(F("HTTP connect: "), result); |
82 | 78 |
|
83 | 79 | unsigned int moisture = random(1023); |
84 | 80 | char voltage[6]; |
85 | | - readVoltage(voltage); |
86 | | - |
87 | | - sprintf(body, "[{\"weatherEntries\":[{\"moisture\": %d, \"currentVoltage\": %s}], \"name\": \"Arduino\"}]", moisture, voltage); |
| 81 | + http.readVoltage(voltage); |
| 82 | + |
| 83 | + sprintf(body, "[{\"weatherEntry\":[{\"m\": %d, \"cv\": %s}], \"n\": \"Arduino\"}]", moisture, voltage); |
88 | 84 | Serial.println(body); |
89 | | - result = http.post("your.domain/api/devices", body, response); |
| 85 | + |
| 86 | + result = http.post("your.api", body, response); |
90 | 87 | print(F("HTTP POST: "), result); |
91 | 88 | if (result == SUCCESS) { |
92 | 89 | Serial.println(response); |
93 | 90 | StaticJsonBuffer<32> jsonBuffer; |
94 | 91 | JsonObject& root = jsonBuffer.parseObject(response); |
95 | | - lastRunTime = millis(); |
| 92 | + lastRunTime = currentMillis(); |
96 | 93 | waitForRunTime = root["waitForRunTime"]; |
97 | 94 |
|
98 | 95 | print(F("Last run time: "), lastRunTime); |
|
0 commit comments