Skip to content

Commit 1214483

Browse files
committed
Add readme and examples
1 parent 21069f4 commit 1214483

6 files changed

Lines changed: 110 additions & 99 deletions

File tree

Readme.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11

22
# Arduino SIM800L library
3-
A smart HTTP library based on Seeeduino that implements the AT HTTP commands to perform GET and POST requests to a JSON API.
3+
A smart HTTP library based on Seeeduino that implements the AT HTTP commands to perform GET and POST requests to a JSON API as well as FTP uploads.
44

55
## Support
66
* Your board has to support the standard SoftwareSerial library. It doesn't work with HardwareSerial based boards for the moment.
77
* The API response format has to be a valid JSON.
88
* The library has been tested against Arduino Uno and Arduino Nano.
99

1010
## Instalation
11-
Download the library and then import it.
11+
Download the ZIP library and then import it: Sketch -> Include Library -> Add .ZIP Library ...
1212

1313
## Quick start!
1414

15-
Here's some code to perform a GET request! :+1:
15+
How to do a GET request! :+1:
1616

1717
``` c++
1818
unsigned int RX_PIN = 7;
1919
unsigned int TX_PIN = 8;
2020
unsigned int RST_PIN = 12;
21+
22+
const char BEARER[] PROGMEM = "gprs-service.com";
23+
2124
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN);
22-
http.configureBearer("your.mobile.service.provider.apn");
23-
http.connect();
25+
http.connect("your.mobile.service.provider.apn");
2426

2527
char response[256];
2628
Result result = http.get("your.api.com", response);
@@ -31,15 +33,16 @@ http.disconnect();
3133

3234
```
3335
34-
Here's some code to perform a POST request! :+1:
36+
How to do a POST request! :+1:
3537
3638
``` c++
3739
unsigned int RX_PIN = 7;
3840
unsigned int TX_PIN = 8;
3941
unsigned int RST_PIN = 12;
42+
const char BEARER[] PROGMEM = "gprs-service.com";
43+
4044
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN);
41-
http.configureBearer("your.mobile.service.provider.apn");
42-
http.connect();
45+
http.connect(BEARER);
4346
4447
char response[256];
4548
Result result = http.post("your.api.com", "{\"date\":\"12345678\"}", response);
@@ -49,10 +52,27 @@ Serial.println(response);
4952
http.disconnect();
5053
```
5154

52-
I suggest the [ArduinoJSON](https://github.com/bblanchon/ArduinoJson) library for parsing the JSON response, then you can play with the values easily.
55+
How to do a FTP upload! :+1:
5356

57+
``` c++
58+
unsigned int RX_PIN = 7;
59+
unsigned int TX_PIN = 8;
60+
unsigned int RST_PIN = 12;
61+
const char BEARER[] PROGMEM = "gprs-service.com";
62+
const char FTP_SERVER[] PROGMEM = "ftp.server";
63+
const char FTP_USER[] PROGMEM = "user";
64+
const char FTP_PASS[] PROGMEM = "pass";
65+
66+
FTP ftp(9600, RX_PIN, TX_PIN, RST_PIN);
67+
ftp.putBegin(BEARER, "example.txt", FTP_SERVER, FTP_USER, FTP_PASS);
68+
ftp.putWrite("hello!", sizeof("hello!"));
69+
ftp.putEnd();
70+
71+
```
72+
73+
I suggest the [ArduinoJSON](https://github.com/bblanchon/ArduinoJson) library for parsing the JSON response, then you can play with the values easily.
5474
55-
## How it works?
75+
## HTTP. How it works?
5676
In order to perform a request, the library follows these steps:
5777
5878
##### Configure Bearer:

examples/HttpExample.ino

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,64 @@
11
#include <ArduinoJson.h>
22
#include <Http.h>
33

4-
unsigned long lastRunTime = 0;
5-
unsigned long waitForRunTime = 0;
4+
#define RST_PIN 8
5+
#define RX_PIN 10
6+
#define TX_PIN 9
67

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);
8+
const char BEARER[] PROGMEM = "your.mobile.service.provider.apn";
119

1210
// the setup routine runs once when you press reset:
13-
void setup() {
11+
void setup()
12+
{
1413
Serial.begin(9600);
15-
while(!Serial);
14+
while (!Serial)
15+
;
1616
Serial.println("Starting!");
1717
}
1818

1919
// the loop routine runs over and over again forever:
20-
void loop() {
21-
if (shouldTrackTimeEntry()) trackTimeEntry();
22-
}
23-
24-
// functions
25-
void print(const __FlashStringHelper *message, int code = -1){
26-
if (code != -1){
27-
Serial.print(message);
28-
Serial.println(code);
29-
}
30-
else {
31-
Serial.println(message);
32-
}
33-
}
20+
void loop()
21+
{
22+
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN);
3423

35-
bool shouldTrackTimeEntry(){
36-
// This calculation uses the max value the unsigned long can store as key. Remember when a negative number
37-
// is assigned or the maximun is exceeded, then the module is applied to that value.
38-
unsigned long elapsedTime = millis() - lastRunTime;
39-
print(F("Elapsed time: "), elapsedTime);
40-
return elapsedTime >= waitForRunTime;
41-
}
42-
43-
void trackTimeEntry(){
44-
4524
char response[32];
4625
char body[90];
4726
Result result;
4827

49-
print(F("Cofigure bearer: "), http.configureBearer("movistar.es"));
50-
result = http.connect();
51-
print(F("HTTP connect: "), result);
28+
// Notice the bearer must be a pointer to the PROGMEM
29+
result = http.connect(BEARER);
30+
Serial.print(F("HTTP connect: "));
31+
Serial.println(result);
5232

53-
sprintf(body, "{\"name\": \"%s\"}", "Arduino");
54-
result = http.post("your.domain/api/devices", body, response);
55-
print(F("HTTP POST: "), result);
56-
if (result == SUCCESS) {
33+
sprintf(body, "{\"title\": \"%s\", \"body\": \"%s\", \"user_id\": \"%d\"}", "Arduino", "Test", 1);
34+
result = http.post("https://your.api", body, response);
35+
Serial.print(F("HTTP POST: "));
36+
Serial.println(result);
37+
if (result == SUCCESS)
38+
{
5739
Serial.println(response);
58-
StaticJsonBuffer<32> jsonBuffer;
59-
JsonObject& root = jsonBuffer.parseObject(response);
60-
lastRunTime = millis();
61-
waitForRunTime = root["waitForRunTime"];
62-
63-
print(F("Last run time: "), lastRunTime);
64-
print(F("Next post in: "), waitForRunTime);
40+
StaticJsonBuffer<64> jsonBuffer;
41+
JsonObject &root = jsonBuffer.parseObject(response);
42+
43+
const char *id = root[F("id")];
44+
Serial.print(F("ID: "));
45+
Serial.println(id);
6546
}
6647

67-
result = http.get("your.domain/api/timing", response);
68-
print(F("HTTP GET: "), result);
69-
if (result == SUCCESS) {
48+
result = http.get("https://your.api", response);
49+
Serial.print(F("HTTP GET: "));
50+
Serial.println(result);
51+
if (result == SUCCESS)
52+
{
7053
Serial.println(response);
71-
StaticJsonBuffer<32> jsonBuffer;
72-
JsonObject& root = jsonBuffer.parseObject(response);
73-
lastRunTime = millis();
74-
waitForRunTime = root["waitForRunTime"];
75-
76-
print(F("Last run time: "), lastRunTime);
77-
print(F("Next post in: "), waitForRunTime);
54+
StaticJsonBuffer<64> jsonBuffer;
55+
JsonObject &root = jsonBuffer.parseObject(response);
56+
57+
const char *id = root[F("id")];
58+
Serial.print(F("ID: "));
59+
Serial.println(id);
7860
}
79-
80-
print(F("HTTP disconnect: "), http.disconnect());
61+
62+
Serial.print(F("HTTP disconnect: "));
63+
Serial.print(http.disconnect());
8164
}

examples/WeatherStationExample.ino

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
#include <ArduinoJson.h>
2-
#include <Http.h>
3-
#include <Ftp.h>
42
#include <LowPower.h>
53
#include <SD.h>
64

5+
#include <Http.h>
6+
#include <Ftp.h>
7+
78
#include "examples/utils/logger.h"
89
#include "examples/utils/valve.h"
910
#include "examples/utils/sensor.h"
1011
#include "examples/utils/arducam.h"
1112

1213
#define SD_CS_PIN 3
13-
1414
#define RST_PIN 8
1515
#define RX_PIN 10
1616
#define TX_PIN 9
17-
#define DEBUG true
1817

1918
#define BAUD_RATE 19200
2019
#define MAX_RETRIES 10
2120

2221
#define CLOSE_VALVE_STATE 0
2322
#define OPEN_VALVE_STATE 1
2423

25-
const char BEARER[] PROGMEM = "";
26-
const char FTP_SERVER[] PROGMEM = "";
27-
const char FTP_USER[] PROGMEM = "";
28-
const char FTP_PASS[] PROGMEM = "";
24+
const char BEARER[] PROGMEM = "your.bearer";
25+
const char FTP_SERVER[] PROGMEM = "ftp.server";
26+
const char FTP_USER[] PROGMEM = "user";
27+
const char FTP_PASS[] PROGMEM = "pass";
2928
const char BODY_FORMAT[] PROGMEM = "{\"w\":{\"m\":%d,\"t\":%d,\"h\":%d,\"mv\":%d,\"sv\":%d,\"st\":%d}}";
30-
const char ENDPOINT[] PROGMEM = "";
29+
const char ENDPOINT[] PROGMEM = "https://your.api.com";
3130
const char OPEN_VALVE[] PROGMEM = "open-valve";
3231
const char CLOSE_VALVE[] PROGMEM = "close-valve";
3332
const char RESET[] PROGMEM = "reset";
@@ -47,7 +46,7 @@ void(*reset) (void) = 0;
4746
void resetArudino(){
4847
info(F("Reset!"));
4948
delay(1000);
50-
resetArudino();
49+
reset();
5150
}
5251

5352
void sleep() {
@@ -58,14 +57,16 @@ void sleep() {
5857
}
5958

6059
unsigned int readLipoVoltage() {
61-
HTTP http(BAUD_RATE, RX_PIN, TX_PIN, RST_PIN, DEBUG);
60+
HTTP http(BAUD_RATE, RX_PIN, TX_PIN, RST_PIN);
61+
http.wakeUp();
6262
unsigned int voltage = 0;
6363
for (unsigned int i = 0; i < 10; ++i) {
6464
unsigned int cv = http.readVoltage();
6565
if (cv > voltage) {
6666
voltage = cv;
6767
}
6868
}
69+
http.sleep();
6970
return voltage;
7071
}
7172

@@ -83,7 +84,7 @@ void uploadFile(const char *filename) {
8384
File dataFile = SD.open(filename);
8485

8586
if (dataFile) {
86-
FTP ftp(BAUD_RATE, RX_PIN, TX_PIN, RST_PIN, DEBUG);
87+
FTP ftp(BAUD_RATE, RX_PIN, TX_PIN, RST_PIN);
8788
ftp.wakeUp();
8889
ftp.putBegin(BEARER, filename, FTP_SERVER, FTP_USER, FTP_PASS);
8990

@@ -121,15 +122,15 @@ void uploadFile(const char *filename) {
121122
}
122123

123124
Result postEntry(char *response) {
125+
info(F("SRAM: "), false);
126+
info(availableMemory(), true);
127+
124128
unsigned int temperature = readTemperature();
125129
unsigned int humidity = readHumidity();
126130
unsigned int moisture = readMoisture();
127131
unsigned int litioVoltage = readLitioVoltage();
128132
unsigned int liPoVoltage = readLipoVoltage();
129133

130-
info(F("SRAM: "), false);
131-
info(availableMemory(), true);
132-
133134
char body[70];
134135
Result result;
135136
sprintf_P(body, BODY_FORMAT,
@@ -145,7 +146,7 @@ Result postEntry(char *response) {
145146
strcpy_P(endpoint, ENDPOINT);
146147
info(endpoint);
147148

148-
HTTP http(BAUD_RATE, RX_PIN, TX_PIN, RST_PIN, DEBUG);
149+
HTTP http(BAUD_RATE, RX_PIN, TX_PIN, RST_PIN);
149150
http.wakeUp();
150151
http.connect(BEARER);
151152
result = http.post(endpoint, body, response);
@@ -157,6 +158,8 @@ Result postEntry(char *response) {
157158

158159
void manageGarden() {
159160
char buff[32];
161+
buildImageName(buff, currentImage);
162+
Serial.println(buff);
160163
Result result = postEntry(buff);
161164

162165
if (result == SUCCESS) {
@@ -182,8 +185,8 @@ void manageGarden() {
182185

183186
buildImageName(buff, currentImage);
184187
if (takePicture(buff)){
185-
currentImage++;
186188
uploadFile(buff);
189+
currentImage++;
187190
}
188191
}
189192
else {
@@ -213,8 +216,9 @@ void setup() {
213216
currentState = CLOSE_VALVE_STATE;
214217

215218
info(F("Starting!"));
216-
openValve();
217-
closeValve();
219+
220+
//openValve();
221+
//closeValve();
218222
}
219223

220224
/*

examples/utils/arducam.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
const char IMG_NAME[] PROGMEM = "%d.jpg";
1313

1414
void buildImageName(char *filename, uint8_t currentImage) {
15-
// Open the new file if not exists. currentImage is 0 when the int has overflow, since it start at 1.
16-
// It iterates til we reach a non existent image in order to not override it
17-
1815
sprintf_P(filename, IMG_NAME, currentImage);
19-
currentImage ++;
2016
}
2117

2218
bool takePicture(const char *imageName)

examples/utils/logger.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,26 @@ const char LOG_INT[] PROGMEM = "%d";
1010

1111
void info(const char *message, bool newLine = true)
1212
{
13+
newLine ? Serial.println(message) : Serial.print(message);
14+
1315
File file = SD.open(LOG_FILE, FILE_WRITE);
1416
if (file)
1517
{
1618
newLine ? file.println(message) : file.print(message);
1719
file.close();
1820
}
19-
newLine ? Serial.println(message) : Serial.print(message);
2021
}
2122

2223
void info(const __FlashStringHelper *message, bool newLine = true)
2324
{
25+
newLine ? Serial.println(message) : Serial.print(message);
26+
2427
File file = SD.open(LOG_FILE, FILE_WRITE);
2528
if (file)
2629
{
2730
newLine ? file.println(message) : file.print(message);
2831
file.close();
2932
}
30-
newLine ? Serial.println(message) : Serial.print(message);
3133
}
3234

3335
void info(long message, bool newLine = true)

0 commit comments

Comments
 (0)