-
Notifications
You must be signed in to change notification settings - Fork 2
/
sendPostRequest.ino
86 lines (73 loc) · 2.14 KB
/
sendPostRequest.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <SoftwareSerial.h>
// SoftwareSerial object for communication with gprsShield
SoftwareSerial gprsShield(7, 8); // RX, TX
void testPostRequest(String jsonToSend){
//Example format of JSON:
// String jsonToSend="{\"uploadedAt\":\"2023-06-26T20:18:22.826Z\",\"data\":[{\"unit\":\"C\",\"reading\":31}]}";
sendCommand("AT");
ShowSerialData();
sendCommand("AT+CIPSHUT");
ShowSerialData();
delay(500);
sendCommand("AT+SAPBR=0,1");
delay(2000);
ShowSerialData();
sendCommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
ShowSerialData();
sendCommand("AT+SAPBR=3,1,\"APN\",\"internet.apn\"");
ShowSerialData();
sendCommand("AT+SAPBR=1,1");
delay(2000);
ShowSerialData();
sendCommand("AT+HTTPINIT");
delay(1000);
ShowSerialData();
sendCommand("AT+HTTPPARA=\"CID\",1");
ShowSerialData();
sendCommand("AT+HTTPPARA=\"URL\",\"http://example.com/data\"");
ShowSerialData();
sendCommand("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
ShowSerialData();
sendCommand(("AT+HTTPDATA=" + String(jsonToSend.length()) + ",20000").c_str());
delay(6000);
SIM900.println(jsonToSend);
delay(16000);
ShowSerialData();
sendCommand("AT+HTTPACTION=1");
delay(20000);
ShowSerialData();
sendCommand("AT+HTTPREAD");
ShowSerialData();
sendCommand("AT+HTTPTERM");
ShowSerialData();
sendCommand("AT+CIPSHUT");
ShowSerialData();
}
void sendCommand(const char* command) {
Serial.print("C: ");
Serial.println(command);
gprsShield.println(command);
delay(1000);
}
String ShowSerialData() {
String response="";
while (gprsShield.available()) {
response = gprsShield.readString();
Serial.println("Res from gprsShield:");
Serial.println(response);
}
return response;
// delay(1000);
}
void setup() {
int baudRate = 9600;
Serial.begin(baudRate); // Serial monitor
gprsShield.begin(baudRate); // GSM module
// delay(1000);
String jsonToSend="{\"uploadedAt\":\"2023-06-26T20:18:22.826Z\",\"data\":[{\"unit\":\"C\",\"reading\":31}]}";
//please give some time for the SIM card to register on the network before running this command
delay(7000);
testPostRequest(jsonToSend);
}
void loop() {
}