So get this- one variable out of thirteen doesn't transmit via ESP Now

Okay, so I may have been crazy when I got here, and it’s pointless to think about the one thing that may have pushed someone over the edge, but this one might drive me crazy if I’m not already crazy.

I have a lot of variables:

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 TVOC;
  float H2;
  float EtOH;
  int readingId;
} struct_message;

But they’re divided among three sensor pods. All of those variables transmit, except for ‘pressure’.
The pods say they’re transmitting pressure- 30. Both say 30.
I can’t get the receiver to receive it.

I know it worked a few times, but it was during periods of high flux, when too many independent variables were variable.

But I have three sensor pods feeding ESP Now into a receiver. EVERYTHING works but pressure.
Please, please somebody see something that can help me stop maybe going crazier than I would have been.

Here’s Sender1 and Receiver.
Sender1:

/*

  Rui Santos

  Complete project details at https://RandomNerdTutorials.com/esp32-esp-now-wi-fi-web-server/

  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 <Arduino.h>

#include <Arduino_JSON.h>

#include <esp_now.h>

#include <esp_wifi.h>

#include <WiFi.h>

#include <Adafruit_Sensor.h>

#include "Adafruit_BME680.h"

//#include "Adafruit_SGP30.h"

Adafruit_BME680 bme; // I2C

//Adafruit_SGP30 sgp;

JSONVar board;

#define BOARD_ID 1

#define SEALEVELPRESSURE_HPA (1026.25)

unsigned int readingId = 0;

// MAC Address of the receiver

uint8_t broadcastAddress[] = {0x8C, 0xaa, 0xb5, 0x86, 0x1C, 0x94};

// Structure example to send data

// Must match the receiver 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 TVOC;

  float H2;

  float EtOH;

  unsigned int readingId;

} struct_message;

// Create a struct_message called myData

struct_message myData;

// Insert your SSID

constexpr char WIFI_SSID[] = "TP-Link_7C28";

int32_t getWiFiChannel(const char *ssid)

{

  if (int32_t n = WiFi.scanNetworks())

  {

    for (uint8_t i = 0; i < n; i++)

    {

      if (!strcmp(ssid, WiFi.SSID(i).c_str()))

      {

        return WiFi.channel(i);

      }

    }

  }

  return 0;

}

// callback 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()

{

  Serial.begin(9600);

  while (!Serial)

    ;

  Serial.println(F("BME680 test"));

  if (!bme.begin())

  {

    Serial.println(F("BME680 sensor test failed."));

    while (1)

      ;

  }

 /* Serial.println("SGP30 test");

  if (!sgp.begin())

  {

    Serial.println("SGP30 not found :(");

    while (1)

      ;

  }

  */

  bme.setTemperatureOversampling(BME680_OS_8X);

  bme.setHumidityOversampling(BME680_OS_2X);

  bme.setPressureOversampling(BME680_OS_4X);

  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);

  bme.setGasHeater(320, 150); // 320*C for 150 ms

  // Set device as a Wi-Fi Station and set channel

  WiFi.mode(WIFI_STA);

  int32_t channel = getWiFiChannel(WIFI_SSID);

  WiFi.printDiag(Serial); // Uncomment to verify channel number before

  esp_wifi_set_promiscuous(true);

  esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);

  esp_wifi_set_promiscuous(false);

  WiFi.printDiag(Serial); // Uncomment to verify channel change after

  // Init ESP-NOW

  if (esp_now_init() != ESP_OK)

  {

    Serial.println("Error initializing ESP-NOW");

    return;

  }

  // 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

  esp_now_peer_info_t peerInfo;

  memset(&peerInfo, 0, sizeof(peerInfo));

  memcpy(peerInfo.peer_addr, broadcastAddress, 6);

  peerInfo.encrypt = false;

  // Add peer

  if (esp_now_add_peer(&peerInfo) != ESP_OK)

  {

    Serial.println("Failed to add peer");

    return;

  }

}

void loop()

{

  readingId = readingId + 1;

  // Tell BME680 to begin measurement.

  unsigned long endTime = bme.beginReading();

  if (endTime == 0)

  {

    Serial.println(F("BME680 measurement failed :("));

    return;

  }

  if (!bme.endReading())

  {

    Serial.println(F("Failed to complete reading :("));

    return;

  }

  Serial.print("Temperature: ");

  Serial.print(bme.temperature, 1);

  Serial.println(" °C");

  Serial.print("Humidity:    ");

  Serial.print(bme.humidity, 0);

  Serial.println(" %");

  Serial.print("Press:       ");

  Serial.print((bme.pressure / 100.0) * 0.03, 0);

  Serial.println(" in Hg");

  Serial.print("VOC = ");

  Serial.print(bme.gas_resistance);

  Serial.println(" KOhms");

  Serial.print("readingId = ");

  Serial.print(readingId);

  Serial.println();

 

float pressure = ((bme.pressure / 100.0) * 0.03, 0);

  // Set values to send

  myData.id = BOARD_ID;

  myData.temperature = bme.temperature;

  myData.humidity = bme.humidity;

  myData.pressure = pressure;

  myData.VOC = bme.gas_resistance / 1000;

  myData.readingId = readingId;

/*

  myData.CO2 = sgp.eCO2;

  myData.TVOC = sgp.TVOC;

  myData.H2 = sgp.rawH2;

  myData.EtOH = sgp.rawEthanol;

  myData.moisture = 0;

  */

  // Send message via ESP-NOW

  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)&myData, sizeof(myData));

  if (result == ESP_OK)

  {

    Serial.println("Sent with success");

  }

  else

  {

    Serial.println("Error sending the data");

  }

  delay(5000);

}

Receiver:

#include <Arduino.h>

#include <esp_now.h>

#include <WiFi.h>

#include "ESPAsyncWebServer.h"

#include <Arduino_JSON.h>

#include "Adafruit_GFX.h"

#include "Fonts/FreeSans9pt7b.h"

#include <MCUFRIEND_kbv.h>

#include <NTPClient.h>

#include <WiFiUdp.h>

#define NTP_OFFSET -14400      // In seconds

#define NTP_INTERVAL 60 * 1000 // In miliseconds

#define NTP_ADDRESS "pool.ntp.org"

#define CYAN 0x07FF

#define MAGENTA 0xF81F

#define YELLOW 0xFFE0

#define BLACK 0x0000

#define PURPLE 0x780F

#define PINK 0xFC9F

// Replace with your network credentials (STATION)

const char *ssid = "TP-Link_7C28";

const char *password = "64411811";

// 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 TVOC;

  float H2;

  float EtOH;

  int readingId;

} struct_message;

struct_message incomingReadings;

WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);

MCUFRIEND_kbv tft;

JSONVar board;

AsyncWebServer server(80);

AsyncEventSource events("/events");

// callback function that will be executed when data is received

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

{

  // Copies the sender mac address to a string

  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(&incomingReadings, incomingData, sizeof(incomingReadings));

  board["id"] = incomingReadings.id, len;

  board["temperature"] = incomingReadings.temperature;

  board["humidity"] = incomingReadings.humidity;

  board["pressure"] = incomingReadings.pressure;

  board["Gas"] = incomingReadings.Gas;

  board["CO"] = incomingReadings.CO;

  board["smoke"] = incomingReadings.smoke;

  board["moisture"] = incomingReadings.moisture;

  board["UV"] = incomingReadings.UV;

  board["CO2"] = incomingReadings.CO2;

  board["NH3"] = incomingReadings.NH3;

  board["NO2"] = incomingReadings.NO2;

  board["VOC"] = incomingReadings.VOC;

  board["TVOC"] = incomingReadings.TVOC;

  board["H2"] = incomingReadings.H2;

  board["EtOH"] = incomingReadings.EtOH;

  board["readingId"] = incomingReadings.readingId;

  Serial.printf("Board ID %u: %u bytes\n", incomingReadings.id, len);

  String jsonString = JSON.stringify(board);

  events.send(jsonString.c_str(), "new_readings", millis());

}

const char index_html[] PROGMEM = R"rawliteral(

<!DOCTYPE HTML><html>

<head>

  <title>Monster Laboratories Smart Home</title>

  <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">

  <link rel="icon" href="data:,">

  <style>

    html {font-family: Arial; display: inline-block; text-align: center;}

    p {  font-size: 1.2rem;}

    body {  margin: 0;}

    .topnav { overflow: hidden; background-color: #2f4468; color: white; font-size: 1.7rem; }

    .content { padding: 20px; }

    .card { background-color: white; box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5); }

    .cards { max-width: 3500px; margin: 0 auto; display: grid; grid-gap: 2rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

    .reading { font-size: 2.8rem; }

    .packet { color: #bebebe; }

    .card.temperature { color: #fd7e14; }

    .card.humidity { color: #1b78e2; }

    .card.pressure { color: #17b8e2; }

    .card.UV { color: #17b8e2; }

  </style>

</head>

<body>

  <div class="topnav">

    <h3>Monster Laboratories SmartHome- Air Quality</h3>

  </div>

  <div class="content">

    <div class="cards">

     

      <div class="card temperature">

        <h4><i class="fas fa-thermometer-half"></i> Pod 1 - TEMPERATURE</h4>

        <p><span class="reading"><span id="t1"></span> &deg;C</span></p>

        <p class="packet">Reading ID: <span id="rt1"></span></p>

      </div>

       <div class="card humidity">

        <h4><i class="fas fa-tint"></i> Pod 1 - HUMIDITY</h4>

        <p><span class="reading"><span id="h1"></span> &percnt;</span></p>

        <p class="packet">Reading ID: <span id="rh1"></span></p>

      </div>    

     

      <div class="card pressure">

        <h4><i class="fas fa-tint"></i> Pod 1 - PRESSURE</h4>

        <p><span class="reading"><span id="p1"></span> "Hg</span></p>

        <p class="packet">Reading ID: <span id="rp1"></span></p>

      </div>  

      <div class="card temperature">

        <h4><i class="fas fa-thermometer-half"></i> Pod 3 - TEMPERATURE</h4>

        <p><span class="reading"><span id="t3"></span> &deg;C</span></p>

        <p class="packet">Reading ID: <span id="rt3"></span></p>

      </div>

     

      <div class="card UV">

        <h4><i class="fas fa-tint"></i> Pod 3 - UVindex</h4>

        <p><span class="reading"><span id="u1"></span> ... </span></p>

        <p class="packet">Reading ID: <span id="ru1"></span></p>

      </div>

      <div class="card pressure">

        <h4><i class="fas fa-tint"></i> Pod 3 - PRESSURE</h4>

        <p><span class="reading"><span id="p3"></span> "Hg</span></p>

        <p class="packet">Reading ID: <span id="rp3"></span></p>

      </div>  

    </div>

  </div>

<script>

if (!!window.EventSource) {

 var source = new EventSource('/events');

 

 source.addEventListener('open', function(e) {

  console.log("Events Connected");

 }, false);

 source.addEventListener('error', function(e) {

  if (e.target.readyState != EventSource.OPEN) {

    console.log("Events Disconnected");

  }

 }, false);

 

 source.addEventListener('message', function(e) {

  console.log("message", e.data);

 }, false);

 

 source.addEventListener('new_readings', function(e) {

  console.log("new_readings", e.data);

  var obj = JSON.parse(e.data);

  document.getElementById("t"+obj.id).innerHTML = obj.temperature.toFixed(0);

  document.getElementById("h"+obj.id).innerHTML = obj.humidity.toFixed(0);

  document.getElementById("p"+obj.id).innerHTML = obj.pressure.toFixed(0);

  document.getElementById("u"+obj.id).innerHTML = obj.UV.toFixed(0);

  document.getElementById("rt"+obj.id).innerHTML = obj.readingId;

  document.getElementById("rh"+obj.id).innerHTML = obj.readingId;

  document.getElementById("rp"+obj.id).innerHTML = obj.readingId;

  document.getElementById("ru"+obj.id).innerHTML = obj.readingId;

  console.log(obj.readingId);

 }, false);

}

</script>

 

</body>

</html>)rawliteral";

;

void setup()

{

  Serial.begin(9600);

  uint16_t ID = tft.readID();

  tft.begin(ID);

  // Set the device as a Station and Soft Access Point simultaneously

  WiFi.mode(WIFI_AP_STA);

  // Set device as a Wi-Fi Station

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)

  {

    delay(1000);

    Serial.println("Setting as a Wi-Fi Station..");

  }

  IPAddress local_IP(192, 168, 0, 100);

  Serial.print("Station IP Address: ");

  Serial.println(WiFi.localIP());

  Serial.print("Wi-Fi Channel: ");

  Serial.println(WiFi.channel());

  // Init ESP-NOW

  if (esp_now_init() != ESP_OK)

  {

    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_register_recv_cb(OnDataRecv);

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)

            { request->send_P(200, "text/html", index_html); });

  events.onConnect([](AsyncEventSourceClient *client)

                   {

    if (client->lastId())

    {

      Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());

    }

    // send event with message "hello!", id current millis

    // and set reconnect delay to 1 second

    client->send("hello!", NULL, millis(), 10000); });

  server.addHandler(&events);

  server.begin();

}

void loop()

{  

timeClient.begin();

timeClient.update();

String formattedTime = timeClient.getFormattedTime();

tft.fillScreen(BLACK);

tft.setFont(&FreeSans9pt7b);

tft.setTextSize(1);

tft.setTextColor(YELLOW);

tft.setCursor(1, 20);

tft.print("Station IP : ");

tft.println(WiFi.localIP());

if(incomingReadings.id == 1){

    tft.setTextColor(CYAN);

 }

else if (incomingReadings.id == 2){

    tft.setTextColor(PINK);

 }

else if (incomingReadings.id == 3){

    tft.setTextColor(MAGENTA);

 }

tft.print("Time: ");

tft.println(formattedTime);

tft.print("Board ID:    ");

tft.println(incomingReadings.id);

tft.print("Temperature: ");

tft.print(incomingReadings.temperature, 1);

tft.println(" °C");

tft.print("Pressure:    ");

tft.print(incomingReadings.pressure, 0);

tft.println(" in Hg");

tft.print("Humidity:    ");

tft.print(incomingReadings.humidity, 0);

tft.println(" %");

tft.print("CO:           ");

tft.print(incomingReadings.CO, 0);

tft.println(" PPM");

tft.print("LPG:         ");

tft.print(incomingReadings.Gas, 0);

tft.println(" PPM");

tft.print("Smoke:     ");

tft.println(incomingReadings.smoke, 0);

tft.print("Uv index:    ");

tft.println(incomingReadings.UV, 0);

tft.print("CO2:         "),

tft.print(incomingReadings.CO2, 0);

tft.println(" PPM");

tft.print("VOC:         ");

tft.print(incomingReadings.VOC, 0);

tft.println(" PPM");

tft.print("TVOC:       ");

tft.print(incomingReadings.TVOC, 0);

tft.println(" PPM");

tft.print("H2:            ");

tft.print(incomingReadings.H2, 0);

tft.println(" PPM");

tft.print("EtOH:        ");

tft.print(incomingReadings.EtOH, 0);

tft.println(" PPM");

tft.print("NH3:         "),

tft.println(incomingReadings.NH3, 0);

tft.println(" PPM");

tft.print("NO2:         "),

tft.print(incomingReadings.NO2, 0);

tft.println(" PPM");

tft.print("Reading ID:  ");

tft.print(incomingReadings.readingId);

Serial.print("Pressure:    ");

Serial.print(incomingReadings.pressure, 0);

Serial.println(" in Hg");

static unsigned long lastEventTime = millis();

static const unsigned long EVENT_INTERVAL_MS = 5000;

if ((millis() - lastEventTime) > EVENT_INTERVAL_MS)

   {

        events.send("ping", NULL, millis());

        lastEventTime = millis();

   }

delay(10000);

 }```

What is that ,0 doing there? Looks to me like that causes pressure to always get the result of the expression 0, aka, 0.

You printed it like

the 0 there was the number of decimal places

But that is only valid as the argument in Serial.print. Just send it like

float pressure = (bme.pressure / 100.0) * 0.03;

(Though I’m still not sure why you would want to divide by 100 and then times 3%.)

Yes, the ‘, 0’ was eliminating the decimal. I saw that two-step math problem and knew I would fix it, but I can’t accept that computers refuse to do math.

VOC works fine, and that’s the same thing:
myData.VOC = (bme.gas_resistance / 1000);

I wanted to replace it with a variable, “pressure,” but I guess I put it too early. I just put it right after bme.beginReading(), and it’s transmitting like a champ.

Thank you.

Strictly said, putting , <number> after a floating point expression is not doing any decimal places capping, in C/C++ that’s just starting a new statement, and the result of the statement 0 is 0. If you wanted to round to 0 decimal places (aka the nearest integer), you could be using just the round function from math.h, but you’ll waste space in the float that way, so better to transmit the full-precision float. Receiver can still decide to throw away some precision.

Really cool that I just watched you type that.
The only purpose is limiting the display. I did try to transmit ‘temperature, 1’, for example, but it wouldn’t accept any arguments in the ESP-Now functions.