From 12d0a2dd5f3cdf141fee60fb12b7e80faab9a5f7 Mon Sep 17 00:00:00 2001 From: Dooho Yi Date: Tue, 16 Feb 2021 12:48:58 +0900 Subject: [PATCH] speed test esp_now --> very good. --- espnow/platformio.ini | 43 +++++++++++++++++++++ espnow/src/main.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 espnow/platformio.ini create mode 100644 espnow/src/main.cpp diff --git a/espnow/platformio.ini b/espnow/platformio.ini new file mode 100644 index 0000000..8559058 --- /dev/null +++ b/espnow/platformio.ini @@ -0,0 +1,43 @@ +; < NOTE > + +; to enable verbose output add option --> +; $ platformio run --verbose + +; to make this permanent for the proj. --> +; $ platformio settings set force_verbose Yes + +; then confirm the change --> +; $ platformio settings get + + +; // pio v 4.0 'Build options' +; - build_type +; - build_flags +; - src_build_flags +; - build_unflags +; - src_filter +; - targets + + +[platformio] +default_envs = d1_mini_pro + +[env] +framework = arduino +upload_speed = 460800 +upload_port = /dev/ttyUSB0 +lib_deps = + +[env:nodemcuv2] +platform = espressif8266 +board = nodemcuv2 +lib_deps = + ${env.lib_deps} + +[env:d1_mini_pro] +platform = espressif8266 +board = d1_mini_pro +upload_speed = 460800 +; 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 +lib_deps = + ${env.lib_deps} diff --git a/espnow/src/main.cpp b/espnow/src/main.cpp new file mode 100644 index 0000000..07a36c7 --- /dev/null +++ b/espnow/src/main.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +//uint8_t broadcastAddress[] = {0xF4, 0xCF, 0xA2, 0xED, 0xB3, 0xC5}; +uint8_t broadcastAddress[] = {0xF4, 0xCF, 0xA2, 0xED, 0xB7, 0x21}; + +typedef struct struct_message { + char a[32]; + int b; + float c; + String d; + bool e; +} struct_message; + +struct_message myData; + +unsigned long lastTime = 0; +unsigned long timerDelay = 20; // send readings timer + +// Callback when data is sent +void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) { + Serial.print("Last Packet Send Status: "); + if (sendStatus == 0) { + Serial.println("Delivery success"); + } + else{ + Serial.println("Delivery fail"); + } +} + +// Callback function that will be executed when data is received +void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) { + memcpy(&myData, incomingData, sizeof(myData)); + Serial.print("Bytes received: "); + Serial.println(len); + Serial.print("Char: "); + Serial.println(myData.a); + Serial.print("Int: "); + Serial.println(myData.b); + Serial.print("Float: "); + Serial.println(myData.c); + Serial.print("String: "); + Serial.println(myData.d); + Serial.print("Bool: "); + Serial.println(myData.e); + Serial.println(); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.print("ESP8266 Board MAC Address: "); + Serial.println(WiFi.macAddress()); + + WiFi.mode(WIFI_STA); + + // Init ESP-NOW + if (esp_now_init() != 0) { + Serial.println("Error initializing ESP-NOW"); + return; + } + + // Once ESPNow is successfully Init, we will register for Send CB to + // get the status of Trasnmitted packet + esp_now_set_self_role(ESP_NOW_ROLE_COMBO); + esp_now_register_send_cb(OnDataSent); + esp_now_register_recv_cb(OnDataRecv); + + // Register peer + esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0); +} + +void loop() { + if ((millis() - lastTime) > timerDelay) { + static int count = 0; + count++; + // Set values to send + strcpy(myData.a, "THIS IS A CHAR"); + myData.b = count; + myData.c = 1.2; + myData.d = "Hello"; + myData.e = false; + + // Send message via ESP-NOW + esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData)); + + lastTime = millis(); + } +}