Error en datos resividos de ESPNOW - hc-sr04

he estado teniendo problemas al momento de recibir información, cuando envío información de un sensor ultrasónico Hc-sr04 la información que se recibe es incorrecta, sin embargo al enviar cualquier otro dato numérico la información llega correctamente.
Transmisor:
//ESP-Now power saving protocol
#include <esp_now.h>

#include <WiFi.h>

// Channel used for connection
#define CHANNEL 1
uint8_t EspNowAverage = 0;
// Configuramos los pines del sensor Trigger y Echo
const int PinTrig = 14;
const int PinEcho = 15;
float Promedio = 0;
unsigned long myTime;
// Structure with information about the next peer
esp_now_peer_info_t peer;
const float Muestras = 50;
float distancia;
// Constante velocidad sonido en cm/s
const float VelSon = 34000.0;

// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// data to determine the size of the readings array.

// Mac Address of the peer to which we will send the data
uint8_t peerMacAddress[] = {0x84, 0xCC, 0xA8, 0x65, 0x06, 0x64};

void setup() {
Serial.begin(115200);
// Ponemos el pin Trig en modo salida
pinMode(PinTrig, OUTPUT);
// Ponemos el pin Echo en modo entrada
pinMode(PinEcho, INPUT);
// put your setup code here, to run once:

// Call the function that initializes the station mode
modeStation();

// Call function that initializes ESPNow
InitESPNow();

// Add the peer
addPeer(peerMacAddress);

// Registers the callback that will inform us about the status of the submission.
// The function that will be executed is onDataSent and is stated below
esp_now_register_send_cb(OnDataSent);
}

//loop function being used to smooth analog input values
// void OnDataSent function sends values over and over again
void loop() {
if (millis() - myTime >= 1000) {

Promedio = 0;
for (int i = 0; i < Muestras; i++) {
  iniciarTrigger();
  unsigned long tiempo = pulseIn(PinEcho, HIGH);
  distancia = tiempo * 0.000001 * VelSon / 2.0;
  Promedio = Promedio + distancia;
}
Promedio = (float)round(Promedio / Muestras);
EspNowAverage=Promedio;
readAndSend();
Serial.println(EspNowAverage);

}

}

// Function responsible for pin reading and sending of data to the peer
void readAndSend() {
// Read the data of the pin
uint8_t data = EspNowAverage;
// Send the data to the peer
send(&data, peerMacAddress);
}

// Function that serves as a callback to warn us about the sending situation we made
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}

// Function to initialize the station mode
void modeStation() {
// We put the ESP in station mode
WiFi.mode(WIFI_STA);
// We show on Serial Monitor the Mac Address
// of this ESP when in station mode
Serial.print("Mac Address in Station: ");
Serial.println(WiFi.macAddress());
}

// ESPNow startup function
void InitESPNow() {
// If the initialization was successful
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
// If initialization failed
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}

// Function that adds a new peer through your MAC address
void addPeer(uint8_t *peerMacAddress) {
// We inform the channel
peer.channel = CHANNEL;
// 0 not to use encryption or 1 to use
peer.encrypt = 0;
// Copy array address to structure
memcpy(peer.peer_addr, peerMacAddress, 6);
// Add slave
esp_now_add_peer(&peer);
}

// Function that will send the data to the peer that has the specified mac address
void send(const uint8_t *data, uint8_t *peerMacAddress) {
esp_err_t result = esp_now_send(peerMacAddress, data, sizeof(data));
}

// Método que inicia la secuencia del Trigger para comenzar a medir
void iniciarTrigger()
{
// Ponemos el Triiger en estado bajo y esperamos 2 ms
digitalWrite(PinTrig, LOW);
delayMicroseconds(2);

// Ponemos el pin Trigger a estado alto y esperamos 10 ms
digitalWrite(PinTrig, HIGH);
delayMicroseconds(10);

// Comenzamos poniendo el pin Trigger en estado bajo
digitalWrite(PinTrig, LOW);
}

Receptor:

//Libraries espnow and wifi
#include <esp_now.h>
#include <WiFi.h>

// Channel used for connection
#define CHANNEL 1

uint8_t data[1];

void setup() {
Serial.begin(115200);

modeStation();

InitESPNow();

// Registers the callback that will inform us that we receive data. The function to be executed is onDataRecv and is declared below
esp_now_register_recv_cb(onDataRecv);

// Registers the callback that will inform us
// about the status of the submission.
// The function to be executed
// is onDataSent and is declared below
esp_now_register_send_cb(onDataSent);

}

// We send the data as soon as we get from the other esp by the callback
void loop() {

}

// Function that serves as a callback to warn us we received data
void onDataRecv(const uint8_t *mac_addr, const uint8_t *data, int len) {
// Place the received data at the output of the pin
Serial.println(*data);
}

// Function that serves as a callback to warn us about the sending situation we made
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}

// Function to initialize the station mode
void modeStation() {
// We put the ESP in station mode
WiFi.mode(WIFI_STA);
// We show on Serial Monitor the Mac Address
// of this ESP when in station mode
Serial.print("Mac Address in Station: ");
Serial.println(WiFi.macAddress());
}

// ESPNow startup function
void InitESPNow() {
// If the initialization was successful
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
// If initialization failed
else {
Serial.println("ESPNow Init Failed");
ESP.restart();
}
}

Moderador:
Bienvenido al foro Arduino en Español
Te invito a que leas las normas del foro y edites este post de modo que el código se vea como corresponde.
Entra a editar tu post, selecciona todo el código, lo cortas, click en </> y luego pegas y quedará correctamente.
Otra falta es que luego de leer las normas comprendes que hay secciones para cada cosa. Todo lo que no es AVR o sea Arduino original o clon va en Microcontroladores.
Movido a dicha sección.
Gracias por comprender y leer las normas.

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