-
Notifications
You must be signed in to change notification settings - Fork 3
/
mbedcpluscode
384 lines (342 loc) · 11.2 KB
/
mbedcpluscode
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
//----------------------------------------------------------------------------
// The confidential and proprietary information contained in this file may
// only be used by a person authorised under and to the extent permitted
// by a subsisting licensing agreement from ARM Limited or its affiliates.
//
// (C) COPYRIGHT 2016 ARM Limited or its affiliates.
// ALL RIGHTS RESERVED
//
// This entire notice must be reproduced on all copies of this file
// and copies of this file may only be made by a person if such person is
// permitted to do so under the terms of a subsisting license agreement
// from ARM Limited or its affiliates.
//----------------------------------------------------------------------------
//Note for the JSON interface with the server:
// {id:%macaddress%} is sent first
// queries for events are the sole requests, but also have the form {query : 1}
// if there are events they should be expressed as:
// {"create": %event_id%, "temp_s": %threshold%, "heat_b": %threshold%, "end": "end"}
// where arbitary amounts of events can be processed at once.
// the syntax for deletion is {delete : %eventid%} Note: no end specifier.
// events are {event : %eventid%} NOTE STILL NEED TO DELETE THE TRIGGER EVENT (but maybe not do for the presentation?)
// polls are {mbed_id : %mac_address%, sensor readings...}
//NOTE UPDATE SEND TO SERVER AND GET FROM SERVER WITH IP ADDRESS OF SERVER!
#include "mbed.h"
#include "C12832.h"
#include "CCS811.h"
#include "Sht31.h"
#include "TSL2561.h"
#include "MMA7660.h"
#include "OdinWiFiInterface.h"
#include "http_request.h"
#include <map>
#include <vector>
#include <string>
#include <sstream>
struct EventB
{
long event_id;
bool c_temp;
float s_temp, b_temp;
bool c_humd;
float s_humd, b_humd;
bool c_xacc;
float s_xacc, b_xacc;
bool c_yacc;
float s_yacc, b_yacc;
bool c_zacc;
float s_zacc, b_zacc;
bool c_visi;
float s_visi, b_visi;
bool c_infa;
float s_infa, b_infa;
};
struct Readings
{
float temp;
float humd;
float xacc;
float yacc;
float zacc;
float visi;
float infa;
};
// GLOBAL VARIABLES HERE
Sht31 temp_sensor(PF_0, PF_1);
MMA7660 accel(PF_0, PF_1);
TSL2561 light_sensor(PF_0, PF_1, TSL2561_ADDR_HIGH);
C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
OdinWiFiInterface wifi;
std::vector<EventB> event_bindings;
// FUNCTION DEFINTIONS HERE
std::string to_string(int val)
{
std::stringstream ss;
ss << val;
return ss.str();
}
float to_float(std::string stra)
{
std::stringstream temp;
temp << stra;
float x;
temp >> x;
return x;
}
void lcd_print(const char* message) {
lcd.cls();
lcd.locate(0, 3);
lcd.printf(message);
}
void send_to_server(const char *json)
{
//lcd_print("sending");
NetworkInterface* net = &wifi;
HttpRequest* request = new HttpRequest(net, HTTP_POST, "http://10.25.1.211:8080/mbed");
request->set_header("Content-Type", "application/json");;
HttpResponse* response = request->send(json, strlen(json));
delete request;
//lcd_print("sent");
}
std::string check_server()
{
NetworkInterface* net = &wifi;
HttpRequest* request = new HttpRequest(net, HTTP_GET, "http://10.25.1.211:8080/mbed");
request->set_header("Content-Type", "application/json");;
std::string query = std::string("{\"query\":\"")+wifi.get_mac_address()+"\"}";
lcd_print(query.c_str());
HttpResponse* response = request->send(query.c_str(), strlen(query.c_str()));
delete request;
return std::string(response->get_body_as_string().c_str());
}
std::string jsonit(const std::vector<std::string> &names, const std::vector<float> &vals)
{
std::string result = "{";
for (int i = 0; i < names.size(); i++)
{
result += '"';
result += names[i];
result += '"';
result += ':';
result += to_string(vals[i]);
result += ',';
}
lcd_print(result.c_str());
result.erase(result.size()-1, 1); //erases the last character (would use pop_back but no c++11...)
result += '}';
return result;
}
void dejson(std::vector<std::string> &names, std::vector<float> &vals, std::string stra) //not proper, obviously (but fit for purpose).
{
std::string buffer;
for (int i = 0; i < stra.size(); i++)
{
char c = stra[i];
if (c != '{' && c != ' ' && c != '"'){
if (c == ':')
{
names.push_back(buffer);
buffer = "";
}
else if (c == ',' || c == '}')
{
vals.push_back(to_float(buffer));
buffer = "";
}
else
{
buffer += c;
}}
}
}
void add_event(EventB eb)
{
event_bindings.push_back(eb);
}
void del_event(long e_id) //note could optimize by sorting, but meh - small lists...
{
for (int i = 0; i < event_bindings.size(); i++)
{
EventB e_b = event_bindings[i];
if (e_b.event_id == e_id)
{
event_bindings.erase(event_bindings.begin()+i);
}
break;
}
}
void poll_s_event() //request the server for any events (
{
std::string json = check_server();
if (json == "{}")
{
return;
}
std::vector<std::string> names;
std::vector<float> vals;
for (int i=0; i < names.size(); i++)
{
std::string name = names[i];
if (name == "delete")
{
del_event(long(vals[i]));
}
else if (name == "create")
{
long id = long(vals[i]);
bool ct = false; float st = 0; float bt = 0;
bool ch = false; float sh = 0; float bh = 0;
bool cx = false; float sx = 0; float bx = 0;
bool cy = false; float sy = 0; float by = 0;
bool cz = false; float sz = 0; float bz = 0;
bool cv = false; float sv = 0; float bv = 0;
bool ci = false; float si = 0; float bi = 0;
i++;
for (; i < names.size(); i++)
{
if (name == "temp_s"){st = vals[i]; ct = true;}
else if (name == "temp_b"){bt = vals[i]; ct = true;}
else if (name == "humd_s"){sh = vals[i]; ch = true;}
else if (name == "humd_b"){bh = vals[i]; ch = true;}
else if (name == "xacc_s"){sx = vals[i]; cx = true;}
else if (name == "xacc_b"){bx = vals[i]; cx = true;}
else if (name == "yacc_s"){sy = vals[i]; cy = true;}
else if (name == "yacc_b"){by = vals[i]; cy = true;}
else if (name == "zacc_s"){sz = vals[i]; cz = true;}
else if (name == "zacc_b"){bz = vals[i]; cz = true;}
else if (name == "visi_s"){sv = vals[i]; cv = true;}
else if (name == "visi_b"){bv = vals[i]; cv = true;}
else if (name == "infa_s"){si = vals[i]; ci = true;}
else if (name == "infa_b"){bi = vals[i]; ci = true;}
else if (name == "end")
{
EventB e_b = {.event_id = id,
.c_temp = ct, .s_temp = st, .b_temp = bt,
.c_humd = ch, .s_humd = sh, .b_humd = bh,
.c_xacc = cx, .s_xacc = sx, .b_xacc = bx,
.c_yacc = cy, .s_yacc = sy, .b_yacc = by,
.c_zacc = cz, .s_zacc = sz, .b_zacc = bz,
.c_visi = cv, .s_visi = sv, .b_visi = bv,
.c_infa = ci, .s_infa = si, .b_infa = bi};
add_event(e_b);
break;
}
}
}
}
}
Readings poll_sensors() //get a sensor reading
{
Readings rar = {.temp = temp_sensor.readTemperature(),
.humd = temp_sensor.readHumidity(),
.xacc = accel.x(),
.yacc = accel.y(),
.zacc = accel.z(),
.visi = light_sensor.getLuminosity(TSL2561_VISIBLE),
.infa = light_sensor.getLuminosity(TSL2561_INFRARED)};
return rar;
}
void send_sensor_readings()
{
Readings r = poll_sensors();
std::vector<std::string> names;
names.push_back("temp"); names.push_back("humd"); names.push_back("xacc");
names.push_back("yacc"); names.push_back("zacc"); names.push_back("visi");
names.push_back("infa");
std::vector<float> vals;
vals.push_back(r.temp); vals.push_back(r.humd); vals.push_back(r.xacc);
vals.push_back(r.yacc); vals.push_back(r.zacc); vals.push_back(r.visi);
vals.push_back(r.infa);
std::string almost = jsonit(names, vals);
almost.erase(0,1); //remove the early curly bracket
std::string to_send = std::string("{\"mbed_id\" : \"") + wifi.get_mac_address() + "\"," + almost;
send_to_server(to_send.c_str());
}
void eventcall(int id) //call the event, i.e. flag to the server
{
char buffer[20];
sprintf(buffer, "%s %d", "event triggered:", id);
std::string jcode = std::string("{\"event\":") + to_string(id) + "}";
send_to_server(jcode.c_str());
//lcd_print(buffer); //temp test message
// send the message to the server with the event id
}
void check_events() //check all events to see if any are triggered
{
Readings r = poll_sensors();
for (int i = 0; i < event_bindings.size(); i++)
{
EventB e_b = event_bindings[i];
bool outof = false;
if (e_b.c_temp && !(e_b.s_temp < r.temp && r.temp < e_b.b_temp))
{
outof = true;
}
if (e_b.c_humd && !(e_b.s_humd < r.humd && r.humd < e_b.b_humd))
{
outof = true;
}
if (e_b.c_xacc && !(e_b.s_xacc < r.xacc && r.xacc < e_b.b_xacc))
{
outof = true;
}
if (e_b.c_yacc && !(e_b.s_yacc < r.yacc && r.yacc < e_b.b_yacc))
{
outof = true;
}
if (e_b.c_zacc && !(e_b.s_zacc < r.zacc && r.zacc < e_b.b_zacc))
{
outof = true;
}
if (e_b.c_visi && !(e_b.s_visi < r.visi && r.visi < e_b.b_visi))
{
outof = true;
}
if (e_b.c_infa && !(e_b.s_infa < r.infa && r.infa < e_b.b_infa))
{
outof = true;
}
if (outof)
{
eventcall(e_b.event_id);
}
else
{
lcd_print("");
}
}
}
bool init() //initialization, setting up wifi, and sending mac address to server
{
lcd_print("connecting...");
int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
if (ret != 0) {
lcd_print("Connection error.");
return false;
}
lcd_print("Successfully connected!");
lcd_print(wifi.get_mac_address());
send_to_server((std::string("{\"id\":\"")+wifi.get_mac_address()+"\"}").c_str()); //give the server the mbed id
return true;
// send the address to the server
// back will be returned many requests to make events
}
int main() {
//init();
//while (true)
//{
// send_to_server("{\"yo\", 1}");
// wait_ms(100);
//}
if (!init())
{
return -1;
}
while (true)
{
wait_ms(10);
poll_s_event();
check_events();
}
// MAIN CODE HERE
}