Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //---------------------------------------- เรียกใช้ Library
- #include <esp_now.h>
- #include <WiFi.h>
- //----------------------------------------
- //---------------------------------------- ประกาศขา ของ LED และ Switch
- #define LED_Pin 4
- #define BTN_Pin 2
- //----------------------------------------
- int BTN_State; //--> Variable to hold the button state.
- uint8_t broadcastAddress[] = {0x30, 0xAE, 0xA4, 0x07, 0xBF, 0x0D}; //--> ประกาศ MAC ของอีกฝั่ง (ในที่นี้ ผม จะ ใส่เป็น MAC ของฝั่ง A)
- int LED_State_Send = 0; //--> สร้างตัวแปรเพื่อเก็บค่า ที่จะส่งไปยัง ฝั่ง A
- int LED_State_Receive; //--> สร้างตัวแปรเผื่อรับค่า ฝั่ง A และสั่งงาน LED ของ ฝั่ง B
- String success; //--> สร้างตัวแปร Boolean เพื่อเก็บค่า ตรวจสอบการส่งข้อมูล
- // โครงสร้างข้อมูล struct_message
- typedef struct struct_message {
- int led;
- }
- struct_message_send;
- // สร้าง ชุดข้อมูล send_data เพื่อส่งข้อมูลไปยังฝั่ง A
- struct_message send_Data;
- // สร้าง ชุดข้อมูล receive_data เพื่อส่งข้อมูลจากฝั่ง A
- struct_message receive_Data;
- //----------------------------------------
- // สร้าง ฟังก์ชั่น เพื่อตรวจสอบการส่ง ข้อมูล
- void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
- Serial.print("\r\nLast Packet Send Status:\t");
- Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
- if (status ==0){
- success = "Delivery Success :)";
- }
- else{
- success = "Delivery Fail :(";
- }
- Serial.println(">>>>>");
- }
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Callback when data is received
- // สร้าง ฟังก์ชั่น เพื่อรับค่าที่ส่งมาจาก ฝั่ง B
- void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
- memcpy(&receive_Data, incomingData, sizeof(receive_Data));
- Serial.println();
- Serial.println("<<<<< Receive Data:");
- Serial.print("Bytes received: ");
- Serial.println(len);
- // ให้ตัวแปร สั่งงาน LED = ค่าที่ได้รับจาก ฝั่ง A
- LED_State_Receive = receive_Data.led;
- Serial.print("Receive Data: ");
- Serial.println(LED_State_Receive);
- Serial.println("<<<<<");
- // สั่งงานสถานะ LED ให้ตรงกับข้อมูลที่ส่งมาจาก ฝั่ง A
- digitalWrite(LED_Pin, LED_State_Receive);
- }
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ VOID SETUP
- void setup() {
- Serial.begin(115200);
- pinMode(LED_Pin, OUTPUT);
- pinMode(BTN_Pin, INPUT);
- WiFi.mode(WIFI_STA); //--> เปลี่ยนโหมด WiFi เป็น Stations
- //----------------------------------------Init ESP-NOW
- if (esp_now_init() != ESP_OK) { // เริ่มต้นใช้งาน ESP-NOW / ตรวจสอบการเปิดใช้งาน ESP-NOW
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- //----------------------------------------
- //----------------------------------------//แสดงสถานะ ของ Packet ข้อมูล
- // get the status of Trasnmitted packet
- esp_now_register_send_cb(OnDataSent);
- //----------------------------------------
- //----------------------------------------Register peer
- esp_now_peer_info_t peerInfo;
- memcpy(peerInfo.peer_addr, broadcastAddress, 6);
- peerInfo.channel = 0;
- peerInfo.encrypt = false;
- //----------------------------------------
- //----------------------------------------Add peer
- if (esp_now_add_peer(&peerInfo) != ESP_OK){
- Serial.println("Failed to add peer");
- return;
- }
- //----------------------------------------
- esp_now_register_recv_cb(OnDataRecv); //--> Register for a callback function that will be called when data is received
- }
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- void loop() {
- BTN_State = digitalRead(BTN_Pin); //--> ให้ตัวแปร BTN_STATE = ค่าที่อ่านได้จาก สวิตช์
- //----------------------------------------When the button is pressed it will send data to control the LED on the ESP32 Target.
- if(BTN_State == 1) { // ถ้า สวิตช์ มีการกด
- LED_State_Send = !LED_State_Send; // ให้ตัวแปร LED_State_Send ไม่เท่ากับค่า LED_State_send
- send_Data.led = LED_State_Send; // ให้ชุดข้อมูล send_data.led = ตัวแปร LED_State_send
- Serial.println();
- Serial.print(">>>>> ");
- Serial.println("Send data");
- //----------------------------------------Send message via ESP-NOW
- // ส่งข้อมูลผ่าน ESP-NOW ไปยัง ฝั่ง A
- esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &send_Data, sizeof(send_Data));
- if (result == ESP_OK) {
- Serial.println("Sent with success");
- }
- else {
- Serial.println("Error sending the data");
- }
- //----------------------------------------
- //----------------------------------------Wait for the button to be released. Release the button first to send the next data.
- while(BTN_State == 1) {
- BTN_State = digitalRead(BTN_Pin);
- delay(10);
- }
- //----------------------------------------
- }
- //----------------------------------------
- }
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Add Comment
Please, Sign In to add comment