-
Notifications
You must be signed in to change notification settings - Fork 0
/
webthings_version.ino
287 lines (221 loc) · 6.91 KB
/
webthings_version.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
/*
rp2040_webthing - final project of Enabling Technologies for IIoT Summer School, 2022
This version of the arduino code exposes the functionality as a Web Thing
Required hardware:
- Arduino Nano rp2040 connect
*/
#define LARGE_JSON_BUFFERS 1
#include <Arduino.h>
#include "Thing.h"
#define ARDUINO_SAMD_NANO_33_IOT
#include "WebThingAdapter.h"
#include "arduino_secrets.h"
#include "LSM6DSOXSensor.h"
#include "lsm6dsox_activity_recognition_for_mobile.h"
#ifdef ARDUINO_SAM_DUE
#define DEV_I2C Wire1
#elif defined(ARDUINO_ARCH_STM32)
#define DEV_I2C Wire
#elif defined(ARDUINO_ARCH_AVR)
#define DEV_I2C Wire
#else
#define DEV_I2C Wire
#endif
#define INT_1 INT_IMU
/* Components */
LSM6DSOXSensor AccGyr(&DEV_I2C, LSM6DSOX_I2C_ADD_L);
/* MLC */
ucf_line_t *ProgramPointer;
int32_t LineCounter;
int32_t TotalNumberOfLine;
volatile int mems_event = 0;
void INT1Event_cb();
void setMovementProp(uint8_t status);
/* WebThingAdapter configurations */
char ssid[] = SECRET_SSID;
char password[] = SECRET_PASS;
WebThingAdapter *adapter;
const char *sensorTypes[] = {nullptr};
ThingDevice activitySensor("activity-sensor-1", "Arduino Activity Sensor", sensorTypes);
ThingProperty stationary("stationary", "stationary", BOOLEAN, "OnOffProperty");
ThingProperty walking("walking", "walking", BOOLEAN, "OnOffProperty");
ThingProperty jogging("jogging", "jogging", BOOLEAN, "OnOffProperty");
ThingProperty biking("biking", "biking", BOOLEAN, "OnOffProperty");
ThingProperty driving("driving", "driving", BOOLEAN, "OnOffProperty");
ThingProperty unknown("unknown", "unknown", BOOLEAN, "OnOffProperty");
#if defined(LED_BUILTIN)
const int ledPin = LED_BUILTIN;
#else
const int ledPin = 13;
#endif
bool lastOn = false;
void setup() {
/*** Initialize MLC stuff ***/
uint8_t mlc_out[8];
pinMode(LED_BUILTIN, OUTPUT);
// Force INT1 of LSM6DSOX low in order to enable I2C
pinMode(INT_1, OUTPUT);
digitalWrite(INT_1, LOW);
delay(200);
// Initialize serial for output.
Serial.begin(9600);
// Initialize I2C bus.
DEV_I2C.begin();
AccGyr.begin();
AccGyr.Enable_X();
AccGyr.Enable_G();
// Feed the program to Machine Learning Core
// Activity Recognition Default program
ProgramPointer = (ucf_line_t *)lsm6dsox_activity_recognition_for_mobile;
TotalNumberOfLine = sizeof(lsm6dsox_activity_recognition_for_mobile) / sizeof(ucf_line_t);
Serial.println("Activity Recognition for LSM6DSOX MLC");
Serial.print("UCF Number Line=");
Serial.println(TotalNumberOfLine);
for (LineCounter=0; LineCounter<TotalNumberOfLine; LineCounter++) {
if(AccGyr.Write_Reg(ProgramPointer[LineCounter].address, ProgramPointer[LineCounter].data)) {
Serial.print("Error loading the Program to LSM6DSOX at line: ");
Serial.println(LineCounter);
while(1) {
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
}
}
Serial.println("Program loaded inside the LSM6DSOX MLC");
pinMode(INT_1, INPUT);
attachInterrupt(INT_1, INT1Event_cb, RISING);
// We need to wait for a time window before having the first MLC status
delay(3000);
AccGyr.Get_MLC_Output(mlc_out);
/*** Initialize WebThingAdapter stuff ***/
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(115200);
Serial.println("");
Serial.print("Connecting to \"");
Serial.print(ssid);
Serial.println("\"");
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
bool blink = true;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
blink = !blink;
}
digitalWrite(ledPin, LOW); // active low led
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter("w25", WiFi.localIP());
activitySensor.description = "A motion classifier sensor powered by the Arduino rp2040's ML Core";
stationary.title = "stationary";
stationary.readOnly = "true";
walking.title = "walking";
walking.readOnly = "true";
jogging.title = "jogging";
jogging.readOnly = "true";
biking.title = "biking";
biking.readOnly = "true";
driving.title = "driving";
driving.readOnly = "true";
unknown.title = "unknown";
unknown.readOnly = "true";
activitySensor.addProperty(&stationary);
activitySensor.addProperty(&walking);
activitySensor.addProperty(&jogging);
activitySensor.addProperty(&biking);
activitySensor.addProperty(&driving);
activitySensor.addProperty(&unknown);
adapter->addDevice(&activitySensor);
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(activitySensor.id);
// Send the first status obtained
setMovementProp(mlc_out[0]);
adapter->update();
}
void loop() {
adapter->update();
if (mems_event) {
resetPropertyValues();
adapter->update();
mems_event=0;
LSM6DSOX_MLC_Status_t status;
AccGyr.Get_MLC_Status(&status);
if (status.is_mlc1) {
uint8_t mlc_out[8];
AccGyr.Get_MLC_Output(mlc_out);
setMovementProp(mlc_out[0]);
}
}
}
void INT1Event_cb() {
// Interrupt to indicate MEMS event
mems_event = 1;
}
void resetPropertyValues(){
ThingPropertyValue defaultVal;
defaultVal.boolean = false;
stationary.setValue(defaultVal);
walking.setValue(defaultVal);
jogging.setValue(defaultVal);
biking.setValue(defaultVal);
driving.setValue(defaultVal);
unknown.setValue(defaultVal);
}
void setMovementProp(uint8_t status){
ThingPropertyValue stationaryVal;
stationaryVal.boolean = (status==0);
stationary.setValue(stationaryVal);
ThingPropertyValue walkingVal;
walkingVal.boolean = (status==1);
walking.setValue(walkingVal);
ThingPropertyValue joggingVal;
joggingVal.boolean = (status==4);
jogging.setValue(joggingVal);
ThingPropertyValue bikingVal;
bikingVal.boolean = (status==8);
biking.setValue(bikingVal);
ThingPropertyValue drivingVal;
drivingVal.boolean = (status==12);
driving.setValue(drivingVal);
ThingPropertyValue unknownVal;
unknownVal.boolean = !(stationaryVal.boolean || walkingVal.boolean || joggingVal.boolean || bikingVal.boolean || drivingVal.boolean);
unknown.setValue(unknownVal);
formatForSerialDebugging(status);
}
void formatForSerialDebugging(uint8_t status){
String payload;
switch(status) {
case 0:
payload = "Stationary";
break;
case 1:
payload = "Walking";
break;
case 4:
payload = "Jogging";
break;
case 8:
payload = "Biking";
break;
case 12:
payload = "Driving";
break;
default:
payload = "Unknown";
break;
}
Serial.print("Sending payload - ");
Serial.println(payload);
}