Maderdash

EspNOW toggle

Jun 11th, 2022 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3. #define LED_Pin 4
  4. #define BTN_Pin 15
  5.  
  6. bool lock = true;
  7. bool toggle = false;
  8.  
  9. int BTN_State; //--> Variable to hold the button state.
  10.  
  11. uint8_t broadcastAddress[] = {
  12.   0x00,
  13.   0x00,
  14.   0x00,
  15.   0x00,
  16.   0x00,
  17.   0x00
  18. }; //--> REPLACE WITH THE MAC Address of your receiver.
  19.  
  20. int LED_State_Send = 0;
  21.  
  22. int LED_State_Receive;
  23. String success;
  24. typedef struct struct_message {
  25.   int led;
  26. }
  27. struct_message_send;
  28.  
  29. struct_message send_Data;
  30.  
  31. struct_message receive_Data;
  32.  
  33. void OnDataSent(const uint8_t * mac_addr, esp_now_send_status_t status) {
  34.   Serial.print("\r\nLast Packet Send Status:\t");
  35.   Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  36.   if (status == 0) {
  37.     success = "Delivery Success :)";
  38.   } else {
  39.     success = "Delivery Fail :(";
  40.   }
  41.   Serial.println(">>>>>");
  42. }
  43.  
  44. void OnDataRecv(const uint8_t * mac,
  45.   const uint8_t * incomingData, int len) {
  46.   memcpy( & receive_Data, incomingData, sizeof(receive_Data));
  47.   Serial.println();
  48.   Serial.println("<<<<< Receive Data:");
  49.   Serial.print("Bytes received: ");
  50.   Serial.println(len);
  51.   LED_State_Receive = receive_Data.led;
  52.   Serial.print("Receive Data: ");
  53.   Serial.println(LED_State_Receive);
  54.   Serial.println("<<<<<");
  55.  
  56.   if (LED_State_Recived == true && lock == true) {
  57.     toggle = !toggle;
  58.     lock = false;
  59.   }
  60.   if (LED_State_Recived == low && lock == false) {
  61.     lock = true;
  62.   }
  63.   digitalWrite(LED_Pin, LED_State_Receive);
  64. }
  65.  
  66. void setup() {
  67.   Serial.begin(115200);
  68.   pinMode(LED_Pin, OUTPUT);
  69.   pinMode(BTN_Pin, INPUT);
  70.   WiFi.mode(WIFI_STA);
  71.   if (esp_now_init() != ESP_OK) {
  72.     Serial.println("Error initializing ESP-NOW");
  73.     return;
  74.   }
  75.   esp_now_register_send_cb(OnDataSent);
  76.   esp_now_peer_info_t peerInfo;
  77.   memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  78.   peerInfo.channel = 0;
  79.   peerInfo.encrypt = false;
  80.   if (esp_now_add_peer( & peerInfo) != ESP_OK) {
  81.     Serial.println("Failed to add peer");
  82.     return;
  83.   }
  84.   esp_now_register_recv_cb(OnDataRecv); //--> Register for a callback function that will be called when data is received
  85. }
  86.  
  87. void loop() {
  88.   BTN_State = digitalRead(BTN_Pin); //--> Reads and holds button states.
  89.   if (BTN_State == 1) {
  90.     LED_State_Send = !LED_State_Send;
  91.     send_Data.led = LED_State_Send;
  92.     Serial.println();
  93.     Serial.print(">>>>> ");
  94.     Serial.println("Send data");
  95.     esp_err_t result = esp_now_send(broadcastAddress, (uint8_t * ) & send_Data, sizeof(send_Data));
  96.     if (result == ESP_OK) {
  97.       Serial.println("Sent with success");
  98.     } else {
  99.       Serial.println("Error sending the data");
  100.     }
  101.     while (BTN_State == 1) {
  102.       BTN_State = digitalRead(BTN_Pin);
  103.       delay(10);
  104.     }
  105.   }
  106. }
Add Comment
Please, Sign In to add comment