Skip to content

Commit 414dbf7

Browse files
author
Choong Jin Ng
committed
Implemented ESP-NOW functionality between reader and sensor module. Implemented demo flag for sensor module
1 parent 3865e53 commit 414dbf7

File tree

2 files changed

+222
-205
lines changed

2 files changed

+222
-205
lines changed

esp32_sensor_module/esp32_sensor_module.ino

Lines changed: 87 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
#define SW_VERSION "0.0.1"
1717
#define HW_VERSION "0.0.3b"
1818

19+
/**
20+
Flags
21+
*/
22+
#define DEMO // Comment this out for production
23+
1924
/**
2025
Libraries
2126
*/
22-
#include "WiFi.h"
27+
#include <WiFi.h>
2328
#include "Wire.h" // ESP32 Wire.h
29+
#include <esp_wifi.h>
2430
#include <esp_now.h>
2531

2632
/**
@@ -93,7 +99,16 @@ typedef struct IMU_MSG {
9399

94100
#define READER_WIFI_SSID "CANnect_Reader_WiFi_0.1" // SSID of reader WiFi
95101
#define READER_WIFI_PASSWORD "redboats" // Password of reader WiFi
102+
#define READER_WIFI_CHANNEL 14
96103

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
97112

98113
/**
99114
Variables
@@ -146,15 +161,18 @@ void imuSetup(void) {
146161

147162
void wifiSetup(void) {
148163
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());
151167
}
152168

153169
void espNOWSetup(void) {
154170
if (esp_now_init() != ESP_OK) {
155171
Serial.println("Error initializing ESP-NOW");
156172
return;
157173
}
174+
175+
esp_now_register_send_cb(OnDataSent);
158176
}
159177

160178
int writeRegister(uint8_t address, uint8_t value)
@@ -225,9 +243,9 @@ int readGyroscope(float& x, float& y, float& z) {
225243
return 0;
226244
}
227245

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;
231249

232250
return 1;
233251
}
@@ -244,15 +262,15 @@ int readAcceleration(float& x, float& y, float& z) {
244262
return 0;
245263
}
246264

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;
250268

251269
return 1;
252270
}
253271

254272
int readTemperature(float& temp) {
255-
temp = 0; // not implemented yet
273+
temp = 0.0; // not implemented yet
256274

257275
return 1;
258276
}
@@ -263,67 +281,76 @@ char* convert_int16_to_str(int i) { // converts int16 to string. Moreover, resul
263281
return tmp_str;
264282
}
265283

266-
// Callback when data is received
267-
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingD, int len) {
268-
269-
}
270-
271284
// Callback when data is sent
272285
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
273286
Serial.print("\r\nLast Packet Send Status:\t");
274287
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("");
275297
}
276298

277299
/**
278300
Wait forever for connection to reader
279301
*/
280302
void waitForConnectionToReader(void) {
281303
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");
295324
}
296-
297-
Serial.println("Successfully connected to reader");
298325
}
299326

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);
319346
}
320347

321348
/**
322349
Register and add reader
323350
*/
324351
bool espNOWAddReader(const uint8_t *readerMacAddress) {
325352
memcpy(readerInfo.peer_addr, readerMacAddress, 6);
326-
readerInfo.channel = 0;
353+
readerInfo.channel = READER_WIFI_CHANNEL;
327354
readerInfo.encrypt = false;
328355

329356
// Add peer
@@ -332,7 +359,14 @@ bool espNOWAddReader(const uint8_t *readerMacAddress) {
332359
return false;
333360
}
334361

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("");
336370

337371
return true;
338372
}
@@ -354,8 +388,9 @@ void espNOWRemoveReader(void) {
354388

355389
void sendIMUMsg(void) {
356390
IMU_MSG imuMsg;
357-
bool goodData = true;
391+
imuMsg.msgID = "6DOF";
358392

393+
bool goodData = true;
359394
Serial.println("Obtaining data...");
360395

361396
if (readAcceleration(imuMsg.accX, imuMsg.accY, imuMsg.accZ) == 0) {
@@ -374,7 +409,6 @@ void sendIMUMsg(void) {
374409
}
375410

376411
if (goodData && esp_now_is_peer_exist(readerInfo.peer_addr)) {
377-
Serial.println("Sending data");
378412
esp_err_t result = esp_now_send(readerInfo.peer_addr, (uint8_t *) &imuMsg, sizeof(imuMsg));
379413
}
380414
}
@@ -388,29 +422,5 @@ void loop() {
388422
waitForConnectionToReader();
389423
}
390424

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-
415425
delay(100);
416426
}

0 commit comments

Comments
 (0)