ESPnow peer to peer data implementation?
Posted: Mon Dec 24, 2018 4:09 pm
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
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) {
}