Hello,
I require assistance with setting up the secure Arrowhead provider on my ESP8266.
To get the library working I had to downgrade my ESP8266 libraries as per this issue.
For the code skeleton I followed the adapter-quick-demo.
I first tested the setup with the 'Arrowhead.getWebServer()' method in insecure mode and it worked without flaws.
Next I switched on the secure mode and setup the certificates and they seem to be working fine as my provider ("wristband5" with service "pressure5") is succesfully registering with the Arrowhead host at startup and appears in the provider list.
Once I adjusted the sketch to use the 'Arrowhead.getSecureWebServer()' method, it stopped working.
Based on my logging statements it seems as if the ESP8266 is not responding to the calls when the secure web server with HTTPS is used.
Any attempts to access the service fail with a timeout, as no response is coming from the ESP.
I experimented with changing the ports and tried to find the relevant documentation in the core libraries but failed to fix this.
I would be grateful for any pointers into the right direction.
Thanks in advance!
Arduino Sketch
1 //////////////////////////////////////////////////////////////////////////////
2 // LIBRARIES
3
4 #include <TimeLib.h>
5 #include <Dps310.h>
6 #include <WiFiUdp.h>
7 #include <ESP8266WiFi.h>
8 #include <NTPClient.h>
9 #include <ArduinoJson.h>
10 #include <ArrowheadESP.h>
11 #include <Chrono.h>
12 #include <MovingAverage.h>
13 #include <Ewma.h>
14
15 //////////////////////////////////////////////////////////////////////////////
16 // DEFINITIONS
17
18 // ARROWHEAD PARAMETERS
19 ArrowheadESP Arrowhead;
20 #define SENSOR_PIN 16
21 #define SERVER_PORT 9000
22
23 // TREATMENT PARAMETERS
24 MovingAverage<float> smooth_filter(5);
25 MovingAverage<float> avg_filter(50);
26 Ewma exp_filter(0.05);
27
28 // TIME PARAMETERS
29 WiFiUDP ntpUDP;
30 NTPClient timeClient(ntpUDP, "time1.google.com", 0, 60000);
31 unsigned long epochTime = 0;
32
33 // VARIATION CALCULATION PARAMETERS
34 float pressure = 0.0;
35 float temperature = 0.0;
36 float up_pressure = 0.0;
37 float down_pressure = 0.0;
38 float range = 0.0;
39 float position = 0.0;
40 float position2 = 0.0;
41
42 const float ALPHA = 0.1;
43 const float MIN_RANGE = 0.5;
44 float avg_pressure = 0.0;
45
46 // SENSOR PARAMETERS
47 Dps310 Sensor = Dps310();
48 int16_t return_code;
49 int oversampling_rate = 3;
50
51 // ESP8266 PARAMETERS
52 const int led = 2;
53 const int button = 14;
54
55
56 //////////////////////////////////////////////////////////////////////////////
57 // FUNCTION CALCULATE AVERAGE PRESSURE
58 float calc_avg()
59 {
60 int local_count = 0;
61 double average_pressure = 0;
62
63 // Loop to blink led
64 for(local_count = 0; local_count < 30; local_count++)
65 {
66
67 digitalWrite(led, LOW);
68 delay(100);
69 digitalWrite(led, HIGH);
70 delay(100);
71
72 }
73
74 delay(1000);
75
76 digitalWrite(led, HIGH);
77
78 // DWe measure the pressure 150 times and find the aveage value
79 for(local_count = 0; local_count < 150; local_count++)
80 {
81
82 // Measure once the pressure and temperature
83 return_code = Sensor.measureTempOnce(temperature, oversampling_rate);
84 return_code = Sensor.measurePressureOnce(pressure, oversampling_rate);
85
86 // If return_code == 0, there is no error and average_pressure is calculated
87 if (return_code == 0)
88 {
89
90 // Calculate the average pressure
91 avg_filter.push(pressure);
92
93 }
94
95 }
96
97 digitalWrite(led, LOW);
98
99 average_pressure = avg_filter.get();
100
101 // Return the calculated value
102 return average_pressure;
103
104 }
105
106
107 //////////////////////////////////////////////////////////////////////////////
108 // FUNCTION HANDLE SERVICE REQUEST
109 void handleServiceRequest()
110 {
111 Serial.println("Echo request received");
112 String response;
113
114 // Turn on led
115 digitalWrite(led, HIGH);
116
117 // Update current time
118 timeClient.update();
119 epochTime = timeClient.getEpochTime();
120
121 // Build the SenML structure including the data to be sent
122 DynamicJsonDocument root(500);
123 root["Provider name"] = Arrowhead.getArrowheadESPFS().getProviderInfo().systemName;
124 root["Epoch time"] = epochTime;
125 root["Unit"] = "Adimensional";
126 root["Version"] = 1;
127 root["Value"] = position;
128
129 serializeJson(root, response);
130
131 Serial.println("Sending response");
132
133 Arrowhead.getSecureWebServer().send(200, "application/json", response);
134
135 Serial.println("Response sent");
136
137 // Turn off led
138 digitalWrite(led, LOW);
139 }
140
141
142
143
144
145 //////////////////////////////////////////////////////////////////////////////
146 // FUNCTION: SETUP
147 void setup()
148 {
149 bool startupSuccess;
150 String response = "";
151 String serviceRegistryEntry = "";
152
153 // To start printing in screen
154 Serial.begin(115200);
155
156 digitalWrite(led, HIGH);
157
158 // Load files in the /data folder
159 Arrowhead.getArrowheadESPFS().loadConfigFile("netConfig.json");
160 Arrowhead.getArrowheadESPFS().loadSSLConfigFile("sslConfig.json");
161 Arrowhead.getArrowheadESPFS().loadProviderConfigFile("providerConfig.json");
162
163 Arrowhead.useSecureWebServer();
164
165 // Set the Service Registry address and port
166 Arrowhead.setServiceRegistryAddress(
167 Arrowhead.getArrowheadESPFS().getProviderInfo().serviceRegistryAddress,
168 Arrowhead.getArrowheadESPFS().getProviderInfo().serviceRegistryPort);
169
170 Serial.println();
171 Serial.print("Device MAC:");
172 Serial.println(WiFi.macAddress());
173
174 // Connect to the specified WIFI network in /data/netConfig.json
175 // ESP8266 is only compatible with 2.4GHz networks
176 startupSuccess = Arrowhead.begin();
177
178 if (startupSuccess)
179 {
180
181 pinMode(led, OUTPUT);
182 pinMode(button, INPUT);
183
184 // Echo from service serviceRegistryAddress
185 int statusCode = Arrowhead.serviceRegistryEcho(&response);
186
187 Serial.println("Sending echo request...");
188 // Print received answer
189 Serial.print("Status code from server: ");
190 Serial.println(statusCode);
191 Serial.print("Response body from server: ");
192 Serial.println(response);
193
194 // Create JSON-service structure
195 serviceRegistryEntry = "{\"endOfValidity\":\"2035-03-01T12:00:00Z\",\"interfaces\":[\"HTTP-SECURE-JSON\"],\"providerSystem\":{\"address\":\" " + Arrowhead.getIP() + "\",\"authenticationInfo\":\"" + Arrowhead.getArrowheadESPFS().getSSLInfo().publicKey + "\",\"port\":" + SERVER_PORT + ",\"systemName\":\"" + Arrowhead.getArrowheadESPFS().getProviderInfo().systemName + "\"},\"secure\":\"CERTIFICATE\",\"serviceDefinition\":\"pressure5\",\"serviceUri\":\"pressure5\",\"version\":1}";
196
197 // Check is service was registered correctly
198 Serial.println("Sending ServiceRegistryEntry...");
199 Serial.println(serviceRegistryEntry);
200
201 response = "";
202 statusCode = Arrowhead.serviceRegistryRegister(serviceRegistryEntry.c_str(), &response);
203 Serial.print("Status code from server: ");
204 Serial.println(statusCode);
205 Serial.print("Response body from server: ");
206 Serial.println(response);
207 }
208
209 // Connect to Service Registry
210 Arrowhead.getSecureWebServer().on("/pressure5", handleServiceRequest);
211 Arrowhead.getSecureWebServer().begin(SERVER_PORT);
212
213 // To initialise the NTP client with the offset according to our GMT timezone
214 timeClient.begin();
215
216 // The sensor's address is 0x77 (default) or 0x76 (if the SDO pin is pulled-down to GND)
217 // We are using the 0x76 address
218 Sensor.begin(Wire);
219 Wire.beginTransmission(0x76);
220
221 // Calculate the upper pressure and the bottom one
222 up_pressure = calc_avg();
223 down_pressure = calc_avg();
224
225 // Obtain the range
226 range = down_pressure - up_pressure;
227 }
228
229
230
231
232
233
234 //////////////////////////////////////////////////////////////////////////////
235 // FUNCTION LOOP
236 void loop()
237 {
238
239 float pressure_mod;
240 int buttonState = 0;
241
242 // Keep network connection up
243 Arrowhead.loop();
244
245 // Gives return code!=0 if something went wrong
246 // Pressure decreases when height increases
247 return_code = Sensor.measurePressureOnce(temperature, oversampling_rate);
248 return_code = Sensor.measurePressureOnce(pressure, oversampling_rate);
249
250 // Apply filters
251 pressure_mod = exp_filter.filter(pressure);
252 smooth_filter.push(pressure_mod);
253 pressure_mod = smooth_filter.get();
254
255 // Update running average
256 avg_pressure = avg_pressure * (1 - ALPHA) + pressure_mod * ALPHA;
257
258 // Adapt range in real-time
259 if (pressure_mod < up_pressure) {
260 up_pressure = pressure_mod;
261 } else if (pressure_mod > down_pressure) {
262 down_pressure = pressure_mod;
263 }
264
265 // Ensure minimum range and center it around the average
266 range = max(down_pressure - up_pressure, MIN_RANGE);
267 up_pressure = avg_pressure - range / 2;
268 down_pressure = avg_pressure + range / 2;
269
270 // Calculate position (0-100 range, 50 is neutral)
271 position = 100 * (pressure_mod - avg_pressure) / (range / 2) + 50;
272 position = constrain(position, 0, 100);
273
274 Serial.print("Pressure: ");
275 Serial.print(pressure_mod);
276 Serial.print(" Position: ");
277 Serial.println(position);
278
279 delay(100);
280
281 }
Hello,
I require assistance with setting up the secure Arrowhead provider on my ESP8266.
To get the library working I had to downgrade my ESP8266 libraries as per this issue.
For the code skeleton I followed the adapter-quick-demo.
I first tested the setup with the 'Arrowhead.getWebServer()' method in insecure mode and it worked without flaws.
Next I switched on the secure mode and setup the certificates and they seem to be working fine as my provider ("wristband5" with service "pressure5") is succesfully registering with the Arrowhead host at startup and appears in the provider list.
Once I adjusted the sketch to use the 'Arrowhead.getSecureWebServer()' method, it stopped working.
Based on my logging statements it seems as if the ESP8266 is not responding to the calls when the secure web server with HTTPS is used.
Any attempts to access the service fail with a timeout, as no response is coming from the ESP.
I experimented with changing the ports and tried to find the relevant documentation in the core libraries but failed to fix this.
I would be grateful for any pointers into the right direction.
Thanks in advance!
Arduino Sketch