zXeR

ESP-NOW(B)

Jun 26th, 2022 (edited)
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //---------------------------------------- เรียกใช้ Library
  2. #include <esp_now.h>
  3. #include <WiFi.h>
  4. //----------------------------------------
  5.  
  6. //---------------------------------------- ประกาศขา ของ LED และ Switch
  7. #define LED_Pin   4
  8. #define BTN_Pin   2
  9. //----------------------------------------
  10.  
  11. int BTN_State; //--> Variable to hold the button state.
  12.  
  13. uint8_t broadcastAddress[] = {0x30, 0xAE, 0xA4, 0x07, 0xBF, 0x0D}; //--> ประกาศ MAC ของอีกฝั่ง (ในที่นี้ ผม จะ ใส่เป็น MAC ของฝั่ง A)
  14.  
  15. int LED_State_Send = 0; //--> สร้างตัวแปรเพื่อเก็บค่า ที่จะส่งไปยัง ฝั่ง A
  16.  
  17. int LED_State_Receive; //--> สร้างตัวแปรเผื่อรับค่า ฝั่ง A และสั่งงาน LED ของ ฝั่ง B
  18.  
  19. String success; //--> สร้างตัวแปร Boolean เพื่อเก็บค่า ตรวจสอบการส่งข้อมูล
  20.  
  21. // โครงสร้างข้อมูล struct_message
  22. typedef struct struct_message {
  23.     int led;
  24. }
  25. struct_message_send;
  26.  
  27. // สร้าง ชุดข้อมูล send_data เพื่อส่งข้อมูลไปยังฝั่ง A
  28. struct_message send_Data;
  29.  
  30. // สร้าง ชุดข้อมูล receive_data เพื่อส่งข้อมูลจากฝั่ง A
  31. struct_message receive_Data;
  32. //----------------------------------------
  33.  
  34.  
  35. // สร้าง ฟังก์ชั่น เพื่อตรวจสอบการส่ง ข้อมูล
  36. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  37.   Serial.print("\r\nLast Packet Send Status:\t");
  38.   Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  39.   if (status ==0){
  40.     success = "Delivery Success :)";
  41.   }
  42.   else{
  43.     success = "Delivery Fail :(";
  44.   }
  45.   Serial.println(">>>>>");
  46. }
  47. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  48.  
  49. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Callback when data is received
  50.  
  51. // สร้าง ฟังก์ชั่น เพื่อรับค่าที่ส่งมาจาก ฝั่ง B
  52. void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  53.   memcpy(&receive_Data, incomingData, sizeof(receive_Data));
  54.   Serial.println();
  55.   Serial.println("<<<<< Receive Data:");
  56.   Serial.print("Bytes received: ");
  57.   Serial.println(len);
  58.  
  59.   // ให้ตัวแปร สั่งงาน LED = ค่าที่ได้รับจาก ฝั่ง A
  60.   LED_State_Receive = receive_Data.led;
  61.   Serial.print("Receive Data: ");
  62.   Serial.println(LED_State_Receive);
  63.   Serial.println("<<<<<");
  64.  
  65.   // สั่งงานสถานะ LED ให้ตรงกับข้อมูลที่ส่งมาจาก ฝั่ง A
  66.   digitalWrite(LED_Pin, LED_State_Receive);
  67. }
  68. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  69.  
  70. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ VOID SETUP
  71. void setup() {
  72.   Serial.begin(115200);
  73.  
  74.   pinMode(LED_Pin, OUTPUT);
  75.   pinMode(BTN_Pin, INPUT);
  76.  
  77.   WiFi.mode(WIFI_STA); //--> เปลี่ยนโหมด WiFi เป็น Stations
  78.  
  79.   //----------------------------------------Init ESP-NOW
  80.   if (esp_now_init() != ESP_OK) { // เริ่มต้นใช้งาน ESP-NOW / ตรวจสอบการเปิดใช้งาน ESP-NOW
  81.     Serial.println("Error initializing ESP-NOW");
  82.     return;
  83.   }
  84.   //----------------------------------------
  85.  
  86.   //----------------------------------------//แสดงสถานะ ของ Packet ข้อมูล
  87.   // get the status of Trasnmitted packet
  88.   esp_now_register_send_cb(OnDataSent);
  89.   //----------------------------------------
  90.  
  91.   //----------------------------------------Register peer
  92.   esp_now_peer_info_t peerInfo;
  93.   memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  94.   peerInfo.channel = 0;  
  95.   peerInfo.encrypt = false;
  96.   //----------------------------------------
  97.  
  98.   //----------------------------------------Add peer        
  99.   if (esp_now_add_peer(&peerInfo) != ESP_OK){
  100.     Serial.println("Failed to add peer");
  101.     return;
  102.   }
  103.   //----------------------------------------
  104.  
  105.   esp_now_register_recv_cb(OnDataRecv); //--> Register for a callback function that will be called when data is received
  106. }
  107. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  108.  
  109. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  110. void loop() {
  111.   BTN_State = digitalRead(BTN_Pin); //--> ให้ตัวแปร  BTN_STATE = ค่าที่อ่านได้จาก สวิตช์
  112.  
  113.   //----------------------------------------When the button is pressed it will send data to control the LED on the ESP32 Target.
  114.   if(BTN_State == 1) { // ถ้า สวิตช์ มีการกด
  115.     LED_State_Send = !LED_State_Send; // ให้ตัวแปร LED_State_Send ไม่เท่ากับค่า LED_State_send
  116.     send_Data.led = LED_State_Send; // ให้ชุดข้อมูล send_data.led = ตัวแปร LED_State_send
  117.  
  118.     Serial.println();
  119.     Serial.print(">>>>> ");
  120.     Serial.println("Send data");
  121.  
  122.     //----------------------------------------Send message via ESP-NOW
  123.     // ส่งข้อมูลผ่าน ESP-NOW ไปยัง ฝั่ง A
  124.     esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &send_Data, sizeof(send_Data));
  125.      
  126.     if (result == ESP_OK) {
  127.       Serial.println("Sent with success");
  128.     }
  129.     else {
  130.       Serial.println("Error sending the data");
  131.     }
  132.     //----------------------------------------
  133.    
  134.     //----------------------------------------Wait for the button to be released. Release the button first to send the next data.
  135.     while(BTN_State == 1) {
  136.       BTN_State = digitalRead(BTN_Pin);
  137.       delay(10);
  138.     }
  139.     //----------------------------------------
  140.   }
  141.   //----------------------------------------
  142. }
  143. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  144. //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Add Comment
Please, Sign In to add comment