หลังจาก verify มี error
Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "NodeMCU-32S, 80MHz, 921600, None"
C:\abee_esp32\020_esp_now_MAC_ADDRESS_01_esp32\020_esp_now_MAC_ADDRESS_01_esp32.ino: In function 'void setup()':
020_esp_now_MAC_ADDRESS_01_esp32:87:28: error: invalid conversion from 'void (*)(uint8_t*, esp_now_send_status_t)' {aka 'void (*)(unsigned char*, esp_now_send_status_t)'} to 'esp_now_send_cb_t' {aka 'void (*)(const unsigned char*, esp_now_send_status_t)'} [-fpermissive]
esp_now_register_send_cb(OnDataSent);
^~~~~~~~~~
In file included from C:\abee_esp32\020_esp_now_MAC_ADDRESS_01_esp32\020_esp_now_MAC_ADDRESS_01_esp32.ino:4:
C:\Users\NECgen4UEFI\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.4/tools/sdk/esp32/include/esp_wifi/include/esp_now.h:165:54: note: initializing argument 1 of 'esp_err_t esp_now_register_send_cb(esp_now_send_cb_t)'
esp_err_t esp_now_register_send_cb(esp_now_send_cb_t cb);
~~~~~~~~~~~~~~~~~~^~
Multiple libraries were found for "Adafruit_Sensor.h"
Used: C:\Users\NECgen4UEFI\Documents\Arduino\libraries\Adafruit_Sensor-master
Not used: C:\Users\NECgen4UEFI\Documents\Arduino\libraries\Adafruit_Unified_Sensor
Multiple libraries were found for "WiFi.h"
Used: C:\Users\NECgen4UEFI\Documents\ArduinoData\packages\esp32\hardware\esp32\2.0.4\libraries\WiFi
Not used: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.57.0_x86__mdqgnx93n4wtt\libraries\WiFi
exit status 1
invalid conversion from 'void (*)(uint8_t*, esp_now_send_status_t)' {aka 'void (*)(unsigned char*, esp_now_send_status_t)'} to 'esp_now_send_cb_t' {aka 'void (*)(const unsigned char*, esp_now_send_status_t)'} [-fpermissive]
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
จากโค้ด
#include <Adafruit_Sensor.h>
#include <DHT.h>
//(ESP32)
#include <esp_now.h>
#include <WiFi.h>
// (ESP8266)
//#include <espnow.h>
//#include <ESP8266WiFi.h>
float humidity ;
float temperature;
uint8_t broadcastAddress[] = {0xC8, 0xC9, 0x84, 0xA3, 0x64, 0x2B};
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
float incomingTemperature;
float incomingHumidity;
const long interval = 10000;
unsigned long previousMillis = 0; // will store last time DHT was updated
// Variable to store if sending data was successful
String success;
typedef struct struct_message {
float temp;
float hum;
} struct_message;
struct_message DHTReadings;
struct_message incomingReadings;
esp_now_peer_info_t peerInfo;
// Callback when data is sent
//4 ฟังก์ชั่น OnDataSend
//ESP8266
//void OnDataSent(uint8_t *mac_addr, uint8_t status
//ESP32
void OnDataSent(uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("Last Packet Send Status: ");
// 5
//ESP8266
//sendStatus == 0
//ESP32
if (status == ESP_NOW_SEND_SUCCESS){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
}
// Callback when data is received
//(ESP8266)
//void OnDataRecv(uint8_t * mac, uint8_t *incomingData, **uint8_t** len)
//(ESP32)
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemperature = incomingReadings.temp;
incomingHumidity = incomingReadings.hum;
}
void setup() {
Serial.begin(9600);
//ตั้งเป็นโหมด Wi-Fi Station
WiFi.mode(WIFI_STA);
Serial.print("macAddress=");
Serial.println(WiFi.macAddress());
// สั่งให้เริ่ม ESP-NOW
// (ESP8266)[/u]
//esp_now_init() != 0
//(ESP32)
//esp_now_init() != ESP_OK
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set ESP-NOW Role
// esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
//6
//Register peer
//ESP8266
// esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
// esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
//ESP32
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
///-d
Serial.println(F("DHTxx test!"));
dht.begin();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you updated the DHT values
previousMillis = currentMillis;
//Get DHT readings
getReadings();
//Set values to send
DHTReadings.temp = temperature;
DHTReadings.hum = humidity;
// สั่งให้ส่งข้อมูล
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &DHTReadings, sizeof(DHTReadings));;
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
printIncomingReadings();
delay(10000);
}
}
void getReadings()
{
Serial.println("ผลที่อ่านได้จากESP32");
delay(2000);
// Read Temperature
temperature = dht.readTemperature();
delay(2000);
// Read temperature as Fahrenheit (isFahrenheit = true)
//float t = dht.readTemperature(true);
if (isnan(temperature)){
Serial.println("มีข้อผิดพลาด");
temperature = 0.0;
}
else
{
Serial.print("temperature=");
Serial.println(temperature);
}
humidity = dht.readHumidity();
delay(2000);
if (isnan(humidity)){
Serial.println("มีข้อผิดพลาด");
humidity = 0.0;
}
else
{
Serial.print("humidity=");
Serial.println(humidity);
}
}
void printIncomingReadings(){
// Display Readings in Serial Monitor
Serial.println("รับจาก NodeMCU8266 ");
Serial.print("Temperature: ");
Serial.print(incomingTemperature);
Serial.println(" ºC");
Serial.print("Humidity: ");
Serial.print(incomingHumidity);
Serial.println(" %");
}
ต้องแก้จุดไหนบ้างครับ