i tried to make a monitoring system using nodemcu esp8266 with 3 sensor (dht22,mq135,water level) for my project.
i’ve written this lines of code :
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_TEMPLATE_ID "TMPL6z0RDKd8g"
#define BLYNK_TEMPLATE_NAME "fyp"
#define BLYNK_AUTH_TOKEN "MzNiiGHCM9XPH8JSHtx9T0fr00X6Owtq"
// Define your WiFi credentials and Blynk authentication token
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "bukan untuk kamu";
char pass[] = "brendenpaul";
// Initialize DHT sensor
#define DHTPIN D2 // Digital pin where the DHT22 is connected
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Initialize analog pin for MQ135
const int mq135Pin = A0;
// Initialize digital pin for water level sensor
const int waterLevelPin = D3;
void setup() {
Serial.begin(9600);
// Connect to Wi-Fi
Blynk.begin(auth, ssid, pass);
// Initialize DHT sensor
dht.begin();
}
void loop() {
Blynk.run();
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Read air quality from MQ135 sensor
int airQuality = analogRead(mq135Pin);
// Read water level sensor
int waterLevel = digitalRead(waterLevelPin);
// Print sensor data to Serial Monitor
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("Air Quality: ");
Serial.println(airQuality);
Serial.print("Water Level: ");
Serial.println(waterLevel);
// Send data to Blynk app
Blynk.virtualWrite(V1, temperature); // Virtual Pin V1 for temperature
Blynk.virtualWrite(V2, humidity); // Virtual Pin V2 for humidity
Blynk.virtualWrite(V3, airQuality); // Virtual Pin V3 for air quality
Blynk.virtualWrite(V4, waterLevel); // Virtual Pin V4 for water level
delay(10000); // Delay for 10 seconds before reading the sensors again
}
i did connected the nodemcu to the server once but after troubleshooting the project because the input readings not correct. when i tried to reconnect the project again, it appear offline again. can anyone help?
thank you bill, i did change my coding based on the link. but still got offline message
my new codes:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
char auth[] = "O5u6ObuMwhfBtM59zSGF1cRxpXDFSnC-"; // Replace with your Blynk Auth Token
char ssid[] = "bukan untuk kamu"; // Replace with your Wi-Fi SSID
char pass[] = "brendenpaul"; // Replace with your Wi-Fi password
#define DHTPIN 2 // Signal pin of the DHT22 connected to GPIO 2
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
const int MQ135Pin = A0; // Analog pin for MQ135 gas sensor
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // in Celsius
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read data from MQ135 gas sensor and send it to Virtual Pin 3
int mqValue = analogRead(MQ135Pin);
Blynk.virtualWrite(V3, mqValue);
// Send temperature data to Virtual Pin 1 and humidity data to Virtual Pin 2
Blynk.virtualWrite(V1, t); // Temperature to Virtual Pin 1
Blynk.virtualWrite(V2, h); // Humidity to Virtual Pin 2
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
// Setup a timer to send data to Blynk every 1000ms (1 second)
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}
@Brenden_Alt Please edit your posts, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.
Add: #define BLYNK_PRINT Serial
near the top of your sketch, and ensure that your serial monitor is set to the baud rate you define in your Serial.begin() command.
You should then get some useful serial output that points you in the right direction.
If you need assistance with understanding what you’re seeing, or solving the issues that are highlighted in your serial monitor, then copy the serial monitor text and paste it (with triple backticks) into a post.
So not post a screenshot of a portion of your serial output.
@PeteKnight i followed your instruction but serial monitor is still blank.
iill show you my full code to give more insight:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#define BLYNK_PRINT Serial // Add this line to enable Blynk debug output
char auth[] = "MzNiiGHCM9XPH8JSHtx9T0fr00X6Owtq"; // Replace with your Blynk Auth Token
char ssid[] = "bukan untuk kamu"; // Replace with your Wi-Fi SSID
char pass[] = "brendenpaul"; // Replace with your Wi-Fi password
#define DHTPIN 2 // Signal pin of the DHT22 connected to GPIO 2
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
const int MQ135Pin = A0; // Analog pin for MQ135 gas sensor
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // in Celsius
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read data from MQ135 gas sensor and send it to Virtual Pin 3
int mqValue = analogRead(MQ135Pin);
Blynk.virtualWrite(V3, mqValue);
// Send temperature data to Virtual Pin 1 and humidity data to Virtual Pin 2
Blynk.virtualWrite(V1, t); // Temperature to Virtual Pin 1
Blynk.virtualWrite(V2, h); // Humidity to Virtual Pin 2
}
void setup()
{
Serial.begin(115200); // Set the baud rate to match your serial monitor
Blynk.begin(auth, ssid, pass);
dht.begin();
// Setup a timer to send data to Blynk every 1000ms (1 second)
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}
is there any specific reason for my project not perform as intended?
x
Presumably you’ve created a template and device in the Blynk web console?
Go to web console > device > device info and you’ll see the three lines of firmware configuration.
Try removing all connections from your device except the USB cable and reboot. If your serial monitor is still blank then your device may be faulty.
Your device is rebooting because of a watchdog timer issue, installing the exception decode in the IDE and pasting the result into it will give more info. Which you should share.
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6z0RDKd8g"
#define BLYNK_TEMPLATE_NAME "fyp"
#define BLYNK_AUTH_TOKEN "MzNiiGHCM9XPH8JSHtx9T0fr00X6Owtq"
char auth[] = "MzNiiGHCM9XPH8JSHtx9T0fr00X6Owtq"; // Replace with your Blynk Auth Token
char ssid[] = "bukan untuk kamu"; // Replace with your Wi-Fi SSID
char pass[] = "brendenpaul"; // Replace with your Wi-Fi password
#define DHTPIN 2 // Signal pin of the DHT22 connected to GPIO 2
#define DHTTYPE DHT22 // DHT 22 (AM2302) sensor
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
const int MQ135Pin = A0; // Analog pin for MQ135 gas sensor
const int WaterLevelPin = D3; // Digital pin for water level sensor
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // in Celsius
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read data from MQ135 gas sensor and send it to Virtual Pin 3
int mqValue = analogRead(MQ135Pin);
Blynk.virtualWrite(V3, mqValue);
// Read data from water level sensor and send it to Virtual Pin 4
int waterLevel = digitalRead(WaterLevelPin);
Blynk.virtualWrite(V4, waterLevel); // Water level data to Virtual Pin 4
// Send temperature data to Virtual Pin 1 and humidity data to Virtual Pin 2
Blynk.virtualWrite(V1, t); // Temperature to Virtual Pin 1
Blynk.virtualWrite(V2, h); // Humidity to Virtual Pin 2
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
dht.begin();
// Setup a timer to send data to Blynk every 1000ms (1 second)
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
}