forked from lets-fiware/fiware-iot-device
/
context-provider-01.ino
180 lines (140 loc) · 5.02 KB
/
context-provider-01.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
/**
* context-provider-01.ino
*
* Context Provider
*
* Copyright 2018 Kazuhito Suda
*
* Created on: November 18, 2018
*
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h> // Adafruit Unified Sensor
#include <Adafruit_BME280.h> // Adafruit BME280 libary
#include <ArduinoJson.h> // ArduinoJson 5.13.3
#define SEALEVELPRESSURE_HPA (1013.25)
#define I2C_SDA 4 // BME280 SDA <---> ESP32 GPIO4
#define I2C_SCL 5 // BME280 SCL <---> ESP32 GPIO5
Adafruit_BME280 bme; // I2C
char essid[] = "your essid";
char passphrase[] = "your passphrase";
const char entityType[] = "WeatherObserved";
const char entityId[] = "urn:ngsi-ld:WeatherObserved:sensor002";
WebServer server(80);
void setup() {
Serial.begin(115200);
Serial.println("Start");
setupBME280();
connectWifi();
server.on("/v1/weatherObserved/queryContext", queryContext);
server.onNotFound(handleNotFound);
server.begin();
}
void loop() {
server.handleClient();
}
void queryContext() {
if (server.method() != HTTP_POST && server.args() != 1) {
sendErrorMessage(400, "BadRequest", "service not found");
return;
}
String message = "";
for (uint8_t i=0; i<server.args(); i++){
message += server.argName(i) + ": " + server.arg(i) + "\n";
}
Serial.print(message);
StaticJsonBuffer<500> jsonBufferPost;
JsonObject& rootPost = jsonBufferPost.parseObject(server.arg(0));
if (!rootPost.success()) {
sendErrorMessage(400, "BadRequest", "post data is incorrect");
return;
}
JsonObject& entities = rootPost["entities"][0];
auto type = entities["type"].as<char*>();
if (type == NULL || strcmp(type, entityType) != 0) {
sendErrorMessage(404, "Not found", "entity type");
return;
}
auto id = entities["id"].as<char*>();
if (id == NULL || strcmp(id, entityId) != 0) {
sendErrorMessage(404, "Not found", "entity id");
return;
}
const size_t bufferSize = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + 50;
StaticJsonBuffer<bufferSize> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonArray& contextResponses = root.createNestedArray("contextResponses");
JsonObject& contextResponses_0 = contextResponses.createNestedObject();
JsonObject& contextResponses_0_contextElement = contextResponses_0.createNestedObject("contextElement");
JsonArray& contextResponses_0_contextElement_attributes = contextResponses_0_contextElement.createNestedArray("attributes");
JsonArray& attributes = rootPost["attributes"];
for (int i = 0; i < attributes.size(); i++ ) {
auto attr = attributes[i].as<char*>();
if (attr == NULL || !addAttributes(contextResponses_0_contextElement_attributes, attr)) {
sendErrorMessage(404, "Not found", "attribute");
return;
}
}
contextResponses_0_contextElement["id"] = entityId;
contextResponses_0_contextElement["isPattern"] = "false";
contextResponses_0_contextElement["type"] = entityType;
JsonObject& statusCode = contextResponses_0.createNestedObject("statusCode");
statusCode["code"] = "200";
statusCode["reasonPhrase"] = "OK";
String postData = "";
root.printTo(postData);
Serial.println(postData);
server.send(200, "application/json", postData);
}
bool addAttributes(JsonArray& jsonArray, const char *attrName) {
if (!strcmp(attrName, "temperature")) {
JsonObject& attr = jsonArray.createNestedObject();
attr["name"] = "temperature";
attr["type"] = "Number";
attr["value"] = bme.readTemperature();
} else if (!strcmp(attrName, "relativeHumidity")) {
JsonObject& attr = jsonArray.createNestedObject();
attr["name"] = "relativeHumidity";
attr["type"] = "Number";
attr["value"] = bme.readHumidity();
} else if (!strcmp(attrName, "atmosphericPressure")) {
JsonObject& attr = jsonArray.createNestedObject();
attr["name"] = "atmosphericPressure";
attr["type"] = "Number";
attr["value"] = bme.readPressure() / 100.0F;
} else {
return false;
}
return true;
}
void handleNotFound() {
sendErrorMessage(400, "BadRequest", "service not found");
}
void sendErrorMessage(int status, String err, String description) {
String message = "{\"error\":\"" + err + "\",\"description\":\"" + description + "\"}";
server.send(status, "application/json", message);
}
void connectWifi() {
WiFi.mode(WIFI_STA);
WiFi.begin(essid, passphrase);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("");
Serial.println("Connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void setupBME280() {
Wire.begin(I2C_SDA, I2C_SCL);
bool status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}