Skip to content

Commit 4b532dc

Browse files
committed
Improve library latency by removing unnecessary delays and flushing the buffer
1 parent 5bb9ed2 commit 4b532dc

3 files changed

Lines changed: 108 additions & 76 deletions

File tree

Http.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
134134
}
135135

136136
purgeSerial();
137-
delay(500);
138137
sendCmd(body);
139138

140139
if (sendCmdAndWaitForResp(HTTP_POST, HTTP_2XX, delayToDownload) == TRUE) {
Lines changed: 103 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,130 @@
1-
#include <LowPower.h>
1+
22
#include <ArduinoJson.h>
33
#include <Http.h>
4+
#include <DHT.h>
5+
#include <Battery.h>
6+
//#include <NewPing.h>
47

5-
unsigned long lastRunTime = 0;
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);
12-
13-
/*
14-
* the setup routine runs once when you press reset:
15-
*/
16-
void setup() {
17-
Serial.begin(9600);
18-
while(!Serial);
19-
Serial.println("Starting!");
20-
}
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
16+
#define DHTTYPE DHT11
17+
#define OPEN_VALVE_PIN 5
18+
#define LIPO_BATTERY_PIN 5
19+
#define LITIO_BATTERY_PIN 6
20+
#define ENDPOINT "https://your-endpoint"
21+
#define BODY_FORMAT "{\"w\":{\"m\": %d, \"t\": %d, \"h\": %d, \"mv\": %d, \"sv\": %d}}"
22+
//#define BEARER "gprs-service.com"
23+
//#define BEARER "movistar.es"
24+
#define BEARER "gprs-service.com"
25+
#define DEBUG 0
2126

22-
/*
23-
* the loop routine runs over and over again forever:
24-
*/
25-
void loop() {
26-
if (shouldTrackTimeEntry()){
27-
http.wakeUp();
28-
trackTimeEntry();
29-
}
30-
else {
31-
http.sleep();
32-
}
33-
}
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);
3433

3534
/*
3635
* functions
3736
*/
38-
void print(const __FlashStringHelper *message, int code = -1){
39-
if (code != -1){
40-
Serial.print(message);
41-
Serial.println(code);
42-
}
43-
else {
44-
Serial.println(message);
45-
}
46-
}
4737

48-
unsigned long currentMillis(){
49-
return millis() + millisOffset;
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);
43+
Serial.println(F("Close Valve"));
44+
digitalWrite(OPEN_VALVE_PIN, HIGH);
5045
}
5146

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-
58-
bool shouldTrackTimeEntry(){
59-
/*
60-
* This calculation uses the max value the unsigned long can store as key. Remember when a negative number
61-
* is assigned or the maximun is exceeded, then the module is applied to that value.
62-
*/
63-
sleepEightSeconds();
64-
unsigned long elapsedTime = currentMillis() - lastRunTime;
65-
print(F("Elapsed time: "), elapsedTime);
66-
return elapsedTime >= waitForRunTime;
47+
unsigned int readMoisture() {
48+
unsigned long total = 0;
49+
for (unsigned int i=0; i<100; ++i){
50+
total += analogRead(MOISTURE_PIN);
51+
}
52+
return total/100;
6753
}
6854

69-
void trackTimeEntry(){
55+
void manageGarden(){
56+
int humidity = dht.readHumidity();
57+
Serial.print(F("Humidity % "));
58+
Serial.println(humidity);
59+
int temperature = dht.readTemperature();
60+
Serial.print(F("Temperature "));
61+
Serial.println(temperature);
62+
unsigned int moisture = readMoisture();
63+
Serial.print(F("Moisture "));
64+
Serial.println(moisture);
65+
unsigned int litioBatteryVoltage = litioBattery.voltage();
66+
Serial.print(F("Litio voltage "));
67+
Serial.println(litioBatteryVoltage);
68+
unsigned int liPoBatteryVoltage = liPoBattery.voltage();
69+
Serial.print(F("LiPo voltage "));
70+
Serial.println(liPoBatteryVoltage);
71+
//Serial.print(F("Water tank distance cm: "));
72+
//float distance = sonar.ping_cm();
73+
//Serial.println(distance);
7074

7175
char response[32];
7276
char body[90];
7377
Result result;
78+
sprintf(body, BODY_FORMAT, moisture, temperature, humidity, litioBatteryVoltage, liPoBatteryVoltage);
79+
Serial.println(body);
7480

75-
print(F("Cofigure bearer: "), http.configureBearer("your.apn"));
76-
result = http.connect();
77-
print(F("HTTP connect: "), result);
78-
79-
unsigned int moisture = random(1023);
80-
char voltage[6];
81-
http.readVoltage(voltage);
8281

83-
sprintf(body, "[{\"weatherEntry\":[{\"m\": %d, \"cv\": %s}], \"n\": \"Arduino\"}]", moisture, voltage);
84-
Serial.println(body);
82+
http.configureBearer(BEARER);
83+
result = http.connect();
84+
result = http.post(ENDPOINT, body, response);
85+
http.disconnect();
8586

86-
result = http.post("your.api", body, response);
87-
print(F("HTTP POST: "), result);
8887
if (result == SUCCESS) {
8988
Serial.println(response);
9089
StaticJsonBuffer<32> jsonBuffer;
9190
JsonObject& root = jsonBuffer.parseObject(response);
92-
lastRunTime = currentMillis();
93-
waitForRunTime = root["waitForRunTime"];
9491

95-
print(F("Last run time: "), lastRunTime);
96-
print(F("Next post in: "), waitForRunTime);
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"]);
99+
}
100+
}
101+
}
102+
103+
/*
104+
* the setup routine runs once when you press reset:
105+
*/
106+
void setup() {
107+
if (DEBUG){
108+
Serial.begin(9600);
109+
while(!Serial);
110+
Serial.println(F("Starting!"));
97111
}
98112

99-
print(F("HTTP disconnect: "), http.disconnect());
100-
}
113+
pinMode(MOISTURE_PIN, INPUT);
114+
pinMode(OPEN_VALVE_PIN, OUTPUT);
115+
pinMode(OPEN_VALVE_PIN, HIGH);
116+
117+
openValveFor(1000);
118+
dht.begin();
119+
liPoBattery.begin(5000, 1.0);
120+
litioBattery.begin(5000, 1.0);
121+
}
122+
123+
/*
124+
* the loop routine runs over and over again forever:
125+
*/
126+
void loop() {
127+
http.wakeUp();
128+
manageGarden();
129+
http.sleep();
130+
}

Sim800.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ int SIM800::preInit(void)
4040
delay(3000);
4141

4242
purgeSerial();
43+
serialSIM800.flush();
4344

4445
return TRUE;
4546
}
@@ -68,7 +69,7 @@ int SIM800::readBuffer(char *buffer, int count, unsigned int timeOut)
6869
break;
6970
}
7071
}
71-
delay(500);
72+
7273
while(serialSIM800.available()) {
7374
serialSIM800.read();
7475
}
@@ -85,7 +86,10 @@ void SIM800::cleanBuffer(char *buffer, int count)
8586
void SIM800::sendCmd(const char* cmd)
8687
{
8788
serialSIM800.listen();
89+
serialSIM800.flush();
90+
delay(500);
8891
serialSIM800.write(cmd);
92+
serialSIM800.flush();
8993
}
9094

9195
int SIM800::sendATTest(void)
@@ -128,7 +132,6 @@ void SIM800::sendEndMark(void)
128132

129133
int SIM800::sendCmdAndWaitForResp(const char* cmd, const char *resp, unsigned timeout)
130134
{
131-
delay(1000);
132135
sendCmd(cmd);
133136
return waitForResp(resp,timeout);
134137
}

0 commit comments

Comments
 (0)