16
16
#define SW_VERSION " 0.0.1"
17
17
#define HW_VERSION " 0.0.3b"
18
18
19
+ /* *
20
+ Flags
21
+ */
22
+ #define DEMO // Comment this out for production
23
+
19
24
/* *
20
25
Libraries
21
26
*/
22
- #include " WiFi.h"
27
+ #include < WiFi.h>
23
28
#include " Wire.h" // ESP32 Wire.h
29
+ #include < esp_wifi.h>
24
30
#include < esp_now.h>
25
31
26
32
/* *
@@ -93,7 +99,16 @@ typedef struct IMU_MSG {
93
99
94
100
#define READER_WIFI_SSID " CANnect_Reader_WiFi_0.1" // SSID of reader WiFi
95
101
#define READER_WIFI_PASSWORD " redboats" // Password of reader WiFi
102
+ #define READER_WIFI_CHANNEL 14
96
103
104
+ #ifdef DEMO
105
+ #define SCALING_FACTOR 10 // Scales the data for demo purposes
106
+ #else
107
+ #define SCALING_FACTOR 1
108
+ #endif
109
+
110
+ uint8_t sensorMACAddress[] = {0xF0 , 0x08 , 0xD1 , 0xD3 , 0x6D , 0xA0 }; // sensor's address - hard-coded for now
111
+ uint8_t readerMACAddress[] = {0xF0 , 0x08 , 0xD1 , 0xD3 , 0x6D , 0xA1 }; // reader's address - hard-coded for now
97
112
98
113
/* *
99
114
Variables
@@ -146,15 +161,18 @@ void imuSetup(void) {
146
161
147
162
void wifiSetup (void ) {
148
163
WiFi.mode (ESP32_WIFI_MODE);
149
- // selfMACAddress = WiFi.macAddress();
150
- // Serial.println(selfMACAddress);
164
+ esp_wifi_set_mac (ESP_IF_WIFI_STA, &sensorMACAddress[0 ]); // temporarily
165
+
166
+ Serial.println (WiFi.macAddress ());
151
167
}
152
168
153
169
void espNOWSetup (void ) {
154
170
if (esp_now_init () != ESP_OK) {
155
171
Serial.println (" Error initializing ESP-NOW" );
156
172
return ;
157
173
}
174
+
175
+ esp_now_register_send_cb (OnDataSent);
158
176
}
159
177
160
178
int writeRegister (uint8_t address, uint8_t value)
@@ -225,9 +243,9 @@ int readGyroscope(float& x, float& y, float& z) {
225
243
return 0 ;
226
244
}
227
245
228
- x = data[0 ] * 2000.0 / 32768.0 ;
229
- y = data[1 ] * 2000.0 / 32768.0 ;
230
- z = data[2 ] * 2000.0 / 32768.0 ;
246
+ x = SCALING_FACTOR * data[0 ] * 2000.0 / 32768.0 ;
247
+ y = SCALING_FACTOR * data[1 ] * 2000.0 / 32768.0 ;
248
+ z = SCALING_FACTOR * data[2 ] * 2000.0 / 32768.0 ;
231
249
232
250
return 1 ;
233
251
}
@@ -244,15 +262,15 @@ int readAcceleration(float& x, float& y, float& z) {
244
262
return 0 ;
245
263
}
246
264
247
- x = data[0 ] * 4.0 / 32768.0 ;
248
- y = data[1 ] * 4.0 / 32768.0 ;
249
- z = data[2 ] * 4.0 / 32768.0 ;
265
+ x = SCALING_FACTOR * data[0 ] * 4.0 / 32768.0 ;
266
+ y = SCALING_FACTOR * data[1 ] * 4.0 / 32768.0 ;
267
+ z = SCALING_FACTOR * data[2 ] * 4.0 / 32768.0 ;
250
268
251
269
return 1 ;
252
270
}
253
271
254
272
int readTemperature (float & temp) {
255
- temp = 0 ; // not implemented yet
273
+ temp = 0.0 ; // not implemented yet
256
274
257
275
return 1 ;
258
276
}
@@ -263,67 +281,76 @@ char* convert_int16_to_str(int i) { // converts int16 to string. Moreover, resul
263
281
return tmp_str;
264
282
}
265
283
266
- // Callback when data is received
267
- void OnDataRecv (const uint8_t * mac, const uint8_t *incomingD, int len) {
268
-
269
- }
270
-
271
284
// Callback when data is sent
272
285
void OnDataSent (const uint8_t *mac_addr, esp_now_send_status_t status) {
273
286
Serial.print (" \r\n Last Packet Send Status:\t " );
274
287
Serial.println (status == ESP_NOW_SEND_SUCCESS ? " Delivery Success" : " Delivery Fail" );
288
+
289
+ Serial.print (" Mac: " );
290
+ Serial.print (mac_addr[0 ], HEX);
291
+ Serial.print (mac_addr[1 ], HEX);
292
+ Serial.print (mac_addr[2 ], HEX);
293
+ Serial.print (mac_addr[3 ], HEX);
294
+ Serial.print (mac_addr[4 ], HEX);
295
+ Serial.print (mac_addr[5 ], HEX);
296
+ Serial.println (" " );
275
297
}
276
298
277
299
/* *
278
300
Wait forever for connection to reader
279
301
*/
280
302
void waitForConnectionToReader (void ) {
281
303
bool success = false ;
282
-
283
- while (!success) {
284
- int numNetworks = WiFi.scanNetworks ();
285
- Serial.println (" Waiting for Reader" );
286
- delay (NETWORK_SCAN_REST_INTERVAL);
287
-
288
- if (numNetworks != 0 ) {
289
- for (int ii = 0 ; ii < numNetworks; ++ii) {
290
- if (WiFi.SSID (ii) == READER_WIFI_SSID) {
291
- success = connectToReader (WiFi.BSSID (ii));
292
- }
293
- }
294
- }
304
+ //
305
+ // while (!success) {
306
+ // int numNetworks = WiFi.scanNetworks();
307
+ // Serial.println("Waiting for Reader");
308
+ // delay(NETWORK_SCAN_REST_INTERVAL);
309
+ //
310
+ // if (numNetworks != 0) {
311
+ // for (int ii = 0; ii < numNetworks; ++ii) {
312
+ // if (WiFi.SSID(ii) == READER_WIFI_SSID) {
313
+ // success = connectToReader(WiFi.BSSID(ii));
314
+ // }
315
+ // }
316
+ // }
317
+ // }
318
+
319
+ // temporary
320
+ success = connectToReader (&readerMACAddress[0 ]);
321
+
322
+ if (success) {
323
+ Serial.println (" Successfully connected to reader" );
295
324
}
296
-
297
- Serial.println (" Successfully connected to reader" );
298
325
}
299
326
300
- bool connectToReader (const uint8_t *readerMacAddress ) {
301
- int numAttempts = 0 ;
302
- Serial.println (" Found reader!" );
303
- WiFi.begin (READER_WIFI_SSID, READER_WIFI_PASSWORD);
304
- Serial.print (" Attempting to connect to Reader" );
305
-
306
- do {
307
- delay (500 );
308
- numAttempts++;
309
- Serial.print (" ." );
310
- } while (WiFi.status () != WL_CONNECTED && numAttempts < NETWORK_CONNECT_ATTEMPTS);
311
- Serial.println (" ." );
312
-
313
- if (WiFi.status () != WL_CONNECTED) {
314
- Serial.println (" Unable to connect to Reader" );
315
- return false ;
316
- }
317
-
318
- return espNOWAddReader (readerMacAddress );
327
+ bool connectToReader (const uint8_t *mac ) {
328
+ // int numAttempts = 0;
329
+ // Serial.println("Found reader!");
330
+ // WiFi.begin(READER_WIFI_SSID, READER_WIFI_PASSWORD);
331
+ // Serial.print("Attempting to connect to Reader");
332
+ //
333
+ // do {
334
+ // delay(500);
335
+ // numAttempts++;
336
+ // Serial.print(".");
337
+ // } while (WiFi.status() != WL_CONNECTED && numAttempts < NETWORK_CONNECT_ATTEMPTS);
338
+ // Serial.println(".");
339
+ //
340
+ // if (WiFi.status() != WL_CONNECTED) {
341
+ // Serial.println("Unable to connect to Reader");
342
+ // return false;
343
+ // }
344
+
345
+ return espNOWAddReader (mac );
319
346
}
320
347
321
348
/* *
322
349
Register and add reader
323
350
*/
324
351
bool espNOWAddReader (const uint8_t *readerMacAddress) {
325
352
memcpy (readerInfo.peer_addr , readerMacAddress, 6 );
326
- readerInfo.channel = 0 ;
353
+ readerInfo.channel = READER_WIFI_CHANNEL ;
327
354
readerInfo.encrypt = false ;
328
355
329
356
// Add peer
@@ -332,7 +359,14 @@ bool espNOWAddReader(const uint8_t *readerMacAddress) {
332
359
return false ;
333
360
}
334
361
335
- Serial.println (" Successfully added reader!" );
362
+ Serial.print (" Reader mac address is: " );
363
+ Serial.print (readerInfo.peer_addr [0 ], HEX);
364
+ Serial.print (readerInfo.peer_addr [1 ], HEX);
365
+ Serial.print (readerInfo.peer_addr [2 ], HEX);
366
+ Serial.print (readerInfo.peer_addr [3 ], HEX);
367
+ Serial.print (readerInfo.peer_addr [4 ], HEX);
368
+ Serial.print (readerInfo.peer_addr [5 ], HEX);
369
+ Serial.println (" " );
336
370
337
371
return true ;
338
372
}
@@ -354,8 +388,9 @@ void espNOWRemoveReader(void) {
354
388
355
389
void sendIMUMsg (void ) {
356
390
IMU_MSG imuMsg;
357
- bool goodData = true ;
391
+ imuMsg. msgID = " 6DOF " ;
358
392
393
+ bool goodData = true ;
359
394
Serial.println (" Obtaining data..." );
360
395
361
396
if (readAcceleration (imuMsg.accX , imuMsg.accY , imuMsg.accZ ) == 0 ) {
@@ -374,7 +409,6 @@ void sendIMUMsg(void) {
374
409
}
375
410
376
411
if (goodData && esp_now_is_peer_exist (readerInfo.peer_addr )) {
377
- Serial.println (" Sending data" );
378
412
esp_err_t result = esp_now_send (readerInfo.peer_addr , (uint8_t *) &imuMsg, sizeof (imuMsg));
379
413
}
380
414
}
@@ -388,29 +422,5 @@ void loop() {
388
422
waitForConnectionToReader ();
389
423
}
390
424
391
- // float x, y, z;
392
- //
393
- // if (gyroscopeAvailable()) {
394
- // readGyroscope(x, y, z);
395
- //
396
- // Serial.print("Gyroscope: ");
397
- // Serial.print(x);
398
- // Serial.print('\t');
399
- // Serial.print(y);
400
- // Serial.print('\t');
401
- // Serial.println(z);
402
- // }
403
- //
404
- // if (accelerationAvailable()) {
405
- // readAcceleration(x, y, z);
406
- //
407
- // Serial.print("Acceleration: ");
408
- // Serial.print(x);
409
- // Serial.print('\t');
410
- // Serial.print(y);
411
- // Serial.print('\t');
412
- // Serial.println(z);
413
- // }
414
-
415
425
delay (100 );
416
426
}
0 commit comments