ESP32 ESP-NOW

kareemwaheed94
Posts: 1
Joined: Tue Oct 27, 2020 2:40 pm

ESP32 ESP-NOW

Postby kareemwaheed94 » Tue Oct 27, 2020 2:43 pm

currently I've 2xESP32 & 1xNodeMCU

one ESP32 is connected to the wifi as webserver for now

the other ESP32 is a HUB which will collect readings from NodeMCUs then will forward it to the webserver

the nodeMCU will be sender will send the data to the HUB

(This hub is because ESP-NOW and WIFI doesn't work with me direct)

the issue I've now that the packets are inconsistent in sending sometime it sent and sometimes it failed, and I can't see why

could anyone help me with this please?

- attached the serial log (0 is sent, 1 isn't)

here is the code of both hub and NodeMCU
  1. /**
  2. * ESP-NOW
  3. *
  4. * Sender
  5. */
  6.  
  7. #include <Arduino.h>
  8. #include <ESP8266WiFi.h>
  9. #include <espnow.h>
  10. #include <SPI.h>
  11.  
  12. uint8_t peer1[] = {0x24, 0x62, 0xAB, 0xFF, 0x2C, 0x5C};
  13.  
  14. typedef struct struct_message {
  15.     int id;
  16.     float temp;
  17.     float hum;
  18.     int readingId;
  19. } struct_message;
  20.  
  21. struct_message sendData;
  22.  
  23. // Initialize DHT sensor.
  24.  
  25. void onSent(uint8_t *mac_addr, uint8_t sendStatus) {
  26.  Serial.println("Status:");
  27.  Serial.println(sendStatus);
  28. }
  29.  
  30. void setup() {
  31.  Serial.begin(115200);
  32.  
  33.  
  34.  WiFi.mode(WIFI_STA);
  35.  const char* dummy = "abcdefgh";
  36. //         SSID   pass   channel       BSSID  connect
  37. WiFi.begin(dummy, dummy, 11, false, false);
  38.  // Get Mac Add
  39.  Serial.print("Mac Address: ");
  40.  Serial.print(WiFi.macAddress());
  41.  Serial.println("ESP-Now Sender");
  42.  
  43.  // Initializing the ESP-NOW
  44.  if (esp_now_init() != 0) {
  45.    Serial.println("Problem during ESP-NOW init");
  46.    return;
  47.  }
  48.  
  49.  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
  50.  // Register the peer
  51.  Serial.println("Registering a peer");
  52.  esp_now_add_peer(peer1, ESP_NOW_ROLE_SLAVE, 11, NULL, 0);
  53.  Serial.println("Registering send callback function");
  54.  esp_now_register_send_cb(onSent);
  55. }
  56.  
  57. void loop() {
  58.  sendData.temp = random(10, 30);
  59.  sendData.hum = random(50, 90);
  60.  
  61.  Serial.println("Send a new message");
  62.  esp_now_send(NULL, (uint8_t *) &sendData, sizeof(sendData));
  63.  
  64.  delay(5000);
  65. }
  1. /*
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com/esp32-esp-now-wi-fi-web-server/
  4.  
  5.   Permission is hereby granted, free of charge, to any person obtaining a copy
  6.   of this software and associated documentation files.
  7.  
  8.   The above copyright notice and this permission notice shall be included in all
  9.   copies or substantial portions of the Software.
  10. */
  11.  
  12. #include <esp_now.h>
  13. #include <esp_wifi.h>
  14. #include <WiFi.h>
  15.  
  16. // Set your Board ID (ESP32 Sender #1 = BOARD_ID 1, ESP32 Sender #2 = BOARD_ID 2, etc)
  17. #define BOARD_ID 1
  18.  
  19. // Digital pin connected to the DHT sensor
  20. #define DHTPIN 4
  21.  
  22. // Uncomment the type of sensor in use:
  23. //#define DHTTYPE    DHT11     // DHT 11
  24. //#define DHTTYPE    DHT21     // DHT 21 (AM2301)
  25.  
  26.  
  27. //MAC Address of the receiver 24:62:AB:FE:3F:A0
  28.  
  29. //uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xFF, 0x2C, 0x5C};
  30. uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xFE, 0x3F, 0xA0};
  31. //Structure example to send data
  32. //Must match the receiver structure
  33. typedef struct struct_message {
  34.     int id;
  35.     float temp;
  36.     float hum;
  37.     int readingId;
  38. } struct_message;
  39.  
  40.  
  41. //Create a struct_message called myData
  42. struct_message myData;
  43. struct_message receivData;
  44.  
  45. unsigned long previousMillis = 0;   // Stores last time temperature was published
  46. const long interval = 10000;        // Interval at which to publish sensor readings
  47.  
  48. unsigned int readingId = 0;
  49.  
  50. // Insert your SSID
  51. constexpr char WIFI_SSID[] = "SAP_1";
  52.  
  53. int32_t getWiFiChannel(const char *ssid) {
  54.   if (int32_t n = WiFi.scanNetworks()) {
  55.       for (uint8_t i=0; i<n; i++) {
  56.           if (!strcmp(ssid, WiFi.SSID(i).c_str())) {
  57.               return WiFi.channel(i);
  58.           }
  59.       }
  60.   }
  61.   return 0;
  62. }
  63.  
  64. float readDHTTemperature() {
  65.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  66.   // Read temperature as Celsius (the default)
  67.   float t = 11;
  68.   // Read temperature as Fahrenheit (isFahrenheit = true)
  69.   //float t = dht.readTemperature(true);
  70.   // Check if any reads failed and exit early (to try again).
  71.   if (isnan(t)) {  
  72.     Serial.println("Failed to read from DHT sensor!");
  73.     return 0;
  74.   }
  75.   else {
  76.     Serial.println(t);
  77.     return t;
  78.   }
  79. }
  80.  
  81. float readDHTHumidity() {
  82.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  83.   float h = 225;
  84.   if (isnan(h)) {
  85.     Serial.println("Failed to read from DHT sensor!");
  86.     return 0;
  87.   }
  88.   else {
  89.     Serial.println(h);
  90.     return h;
  91.   }
  92. }
  93.  
  94. // callback when data is sent
  95. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  96.   Serial.print("\r\nLast Packet Send Status:\t");
  97.   Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  98. }
  99.  
  100. void onDataReceiver(const uint8_t * mac, const uint8_t *incomingData, int len) {
  101.   Serial.println("Message received.");
  102.   // We don't use mac to verify the sender
  103.   // Let us transform the incomingData into our message structure
  104.  memcpy(&receivData, incomingData, sizeof(receivData));
  105.  Serial.println("=== Data ===");
  106.  Serial.print("Mac address: ");
  107.  for (int i = 0; i < 6; i++) {
  108.      Serial.print("0x");
  109.      Serial.print(mac[i], HEX);
  110.      Serial.print(":");
  111.  }
  112.    
  113.  Serial.print("\n\nMessage: ");
  114.  Serial.println(receivData.id);
  115.  Serial.println(receivData.temp);
  116.  Serial.println();
  117.  
  118.  //Send message via ESP-NOW
  119.     esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
  120.     if (result == ESP_OK) {
  121.       Serial.println("Sent with success");
  122.     }
  123.     else {
  124.       Serial.println("Error sending the data");
  125.     }
  126. }
  127.  
  128. void setup() {
  129.   //Init Serial Monitor
  130.   Serial.begin(115200);
  131.  
  132.  
  133.   // Set device as a Wi-Fi Station and set channel
  134.   WiFi.mode(WIFI_STA);
  135.  
  136.   int32_t channel = getWiFiChannel(WIFI_SSID);
  137.  
  138.   //WiFi.printDiag(Serial); // Uncomment to verify channel number before
  139.   esp_wifi_set_promiscuous(true);
  140.   esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
  141.   esp_wifi_set_promiscuous(false);
  142.   WiFi.printDiag(Serial); // Uncomment to verify channel change after
  143.  
  144.   //Init ESP-NOW
  145.   if (esp_now_init() != ESP_OK) {
  146.     Serial.println("Error initializing ESP-NOW");
  147.     return;
  148.   }
  149.  
  150.   // Once ESPNow is successfully Init, we will register for Send CB to
  151.   // get the status of Trasnmitted packet
  152.   esp_now_register_send_cb(OnDataSent);
  153.   esp_now_register_recv_cb(onDataReceiver);
  154.   //Register peer
  155.   esp_now_peer_info_t peerInfo;
  156.   memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  157.   peerInfo.encrypt = false;
  158.  
  159.   //Add peer      
  160.   if (esp_now_add_peer(&peerInfo) != ESP_OK){
  161.     Serial.println("Failed to add peer");
  162.     return;
  163.   }
  164. }
  165.  
  166. void loop() {
  167.   unsigned long currentMillis = millis();
  168.   if (currentMillis - previousMillis >= interval) {
  169.     // Save the last time a new reading was published
  170.     previousMillis = currentMillis;
  171.     //Set values to send
  172.     myData.id = BOARD_ID;
  173.     myData.temp = readDHTTemperature();
  174.     myData.hum = readDHTHumidity();
  175.     myData.readingId = readingId++;
  176.      
  177.    
  178.   }
  179. }
Image

Who is online

Users browsing this forum: ok-home and 109 guests