-->
Page 1 of 3

ESPnow peer to peer data implementation?

PostPosted: Mon Dec 24, 2018 4:09 pm
by Zim
Hi. I have recently experimented with ESPnow and am amazed on how fast it is. I am trying to monitor the AO pin on the master and send it to the slave each time it boots up. The data being monitored is actually the battery voltage on the master. I have found a code example and simplified it. I just can't figure out how to implement the AO data into the data to be sent. It seems you have to specify the data type, but its beyond me.
The message "Hello world" gets through. How do i send just the "battvolts"
Please help
Thanks


Code: Select all// Mastershortend.ino    sends the data
#include <ESP8266WiFi.h>
extern "C" {
#include <espnow.h>
}
uint8_t remoteMac[] = {0x36, 0x33, 0x33, 0x33, 0x33, 0x33}; //Address of slave which receives
#define WIFI_CHANNEL 4

const int  analogPin = A0; //ADC  for reading battery voltage
int batt = 0;              //  0-1023
int battvolts = 0;         //  0-1023 to volts

struct __attribute__((packed)) DataStruct {  //   What
    char text[32];                           //    is
};                                           //   This
DataStruct myData;                           //   about?

void setup() {
    pinMode(analogPin, INPUT);
    Serial.begin(9600);
    WiFi.mode(WIFI_STA); // Station mode for Master
    WiFi.disconnect();   // shut off org esp wifi
    esp_now_init();
    esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
    esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);
    esp_now_register_send_cb(sendCallBackFunction);
   
    strcpy(myData.text, "Hello World");      // this message get through
                                             // How would I implament battvolts?
    Serial.println();
    Serial.println(myData.text);
    }

void loop() {
  batt = analogRead(analogPin);         // read the A0 input pin  0-1023
  battvolts = batt * 0.0161             // converts to actual volt reading
  sendData();
    }
void sendData() {
        uint8_t bs[sizeof(myData)];
        memcpy(bs, &myData, sizeof(myData));
        esp_now_send(NULL, bs, sizeof(myData));
        Serial.println("Sleep zzzzzzz");
        ESP.deepSleep(0);                  // deep sleep until PIR grounds reset
 }
void sendCallBackFunction(uint8_t* mac, uint8_t sendStatus) {
  }

Re: ESPnow peer to peer data implementation?

PostPosted: Mon Dec 24, 2018 7:55 pm
by QuickFix
What ESP are you using?
Some boards, like the NodeMCU, has a voltage divider that can give unexpected results when not taking into account.
Especially since for VCC-measuring mode A0 needs to be floating, so you'll have a slight problem when using a NodeMCU. :idea:

Re: ESPnow peer to peer data implementation?

PostPosted: Mon Dec 24, 2018 8:16 pm
by Zim
Hi
Using a D1 mini. No problem getting the reading. The issue I have is how to get these readings into the send function of ESPnow. Very little documentation on this. I really need a simple one way communication example with data from a output pin. Even something like a simple remote control.

Thanks
Zim

Re: ESPnow peer to peer data implementation?

PostPosted: Wed Dec 26, 2018 4:34 am
by torntrousers
The code
Code: Select allesp_now_send(NULL, bs, sizeof(myData));
is doing the send and it sends just a simple array of bytes from the bs variable. The sendData function 'flattens' the DataStruct struct into that bs byte array. If you want to add battvolts you can add it to the DataStruct and it should automatically get added to the sent data by sendData. Eg, to send both the text and battvolts try:
Code: Select allstruct __attribute__((packed)) DataStruct { 
    char text[32];         
    int battvolts;
};                                         

There are similar examples: https://github.com/HarringayMakerSpace/ ... ay.ino#L29