/*************************************************************
ESP32 + MQ-135 + DHT22
*************************************************************/
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// DHT22
#include "DHT.h"
#define DHTPIN 4 // Pin-ul digital conectat la DHT22
const int mq135Pin = 0;
#define DHTTYPE DHT22
#define REPORTING_PERIOD_MS 1000
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
BlynkTimer timer;
// Initializare DHT22
DHT dht(DHTPIN, DHTTYPE);
// Variabila delays
uint32_t tsLastReport = 0;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
// Update state
Blynk.virtualWrite(V1, value);
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V2, millis() / 1000);
if (millis() - tsLastReport > REPORTING_PERIOD_MS)
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Blynk.virtualWrite(V4, h);
Blynk.virtualWrite(V5, t);
Blynk.virtualWrite(V6, hic);
int sensorValue = analogRead(mq135Pin); // Pin 0 used
Serial.println(sensorValue); // Reading ONLY value of 0
tsLastReport = millis();
}
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
dht.begin();
}
void loop()
{
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}
This is working!
// Define the analog pin connected to the MQ-135 sensor
const int mq135Pin = 0;
uint32_t tsLastReport = 0;
#define REPORTING_PERIOD_MS 1000
void setup() {
// Start the serial communication
Serial.begin(115200);
}
void loop() {
if (millis() - tsLastReport > REPORTING_PERIOD_MS)
{
int sensorValue = analogRead(mq135Pin);
Serial.println(sensorValue);
tsLastReport = millis();
}
}
(There's no point in sending it to Blynk if there is no value)
First code serial output:
Second code serial output:
Also the range of values change for every pin taken. As of now, with the 1st code which is displaying 0 values and 2nd working code, the voltage between literal PIN0 and GND is the same ~2.7V. I have to mention that I connected 1KOhm resistor between AO of MQ-135 and PIN0 (such that it takes no damage, because these ESP32 pins operate at 3.3V - that's what I remember I read.)
I also mention that I've disconnected my LCD which could be a power drainer.
Any advices?