Notifications
Clear all

Advice on ADXL345 SPI code not working using ESPnow

4 Posts
2 Users
0 Likes
626 Views
(@abdul)
Member
Joined: 1 year ago
Posts: 14
Topic starter  

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

   
Quote
(@abdul)
Member
Joined: 1 year ago
Posts: 14
Topic starter  

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.


   
ReplyQuote
(@abdul)
Member
Joined: 1 year ago
Posts: 14
Topic starter  

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

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6970
 

@abdul One problem is you do not appear to be checking if the send actually worked. Add the 'result' to the debug output.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote