This code builds fine, and it looks like the ESP Now thinks it’s working again.
But I can’t upload with my sensors connected.
And when I upload this code, when I power the I2C sensors, I get normal-looking serial output (the sensors aren’t sensing anything…). When I power the 5V LPG sensor, I get gibberish:
Last Packet Send Status: Delivery Fail
KR��B�C�KR����r9C�)�
��̀�!��)�
�������I�����b��C�IR�z�愥��)��!����)�
�������)�
� -c����)�
��%
I’m using a WeMos D1 Mini/32. The errors I was getting until I got the script to act like it was working all pointed at Adafruit BusIO, with its I2CDevice. Actually, it pointed to line 73 of 12CDevice.cpp.
/*
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 <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include "Adafruit_SGP30.h"
Adafruit_SGP30 sgp;
Adafruit_BME680 bme;
#define BOARD_ID 1
//MAC Address of the receiver
uint8_t broadcastAddress[] = {0x3C, 0x61, 0x05, 0x12, 0x84, 0x28};
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message
{
int id;
float temperature;
float humidity;
float pressure;
float LPG;
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;
//Create a struct_message called myData
struct_message myData;
unsigned int readingId = 0;
// 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 not found"));
while (1)
;
}
Serial.println("SGP30 test");
if (!sgp.begin())
{
Serial.println("SGP30 not found :(");
while (1)
;
}
}
// 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;
}
pinMode(A0, INPUT);
}
void calibrate()
{
// Set up oversampling and filter initialization
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
}
void loop()
{
float CO2 = sgp.eCO2;
float H2 = sgp.rawH2;
float EtOH = sgp.rawEthanol;
float VOC = bme.gas_resistance/1000;
float TVOC = sgp.TVOC;
float temperature = bme.temperature;
float humidity = bme.humidity;
float pressure = bme.pressure;
float CO = 0;
float LPG = 0;
float NH3 = 0;
float NO2 = 0;
for (int i = 0; i < 100; i++)
{
LPG = LPG + analogRead(36);
}
LPG = LPG / 25006; // get average reading
delay(50);
for (int i = 0; i < 100; i++)
{
CO = CO + analogRead(39);
}
CO = CO / 25006; // get average reading
delay(50);
for (int i = 0; i < 100; i++)
{
NH3 = NH3 + analogRead(34);
}
NH3 = NH3 / 25006; // get average reading
delay(50);
for (int i = 0; i < 100; i++)
{
NO2 = NO2 + analogRead(38);
}
NO2 = NO2 / 25006; // get average reading
delay(50);
float readingId = readingId++;
// Set values to send
myData.id = BOARD_ID;
myData.temperature = temperature;
myData.humidity = humidity;
myData.pressure = pressure;
myData.LPG = LPG;
myData.CO = CO;
myData.smoke = 0;
myData.moisture = 0;
myData.UV = 0;
myData.CO2 = CO2;
myData.NH3 = NH3;
myData.NO2 = NO2;
myData.VOC = VOC;
myData.TVOC = TVOC;
myData.H2 = H2;
myData.EtOH = EtOH;
myData.readingId = readingId;
// 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");
}
Serial.print(F("Temperature ="));
Serial.print(temperature, 0);
Serial.println(F(" *C"));
Serial.print(F("Humidity = "));
Serial.print(humidity, 0);
Serial.println(F(" %"));
Serial.print(F("LPG = "));
Serial.println(LPG, 0);
Serial.print(F("H2 = "));
Serial.println(H2, 0);
Serial.print(F("TVOC = "));
Serial.println(TVOC, 0);
delay(1000);
}