-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi_IoT_for_demo.ino
432 lines (372 loc) · 9.45 KB
/
wifi_IoT_for_demo.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "LDHT.h"
#include <HttpClient.h>
#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <LDateTime.h>
#define WIFI_AP "HSNL-EXP-AP"
#define WIFI_PASSWORD "hsnl33564"
#define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
#define per 50
#define per1 3
#define DEVICEID "DG4xZQrg" // Input your deviceId
#define DEVICEKEY "0lEQimFFUcAE0Rc1" // Input your deviceKey
#define SITE_URL "api.mediatek.com"
#define FAN_PORT 10
#define LED_PORT 6
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
LDHT dht(DHTPIN, DHTTYPE);
LWiFiClient c;
unsigned int rtc;
unsigned int lrtc;
unsigned int rtc1;
unsigned int lrtc1;
char port[4]={0};
char connection_info[21]={0};
char ip[21]={0};
int portnum;
int val = 0;
String tcpdata = String(DEVICEID) + "," + String(DEVICEKEY) + ",0";
String upload_data;
String tcpcmd_LED_on = "wifi_led,1";
String tcpcmd_LED_off = "wifi_led,0";
String tcpcmd_fan_on="wifi_fan,1";
String tcpcmd_fan_off="wifi_fan,0";
String str="0";
String str_temp="";
LWiFiClient c2;
HttpClient http(c2);
//int index_v;
//int index_y=1;
float temp=0.0;
float humid_air=0.0;
int light=0;
int pinLight = A0;
int pinMoisture=A1;
void setup()
{
dht.begin();// temp and moisture sensor
LTask.begin();
LWiFi.begin();
Serial.begin(115200);
while(!Serial) delay(1000); /* comment out this line when Serial is not present, ie. run this demo without connect to PC */
Serial.println("Connecting to AP");
while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
{
delay(1000);
}
Serial.println("");
Serial.println("calling connection");
while (!c2.connect(SITE_URL, 80))
{
Serial.println("Re-Connecting to WebSite");
delay(1000);
}
delay(100);
pinMode(LED_PORT, OUTPUT);
pinMode(FAN_PORT, OUTPUT);
getconnectInfo();
connectTCP();
}
void getconnectInfo(){
//calling RESTful API to get TCP socket connection
c2.print("GET /mcs/v2/devices/");
c2.print(DEVICEID);
c2.println("/connections.csv HTTP/1.1");
c2.print("Host: ");
c2.println(SITE_URL);
c2.print("deviceKey: ");
c2.println(DEVICEKEY);
c2.println("Connection: close");
c2.println();
delay(500);
int errorcount = 0;
while (!c2.available())
{
Serial.println("waiting HTTP response: ");
Serial.println(errorcount);
errorcount += 1;
if (errorcount > 10) {
c2.stop();
return;
}
delay(100);
}
int err = http.skipResponseHeaders();
int bodyLen = http.contentLength();
Serial.print("Content length is: ");
Serial.println(bodyLen);
Serial.println();
char c;
int ipcount = 0;
int count = 0;
int separater = 0;
while (c2)
{
int v = c2.read();
if (v != -1)
{
c = v;
Serial.print(c);
connection_info[ipcount]=c;
if(c==',')
separater=ipcount;
ipcount++;
}
else
{
Serial.println("no more content, disconnect");
c2.stop();
}
}
Serial.print("The connection info: ");
Serial.println(connection_info);
int i;
for(i=0;i<separater;i++)
{ ip[i]=connection_info[i];
}
int j=0;
separater++;
for(i=separater;i<21 && j<5;i++)
{ port[j]=connection_info[i];
j++;
}
Serial.println("The TCP Socket connection instructions:");
Serial.print("IP: ");
Serial.println(ip);
Serial.print("Port: ");
Serial.println(port);
portnum = atoi (port);
Serial.println(portnum);
} //getconnectInfo
void upload_temp(float t){
//calling RESTful API to upload datapoint to MCS to report LED status
Serial.println("calling connection");
LWiFiClient c2;
while (!c2.connect(SITE_URL, 80))
{
Serial.println("Re-Connecting to WebSite");
delay(1000);
}
delay(100);
upload_data = "Display_Temp,,"+String(t);
int thislength = upload_data.length();
HttpClient http(c2);
c2.print("POST /mcs/v2/devices/");
c2.print(DEVICEID);
c2.println("/datapoints.csv HTTP/1.1");
c2.print("Host: ");
c2.println(SITE_URL);
c2.print("deviceKey: ");
c2.println(DEVICEKEY);
c2.print("Content-Length: ");
c2.println(thislength);
//c2.println(dataLength);
c2.println("Content-Type: text/csv");
c2.println("Connection: close");
c2.println();
c2.println(upload_data);
// c2.println(data);
delay(500);
}
void upload_moisture(float t){
//calling RESTful API to upload datapoint to MCS to report LED status
Serial.println("calling connection");
LWiFiClient c2;
while (!c2.connect(SITE_URL, 80))
{
Serial.println("Re-Connecting to WebSite");
delay(1000);
}
delay(100);
upload_data = "Display_Moisture,,"+String(t);
int thislength = upload_data.length();
HttpClient http(c2);
c2.print("POST /mcs/v2/devices/");
c2.print(DEVICEID);
c2.println("/datapoints.csv HTTP/1.1");
c2.print("Host: ");
c2.println(SITE_URL);
c2.print("deviceKey: ");
c2.println(DEVICEKEY);
c2.print("Content-Length: ");
c2.println(thislength);
//c2.println(dataLength);
c2.println("Content-Type: text/csv");
c2.println("Connection: close");
c2.println();
c2.println(upload_data);
// c2.println(data);
delay(500);
}
void upload_light(String t){
//calling RESTful API to upload datapoint to MCS to report LED status
Serial.println("calling connection");
LWiFiClient c2;
while (!c2.connect(SITE_URL, 80))
{
Serial.println("Re-Connecting to WebSite");
delay(1000);
}
delay(100);
upload_data = "Display_Light,,"+t;
int thislength = upload_data.length();
HttpClient http(c2);
c2.print("POST /mcs/v2/devices/");
c2.print(DEVICEID);
c2.println("/datapoints.csv HTTP/1.1");
c2.print("Host: ");
c2.println(SITE_URL);
c2.print("deviceKey: ");
c2.println(DEVICEKEY);
c2.print("Content-Length: ");
c2.println(thislength);
c2.println("Content-Type: text/csv");
c2.println("Connection: close");
c2.println();
c2.println(upload_data);
delay(500);
}
void uploadstatus(){
//calling RESTful API to upload datapoint to MCS to report LED status
Serial.println("calling connection");
LWiFiClient c2;
while (!c2.connect(SITE_URL, 80))
{
Serial.println("Re-Connecting to WebSite");
delay(1000);
}
delay(100);
int thislength = upload_data.length();
//String data = "Display_Temp,,"+String(temp);
// int dataLength = data.length();
HttpClient http(c2);
c2.print("POST /mcs/v2/devices/");
c2.print(DEVICEID);
c2.println("/datapoints.csv HTTP/1.1");
c2.print("Host: ");
c2.println(SITE_URL);
c2.print("deviceKey: ");
c2.println(DEVICEKEY);
c2.print("Content-Length: ");
c2.println(thislength);
//c2.println(dataLength);
c2.println("Content-Type: text/csv");
c2.println("Connection: close");
c2.println();
c2.println(upload_data);
// c2.println(data);
delay(500);
int errorcount = 0;
while (!c2.available())
{
Serial.print("waiting HTTP response: ");
Serial.println(errorcount);
errorcount += 1;
if (errorcount > 10) {
c2.stop();
return;
}
delay(100);
}
int err = http.skipResponseHeaders();
int bodyLen = http.contentLength();
Serial.print("Content length is: ");
Serial.println(bodyLen);
Serial.println();
while (c2)
{
int v = c2.read();
if (v != -1)
{
Serial.print(char(v));
}
else
{
Serial.println("no more content, disconnect");
c2.stop();
}
}
}
void connectTCP(){
//establish TCP connection with TCP Server with designate IP and Port
c.stop();
Serial.println("Connecting to TCP");
Serial.println(ip);
Serial.println(portnum);
while (0 == c.connect(ip, portnum))
{
Serial.println("Re-Connecting to TCP");
delay(1000);
}
Serial.println("send TCP connect");
c.println(tcpdata);
c.println();
Serial.println("waiting TCP response:");
} //connectTCP
void heartBeat(){
Serial.println("send TCP heartBeat");
c.println(tcpdata);
c.println();
} //heartBeat
void manual_mode(){
}
void auto_mode(){
}
void loop()
{
light=analogRead(pinLight);
Serial.println("------------------------------");
Serial.print("light = ");
Serial.println(light);
// upload temp sensor
if(dht.read())
{
temp=dht.readTemperature();
humid_air=dht.readHumidity();
Serial.println("------------------------------");
Serial.print("temperature = ");
Serial.println(temp);
Serial.print("humidity = ");
Serial.println(humid_air);
upload_temp(temp);
}
//Check for TCP socket command from MCS Server
String tcpcmd="";
while (c.available())
{
int v = c.read();
if (v != -1)
{
Serial.print((char)v);
tcpcmd += (char)v;
str_temp=tcpcmd;
if (tcpcmd.substring(40).equals(tcpcmd_LED_on)){
digitalWrite(LED_PORT, HIGH);
tcpcmd="";
} else if(tcpcmd.substring(40).equals(tcpcmd_LED_off)){
digitalWrite(LED_PORT, LOW);
tcpcmd="";
}
else if (tcpcmd.substring(40).equals(tcpcmd_fan_on)){
digitalWrite(FAN_PORT, HIGH);
tcpcmd="";
}else if(tcpcmd.substring(40).equals(tcpcmd_fan_off)){
digitalWrite(FAN_PORT, LOW);
tcpcmd="";
}
}
}
LDateTime.getRtc(&rtc);
if ((rtc - lrtc) >= per) {
heartBeat();
lrtc = rtc;
}
//Check for report datapoint status interval
LDateTime.getRtc(&rtc1);
if ((rtc1 - lrtc1) >= per1) {
uploadstatus();
lrtc1 = rtc1;
}
}