Skip to content

Commit a865b72

Browse files
committed
Update examples and Readme
1 parent e1421d1 commit a865b72

3 files changed

Lines changed: 116 additions & 3 deletions

File tree

HttpExample/HttpExample.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
unsigned long lastRunTime = 0;
55
unsigned long waitForRunTime = 0;
6-
HTTP http;
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);
711

812
// the setup routine runs once when you press reset:
913
void setup() {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

Readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Download the library and then import it.
1414
Here's some code to perform a GET request! :+1:
1515

1616
``` c++
17-
HTTP http;
17+
unsigned int RX_PIN = 7;
18+
unsigned int TX_PIN = 8;
19+
unsigned int RST_PIN = 12;
20+
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN);
1821
http.configureBearer("movistar.es");
1922
http.connect();
2023

@@ -31,7 +34,10 @@ http.disconnect();
3134
Here's some code to perform a POST request! :+1:
3235
3336
``` c++
34-
HTTP http;
37+
unsigned int RX_PIN = 7;
38+
unsigned int TX_PIN = 8;
39+
unsigned int RST_PIN = 12;
40+
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN);
3541
http.configureBearer("movistar.es");
3642
http.connect();
3743

0 commit comments

Comments
 (0)