Invalid conversion avec espnow

Bonjour à tous,

Je suis en train de galérer sur une conversion invalide et je n'arrive pas à m'en sortir.

Je suis ce tuto :

Je vois bien que le problème se situe au niveau de le fonction "void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len)"

Mais je n'arrive pas à trouver ce qui cloche.

Voici mon code :

// Import required libraries
//#include <Arduino.h>
#include <ESP8266WiFi.h>
//#include <Hash.h>
//#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
//#include <Adafruit_Sensor.h>
//#include <time.h>
//#include <Wire.h>
#include <espnow.h>// https://github.com/esp8266/Arduino/blob/master/tools/sdk/include/espnow.h

uint8_t broadcastAddress[] = {0x80, 0x7d, 0x3a, 0x44, 0x5a, 0xb6};//80:7d:3a:44:5a:b6

// Replace with your network credentials
const char* ssid = "********";
const char* password = "********";

IPAddress ip(192, 168, 0, 39);
AsyncWebServer server(1039);


// current temperature & humidity, updated in loop()
float t1 = 0.0; // temperature exterieur
float h1 = 0.0; // temperature interieur
float newT1 = 0;
float newH1 = 0;

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
 int id;   // Numéro de la carte emettrice
 float t1;
 float h1;
} struct_message;
struct_message myData;

// Create a structure to hold the readings from each board
struct_message board1;
struct_message board2;
struct_message board3; 

// Create an array with all the structures
struct_message boardsStruct[3] = {board1, board2, board3};

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <style>
    html 
    {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 2.5rem; }
    p { font-size: 2.5rem; }
    .units { font-size: 1.2rem; }
    .dht-labels
    {
      font-size: 1.5rem;
      vertical-align:middle;
      padding-bottom: 5px;
    }
    
  </style>
</head>
<body>
  <h2>Capteur serre</h2>
  <p>
    <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> 
    <span class="dht-labels">Temperature Exterieur</span> 
    <br>
    <span id="temperature1">%TEMPERATURE1%</span>
    <sup class="units">&deg;C</sup>
  </p>

  <p>
    <i class="fas fa-tint" style="color:#00add6;"></i> 
    <span class="dht-labels">Humidity Exterieur</span>
    <br>
    <span id="humidity1">%HUMIDITY1%</span>
    <sup class="units">%%</sup>
  </p>

</body>
<script>
setInterval(function ( ) 
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() 
  {
    if (this.readyState == 4 && this.status == 200) 
    {
      document.getElementById("temperature1").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/temperature1", true);
  xhttp.send();
}, 10000 ) ;


setInterval(function ( ) 
{
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() 
  {
    if (this.readyState == 4 && this.status == 200) 
    {
      document.getElementById("humidity1").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/humidity1", true);
  xhttp.send();
}, 10000 ) ;


</script>
</html>)rawliteral";

// Replaces placeholder with DHT values
String processor(const String& var)
{
  //Serial.println(var);
  if(var == "TEMPERATURE INTERIEUR")
  {
    return String(t1);
  }
  else if(var == "HUMIDITE INTERIEUR")
  {
    return String(h1);
  }
  return String();
}

// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int 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));
  Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
  // Update the structures with the new incoming data
  boardsStruct[myData.id-1].t1 = myData.t1;
  boardsStruct[myData.id-1].h1 = myData.h1;
  Serial.printf("temp value: %d \n", boardsStruct[myData.id-1].t1);
  Serial.printf("hum value: %d \n", boardsStruct[myData.id-1].h1);
  Serial.println();
}

void setup(){
// Serial port for debugging purposes
Serial.begin(115200);

// Set device as a Wi-Fi Station
WiFi.mode(WIFI_AP_STA);
 // Init ESP-NOW
 if (esp_now_init() != 0) 
 {
   Serial.println("Error initializing ESP-NOW");
   return;
 }
 Serial.print(F("\nReciever initialized : "));
 Serial.println(WiFi.macAddress());
 // Define receive function
 esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
 esp_now_register_recv_cb(OnDataRecv);

  
  //dateCourante2=millis();
  //pinMode(ledpin, OUTPUT);
  IPAddress dnServer(89, 2, 0, 1);
  IPAddress gateway(192, 168, 0, 1);
  IPAddress subnet(255, 255, 255, 0);
   
  WiFi.config(ip, dnServer, gateway, subnet);
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
    Serial.println(".");
  }

  // Print ESP8266 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
  {
    request->send_P(200, "text/html", index_html, processor);
  });
  server.on("/temperature1", HTTP_GET, [](AsyncWebServerRequest *request)
  {
    request->send_P(200, "text/plain", String(t1).c_str());
  });
  
  server.on("/humidity1", HTTP_GET, [](AsyncWebServerRequest *request)
  {
    request->send_P(200, "text/plain", String(h1).c_str());
  });

  
  // Start server
  server.begin();

  Serial.println(WiFi.macAddress());
 }
 
void loop(){ 
 
  

  if (newT1 != board1t1 || newH1 != board1h1) 
  {
  
    // Read Temp
    newT1 = board1t1;
    // si la lecture de la température a échoué, ne pas modifier la valeur t1
    if (isnan(newT1)) 
    {
      Serial.println("Failed to read from DHT sensor!");
    }
    else 
    {
      t1 = newT1;
      Serial.print("temp : ");
      Serial.println(t1);
      
    }
    // Read Humidity
    newH1 = board1h1;
    
    // si la lecture de l'humidité a échoué, ne pas modifier la valeur h1
    if (isnan(newH1)) 
    {
      Serial.println("Failed to read from DHT sensor!");
    }
    else 
    {
      h1 = newH1;
      Serial.print("hum : ");
      Serial.println(h1);
      
    }
  }
 
}

Voici l'erreur :

Compilation error: invalid conversion from 'void (*)(const uint8_t*, const uint8_t*, int)' {aka 'void (*)(const unsigned char*, const unsigned char*, int)'} to 'esp_now_recv_cb_t' {aka 'void (*)(unsigned char*, unsigned char*, unsigned char)'} [-fpermissive]

J'ai essayé de faire d'autres conversions, changer le snprintf en sprintf ou printf, mais tout ce que j'ai essayé n'a pas fonctionné.

Auriez vous un idée d'ou j'ai merdé ?

ça fonctionne en mettant :

void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len)

Mais sans comprendre vraiment pourquoi, est ce une compatibilité de variable ou une compatibilité de variable avec la librairie ?

C'est ce qui est écrit ici : le 3ème argument doit être un char (donc 8 bits non signé) et pas un int (32 bits signé)

Merci @lesept ,

Je comprend, je me suis focalisé sur le type de donnée (int / char), alors que ce n'était pas le type de donnée le problème, mais la taille.
Taille qui doit etre définit dans la bibliothèque espnow.h.
En gros si j'ai bien compris ?

Mon erreur en suivant le Tuto vient en fait que je me suis mélangé les pinceaux entre l'esp32 et le l'esp8266 qui n'ont pas les même librairies.

Entete de la fonction pour l'esp32 librairie esp_now.h :

OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len)

Entete de la fonction pour l'esp8266 librairie espnow.h :

OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) 

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.