I copied this ESP Now method from Random Nerd Tutorials, which is usually super-reliable.
It doesn’t work.
This is serial output: Ȥl���Ȥl�ɕ2dO,4
Serial speed is correct.
I haven’t figured out how to use the ESP-Prog. Can you help?
#include <Arduino.h>
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp-now-esp8266-nodemcu-arduino-ide/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <Arduino_JSON.h>
#include "ESPAsyncWebServer.h"
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message
{
int id;
float temperature;
float humidity;
float pressure;
float Gas;
float CO;
float smoke;
float moisture;
float UV;
float CO2;
float NH3;
float NO2;
float VOC;
float H2;
unsigned int counter;
} struct_message;
// Create a struct_message called myData
struct_message myData;
JSONVar board;
AsyncEventSource events("/events");
// Callback function that will be executed when data is received
void OnDataRecv(uint8_t *mac_addr, uint8_t *incomingData, uint8_t len)
{
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
memcpy(&myData, incomingData, sizeof(myData));
board["id"] = myData.id;
board["temperature"] = myData.temperature;
board["humidity"] = myData.humidity;
// board["pressure"] = myData.pressure;
board["counter"] = String(myData.counter);
String jsonString = JSON.stringify(board);
events.send(jsonString.c_str(), "new_readings", millis());
Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
Serial.printf("t value: %4.2f \n", myData.temperature);
Serial.printf("h value: %4.2f \n", myData.humidity);
Serial.printf("p value: %4.2f \n", myData.pressure);
Serial.print("ID:");
Serial.print(myData.counter);
Serial.println();
}
void setup()
{
// Initialize Serial Monitor
Serial.begin(9600);
Serial.println("begin");
// Set device as a Wi-Fi Station
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 recv CB to
// get recv packer info
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);
}
void loop()
{
Serial.println("beginffgfg");
}