I edited the library to send the accelerometer data via ESPnow but is seems I missed it somewhere. Please, I am a novice!!!
#include <esp_now.h> #include <WiFi.h> #include <ADXL345_WE.h> #include <SPI.h> #define CS_PIN 5 // Chip Select Pin bool spi = true; // flag that SPI shall be used #define CHANNEL 1 // Replace with your receiver MAC address uint8_t broadcastAddress[] = { 0x11, 0x92, 0xC5, 0xB6, 0x63, 0x92 }; ADXL345_WE myAcc = ADXL345_WE(CS_PIN, spi); // Create a struct to hold accelerometer data typedef struct struct_message { int id; float x; float y; float z; } struct_message; struct_message myData; esp_now_peer_info_t peerInfo; //callback when data is sent void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.print("\r\nLast Packet Send Status:\t"); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); } void setup() { Serial.begin(2000000); Serial.println("ADXL345_Sketch - Basic Data"); if (!myAcc.init()) { Serial.println("ADXL345 not connected!"); while (1) ; } WiFi.mode(WIFI_STA); if (esp_now_init() != ESP_OK) { Serial.println("Error"); return; } esp_now_register_send_cb(OnDataSent); memcpy(peerInfo.peer_addr, broadcastAddress, 6); peerInfo.channel = 0; peerInfo.encrypt = false; if (esp_now_add_peer(&peerInfo) != ESP_OK) { Serial.println("Failed to add peer"); return; } myAcc.setDataRate(ADXL345_DATA_RATE_1600); myAcc.setRange(ADXL345_RANGE_2G); } void loop() { xyzFloat raw = myAcc.getRawValues(); xyzFloat g = myAcc.getGValues(); myData.id = 1; esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&myData, sizeof(myData)); Serial.print(g.x); Serial.print(","); Serial.print(g.y); Serial.print(","); Serial.println(g.z); }
My issue is figuring out what to send to the receiver using &myData. I get this at the sender monitor,
Last Packet Send Status: Delivery Success 0.58,-0.83,-0.06 0.58,-0.83,-0.06 Last Packet Send Status: Delivery Success 0.62,-0.76,-0.09 0.73,-0.69,0.08 Last Packet Send Status: Delivery Success 0.63,-0.81,-0.05 0.63,-0.81,-0.05 0.70,- Last Packet Send Status: Delivery Success
but 0.0, 0.0, 0.0 at the receiver.
Thanks in advance.
The MPU6050 worked as expected. Kindly see the code below.
#include <esp_now.h> #include <WiFi.h> #include <Adafruit_Sensor.h> #include <Adafruit_MPU6050.h> #include <SPI.h> // Check I2C device address and correct line below (0x68) // id, address Adafruit_MPU6050 mpu; const int MPU_addr = 0x68; #define CHANNEL 1 // REPLACE WITH THE RECEIVER'S MAC Address uint8_t broadcastAddress[] = {0x13, 0x93, 0xC5, 0xB6, 0x69, 0x92}; // Structure example to send data // Must match the receiver structure typedef struct struct_message { int id; // must be unique for each sender board float acc_x; float acc_y; float acc_z; } struct_message; // Create a struct_message called myData struct_message myData; // Create peer interface esp_now_peer_info_t peerInfo; // callback when data is sent void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.print("\r\nLast Packet Send Status:\t"); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); } void setup() { // Init Serial Monitor Wire.begin(); Wire.setClock(400000L); Wire.beginTransmission(MPU_addr); Wire.write(0x6B); // PWR_MGMT_1 register Wire.write(0); // set to zero (wakes up the MPU-6050) Wire.endTransmission(true); Serial.begin(2000000); /* Initialise the sensor */ if (!mpu.begin()) { Serial.println("Failed to find MPU6050 chip"); while (1); } // Set device as a Wi-Fi Station WiFi.mode(WIFI_STA); // Init ESP-NOW if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; } // Once ESPNow is successfully Init, we will register for Send CB to // get the status of Trasnmitted packet esp_now_register_send_cb(OnDataSent); // Register peer memcpy(peerInfo.peer_addr, broadcastAddress, 6); peerInfo.channel = 0; peerInfo.encrypt = false; // Add peer if (esp_now_add_peer(&peerInfo) != ESP_OK) { Serial.println("Failed to add peer"); return; } } void loop() { sensors_event_t accel, gyro, temp; mpu.getEvent(&accel,&gyro, &temp); readAccValues(&accel); // Set values to send myData.id = 1; //Serial.print("acc X:"); Serial.print(myData.acc_x); Serial.print(","); Serial.print(myData.acc_y); Serial.print(","); Serial.println(myData.acc_z); // Send message via ESP-NOW esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&myData, sizeof(myData)); /* if (result == ESP_OK) { Serial.println("Sent with success"); } else { Serial.println("Error sending the data"); } delay(10);*/ } void readAccValues(sensors_event_t *event) { // read acceleration event myData.acc_x = event->acceleration.x; myData.acc_y = event->acceleration.y; myData.acc_z = event->acceleration.z; }
@abdul One problem is you do not appear to be checking if the send actually worked. Add the 'result' to the debug output.
Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting