/
send_data.cpp
242 lines (220 loc) · 7.11 KB
/
send_data.cpp
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
// Based on https://github.com/MediaTek-Labs/WeatherStation1.0/blob/master/WeatherStation_1.0.ino
// Update these 2 lines with your group ID and device ID.
#define GROUP "1"
#define DEVICE "1"
// This is the REST service for transmitting the sensor values.
#define SITE_URL "AzureIoTService.cloudapp.net"
#define SITE_PORT 80
// This is a long-lasting TCP socket connection to receive actuation commands from the cloud.
#define SERVER_IP "AzureIoTService.cloudapp.net"
#define SERVER_PORT 2012
/*
Make sure you have a file named password.h containing the lines:
#define WIFI_SSID "yourwifissid"
#define WIFI_KEY "yourwifipassword"
*/
#include <Arduino.h>
#include "password.h"
#include "send_data.h"
#include "led.h"
#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <Wire.h>
#include <math.h>
#include <LDateTime.h>
#include <HttpClient.h>
#include <LFlash.h>
// ================================================
// Define
#undef _DBG_
#define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
#define per 5 // time interval in seconds to upload sensor data to MCS
#define per1 50 // time interval in seconds to initiate TCP heart beat
#define Drv LFlash
// ================================================
// Global variable
// For networking.
char ssid[] = WIFI_SSID;
char key[] = WIFI_KEY;
char ip[] = SERVER_IP;
int portnum = SERVER_PORT;
LWiFiClient c;
LWiFiClient c2;
HttpClient http(c2);
char connection_info[21]=" ";
// This is the Hello message ("HELLO 1 2"), used by the LinkIt ONE to specify the group ID and device ID upon startup.
// The Hello message is also sent periodically by the LinkIt ONE to keep the TCP connection alive with the server.
String hello;
// For real-time clock.
unsigned int rtc;
unsigned int lrtc;
unsigned int lrtc2;
unsigned int rtc2;
unsigned int rtc1;
unsigned int lrtc1;
int val = 0;
int state = 0;
int i = 0;
void fatal_error(const char* str)
{
Serial.println(str);
while(1)
delay(100);
}
// ==============================================================
// TCP Socket server connection functions
void connectTCP(){
// Connect to the TCP server to establish a channel for the cloud to send actuation commands to the LinkIt ONE.
c.stop();
Serial.println("Connecting to TCP socket for receiving actuation commands...");
Serial.print("IP: "); Serial.println(ip);
Serial.print("Port: "); Serial.println(portnum);
while (0 == c.connect(ip, portnum))
{
Serial.println("Re-connecting to TCP socket...");
delay(1000);
}
// Tell the server our group ID and device ID.
Serial.println("Connected to TCP socket for receiving actuation commands");
hello = "HELLO " + String(GROUP) + " " + String(DEVICE);
Serial.print(">>> "); Serial.println(hello);
c.println(hello);
Serial.println("Waiting for TCP response...");
} //connectTCP
void heartBeat(){
// The Hello message is sent periodically by the LinkIt ONE to keep the TCP connection alive with the server.
Serial.println("Sending TCP heartBeat...");
Serial.print(">>> "); Serial.println(hello);
c.println(hello);
} //heartBeat
void senddata(float temperature_sensor_value, int light_sensor_value) {
// Send the sensor values to the cloud.
// TODO: Send a dictionary of <key, value> strings instead.
Serial.println("Sending sensor data to cloud...");
char buffer1[5];
sprintf(buffer1, "%.2f", temperature_sensor_value);
// Open a connection to the HTTP port (usually port 80).
LWiFiClient c2;
unsigned long t1 = millis();
Serial.print("Connecting to "); Serial.print(SITE_URL); Serial.print(" port "); Serial.print(SITE_PORT); Serial.println("...");
while (0 == c2.connect(SITE_URL, SITE_PORT))
{
Serial.println("Reconnecting to cloud REST server...");
delay(1000);
}
delay(500);
// Send a HTTP POST command to transmit the sensor values.
// TODO: May need to do URL encoding of the sensor values if they contain special characters.
Serial.println("Sending POST request to cloud REST server...");
String url = "POST /RecordSensorData.aspx?Group=" + String(GROUP) + "&Device=" + String(DEVICE) +
"&Temperature=" + String(buffer1) + "&LightLevel=" + String(light_sensor_value) + " HTTP/1.1";
Serial.print(">>> "); Serial.println(url);
c2.println(url);
// Send the other HTTP request headers, according to the HTTP specifications.
c2.print("Host: ");
c2.print(SITE_URL); c2.print(":"); c2.println(SITE_PORT);
c2.print("Content-Length: ");
c2.println(0);
c2.println("Content-Type: text/csv");
c2.println("Connection: close");
c2.println();
String dataget = "";
Serial.println("Waiting for HTTP response...");
int errorcount = 0;
while (!c2.available())
{
Serial.println("Waiting for HTTP response...");
errorcount += 1;
if (errorcount > 10) {
c2.stop();
return;
}
delay(100);
}
while (c2)
{
int v = c2.read();
if (v != -1)
{
Serial.print(char(v));
delay(1);
}
else
{
Serial.println("No more content, disconnecting...");
c2.stop();
}
}
Serial.println("\r\nSensor data sent to cloud");
delay(300);
unsigned long t2 = millis();
}
// ==============================================================
// Main program
void send_data_setup() {
Wire.begin();
LTask.begin();
LWiFi.begin();
//while(!Serial) delay(1000); //mark this demo as standalone
Serial.println("Connecting to WiFi access point...");
Serial.println(ssid);
Serial.println(key);
while (0 == LWiFi.connect(ssid, LWiFiLoginInfo(WIFI_AUTH, key))){
Serial.print(".");
delay(1000);
}
Serial.println("WiFi Connected");
delay(100);
Serial.println("LED is set to LOW");
LDateTime.getRtc(&lrtc);
LDateTime.getRtc(&lrtc2);
while (!c2.connect(SITE_URL, SITE_PORT)){
Serial.println("Reconnecting to cloud REST server...");
delay(1000);
}
delay(100);
connectTCP();
}
void send_data_loop(float temperature_sensor_value, int light_sensor_value) {
if (LWiFi.status() == LWIFI_STATUS_DISCONNECTED) {
Serial.println("Disconnected from WiFi");
while (0 == LWiFi.connect(ssid, LWiFiLoginInfo(WIFI_AUTH, key))){
Serial.println("Connecting to WiFi...");
delay(1000);
}
}
LDateTime.getRtc(&rtc);
if ((rtc - lrtc) >= per) {
senddata(temperature_sensor_value, light_sensor_value);
lrtc = rtc;
}
//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;
if (tcpcmd.indexOf("led on") >= 0) {
Serial.print("Received actuator command: "); Serial.println(tcpcmd);
Serial.println("Switching LED ON...");
led_on();
tcpcmd="";
}else if(tcpcmd.indexOf("led off") >= 0) {
Serial.print("Received actuator command: "); Serial.println(tcpcmd);
Serial.println("Switching LED OFF...");
led_off();
tcpcmd="";
}
}
}
//Check for hearbeat interval
LDateTime.getRtc(&rtc2);
if ((rtc2 - lrtc2) >= per1) {
heartBeat();
lrtc2 = rtc2;
}
}