Can't get esp now and wifi to work together.
Posted: Wed Apr 29, 2020 1:54 pm
I have been trying to simply send data from the web(phone) to the "master" device and it shall send the value to the slaves. But for some reason which I can not figure out the data is send by the master but not recived by the slaves.
I got it to work once, but couple of days later it is not working.
Master code
Slave code
I got it to work once, but couple of days later it is not working.
Master code
Code: Select all
#include <esp_now.h>
#include <WiFi.h>
const char* ssid = "ssid>";
const char* password ="pass";
//uint8_t broadcastAddress2[] = {0xCC,0x50,0xE3,0xBE,0xB5,0xD4};
uint8_t broadcastAddress[] = {0xCC,0x50,0xE3,0xBE,0x4C,0xAC};
WiFiServer server(80);
int myData;
void setup() {
// Init Serial Monitor
Serial.begin(115200);
WiFi.mode(WIFI_AP_STA);
Serial.print("Connecting to ::=> "); Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Success");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
// register second peer
// memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
// if (esp_now_add_peer(&peerInfo) != ESP_OK){
// Serial.println("Failed to add peer");
// return;
// }
//
esp_now_init();
esp_now_register_send_cb(OnDataSent);
esp_now_peer_info_t peerInfo;
peerInfo.channel = 0;
peerInfo.encrypt = false;
// register first peer
memcpy(peerInfo.peer_addr, &broadcastAddress, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
pinMode(19, OUTPUT);
pinMode(33, OUTPUT);
}
void loop() {
delay(2000);
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");
}
WiFiClient client = server.available();
if(client) {
String req = client.readStringUntil('\r');
client.flush();
if (req.indexOf("/master_relay19/on") !=-1)
digitalWrite(19,1);
else if (req.indexOf("/master_relay19/off") !=-1)
digitalWrite(19,0);
else if (req.indexOf("/master_relay33/on") !=-1)
digitalWrite(33,1);
else if (req.indexOf("/master_relay33/off") !=-1)
digitalWrite(33,0);
else if (req.indexOf("/slave_relay33/on") !=-1)
myData = 1;
else if (req.indexOf("/slave_relay33/off") !=-1)
myData = 2;
else if (req.indexOf("/slave_relay19/on") !=-1)
myData = 3;
else if (req.indexOf("/slave_relay19/off") !=-1)
myData = 4;
else if (req.indexOf("/lightswitch_relay19/on") !=-1)
myData = 5;
else if (req.indexOf("/lightswitch_relay19/off") !=-1)
myData = 6;
}
}
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char macStr[18];
Serial.print("Packet to: ");
// Copies the sender mac address to a string
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(macStr);
Serial.print(" send status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
Code: Select all
#include <esp_now.h>
#include <WiFi.h>
int myData;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(myData);
if (myData == 5)
digitalWrite(19,1);
else if (myData == 6)
digitalWrite(19,0);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
WiFi.mode(WIFI_AP_STA);
pinMode(19,OUTPUT);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
Serial.println(WiFi.macAddress());
delay(3000);
}