Go Down

Topic: Esp Now wifi, cant recive data on "slave" (Read 146 times) previous topic - next topic

swedaniel8

Hi im trying to send data to one of my esp32(slave) from my other esp32(master).
The master is connected to my personal Wifi.
The problem is that the data is send but not being recived in the slave. I cant understand why.


Master code:
 
Code: [Select]
// {0x24,0x6F,0x28,0xA3,0x10,0x70} slave with two relays
// {0xCC,0x50,0xE3,0xBE,0x4C,0xAC} slave with one relay (lightswitch)

#include <esp_now.h>
#include <WiFi.h>
const char* ssid = "ssid>";
const char* password ="pass";
// REPLACE WITH YOUR RECEIVER MAC Address
//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");
}



Slave code

Code: [Select]
/

#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.disconnect();
  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);
}






Go Up