-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatt.ino
209 lines (181 loc) · 6.55 KB
/
att.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
#include <xOD01.h>
void connectToWifi(String ssid, String pass)
{
String _temp = "AT+CWMODE=1";
Serial.println(_temp.c_str());
delay(100);
Serial.readString();
_temp = "AT+CWJAP=\"" + ssid + "\",\"" + pass + "\"" + "";
Serial.println(_temp.c_str());
delay(200);
uint16_t loop_count = 0;
String res;
do {
res = Serial.readString();
// xOD01 OD01;
// OD01.begin();
// OD01.clear();
// OD01.println(res);
Wire.begin();
ui.connectingToWifi(" Connecting to WiFi");
delay(1000);
loop_count++;
if (loop_count == 20)
break;
} while (strstr(res.c_str(), "WIFI CONNECTED") == NULL);
if (strstr(res.c_str(), "WIFI CONNECTED") != NULL) {
Wire.begin();
ui.wifiPass(" WIFI PASS");
delay(2000);
res = "";
return;
}
if (loop_count == 20)
{
Wire.begin();
ui.wifiFail(" WIFI FAIL");
delay(2000);
}
}
void connectToATT(String ID, String TKN)
{
cw01_vars.DEVICE_ID = ID;
cw01_vars.TOKEN = TKN;
Serial.println("AT+CIPSTART=\"TCP\",\"api.allthingstalk.io\",80");
delay(500);
}
void IoTSendValueToATT(int value, String asset)
{
String att_connected = "";
// while (cw01_button_object.sending_data) {
// basic.pause(100)
// }
//
// cw01_button_object.sending_data = true
do {
sensor_display(screenCount);
cw01_vars.asset_name = asset;
Serial.println("AT+CIPMODE=0");
String payload = "{\"value\": " + String(value) + "}";
String request = "PUT /device/" + cw01_vars.DEVICE_ID + "/asset/" + cw01_vars.asset_name + "/state" + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: api.allthingstalk.io" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Authorization: Bearer " + cw01_vars.TOKEN + cw01_vars.NEWLINE +
"Content-Type:application/json" + cw01_vars.NEWLINE +
"Content-Length: " + String(payload.length()) + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload;
Serial.println("AT+CIPSEND=" + String((request.length() + 2)));
delay(50);
Serial.println(request);
//delay(1000);
att_connected = Serial.readString();
//OD01.println("ATT: " + String(att_connected));
if (strstr(att_connected.c_str(), "link is not valid") != NULL) {
connectToATT(cw01_vars.TOKEN, cw01_vars.DEVICE_ID);
} else {
att_connected = "";
}
//get_status()
} while (strstr(att_connected.c_str(), "link is not valid") != NULL);
//cw01_button_object.sending_data = false
}
void sendLocationToAtt(String asset)
{
String att_connected = "";
// while (cw01_button_object.sending_data) {
// basic.pause(100)
// }
//
// cw01_button_object.sending_data = true
do {
sensor_display(screenCount);
cw01_vars.asset_name = asset;
Serial.println("AT+CIPMODE=0");
delay(100);
String payload = "{\"value\": {\"latitude\":" + data.latitude + ", \"longitude\":" + data.longitude + "} }";
String request = "PUT /device/" + cw01_vars.DEVICE_ID + "/asset/" + cw01_vars.asset_name + "/state" + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: api.allthingstalk.io" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Authorization: Bearer " + cw01_vars.TOKEN + cw01_vars.NEWLINE +
"Content-Type:application/json" + cw01_vars.NEWLINE +
"Content-Length: " + String(payload.length()) + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload;
Serial.println("AT+CIPSEND=" + String((request.length() + 2)));
delay(50);
Serial.println(request);
//delay(1000);
att_connected = Serial.readString();
//OD01.println("ATT: " + String(att_connected));
if (strstr(att_connected.c_str(), "link is not valid") != NULL) {
connectToATT(cw01_vars.TOKEN, cw01_vars.DEVICE_ID);
} else {
att_connected = "";
}
//get_status()
} while (strstr(att_connected.c_str(), "link is not valid") != NULL);
//cw01_button_object.sending_data = false
}
void IoTSendStringToATT(String value, String asset)
{
String att_connected = "";
// while (cw01_button_object.sending_data) {
// basic.pause(100)
// }
//
// cw01_button_object.sending_data = true
do {
sensor_display(screenCount);
cw01_vars.asset_name = asset;
Serial.println("AT+CIPMODE=0");
delay(100);
String payload = "{\"value\": \"" + value + "\"}";
String request = "PUT /device/" + cw01_vars.DEVICE_ID + "/asset/" + cw01_vars.asset_name + "/state" + " HTTP/1.1" + cw01_vars.NEWLINE +
"Host: api.allthingstalk.io" + cw01_vars.NEWLINE +
"User-Agent: CW01/1.0" + cw01_vars.NEWLINE +
"Accept: */*" + cw01_vars.NEWLINE +
"Authorization: Bearer " + cw01_vars.TOKEN + cw01_vars.NEWLINE +
"Content-Type:application/json" + cw01_vars.NEWLINE +
"Content-Length: " + String(payload.length()) + cw01_vars.NEWLINE + cw01_vars.NEWLINE + payload;
Serial.println("AT+CIPSEND=" + String((request.length() + 2)));
delay(50);
Serial.println(request);
//delay(1000);
att_connected = Serial.readString();
//OD01.println("ATT: " + String(att_connected));
if (strstr(att_connected.c_str(), "link is not valid") != NULL) {
connectToATT(cw01_vars.TOKEN, cw01_vars.DEVICE_ID);
} else {
att_connected = "";
}
//get_status()
} while (strstr(att_connected.c_str(), "link is not valid") != NULL);
//cw01_button_object.sending_data = false
}
void sendDataToAtt()
{
if (ack._sw01) {
IoTSendValueToATT(atof(data.pressure.c_str()), "9");
IoTSendValueToATT(atof(data.temperature.c_str()), "8");
IoTSendValueToATT(atof(data.humidity.c_str()), "10");
}
if (ack._sg35) {
if (ack._sn01_gps_ready || mode == 1) {
IoTSendValueToATT(atof(data.pm25.c_str()), "3");
IoTSendValueToATT(atof(data.pm100.c_str()), "2");
IoTSendValueToATT(atof(data.pm10.c_str()), "1");
}
}
if (ack._sg33) {
IoTSendValueToATT(atof(data.tvoc.c_str()), "12");
IoTSendValueToATT(atof(data.co2.c_str()), "11");
}
if (ack._sn01) {
if (mode == 2) {
if ((atof(data.hdop.c_str()) < 30) && (atof(data.hdop.c_str()) > 0)) {
sendLocationToAtt("Location");
}
IoTSendStringToATT(data.hours + ":" + data.minutes + ":" + data.seconds + " Z" , "7");
IoTSendStringToATT(data.year + "-" + data.month + "-" + data.day, "6");
}
}
}