speed test esp_now --> very good.

This commit is contained in:
Dooho Yi 2021-02-16 12:48:58 +09:00
parent 50bac35c13
commit 12d0a2dd5f
2 changed files with 133 additions and 0 deletions

43
espnow/platformio.ini Normal file
View file

@ -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}

90
espnow/src/main.cpp Normal file
View file

@ -0,0 +1,90 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <espnow.h>
//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();
}
}