prjbrook

ESP Now Arduino ESP8266 Sender Worked

Jul 14th, 2022
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 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         "CONTROLLER_NODE"
  33. #define MY_ROLE         ESP_NOW_ROLE_CONTROLLER         // set the role of this device: CONTROLLER, SLAVE, COMBO
  34. #define RECEIVER_ROLE   ESP_NOW_ROLE_SLAVE              // set the role of the receiver
  35. #define WIFI_CHANNEL    1
  36.  
  37. //uint8_t receiverAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};   // please update this with the MAC address of the receiver
  38. uint8_t receiverAddress[] = {0x98, 0xF4, 0xAB, 0xBF, 0xEC, 0xCC};   // Mac adress of Wemos Mini 2
  39.  
  40. struct __attribute__((packed)) dataPacket {
  41.   int sensor1;
  42.   int sensor2;
  43.   float sensor3;
  44. };
  45.  
  46. void transmissionComplete(uint8_t *receiver_mac, uint8_t transmissionStatus) {
  47.   if(transmissionStatus == 0) {
  48.     Serial.println("Data sent successfully");
  49.   } else {
  50.     Serial.print("Error code: ");
  51.     Serial.println(transmissionStatus);
  52.   }
  53. }
  54.  
  55. void setup() {
  56.   Serial.begin(115200);     // initialize serial port
  57.  
  58.   Serial.println();
  59.   Serial.println();
  60.   Serial.println();
  61.   Serial.print("Initializing...");
  62.   Serial.println(MY_NAME);
  63.   Serial.print("My MAC address is: ");
  64.   Serial.println(WiFi.macAddress());
  65.  
  66.   WiFi.mode(WIFI_STA);
  67.   WiFi.disconnect();        // we do not want to connect to a WiFi network
  68.  
  69.   if(esp_now_init() != 0) {
  70.     Serial.println("ESP-NOW initialization failed");
  71.     return;
  72.   }
  73.  
  74.   esp_now_set_self_role(MY_ROLE);  
  75.   esp_now_register_send_cb(transmissionComplete);   // this function will get called once all data is sent
  76.   esp_now_add_peer(receiverAddress, RECEIVER_ROLE, WIFI_CHANNEL, NULL, 0);
  77.  
  78.   Serial.println("Initialized.");
  79. }
  80.  
  81. void loop() {
  82.   dataPacket packet;
  83.  
  84.   packet.sensor1 = 123;
  85.   packet.sensor2 = 456;
  86.   packet.sensor3 = 3.14;
  87.  
  88.   esp_now_send(receiverAddress, (uint8_t *) &packet, sizeof(packet));
  89.  
  90.   delay(3000);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment