1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #include "ESPNOW.h"
uint32_t lastSendTime = 0;
String success = ""; char GPS_Latitude[20]; char GPS_Longitude[20];
struct_message telecommand_data;
lock_message incomingReadings;
esp_now_peer_info_t peerInfo;
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(status); } }
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { memcpy(&incomingReadings, incomingData, sizeof(incomingReadings)); Serial.print("Bytes received: "); Serial.println(len); lock_mode = incomingReadings.lock_mode; rgb_mode = incomingReadings.rgb_mode; strcpy(GPS_Latitude, incomingReadings.GPS_Latitude); strcpy(GPS_Longitude, incomingReadings.GPS_Longitude); } void espnow_setup() {
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) { Serial.println("Error initializing ESP-NOW"); return; }
esp_now_register_send_cb(OnDataSent); memcpy(peerInfo.peer_addr, ESPNOW_DEFAULT_PEER, 6); peerInfo.channel = 0; peerInfo.encrypt = false; if (esp_now_add_peer(&peerInfo) != ESP_OK){ Serial.println("Failed to add peer"); return; } esp_now_register_recv_cb(OnDataRecv); }
void espnow_loop() { uint32_t currentTime = millis(); if (currentTime - lastSendTime >= SEND_INTERVAL) { telecommand_data.rgb_mode = rgb_mode; telecommand_data.lock_mode = lock_mode; telecommand_data.RBX = button_data.RBX; telecommand_data.RBY = button_data.RBY; telecommand_data.LBX = button_data.LBX;
esp_err_t result = esp_now_send(ESPNOW_DEFAULT_PEER, (uint8_t *)&telecommand_data, sizeof(telecommand_data));
if (result == ESP_OK) { Serial.println("[" + String(millis()) + "] 数据已发送"); } else { Serial.println("[" + String(millis()) + "] 发送失败"); }
lastSendTime = millis(); }
}
|