prjbrook

ESP8266 ESP now receiver Worked

Jul 14th, 2022
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. /****************************************************************************************************************************************************
  2. * TITLE: ESP-NOW Getting Started Examples
  3. *
  4. * By Frenoy Osburn
  5. * YouTube Video: https://youtu.be/_cNAsTB5JpM
  6. ****************************************************************************************************************************************************/
  7.  
  8. /********************************************************************************************************************
  9. * Please make sure that you install the board support package for the ESP8266 boards.
  10. * You will need to add the following URL to your Arduino preferences.
  11. * Boards Manager URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
  12. ********************************************************************************************************************/
  13.  
  14. /********************************************************************************************************************
  15. * Board Settings:
  16. * Board: "WeMos D1 R1 or Mini"
  17. * Upload Speed: "921600"
  18. * CPU Frequency: "80MHz"
  19. * Flash Size: "4MB (FS:@MB OTA:~1019KB)"
  20. * Debug Port: "Disabled"
  21. * Debug Level: "None"
  22. * VTables: "Flash"
  23. * IwIP Variant: "v2 Lower Memory"
  24. * Exception: "Legacy (new can return nullptr)"
  25. * Erase Flash: "Only Sketch"
  26. * SSL Support: "All SSL ciphers (most compatible)"
  27. * COM Port: Depends *On Your System*
  28. *********************************************************************************************************************/
  29. #include<ESP8266WiFi.h>
  30. #include<espnow.h>
  31.  
  32. #define MY_NAME "SLAVE_NODE"
  33.  
  34. struct __attribute__((packed)) dataPacket {
  35. int sensor1;
  36. int sensor2;
  37. float sensor3;
  38. };
  39.  
  40. void dataReceived(uint8_t *senderMac, uint8_t *data, uint8_t dataLength) {
  41. char macStr[18];
  42. dataPacket packet;
  43.  
  44. snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", senderMac[0], senderMac[1], senderMac[2], senderMac[3], senderMac[4], senderMac[5]);
  45.  
  46. Serial.println();
  47. Serial.print("Received data from: ");
  48. Serial.println(macStr);
  49.  
  50. memcpy(&packet, data, sizeof(packet));
  51.  
  52. Serial.print("sensor1: ");
  53. Serial.println(packet.sensor1);
  54. Serial.print("sensor2: ");
  55. Serial.println(packet.sensor2);
  56. Serial.print("sensor3: ");
  57. Serial.println(packet.sensor3);
  58. }
  59.  
  60. void setup() {
  61.  
  62. Serial.begin(115200); // initialize serial port
  63.  
  64. Serial.println();
  65. Serial.println();
  66. Serial.println();
  67. Serial.print("Initializing...");
  68. Serial.println(MY_NAME);
  69. Serial.print("My MAC address is: ");
  70. Serial.println(WiFi.macAddress());
  71.  
  72. WiFi.mode(WIFI_STA);
  73. WiFi.disconnect(); // we do not want to connect to a WiFi network
  74.  
  75. if(esp_now_init() != 0) {
  76. Serial.println("ESP-NOW initialization failed");
  77. return;
  78. }
  79.  
  80. esp_now_register_recv_cb(dataReceived); // this function will get called once all data is sent
  81.  
  82. Serial.println("Initialized.");
  83. }
  84.  
  85. void loop() {
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment