I'm using an Esp32 devkit v1 for this program. My code is:
#include <MQ135.h>
#include <DHT.h>
#include <ThingSpeak.h>
#include <WiFi.h>
const char* ssid = "xxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxx";
unsigned long channelID = 1;
const char* writeAPIKey = "xxxxxxxxxxxxxxxx";
WiFiClient client;
#define PIN_MQ135 12 // MQ135 Analog Input Pin
#define DHTPIN 13 // DHT Digital Input Pin
#define DHTTYPE DHT11 // DHT11 or DHT22, depends on your sensor
MQ135 mq135_sensor(PIN_MQ135);
DHT dht(DHTPIN, DHTTYPE);
float temperature, humidity; // Temp and Humid floats, will be measured by the DHT
void setup() {
Serial.begin(9600);
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("Connected.");
dht.begin();
}
}
void loop() {
humidity = dht.readHumidity();
temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float rzero = mq135_sensor.getRZero();
float correctedRZero = mq135_sensor.getCorrectedRZero(temperature, humidity);
float resistance = mq135_sensor.getResistance();
float ppm = mq135_sensor.getPPM();
float correctedPPM = mq135_sensor.getCorrectedPPM(temperature, humidity);
Serial.print("MQ135 RZero: ");
Serial.print(rzero);
Serial.print("\t Corrected RZero: ");
Serial.print(correctedRZero);
Serial.print("\t Resistance: ");
Serial.print(resistance);
Serial.print("\t PPM: ");
Serial.print(ppm);
Serial.print("ppm");
Serial.print("\t Corrected PPM: ");
Serial.print(correctedPPM);
Serial.println("ppm");
delay(300);
}
My problem is that before adding the following line of code: WiFi.begin(ssid, password); the sensor data is displayed correctly, but after adding that line, the following messages are displayed on the monitor.
Although the DHT11 sensor value is not printed in the code, it still displays the readings normally with or without adding the aforementioned line of code.
I need to establish the WiFi connection in order to send the data to ThingSpeak. How could I solve this problem?
