ใช้ ฟังก์ชั่น ESPNOW มี error : invalid conversion from 'void

หลังจาก 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(" %");
}

ต้องแก้จุดไหนบ้างครับ

ตอนนี้ลองเข้าเว๊บ ที่ 3

esp32-ตัวส่ง

/*
  ESP-NOW Demo - Transmit
  esp-now-demo-xmit.ino
  Sends data to Responder
  
  DroneBot Workshop 2022
  https://dronebotworkshop.com
*/
//esp32a
//ผลที่ได้คือ01=  78:21:84:7A:B3:88
//       02=  58:BF:25:9F:CA:1C

// Include Libraries
#include <esp_now.h>
#include <WiFi.h>

// Variables for test data
int int_value;
float float_value;
bool bool_value = true;

// MAC Address of responder - edit as required
uint8_t broadcastAddress[] = {0x58, 0xBF, 0x25, 0x9F, 0xCA, 0x1C};//ส่งไป esp32_02

// Define a data structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  bool d;
} struct_message;

// Create a structured object
struct_message myData;

// Peer info
esp_now_peer_info_t peerInfo;

// Callback function called when data is sent
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");
}

void setup() {
  
  // Set up Serial Monitor
  Serial.begin(115200);
 
  // Set ESP32 as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Initilize ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Register the send callback
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}

void loop() {

  // Create test data

  // Generate a random integer
  int_value = random(1,20);

  // Use integer to make a new float
  float_value = 1.3 * int_value;

  // Invert the boolean value
  bool_value = !bool_value;
  
  // Format structured data
  strcpy(myData.a, "Welcome to the Workshop!");
  myData.b = int_value;
  myData.c = float_value;
  myData.d = bool_value;
  
  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
   
  if (result == ESP_OK) {
    Serial.println("Sending confirmed");
  }
  else {
    Serial.println("Sending error");
  }
  delay(2000);
}

esp32-ตัวรับ

/*
  ESP-NOW Demo - Transmit
  esp-now-demo-xmit.ino
  Sends data to Responder
  
  DroneBot Workshop 2022
  https://dronebotworkshop.com
*/
//esp32a
//ผลที่ได้คือ01=  78:21:84:7A:B3:88
//       02=  58:BF:25:9F:CA:1C
/*
  ESP-NOW Demo - Receive
  esp-now-demo-rcv.ino
  Reads data from Initiator
  
  DroneBot Workshop 2022
  https://dronebotworkshop.com
*/

// Include Libraries
#include <esp_now.h>
#include <WiFi.h>

// Define a data structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  bool d;
} struct_message;

// Create a structured object
struct_message myData;


// Callback function executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Data received: ");
  Serial.println(len);
  Serial.print("Character Value: ");
  Serial.println(myData.a);
  Serial.print("Integer Value: ");
  Serial.println(myData.b);
  Serial.print("Float Value: ");
  Serial.println(myData.c);
  Serial.print("Boolean Value: ");
  Serial.println(myData.d);
  Serial.println();
}

void setup() {
  // Set up Serial Monitor
  Serial.begin(115200);
  
  // Set ESP32 as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Initilize ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Register callback function
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {

}

เป็นแบบ one way
ส่งได้ครับ ลอกโค้ด 100% ยกเว้น ค่า mac address

ลองระยะรับส่งรึยังครับเคยดูรีวิวไกลถึง 500ม แต่ระยะเสถียร แถวๆ 300ม

ยังไม่ลองครับ

ถ้าลองแล้วจะแจ้งให้ทราบอีกทีครับ…

ขอตย. โค้ดespnow ตัวส่งเป็น esp32 และต้วรับเป็นesp8266หน่อยครับ
ผมลอง3ชุดแล้วยังไม่ได้อีกครับ

ขอบคุณ​ครับ​