-
Notifications
You must be signed in to change notification settings - Fork 1
/
4_sensor_node.ino
473 lines (375 loc) · 14.8 KB
/
4_sensor_node.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <JSONVar.h>
#include <Arduino_JSON.h>
#include <JSON.h>
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <BH1750.h>
#include <ThingSpeak.h>
// DHT sensor
#define DHTPIN 0 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22
// Pressure sensor
#define BME_SCK 14
#define BME_MISO 12
#define BME_MOSI 13
#define BME_CS 15
#define SEALEVELPRESSURE_HPA (1013.25)
#define ADDR_6713 0x15 // default I2C slave address
BH1750 lightMeter;
// Thingspeak server credentials
unsigned long channel = 906530;
const char* apiKey = "I156R9EPTSNXFRMT";
Adafruit_BME280 bme;
DHT dht(DHTPIN, DHTTYPE);
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int data [4];
int CO2ppmValue;
// ##################### Update the Wifi SSID, Password and IP adress of the server ##########
// WIFI params
char* WIFI_SSID = "rogu";
char* WIFI_PSWD = "12345678";
String CSE_IP = "onem2m.iiit.ac.in";
// #######################################################
int WIFI_DELAY = 100; //ms
// oneM2M : CSE params
int CSE_HTTP_PORT = 443;
String CSE_NAME = "in-name";
String CSE_M2M_ORIGIN = "admin:admin";
// oneM2M : resources' params
String DESC_CNT_NAME = "DESCRIPTOR";
String DATA_CNT_NAME = "DATA";
String CMND_CNT_NAME = "COMMAND";
int TY_AE = 2;
int TY_CNT = 3;
int TY_CI = 4;
int TY_SUB = 23;
// HTTP constants
int LOCAL_PORT = 9999;
char* HTTP_CREATED = "HTTP/1.1 201 Created";
char* HTTP_OK = "HTTP/1.1 200 OK\r\n";
int REQUEST_TIME_OUT = 5000; //ms
//MISC
int SERIAL_SPEED = 9600;
#define DEBUG
///////////////////////////////////////////
// Global variables
WiFiServer server(LOCAL_PORT); // HTTP Server (over WiFi). Binded to listen on LOCAL_PORT contant
WiFiClient client;
String context = "";
String command = ""; // The received command
// Method for creating an HTTP POST with preconfigured oneM2M headers
// param : url --> the url path of the targted oneM2M resource on the remote CSE
// param : ty --> content-type being sent over this POST request (2 for ae, 3 for cnt, etc.)
// param : rep --> the representaton of the resource in JSON format
String doPOST(String url, int ty, String rep) {
String postRequest = String() + "POST " + url + " HTTP/1.1\r\n" +
"Host: " + CSE_IP + ":" + CSE_HTTP_PORT + "\r\n" +
"X-M2M-Origin: " + CSE_M2M_ORIGIN + "\r\n" +
"Content-Type: application/json;ty=" + ty + "\r\n" +
"Content-Length: " + rep.length() + "\r\n"
"Connection: close\r\n\n" +
rep;
// Connect to the CSE address
Serial.println("connecting to " + CSE_IP + ":" + CSE_HTTP_PORT + " ...");
// Get a client
WiFiClient client;
if (!client.connect(CSE_IP, CSE_HTTP_PORT)) {
Serial.println("Connection failed !");
return "error";
}
// if connection succeeds, we show the request to be send
#ifdef DEBUG
Serial.println(postRequest);
#endif
// Send the HTTP POST request
client.print(postRequest);
// Manage a timeout
unsigned long startTime = millis();
while (client.available() == 0) {
if (millis() - startTime > REQUEST_TIME_OUT) {
Serial.println("Client Timeout");
client.stop();
return "error";
}
}
// If success, Read the HTTP response
String result = "";
if (client.available()) {
result = client.readStringUntil('\r');
// Serial.println(result);
}
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection...");
return result;
}
// Method for creating an ApplicationEntity(AE) resource on the remote CSE (this is done by sending a POST request)
// param : ae --> the AE name (should be unique under the remote CSE)
String createAE(String ae) {
String aeRepresentation =
"{\"m2m:ae\": {"
"\"rn\":\"" + ae + "\","
"\"api\":\"org.demo." + ae + "\","
"\"rr\":\"true\","
"\"poa\":[\"http://" + WiFi.localIP().toString() + ":" + LOCAL_PORT + "/" + ae + "\"]"
"}}";
#ifdef DEBUG
Serial.println(aeRepresentation);
#endif
return doPOST("/" + CSE_NAME, TY_AE, aeRepresentation);
}
// Method for creating an Container(CNT) resource on the remote CSE under a specific AE (this is done by sending a POST request)
// param : ae --> the targeted AE name (should be unique under the remote CSE)
// param : cnt --> the CNT name to be created under this AE (should be unique under this AE)
String createCNT(String ae, String cnt) {
String cntRepresentation =
"{\"m2m:cnt\": {"
"\"rn\":\"" + cnt + "\","
"\"min\":\"" + -1 + "\""
"}}";
return doPOST("/" + CSE_NAME + "/" + ae, TY_CNT, cntRepresentation);
}
// Method for creating an ContentInstance(CI) resource on the remote CSE under a specific CNT (this is done by sending a POST request)
// param : ae --> the targted AE name (should be unique under the remote CSE)
// param : cnt --> the targeted CNT name (should be unique under this AE)
// param : ciContent --> the CI content (not the name, we don't give a name for ContentInstances)
String createCI(String ae, String cnt, String ciContent) {
String ciRepresentation =
"{\"m2m:cin\": {"
"\"con\":\"" + ciContent + "\""
"}}";
return doPOST("/" + CSE_NAME + "/" + ae + "/" + cnt, TY_CI, ciRepresentation);
}
// Method for creating an Subscription (SUB) resource on the remote CSE (this is done by sending a POST request)
// param : ae --> The AE name under which the SUB will be created .(should be unique under the remote CSE)
// The SUB resource will be created under the COMMAND container more precisely.
String createSUB(String ae) {
String subRepresentation =
"{\"m2m:sub\": {"
"\"rn\":\"SUB_" + ae + "\","
"\"nu\":[\"" + CSE_NAME + "/" + ae + "\"], "
"\"nct\":1"
"}}";
return doPOST("/" + CSE_NAME + "/" + ae + "/" + CMND_CNT_NAME, TY_SUB, subRepresentation);
}
// Method to register a module (i.e. sensor or actuator) on a remote oneM2M CSE
void registerModule(String module, bool isActuator, String intialDescription, String initialData) {
if (WiFi.status() == WL_CONNECTED) {
String result;
// 1. Create the ApplicationEntity (AE) for this sensor
result = createAE(module);
if (result == HTTP_CREATED) {
#ifdef DEBUG
Serial.println("AE " + module + " created !");
#endif
// 2. Create a first container (CNT) to store the description(s) of the sensor
result = createCNT(module, DESC_CNT_NAME);
if (result == HTTP_CREATED) {
#ifdef DEBUG
Serial.println("CNT " + module + "/" + DESC_CNT_NAME + " created !");
#endif
// Create a first description under this container in the form of a ContentInstance (CI)
result = createCI(module, DESC_CNT_NAME, intialDescription);
if (result == HTTP_CREATED) {
#ifdef DEBUG
Serial.println("CI " + module + "/" + DESC_CNT_NAME + "/{initial_description} created !");
#endif
}
}
// 3. Create a second container (CNT) to store the data of the sensor
result = createCNT(module, DATA_CNT_NAME);
if (result == HTTP_CREATED) {
#ifdef DEBUG
Serial.println("CNT " + module + "/" + DATA_CNT_NAME + " created !");
#endif
// Create a first data value under this container in the form of a ContentInstance (CI)
result = createCI(module, DATA_CNT_NAME, initialData);
if (result == HTTP_CREATED) {
#ifdef DEBUG
Serial.println("CI " + module + "/" + DATA_CNT_NAME + "/{initial_aata} created !");
#endif
}
}
// 3. if the module is an actuator, create a third container (CNT) to store the received commands
if (isActuator) {
result = createCNT(module, CMND_CNT_NAME);
if (result == HTTP_CREATED) {
#ifdef DEBUG
Serial.println("CNT " + module + "/" + CMND_CNT_NAME + " created !");
#endif
// subscribe to any ne command put in this container
result = createSUB(module);
if (result == HTTP_CREATED) {
#ifdef DEBUG
Serial.println("SUB " + module + "/" + CMND_CNT_NAME + "/SUB_" + module + " created !");
#endif
}
}
}
}
}
}
void init_WiFi() {
Serial.println("Connecting to " + String(WIFI_SSID) + " ...");
WiFi.persistent(false);
WiFi.begin(WIFI_SSID, WIFI_PSWD);
// wait until the device is connected to the wifi network
while (WiFi.status() != WL_CONNECTED) {
delay(WIFI_DELAY);
Serial.print(".");
}
// Connected, show the obtained ip address
Serial.println("WiFi Connected ==> IP Address = " + WiFi.localIP().toString());
}
void init_pressure() {
unsigned status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
}
}
void init_HTTPServer() {
server.begin();
Serial.println("Local HTTP Server started !");
}
void task_HTTPServer() {
// Check if a client is connected
client = server.available();
if (!client)
return;
// Wait until the client sends some data
Serial.println("New client connected. Receiving request... ");
while (!client.available()) {
#ifdef DEBUG_MODE
Serial.print(".");
#endif
delay(5);
}
// Read the request
String request = client.readString();
Serial.println(request);
client.flush();
int start, end;
// identify the right module (sensor or actuator) that received the notification
// the URL used is ip:port/ae
start = request.indexOf("/");
end = request.indexOf("HTTP") - 1;
context = request.substring(start + 1, end);
#ifdef DEBUG
Serial.println(String() + start + " , " + end + " -> " + context + ".");
#endif
// ingore verification messages
if (request.indexOf("vrq") > 0) {
client.flush();
return;
}
//Parse the request and identify the requested command from the device
//Request should be like "[operation_name]"
start = request.indexOf("[");
end = request.indexOf("]"); // first occurence of
command = request.substring(start + 1, end);
#ifdef DEBUG
Serial.println(String() + start + " , " + end + " -> " + command + ".");
#endif
client.flush();
}
/////////////////////////////////////////////////////////////////////////////////
// ######################################################## //
// ######### USE THIS SPACE TO DECLARE VARIABLES ######### //
// ######################################################## //
float temp, hum, soundvolts, pressure, altitude;
//double soundvolts;/
// ######################################################## //
void setup() {
// intialize the serial liaison
Serial.begin(SERIAL_SPEED);
// Connect to WiFi network
init_WiFi();
// Start HTTP server
init_HTTPServer();
// ######### USE THIS SPACE FOR YOUR SETUP CODE ######### //
dht.begin();
Wire.begin();
lightMeter.begin();
init_pressure();
ThingSpeak.begin(client);
// ###################################################### //
}
// Main loop of the µController
void loop() {
// ###############################################################
// ####################### DHT22 sensor ##########################
temp = dht.readTemperature();
hum = dht.readHumidity();
// ###################### Pressure sensor #########################
pressure = bme.readPressure() / 100.0F;
altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
// ####################### Light Sensor ###########################
uint16_t lux = lightMeter.readLightLevel();
// ####################### CO2 Sensor #############################
int co2Value =readC02();
// ################################################################### //
// ######### USE THIS SPACE FOR YOUR SENDING DATA TO SERVER ######### //
// ################################################################### //
/*
CreateCI(AE_NAME,CONTAINER_NAME,SENSOR_VALUE)
CreateCI is what posts your sensor data into the container.
In the below example:
AE_NAME: Team8_Automated_Driving (As stated in the resource tree)
CONTAINER_NAME : node_1 ( or as stated in the resource tree)
SENSOR_VALUE : string of comma separated all sensor values (Eg: 27,25 is temp,hum)
*/
// Storing as a string in a single containers
String sensor_value_string;
sensor_value_string = String("DHT : ") + String(temp) + String(",") + String(hum) + String(", Light : ") + String(lux) + String(", Pressure : ") + String(pressure) + String(",") + String(altitude) + String(", CO2 : ") + String(CO2ppmValue);
createCI("Team42_Ambient_classroom-2_Large_class", "node_2", sensor_value_string);
// Check if the data instance was created.
// Upload to ThingSpeak
ThingSpeak.setField(1, String(temp));
ThingSpeak.setField(2, String(hum));
ThingSpeak.setField(3, String(lux));
ThingSpeak.setField(4, String(pressure));
ThingSpeak.setField(5, String(altitude));
ThingSpeak.setField(6, String(CO2ppmValue));
ThingSpeak.writeFields(channel, apiKey);
delay(15000); // DO NOT CHANGE THIS VALUE
// ################################################################### //
}
int readC02()
{
// start I2C
Wire.beginTransmission(ADDR_6713);
Wire.write(0x04); Wire.write(0x13); Wire.write(0x8B); Wire.write(0x00); Wire.write(0x01);
// end transmission
Wire.endTransmission();
// read report of current gas measurement in ppm
delay(2000);
Wire.requestFrom(ADDR_6713, 4); // request 4 bytes from slave device
data[0] = Wire.read();
data[1] = Wire.read();
data[2] = Wire.read();
data[3] = Wire.read();
Serial.print("Func code: "); Serial.print(data[0],HEX);
Serial.print(" byte count: "); Serial.println(data[1],HEX);
Serial.print("MSB: 0x"); Serial.print(data[2],HEX); Serial.print(" ");
Serial.print("LSB: 0x"); Serial.print(data[3],HEX); Serial.print(" ");
CO2ppmValue = ((data[2] * 0xFF ) + data[3]);
}