The first version of this program had analog inputs at GPIO 2 and GPIO 4.
I get this error
[E][esp32-hal-adc.c:135] __analogRead(): GPIO2: ESP_ERR_TIMEOUT: ADC2 is in use by Wi-Fi.
[E][esp32-hal-adc.c:135] __analogRead(): GPIO4: ESP_ERR_TIMEOUT: ADC2 is in use by Wi-Fi.
I moved them down. IO2 and IO4 are not in this program.
What the heck??
Using WeMos D1R32.
Code:
#include <Arduino.h>
/*
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 <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include "Adafruit_SGP30.h"
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_SGP30 sgp;
Adafruit_BME680 bme; // I2C
// Set your Board ID (ESP32 Sender #1 = BOARD_ID 1, ESP32 Sender #2 = BOARD_ID 2, etc)
#define BOARD_ID 2
//MAC Address of the receiver
uint8_t broadcastAddress[] = {0xFC, 0xF5, 0xC4, 0x2F, 0xA1, 0x04};
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message
{
int id;
float completed;
float temperature;
float humidity;
float pressure;
float altitude;
float LPG;
float CO;
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 long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // Interval at which to publish sensor readings
unsigned int readingId = 0;
// Insert your SSID
constexpr char WIFI_SSID[] = "Fishing24";
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);
//Init Serial Monitor
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)
;
}
pinMode(35, INPUT);
pinMode(34, INPUT);
pinMode(36, INPUT);
pinMode(39, INPUT);
// 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;
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;
}
// 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 LPG = analogRead(35);
float NH3 = analogRead(34);
float NO2 = analogRead(36);
float CO = analogRead(39);
float CO2 = sgp.eCO2;
float VOC = bme.gas_resistance;
float TVOC = sgp.TVOC;
float H2 = sgp.rawH2;
float EtOH = sgp.rawEthanol;
float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
if (!bme.endReading())
{
Serial.println(F("Failed to complete reading :("));
return;
}
Serial.print(F("Reading completed at "));
Serial.println(millis());
float completed = millis();
Serial.print(F("Temperature = "));
Serial.print(bme.temperature, 1);
Serial.println(F(" *C"));
Serial.print(F("Humidity = "));
Serial.print(bme.humidity, 1);
Serial.println(F(" %"));
// Save the last time a new reading was published
previousMillis = currentMillis;
//Set values to send
myData.id = BOARD_ID;
myData.completed = completed;
myData.temperature = (bme.temperature);
myData.humidity = (bme.humidity);
myData.pressure = (bme.pressure);
myData.altitude = altitude;
myData.LPG = LPG;
myData.CO = CO;
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");
}
}
delay(5000);
}
ini:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
esp_now
WiFi
FS
ottowinter/ESPAsyncWebServer-esphome@^1.2.7
https://github.com/arduino-libraries/Arduino_JSON.git
https://github.com/adafruit/Adafruit_Sensor.git
https://github.com/adafruit/Adafruit_BME680.git
https://github.com/adafruit/Adafruit_SGP30.git