Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15238

Help with ESPNOW and UART Serial Comms

$
0
0

This project is designed to monitor 10 Normally Open Inputs which are Pulled HIGH and if one or more of the inputs is pulled LOW to ground by a NO room occupancy sensor, then a Char String is sent via UART 2 on to my network to be read by PuTTY/NodeRed or similar terminal indicting that the room is occupied.
Four of the inputs use an ESP32 Devkit board acting as a MASTER sending data by ESPNOW and the other 6 are using a second ESP32 board acting as a slave.
The Slave has a Waveshare Uart to Ethernet converter which is attached to the network.
Hardware is simple
MASTER
GPIO PINS 32 33 18 19 each to GND when switched by room occupancy sensor
SLAVE

GPIO PINS 4 5 12 13 14 15 to GND when switched by room occupancy sensor
GPIO PIN 16 (RX) to TX on Waveshare Uart to Ethernet module
GPIO PIN 17 (TX) to RX on Waveshare Uart to Ethernet module
Using BAUD 9600, 8 bits, 1 stop bit, no parity, no Xon/Xoff
The Master board connects with Slave OK using ESPNOW and happily transmits the GPIO PIN array data to the Slave.
However to Code I am using for sending the save data via Uart is clunky and primitive and results in some missing data, partial strings and hangups when exported via the Uart

Can someone please review the code below and suggest a slicker way of capturing and exporting the Data received from the Master and Created by the Slave?

MASTER SKETCH
#include <esp_now.h>
#include <WiFi.h>

#define CHANNEL 1
esp_now_peer_info_t slave;
uint8_t gpios[] = {32,33,18,19};

int gpioCount;

uint8_t macSlaves[][6] = {
{0x24, 0x62, 0xAB, 0xF2, 0x14, 0xE4}
};

void setup() {
Serial.begin(19200);

gpioCount = sizeof(gpios)/sizeof(uint8_t);

WiFi.mode(WIFI_STA);

Serial.print("Mac Address in Station: ");
Serial.println(WiFi.macAddress());

InitESPNow();

int slavesCount = sizeof(macSlaves)/6/sizeof(uint8_t);

for(int i=0; i<slavesCount; i++){

slave.channel = CHANNEL;

slave.encrypt = 0;

memcpy(slave.peer_addr, macSlaves[i], sizeof(macSlaves[i]));
esp_now_add_peer(&slave);

}

esp_now_register_send_cb(OnDataSent);

for(int i=0; i<gpioCount; i++){
pinMode(gpios[i], INPUT_PULLUP);
}

send();
}

void InitESPNow() {

if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}

else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}

void send(){
uint8_t values[gpioCount];

for(int i=0; i<gpioCount; i++){

values[i] = digitalRead(gpios[i]);

}

uint8_t broadcast[] = {0x24, 0x62,0xAB,0xF2,0x14,0xE4};
esp_err_t result = esp_now_send(broadcast, (uint8_t*) &values, sizeof(values));
Serial.print("Send Status: ");
if (result == ESP_OK) {
Serial.println("Success");
}

else {
Serial.println("Error");

}
delay(500);
}

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
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.print("Sent to: ");
Serial.println(macStr);
Serial.print("Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");

send();
}

void loop() {

}

SLAVE SKETCH
#include <esp_now.h>
#include <WiFi.h>
#include <HardwareSerial.h>
#define RXD2 16
#define TXD2 17

HardwareSerial SerialPort(2); //Use UART2

//ON SLAVE BOARD
int switchPin1 = 4;// room 4 - Pin 4 on ESP slave - Pulled up - When pulled down to GND shows as B4WARDON
int switchPin5 = 5;// room 5 - Pin 5 on ESP slave - Pulled up - When pulled down to GND shows as B5BATHON
int switchPin3 = 12;// room 1 - Pin 12 on ESP slave - Pulled up - When pulled down to GND shows as B1WARDON
int switchPin2 = 13;// room 1A -Pin 13 on ESP slave - Pulled up - When pulled down to GND shows as B1BATHON
int switchPin4 = 14;// room 1B -Pin 14 on ESP slave - Pulled up - When pulled down to GND shows as B1TOILON
int switchPin6 = 15;// room 4A -Pin 15 on ESP slave - Pulled up - When pulled down to GND shows as B4BATHON

//ON MASTER BOARD
int switchPin7 = 32;// room 3 -Pin 16 sent to ESP slave - Pulled up - When pulled down to GND shows as B3WARDON
int switchPin10 = 33;// room 3A -Pin 17 sent to ESP slave - Pulled up - When pulled down to GND shows as B3BATHON
int switchPin8 = 18;// room 2 - Pin 18 sent to ESP slave - Pulled up - When pulled down to GND shows as B2WARDON
int switchPin9 = 19;// room 2A - Pin 19 sent to ESP slave - Pulled up - When pulled down to GND shows as B2BATHON

uint8_t gpios[] = {32,33,18,19};

int gpioCount;

void setup() {
Serial.begin(19200);
//while(!Serial);
//SerialPort.begin(9600, SERIAL_8N2, 16, 17);
Serial2.begin(19200, SERIAL_8N1, RXD2, TXD2);
//while(!Serial2);

gpioCount = sizeof(gpios)/sizeof(uint8_t);

WiFi.mode(WIFI_STA);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
//Serial.print("Mac Address in Station: ");
//Serial.println(WiFi.macAddress());

InitESPNow();
esp_now_register_recv_cb(OnDataRecv);
for(int i=0; i<gpioCount; i++){

pinMode(gpios[i], OUTPUT);

}
}

void InitESPNow() {

if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}

void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
char macStr[18];
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.print("Received from: ");
//Serial.println(macStr);
//Serial.println("");
//Serial2.print("Received from: ");
//Serial2.println(macStr);
//Serial2.println("");
for(int i=0; i<gpioCount; i++){
digitalWrite(gpios[i], data[i]);
//delay(100);
}

}

void loop() {
if (digitalRead(switchPin1) == LOW)
{
//Serial.println("B4WARDON");

Serial2.println("B4WARDON");

}

if (digitalRead(switchPin2) == LOW)
{
//Serial.println("B1BATHON");

Serial2.println("B1BATHON");

}

if (digitalRead(switchPin3) == LOW)
{
//Serial.println("B1WARDON");

Serial2.println("B1WARDON");

}
if (digitalRead(switchPin4) == LOW)
{
//Serial.println("B1TOILON");

Serial2.println("B1TOILON");

}

if (digitalRead(switchPin5) == LOW)
{
Serial.println("B5BATHON");
delay(10);

Serial2.println("B5BATHON");
delay(10);

}

if (digitalRead(switchPin6) == LOW)
{
//Serial.println("B4BATHON");

Serial2.println("B4BATHON");

}

if (digitalRead(switchPin7) == LOW)
{
//Serial.println("B3WARDON");

Serial2.println("B3WARDON");

}

if (digitalRead(switchPin8) == LOW)
{
//Serial.println("B2WARDON");

Serial2.println("B2WARDON");

}

if (digitalRead(switchPin9) == LOW)
{
//Serial.println("B2BATHON");

Serial2.println("B2BATHON");

}

if (digitalRead(switchPin10) == LOW)
{
//Serial.println("B3BATHON");

Serial2.println("B3BATHON");

}
delay(1000);

}

SAMPLE READOUTS DURING TESTING
MASTER IS SENDING TO SLAVE

Sent to: 24:62:ab:f2:14:e4
Status: Success
Send Status: Success
Sent to: 24:62:ab:f2:14:e4
Status: Success
Send Status: Success
Sent to: 24:62:ab:f2:14:e4
Status: Success
Send Status: Success
Sent to: 24:62:ab:f2:14:e4
Status: Success
Send Status: Success
Sent to: 24:62:ab:f2:14:e4

SAMPLE OUTPUT FROM SLAVE VIA USB SERIAL MONITOR

B1TOILON
B1TOILON
B4WARDON
B4WARDON
B1WARDON
B1WARDON
B2BATHON
B2BATHON
B2BATHON
B2WARDON
B2WARDON
B2WARDON
All working OK

PuTTY Serial data readings from SLAVE when sensors are closed

B2WARDON
B1TOILON
B1TOILON
B1WARDON
B1WARDON
B5BATHON
B5BATHON
B1WARDON
B1WARDON
B1WARDON

Looks OK

But when MASTER Sensors are closed
B2BATHON
B3WARDON
B3WARDON
B3WARDON
B3WARDON
B3WARDON
B3WARDON
B3WARDON
B3WARDON
B3WARDON
▒▒I=95
PuTTY THEN HANGS UP !

4 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 15238

Trending Articles