I'm trying to send data over 2 ESP8266's to one salve but I just get big number even though both strucuts are the same.
I've used this tutorial from here
https://randomnerdtutorials.com/esp-now-many-to-one-esp8266-nodemcu/
I've got this working as described in the tutorial, But I have incoming char data from the serial port on Board1 and Board2 and would like to send this data from each board to the salve so that I can display it on a TFT screen.
At the moment I'm just trying to get it working correctly from one master and then hopefully get it working with the 2 boards.
This is the TX code(all working on getting the serial data and displaying it in the serial monitor.
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <TimedAction.h>
#include <SoftwareSerial.h>
//##########################
//# SET UP VARIABLES #
//##########################
SoftwareSerial ESPserial(2, 4); // RX | TX
const byte numChars = 9; //Number of bits to get
// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x8C, 0xAA, 0xB5, 0x7C, 0x76, 0xE9};//8C:AA:B5:7C:76:E9
boolean newData = false;
// Set your Board ID (ESP32 Sender #1 = BOARD_ID 1, ESP32 Sender #2 = BOARD_ID 2, etc)
#define BOARD_ID 1
#define ECHO_TO_SERIAL 1 //change to zero = no serial output
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int id;
char receivedChars[numChars]; // an array to store the received data
} struct_message;
// Create a struct_message called test to store variables to be sent
struct_message myData;
unsigned long lastTime = 0;
unsigned long timerDelay = 500;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("\r\nLast Packet Send Status: ");
if (sendStatus == 0) {
Serial.println("Delivery success");
}
else {
Serial.println("Delivery fail");
}
}
void printRecord(Print* pr, char sep = ',') {
pr->print(myData.receivedChars); // Print data
pr->println();
}
void setup() {
ESPserial.begin(9600);
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set ESP-NOW role
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
// 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
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
void loop() {
recvWithEndMarker();
if (newData ) { //new data received from eLevel
newData = false;
myData.id = BOARD_ID;
// myData.PRO3600_Disconnected = 10;
Send_data();
}
if ((millis() - lastTime) > timerDelay) {
#if ECHO_TO_SERIAL // Send debug to serial port if enabled
printRecord(&Serial);
#endif //ECHO_TO_SERIAL
lastTime = millis();
}
//byte receivedChars[numChars]; // an array to store the received data
}
void Send_data() {
// Send message via ESP-NOW
esp_now_send(0, (uint8_t *) &myData, sizeof(myData));
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (ESPserial.available() > 0 && newData == false) { // Tried this
// while (gtSerial.available() > 0 && newData == false) { //tried this
rc = ESPserial.read();
if (rc != endMarker) {
myData.receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
myData.receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
// receiveUntilTimeout = true; //tried here
}
}
}
This is the RX side.
#include <ESP8266WiFi.h>
#include <espnow.h>
#include "SPI.h"
#include "TFT_eSPI.h"
#include "U8g2_for_TFT_eSPI.h"
//#define firstScreenCS D4
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
U8g2_for_TFT_eSPI u8f; // U8g2 font instance
const byte numChars = 9; //Number of bits to get
unsigned long lastTime = 0;
unsigned long timerDelay = 500;
// Structure example to receive data
// Must match the sender structure
char buf[20];
#define ECHO_TO_SERIAL 1 //change to zero = no serial output
typedef struct struct_message {
int id;
char receivedChars[numChars]; // an array to store the received data // an array to store the received data
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Create a structure to hold the readings from each board
struct_message Platfomrm_Level;
struct_message Platform_Angle;
// Create an array with all the structures
struct_message boardsStruct[2] = {Platfomrm_Level, Platform_Angle};
// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) {
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
memcpy(&myData, incomingData, sizeof(myData));
Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
// Update the structures with the new incoming data
// boardsStruct[myData.id - 1].receivedChars[numChars] = myData.receivedChars[numChars]; //this just reads 1073669036 in serial monitor
boardsStruct[myData.id - 1].receivedChars = myData.receivedChars;// invalid array assignment boardsStruct[myData.id - 1].receivedChars = myData.receivedChars;
Serial.printf("TILT: %d \n", boardsStruct[myData.id - 1].receivedChars);
Serial.println();
}
void printRecord(Print* pr, char sep = ',') {
pr->print(myData.receivedChars); // Print data
pr->println();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
tft.init();
u8f.begin(tft);
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
// Access the variables for each board
// char board1X = boardsStruct[0].receivedChars; // If I unhighlight this i get error invalid conversion from 'char*' to 'char'
if ((millis() - lastTime) > timerDelay) {
#if ECHO_TO_SERIAL // Send debug to serial port if enabled
printRecord(&Serial);
#endif //ECHO_TO_SERIAL
lastTime = millis();
}
/*
u8f.setFontMode(0); // use u8g2 none transparent mode
u8f.setFontDirection(0); // left to right (this is default)
// digitalWrite(firstScreenCS, LOW);
u8f.setFontMode(0);
u8f.setFontDirection(0);
u8f.setFont(u8g2_font_logisoso58_tf);
u8f.setForegroundColor(TFT_GREEN); // apply color// start writing at this position
u8f.setCursor(230, 100);
u8f.println(" V "); // numerical value
u8f.setFont(u8g2_font_inb63_mn); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
u8f.setForegroundColor(TFT_GREEN); // apply color// start writing at this position
u8f.setCursor(20, 100);
// u8f.print(board1D);
//u8f.print(" "); // numerical value
u8f.setForegroundColor(TFT_YELLOW); // apply color
// u8f.print(" "); // numerical value
u8f.setCursor(20, 180);
// u8f.print(board1.y);
// u8f.print(" "); // numerical value
// digitalWrite(firstScreenCS, HIGH);
*/
}
I've not yet accessed the variables for each board yet so I can display both values on the lcd.
I've placed comments in the code for different error's or whats happening.
The incoming receivedChars is coming correctly as this print's on the Serial monitor (debug enabled), I've tried to create another struct so that I can get data from both boards data coming in.