-
Notifications
You must be signed in to change notification settings - Fork 12
/
Arduino_To_Thingsboard.ino
319 lines (262 loc) · 10.6 KB
/
Arduino_To_Thingsboard.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* Sending Data to Thingsboard
* Working Code!
*
* This is the CURL Command to send data:
* curl -v -X POST -d @telemetry-data-as-object.json http://demo.thingsboard.io:80/api/v1/<ACCESS_TOKEN>/telemetry --header "Content-Type:application/json"
*
* Connected to demo.thingsboard.io (104.196.24.70) port 80 (#0)
* > POST /api/v1/<ACCESS_TOKEN>/telemetry HTTP/1.1
* > Host: demo.thingsboard.io
* > Accept: **
* > Content-Type:application/json
* > Content-Length: 34
* {"temperature":30.1, "voltage":24} //This was the content in my JSON file
*
*
* This is the CURL Command to publish attributes
* curl -v -X POST -d @new-attributes-values.json http://ec2-35-154-9-130.ap-south-1.compute.amazonaws.com:80/api/v1/A1_TEST_TOKEN/attributes --header "Content-Type:application/json"
*/
#include <ArduinoJson.h> //For Creating a Json File
DynamicJsonBuffer jsonBuffer; //Set Buffer size to Dynamic
JsonObject& root = jsonBuffer.createObject(); //Create an object 'root' which is called later to print JSON Buffer
char aux_str[100];
char pin[]="";
char apn[]="airtelgprs.com"; //For airtel gprs connection
char user_name[]="";
char password[]="";
int lengthOfJSON;
char thingsboard_url[]="demo.thingsboard.io";
boolean sim900Status = false;
char port[]="80"; // PORT Connected on
String getStr="";
String AccessToken ="A1_TEST_TOKEN"; //For Device 1
void setup()
{
Serial.begin(9600); //To print serial megssages on Serial Monitor
Serial1.begin(9600); //To connect SIM900A and send AT Commands
power_on(); // POWER ON GSM Module for communication
pinMode(13,OUTPUT);
}
void loop()
{
makeJson(12.30, 32.3, 43.3, 33.3, 54.5, 65.3); //Making JSON text here
/*You can send any values from here and make a json file to send to thingsboard*/
updateThingsboard();
Serial.println("\n----Sleeping----\n");
delay(10000);
}
void updateThingsboard()
{
lengthOfJSON = 0; //Set size of JSON text as '0' initially
if(sim900Status==true)
{
// Selects Single-connection mode
if (sendATcommand2("AT+CIPMUX=0", "OK","ERROR", 1000) == 1) // CIMPUX=0 is already set in Single-connection mode
{
// Waits for status IP INITIAL
while(sendATcommand("AT+CIPSTATUS","INITIAL", 1000) == 0 ); // Check Current Connection Status
delay(2000); //wait 5 sec
snprintf(aux_str, sizeof(aux_str), "AT+CSTT=\"%s\",\"%s\",\"%s\"", apn, user_name, password); //Put GPRS setings
// Sets the APN, user name and password
if (sendATcommand2(aux_str, "OK", "ERROR", 30000) == 1)
{
// Waits for status IP START
while(sendATcommand("AT+CIPSTATUS", "START", 500) == 0 );
delay(2000);
// Brings Up Wireless Connection
if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1)
{
delay(2000);
Serial.println("\n Bringup Wireless Connection ...........");
// Waits for status IP GPRSACT
while(sendATcommand("AT+CIPSTATUS", "GPRSACT", 1000) == 0 );
delay(2000);
// Gets Local IP Address
if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1)
{
// Waits for status IP STATUS
while(sendATcommand("AT+CIPSTATUS", "IP STATUS", 500) == 0 );
delay(2000);
Serial.println("Opening TCP");
snprintf(aux_str, sizeof(aux_str), "AT+CIPSTART=\"TCP\",\"%s\",\"%s\"",thingsboard_url, port); //IP_address
if (sendATcommand2(aux_str, "CONNECT OK", "CONNECT FAIL", 30000) == 1)
{
Serial.println("Connected");
String json="";
root.printTo(json); //Store JSON in a String named 'json'
lengthOfJSON = json.length(); //This gives us total size of our json text
//TCP packet to send POST Request on https (Thingsboard)
getStr="POST /api/v1/"+ AccessToken +"/telemetry HTTP/1.1\r\nHost: demo.thingsboard.io\r\nAccept: */*\r\nContent-Type: application/json\r\nContent-Length:"+lengthOfJSON+"\r\n\r\n"+json;
//TCP packet to send POST Request on https (Thingsboard)
String sendcmd = "AT+CIPSEND="+ String(getStr.length());
if (sendATcommand2(sendcmd, ">", "ERROR", 10000) == 1)
{
delay(100);
sendATcommand2(getStr, "SEND OK", "ERROR", 10000); //Sending Data Here
}
Serial.println("Closing the Socket............");
sendATcommand2("AT+CIPCLOSE", "CLOSE OK", "ERROR", 10000);
}
else
{
Serial.println("Error opening the connection");
}
}
}
}
}
}
Serial.println("Shutting down the connection.........");
sendATcommand2("AT+CIPSHUT", "OK", "ERROR", 10000);
delay(5000);
}//updateThingsboard
void makeJson( float val1, float val2, float val3, float val4, float val5, float val6 )
{
Serial.println("\nMaking JSON text meanwhile\n");
root["Time"] = val1;
root["Milk Temperature"] = val2;
root["AC Voltage"] = val3;
root["Auxillary Temperature"] = val4;
root["Battery Temperature"] = val5;
root["Battery Voltage"] = val6;
}
void power_on()
{
uint8_t answer=0;
sim900Status=true;
int ctT=0;
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(2000);
digitalWrite(13,HIGH);
delay(5000);
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{ Serial.println("SIM ON");
while(answer == 0)
{
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
ctT++;
if(ctT==10)
{
sim900Status=true;
Serial.println("GSM OFF");
power_on();
}//nested if()
}
}//if()
Serial.println("GSM ON Sucessfully");
}//power_on()
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
{
uint8_t x=0, answer=0;
char response[3000]; //Response buffer
unsigned long previous;
memset(response, '\0', 3000); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
Serial1.println(ATcommand); //Response from Serial1
x = 0;
previous = millis();
// this loop waits for the answer
do
{
if(Serial1.available() != 0)
{
// if there is data in the UART input buffer, read it and checks for the asnwer
response[x] = Serial1.read(); //Read Response from Serial1 port
Serial.print(response[x]); //Print response on Serial 0 port
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
}//do
// Waits for the asnwer with time out
while((answer == 0) && ((millis() - previous) < timeout)); //Check till answer = 0 and timout period(ms)
return answer;
}//sendATcommand()
int8_t sendATcommand2(String ATcommand, char* expected_answer1,char* expected_answer2, unsigned int timeout)
{
uint8_t x=0, answer=0;
char response[3000];
unsigned long previous;
memset(response, '\0', 3000); // Initialize the string
delay(100);
while( Serial1.available() > 0) Serial1.read(); // Clean the input buffer
Serial1.println(ATcommand); // Send the AT command
Serial.println(ATcommand);
x = 0;
previous = millis();
// this loop waits for the answer
do
{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(Serial1.available() != 0)
{
response[x] = Serial1.read();
Serial.print(response[x]);
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}//if()
}//do
// Waits for the asnwer with time out
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}//sendATcommand2()
int8_t sendATcommand3(String ATcommand, char* expected_answer1,char* expected_answer2,char*expected_answer3, unsigned int timeout)
{
uint8_t x=0, answer=0;
char response[3000];
unsigned long previous;
memset(response, '\0', 3000); // Initialize the string
delay(100);
while( Serial1.available() > 0) Serial1.read(); // Clean the input buffer
Serial1.println(ATcommand); // Send the AT command
Serial.println(ATcommand);
x = 0;
previous = millis();
// this loop waits for the answer
do
{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(Serial1.available() != 0)
{
response[x] = Serial1.read();
Serial.print(response[x]);
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
else if (strstr(response, expected_answer3) != NULL)
{
answer = 3;
}
}//if()
}//do
// Waits for the answer with time out
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}//sendATcommand3