#define BLYNK_PRINT Serial
#include <LCD.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <SPI.h>
#include <DS3231.h>
#define DHTTYPE DHT11
#define DHT1_PIN 45 // This pin 45 use for sensor sense from DHT11
#define DHT2_PIN 34 // This pin 34 use for sensor sense from DHT11
#define DHT3_PIN 8 // This pin 8 use for sensor sense from DHT11
#define RL 10 //nilai RL =10 pada sensor
#define m -0.417 //gradient calculation from graph
#define b 0.425 //hasil perhitungan perpotongan
#define Ro 30.65 //measurement result of RO,
#define Ro2 32.74 //measurement result of RO,
#define Ro3 164.38 //measurement result of RO,
//#define sensorValue A0 //variable definition
//#define sensorValue2 A15 //variable definition
//#define sensorValue3 A8 //variable definition
DHT dht(DHT1_PIN, DHTTYPE);
DHT dht2(DHT2_PIN, DHTTYPE);
DHT dht3(DHT3_PIN, DHTTYPE);
DS3231 rtc(SDA, SCL); //real time clock
Time t;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address for an unmodified module
LiquidCrystal_I2C lcd2(0x26, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address for an unmodified module
int RELAY = 26; //exhaust fan 1
int RELAY2 = 22; //exhaust fan 2
int RELAY3 = 46; //cooling fan big
int RELAY7 = 53; //cooling fan small
int RELAY4 = 38; //lamp1
int RELAY5 = 30; //lamp2
int RELAY6 = 50; //lamp3
int maxtemp = 27;
int mintemp = 21;
int maxHum = 80;
int minHum = 60;
int maxppm = 20;
int maxtemp2 = 27;
int mintemp2 = 21;
int maxHum2 = 80;
int minHum2 = 60;
int maxppm2 = 20;
int maxtemp3 = 27;
int mintemp3 = 21;
int maxHum3 = 80;
int minHum3 = 60;
int maxppm3 = 20;
const int OnHour = 05; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 43;
const int OffHour = 05; //SET TIME TO OFF RELAY
const int OffMin = 44;
//================================wifi===========================================
// Change the credentials below, so your ESP8266 connects to your router
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
//Android Note3
char auth[] = "K4apzkY-kU_O_Po9NFkVM0rrez5_aCe";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxxx";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1
// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(1000L, sendSensor);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
lcd.begin(16, 2);
lcd.clear();
rtc.begin(); // Initialize the rtc object
lcd2.setBacklightPin(3, POSITIVE);
lcd2.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
lcd2.begin(16, 2);
lcd2.clear();
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); //NOTE: Exhaust Fan is OFF at starting
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2, HIGH); //NOTE: Exhaust Fan is OFF at starting
pinMode(RELAY3, OUTPUT);
digitalWrite(RELAY3, LOW); //NOTE: Cooling Fan is OFF at starting
pinMode(RELAY7, OUTPUT);
digitalWrite(RELAY7, LOW); //NOTE: Cooling Fan is OFF at starting
pinMode(RELAY4, OUTPUT);
digitalWrite(RELAY4, LOW); //NOTE: Lamp 1 is OFF at starting
pinMode(RELAY5, OUTPUT);
digitalWrite(RELAY5, LOW); //NOTE: Lamp 2 is OFF at starting
pinMode(RELAY6, OUTPUT);
digitalWrite(RELAY6, LOW); //NOTE: Lamp 3 is OFF at starting
Serial.begin(9600);
dht.begin();
}
void realtime()
{
lcd2.setCursor(0, 0);
lcd2.print("Time: ");
lcd2.print(rtc.getTimeStr());
lcd2.setCursor(0, 1);
lcd2.print("Date: ");
lcd2.print(rtc.getDateStr());
delay(1000);
}
void sendSensor()
{
//=====================temp humidity ammonia 1=======================================
float temp = dht.readTemperature(); //Temperature reading
float Hum = dht.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue;
float VRL;
float RS;
float ratio;
sensorValue = analogRead(A0);
VRL = sensorValue * (5 / 1023.0); //analog conversion to tegangan
RS = ((5.0 / VRL) - 1) * 10 ; //formula to get RS
ratio = RS / Ro; // find the ratio
float ppm = pow(10, ((log10(ratio) - b) / m)); //formula to get exact ppm
Blynk.virtualWrite(V4, temp); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V6, Hum); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V0, ppm); //Send q value to blynk in V0 virtual pin
// Check if any reads failed and exit early (to try again).
if (isnan(Hum) || isnan(temp) || isnan(sensorValue)) {
lcd.print("Failed");
delay(2000);
Serial.println("Failed to read from 1st sensor!");
return;
}
Serial.print("Temp 1 : ");
Serial.print(temp);
Serial.print("C, Humidity 1: ");
Serial.print(Hum);
Serial.println("%");
Serial.print("Air Quality 1 = ");
Serial.print(ppm);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm, DEC); // Print the value read
Serial.println(sensorValue); // Print the value read
Serial.println();
lcd.setCursor(0, 0);
lcd.print(" Welcome to ");
lcd.setCursor(0, 1);
lcd.print(" PUYUH FARMBEST ");
delay(1000);
lcd.setCursor(0, 0);
lcd.print("Temp1 C ");
lcd.setCursor(6, 0);
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.println("Humi1 % ");
lcd.setCursor(6, 1);
lcd.print(Hum);
delay(1000);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 1 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
delay(1000);
lcd.clear();
//=====================temp humidity ammonia 2=======================================
float temp2 = dht.readTemperature(); //Temperature reading
float Hum2 = dht.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue2;
float VRL2;
float RS2;
float ratio2;
sensorValue2 = analogRead(A15);
VRL2 = sensorValue2 * (5 / 1023.0); //analog conversion to tegangan
RS2 = ((5.0 / VRL2) - 1) * 10 ; //formula to get RS
ratio2 = RS2 / Ro2; // find the ratio
float ppm2 = pow(10, ((log10(ratio2) - b) / m)); //formula to get exact ppm
Blynk.virtualWrite(V7, temp2); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V8, Hum2); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V9, ppm2); //Send q value to blynk in V0 virtual pin
// Check if any reads failed and exit early (to try again).
if (isnan(Hum2) || isnan(temp2) || isnan(sensorValue2)) {
lcd.print("Failed");
delay(1000);
Serial.println("Failed to read from 1st sensor!");
return;
}
Serial.print("Temp 2 : ");
Serial.print(temp2);
Serial.print("C, Humidity 2: ");
Serial.print(Hum2);
Serial.println("%");
Serial.print("Air Quality 2 = ");
Serial.print(ppm2);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm2, DEC); // Print the value read
Serial.println(sensorValue2); // Print the value read
Serial.println();
lcd.setCursor(0, 0);
lcd.print("Temp2 C ");
lcd.setCursor(6, 0);
lcd.print(temp2);
lcd.setCursor(0, 1);
lcd.println("Humi2 % ");
lcd.setCursor(6, 1);
lcd.print(Hum2);
delay(1000);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 2 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm2);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
delay(1000);
lcd.clear();
//=====================temp humidity ammonia 3=======================================
float temp3 = dht.readTemperature(); //Temperature reading
float Hum3 = dht.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue3;
float VRL3;
float RS3;
float ratio3;
sensorValue3 = analogRead(A8);
VRL3 = sensorValue3 * (5 / 1023.0); //analog conversion to tegangan
RS3 = (5.0 / VRL3 - 1) * 10 ; //formula to get RS
ratio3 = RS3 / Ro3; // find the ratio
float ppm3 = pow(10, ((log10(ratio3) - b) / m)); //formula to get exact ppm
Blynk.virtualWrite(V1, temp3); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V2, Hum3); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V3, ppm3); //Send q value to blynk in V0 virtual pin
// Check if any reads failed and exit early (to try again).
if (isnan(Hum3) || isnan(temp3) || isnan(sensorValue3)) {
lcd.print("Failed");
delay(2000);
Serial.println("Failed to read from 1st sensor!");
return;
}
Serial.print("Temp 3 : ");
Serial.print(temp3);
Serial.print("C, Humidity 3: ");
Serial.print(Hum3);
Serial.println("%");
Serial.print("Air Quality 3 = ");
Serial.print(ppm3);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm3, DEC); // Print the value read
Serial.println(sensorValue3); // Print the value read
Serial.println();
lcd.setCursor(0, 0);
lcd.print("Temp3 C ");
lcd.setCursor(6, 0);
lcd.print(temp3);
lcd.setCursor(0, 1);
lcd.println("Humi3 % ");
lcd.setCursor(6, 1);
lcd.print(Hum3);
delay(1000);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 3 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm3);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
delay(1000);
lcd.clear();
//=====================cooling fan & exhaust fan & lamp=======================================
lcd.setCursor(0, 0);
if (Hum > maxHum || temp > maxtemp || Hum2 > maxHum2 || temp2 > maxtemp2 || Hum3 > maxHum3 || temp3 > maxtemp3) {
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY7, HIGH);
lcd.write(" CoolFan is ON ");
lcd.setCursor(0, 1);
lcd.write("Temp Humi ALERT!");
Serial.print("Cooling Fan is ON");
Serial.print("Temp Humidity ALERT!");
Serial.println();
delay(1000);
} else {
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY7, LOW);
lcd.write("Temp Humi are OK");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
Serial.println("Temp & Humidity are OK");
Serial.println("NO WORRIES");
Serial.println();
delay(1000);
}
lcd.setCursor(0, 0);
if (ppm > maxppm || ppm2 > maxppm2 || ppm3 > maxppm3) {
digitalWrite(RELAY, HIGH);
digitalWrite(RELAY2, HIGH);
lcd.write(" ExhaustFan ON ");
lcd.setCursor(0, 1);
lcd.write(" Ammonia ALERT! ");
Serial.println("Exhaust Fan is ON");
Serial.println("Ammonia ALERT!");
Serial.println();
delay(1000);
} else {
digitalWrite(RELAY, LOW);
digitalWrite(RELAY2, LOW);
lcd.write("Ammonia Lvl OKAY");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
Serial.println("Ammonia Level OKAY");
Serial.println("NO WORRIES");
Serial.println();
delay(1000);
}
lcd.setCursor(0, 0);
if (Hum < minHum || temp < mintemp || Hum2 < minHum2 || temp2 < mintemp2 || Hum3 < minHum3 || temp3 < mintemp3) {
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
lcd.write(" Light is ON ");
lcd.setCursor(0, 1);
lcd.write("Temp Humi ALERT!");
Serial.println("Light is ON");
Serial.println("Temp Humidity ALERT!");
Serial.println();
delay(1000);
} else {
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY6, LOW);
lcd.write("Condition = OKAY");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
Serial.println("Condition = OKAY");
Serial.println("NO WORRIES");
Serial.println();
delay(1000);
}
}
void timerSensor()
{
//=====================timer for lamp to ON and OFF==============================
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.println(" ");
delay (1000);
lcd.setCursor(0, 0);
if (t.hour == OnHour && t.min == OnMin) {
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
Serial.println("LIGHT ON");
lcd.print(" LIGHT ON ");
delay(1000);
}
else if (t.hour == OffHour && t.min == OffMin) {
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY6, LOW);
Serial.println("LIGHT OFF");
lcd.print(" LIGHT OFF ");
delay(1000);
}
}
void loop()
{
realtime();
Blynk.run();
timer.run();
}
is there anything wrong on code. or i missed something. the serial monitor shows all the result. but the blynk on app always offline
Yes, there are very many things wrong with your code.
Your void sendSensor()
function contains delays that add-up to 12 seconds at least, assuming that you don’t encounter any NAN failures reading your multiple DHT11 sensors (although actually you appear to be reading the same sensor (dht
) three times, rather than reading dht
, dht2
and dht3
).
This sendSensor()
function, which takes a minimum of 12 seconds to execute, is being called once every second.
The whole idea of using timers is to eliminate the need for delays in your code, so you need to remove all of the delays and re-structure your code - using multiple timers if necessary - to function correctly.
Blynk expects to have the void loop()
executed multiple times per second, so that the Blynk library can communicate with the server on a real-time basis. This is done via the Blynk.run()
command i your void loop()
. If the server hasn’t heard from the device for 10 seconds it will assume that it is offline and update the app to show this.
Pete.
what the next step for me to correct the code? should i decrease the delays for sendsensor…so the main correction is in the void sensor, right?
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(6000L, sendSensor);
it means that i should to reduce into 1 sec?
No, you should remove all delays from your code.
Pete.
alright, i will remove the delay on void sensor
void sendSensor()
{
//=====================timer for lamp to ON and OFF==============================
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.println(" ");
lcd.setCursor(0, 0);
if (t.hour == OnHour && t.min == OnMin) {
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
Serial.println("LIGHT ON");
lcd.print(" LIGHT ON ");
}
else if (t.hour == OffHour && t.min == OffMin) {
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY6, LOW);
Serial.println("LIGHT OFF");
lcd.print(" LIGHT OFF ");
}
//=====================temp humidity ammonia 1=======================================
float temp = dht.readTemperature(); //Temperature reading
float Hum = dht.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue;
float VRL;
float RS;
float ratio;
sensorValue = analogRead(A0);
VRL = sensorValue * (5 / 1023.0); //analog conversion to tegangan
RS = ((5.0 / VRL) - 1) * 10 ; //formula to get RS
ratio = RS / Ro; // find the ratio
float ppm = pow(10, ((log10(ratio) - b) / m)); //formula to get exact ppm
// Temperature and humidity display on LCD
// Check if any reads failed and exit early (to try again).
if (isnan(Hum) || isnan(temp) || isnan(sensorValue)) {
lcd.print("Failed");
Serial.println("Failed to read from 1st sensor!");
return;
}
Blynk.virtualWrite(V4, temp); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V6, Hum); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V0, ppm); //Send q value to blynk in V0 virtual pin
Serial.print("Temp 1 : ");
Serial.print(temp);
Serial.print("C, Humidity 1: ");
Serial.print(Hum);
Serial.println("%");
Serial.print("Air Quality 1 = ");
Serial.print(ppm);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm, DEC); // Print the value read
Serial.println(sensorValue); // Print the value read
lcd.setCursor(0, 0);
lcd.print("Temp1 C ");
lcd.setCursor(6, 0);
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.println("Humi1 % ");
lcd.setCursor(6, 1);
lcd.print(Hum);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 1 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
lcd.clear();
//=====================temp humidity ammonia 2=======================================
float temp2 = dht.readTemperature(); //Temperature reading
float Hum2 = dht.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue2;
float VRL2;
float RS2;
float ratio2;
sensorValue2 = analogRead(A15);
VRL2 = sensorValue2 * (5 / 1023.0); //analog conversion to tegangan
RS2 = ((5.0 / VRL2) - 1) * 10 ; //formula to get RS
ratio2 = RS2 / Ro2; // find the ratio
float ppm2 = pow(10, ((log10(ratio2) - b) / m)); //formula to get exact ppm
// Temperature and humidity display on LCD
// Check if any reads failed and exit early (to try again).
if (isnan(Hum2) || isnan(temp2) || isnan(sensorValue2)) {
lcd.print("Failed");
Serial.println("Failed to read from 1st sensor!");
return;
}
Blynk.virtualWrite(V7, temp2); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V8, Hum2); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V9, ppm2); //Send q value to blynk in V0 virtual pin
Serial.print("Temp 2 : ");
Serial.print(temp2);
Serial.print("C, Humidity 2: ");
Serial.print(Hum2);
Serial.println("%");
Serial.print("Air Quality 2 = ");
Serial.print(ppm2);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm2, DEC); // Print the value read
Serial.println(sensorValue2); // Print the value read
lcd.setCursor(0, 0);
lcd.print("Temp2 C ");
lcd.setCursor(6, 0);
lcd.print(temp2);
lcd.setCursor(0, 1);
lcd.println("Humi2 % ");
lcd.setCursor(6, 1);
lcd.print(Hum2);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 2 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm2);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
lcd.clear();
//=====================temp humidity ammonia 3=======================================
float temp3 = dht.readTemperature(); //Temperature reading
float Hum3 = dht.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue3;
float VRL3;
float RS3;
float ratio3;
sensorValue3 = analogRead(A8);
VRL3 = sensorValue3 * (5 / 1023.0); //analog conversion to tegangan
RS3 = (5.0 / VRL3 - 1) * 10 ; //formula to get RS
ratio3 = RS3 / Ro3; // find the ratio
float ppm3 = pow(10, ((log10(ratio3) - b) / m)); //formula to get exact ppm
// Temperature and humidity display on LCD
// Check if any reads failed and exit early (to try again).
if (isnan(Hum3) || isnan(temp3) || isnan(sensorValue3)) {
lcd.print("Failed");
Serial.println("Failed to read from 1st sensor!");
return;
}
Blynk.virtualWrite(V1, temp3); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V2, Hum3); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V3, ppm3); //Send q value to blynk in V0 virtual pin
Serial.print("Temp 3 : ");
Serial.print(temp3);
Serial.print("C, Humidity 3: ");
Serial.print(Hum3);
Serial.println("%");
Serial.print("Air Quality 3 = ");
Serial.print(ppm3);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm3, DEC); // Print the value read
Serial.println(sensorValue3); // Print the value read
lcd.setCursor(0, 0);
lcd.print("Temp3 C ");
lcd.setCursor(6, 0);
lcd.print(temp3);
lcd.setCursor(0, 1);
lcd.println("Humi3 % ");
lcd.setCursor(6, 1);
lcd.print(Hum3);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 3 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm3);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
lcd.clear();
//=====================cooling fan & exhaust fan & lamp=======================================
lcd.setCursor(0, 0);
if (Hum > maxHum || temp > maxtemp || Hum2 > maxHum2 || temp2 > maxtemp2 || Hum3 > maxHum3 || temp3 > maxtemp3) {
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY7, HIGH);
lcd.write(" CoolFan is ON ");
lcd.setCursor(0, 1);
lcd.write(" ALERT!!! ");
} else {
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY7, LOW);
lcd.write("Condition = OKAY");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
}
lcd.setCursor(0, 0);
if (ppm > maxppm || ppm2 > maxppm2 || ppm3 > maxppm3) {
digitalWrite(RELAY, HIGH);
digitalWrite(RELAY2, HIGH);
lcd.write(" ExhaustFan ON ");
lcd.setCursor(0, 1);
lcd.write(" ALERT!!! ");
} else {
digitalWrite(RELAY, LOW);
digitalWrite(RELAY2, LOW);
lcd.write("Condition = OKAY");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
}
lcd.setCursor(0, 0);
if (Hum < minHum || temp < mintemp || Hum2 < minHum2 || temp2 < mintemp2 || Hum3 < minHum3 || temp3 < mintemp3) {
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
lcd.write(" Light is ON ");
lcd.setCursor(0, 1);
lcd.write(" ALERT!!! ");
} else {
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY6, LOW);
lcd.write("Condition = OKAY");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
}
I have removed all delays in void sensor…
then, what the next step should I do? is it with the timer?
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(12000L, sendSensor);
should i use this set interval? 12 seconds?
It depends what you’re trying to achieve.
You still seem to be reading the same DHT sensor multiple times, rather than reading all three in turn.
Pete.
how do i need to do read all three dht sensors in turn, pete?
cause, i dont understand how to read all three in turn
You’ve defined three DHT objects, dht
, dht2
and dht3
…
In ‘void setup()` you’ve initialised one of them…
And then keep reading this one sensor object…
I’ll let you work-out what code you need to add to initialise your other two sensors and what changes you need to make to read their values at the appropriate places in your code.
Pete.
//=====================temperatures and humidity =======================================
float temp = dht.readTemperature(); //Temperature reading
float Hum = dht.readHumidity(); //Humidity reading
float temp2 = dht.readTemperature(); //Temperature reading
float Hum2 = dht.readHumidity(); //Humidity reading
float temp3 = dht.readTemperature(); //Temperature reading
float Hum3 = dht.readHumidity(); //Humidity reading
if (isnan(Hum) || isnan(temp) || isnan(sensorValue || isnan(Hum2) || isnan(temp2) || isnan(sensorValue2 || isnan(Hum3) || isnan(temp3) || isnan(sensorValue3)) {
lcd.print("Failed");
Serial.println("Failed to read from 1st sensor!");
return;
}
Blynk.virtualWrite(V4, temp); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V6, Hum); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V7, temp2); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V8, Hum2); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V1, temp3); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V2, Hum3); //Send h value to blynk in V6 virtual pin
Serial.print("Temp 1 : ");
Serial.print(temp);
Serial.print("C, Humidity 1: ");
Serial.print(Hum);
Serial.println("%");
lcd.setCursor(0, 0);
lcd.print("Temp1 C ");
lcd.setCursor(6, 0);
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.println("Humi1 % ");
lcd.setCursor(6, 1);
lcd.print(Hum);
Serial.print("Temp 2 : ");
Serial.print(temp2);
Serial.print("C, Humidity 2: ");
Serial.print(Hum2);
Serial.println("%");
lcd.setCursor(0, 0);
lcd.print("Temp2 C ");
lcd.setCursor(6, 0);
lcd.print(temp2);
lcd.setCursor(0, 1);
lcd.println("Humi2 % ");
lcd.setCursor(6, 1);
lcd.print(Hum2);
Serial.print("Temp 3 : ");
Serial.print(temp3);
Serial.print("C, Humidity 3: ");
Serial.print(Hum3);
Serial.println("%");
lcd.setCursor(0, 0);
lcd.print("Temp3 C ");
lcd.setCursor(6, 0);
lcd.print(temp3);
lcd.setCursor(0, 1);
lcd.println("Humi3 % ");
lcd.setCursor(6, 1);
lcd.print(Hum3);
Do you mean like this code?
No.
Pete.
check this
[Unformatted code removed by moderator]*
check this
[Unformatted code removed by moderator]*
@blink please edit your post, 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:
```
Pete.
dht.begin();
dht2.begin();
dht3.begin);
float temp = dht.readTemperature(); //Temperature reading
float Hum = dht.readHumidity(); //Humidity reading
float temp2 = dht2.readTemperature(); //Temperature reading
float Hum2 = dht2.readHumidity(); //Humidity reading
float temp3 = dht3.readTemperature(); //Temperature reading
float Hum3 = dht3.readHumidity(); //Humidity reading
is it right?
#define BLYNK_PRINT Serial
#include <LCD.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include <SPI.h>
#include <DS3231.h>
#define DHTTYPE DHT11
#define DHT1_PIN 45 // This pin 45 use for sensor sense from DHT11
#define DHT2_PIN 34 // This pin 34 use for sensor sense from DHT11
#define DHT3_PIN 8 // This pin 8 use for sensor sense from DHT11
#define RL 10 //nilai RL =10 pada sensor
#define m -0.417 //gradient calculation from graph
#define b 0.425 //hasil perhitungan perpotongan
#define Ro 30.65 //measurement result of RO, refer kalibrasi_mq135 coding
#define Ro2 32.74 //measurement result of RO, refer kalibrasi_mq135 coding
#define Ro3 164.38 //measurement result of RO, refer kalibrasi_mq135 coding
//#define sensorValue A0 //variable definition
//#define sensorValue2 A15 //variable definition
//#define sensorValue3 A8 //variable definition
DHT dht(DHT1_PIN, DHTTYPE);
DHT dht2(DHT2_PIN, DHTTYPE);
DHT dht3(DHT3_PIN, DHTTYPE);
DS3231 rtc(SDA, SCL); //real time clock
Time t;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address for an unmodified module
LiquidCrystal_I2C lcd2(0x26, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the I2C bus address for an unmodified module
int buzzer = 14;
int redLed = 15;
int yelLed = 16;
int greLed = 17;
int RELAY = 26; //exhaust fan 1
int RELAY2 = 22; //exhaust fan 2
int RELAY3 = 46; //cooling fan big
int RELAY7 = 53; //cooling fan small
int RELAY4 = 38; //lamp1
int RELAY5 = 30; //lamp2
int RELAY6 = 50; //lamp3
int maxtemp = 27;
int mintemp = 21;
int maxHum = 80;
int minHum = 60;
int maxppm = 20;
int maxtemp2 = 27;
int mintemp2 = 21;
int maxHum2 = 80;
int minHum2 = 60;
int maxppm2 = 20;
int maxtemp3 = 27;
int mintemp3 = 21;
int maxHum3 = 80;
int minHum3 = 60;
int maxppm3 = 20;
const int OnHour = 23; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 53;
const int OffHour = 23; //SET TIME TO OFF RELAY
const int OffMin = 54;
//================================wifi===========================================
// Change the credentials below, so your ESP8266 connects to your router
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
//Iphone12
//char auth[] = "bhB9KA8itdsMjU57DpJA8HIX174w-M9D";
//Android Note3
char auth[] = "K4apzkY-kU_O_Po9NFkVM0rKz5_ae";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "mfffffffffi";
char pass[] = "3fffffffffff48";
// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1
// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(5000L, sendSensor);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
lcd.begin(16, 2);
lcd.clear();
rtc.begin(); // Initialize the rtc object
lcd2.setBacklightPin(3, POSITIVE);
lcd2.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
lcd2.begin(16, 2);
lcd2.clear();
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
pinMode(redLed, OUTPUT);
digitalWrite(redLed, LOW);
pinMode(yelLed, OUTPUT);
digitalWrite(yelLed, LOW);
pinMode(greLed, OUTPUT);
digitalWrite(greLed, LOW);
pinMode(RELAY, OUTPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, HIGH); //NOTE: Exhaust Fan is OFF at starting
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY2, HIGH); //NOTE: Exhaust Fan is OFF at starting
pinMode(RELAY3, OUTPUT);
digitalWrite(RELAY3, LOW); //NOTE: Cooling Fan is OFF at starting
pinMode(RELAY7, OUTPUT);
digitalWrite(RELAY7, LOW); //NOTE: Cooling Fan is OFF at starting
pinMode(RELAY4, OUTPUT);
digitalWrite(RELAY4, LOW); //NOTE: Lamp 1 is OFF at starting
pinMode(RELAY5, OUTPUT);
digitalWrite(RELAY5, LOW); //NOTE: Lamp 2 is OFF at starting
pinMode(RELAY6, OUTPUT);
digitalWrite(RELAY6, LOW); //NOTE: Lamp 3 is OFF at starting
Serial.begin(9600);
dht.begin();
dht2.begin();
dht3.begin();
}
void realtime()
{
lcd2.setCursor(0, 0);
lcd2.print("Time: ");
lcd2.print(rtc.getTimeStr());
lcd2.setCursor(0, 1);
lcd2.print("Date: ");
lcd2.print(rtc.getDateStr());
delay(1000);
}
void sendSensor()
{
//=====================timer for lamp to ON and OFF==============================
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.println(" ");
Serial.println();
lcd.setCursor(0, 0);
if (t.hour == OnHour && t.min == OnMin) {
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
Serial.println("LIGHT ON");
lcd.print(" LIGHT ON ");
}
else if (t.hour == OffHour && t.min == OffMin) {
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY6, LOW);
Serial.println("LIGHT OFF");
lcd.print(" LIGHT OFF ");
}
//=====================temp humidity ammonia 1=======================================
float temp = dht.readTemperature(); //Temperature reading
float Hum = dht.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue;
float VRL;
float RS;
float ratio;
sensorValue = analogRead(A0);
VRL = sensorValue * (5 / 1023.0); //analog conversion to tegangan
RS = ((5.0 / VRL) - 1) * 10 ; //formula to get RS
ratio = RS / Ro; // find the ratio
float ppm = pow(10, ((log10(ratio) - b) / m)); //formula to get exact ppm
// Temperature and humidity display on LCD
// Check if any reads failed and exit early (to try again).
if (isnan(Hum) || isnan(temp) || isnan(sensorValue)) {
lcd.print("Failed");
Serial.println("Failed to read from 1st sensor!");
return;
}
Blynk.virtualWrite(V4, temp); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V6, Hum); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V0, ppm); //Send q value to blynk in V0 virtual pin
Serial.print("Temp 1 : ");
Serial.print(temp);
Serial.print("C, Humidity 1: ");
Serial.print(Hum);
Serial.println("%");
Serial.print("Air Quality 1 = ");
Serial.print(ppm);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm, DEC); // Print the value read
Serial.println(sensorValue); // Print the value read
Serial.println();
lcd.setCursor(0, 0);
lcd.print(" Welcome to ");
lcd.setCursor(0, 1);
lcd.print(" PUYUH FARMBEST ");
lcd.setCursor(0, 0);
lcd.print("Temp1 C ");
lcd.setCursor(6, 0);
lcd.print(temp);
lcd.setCursor(0, 1);
lcd.println("Humi1 % ");
lcd.setCursor(6, 1);
lcd.print(Hum);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 1 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
lcd.clear();
//=====================temp humidity ammonia 2=======================================
float temp2 = dht2.readTemperature(); //Temperature reading
float Hum2 = dht2.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue2;
float VRL2;
float RS2;
float ratio2;
sensorValue2 = analogRead(A15);
VRL2 = sensorValue2 * (5 / 1023.0); //analog conversion to tegangan
RS2 = ((5.0 / VRL2) - 1) * 10 ; //formula to get RS
ratio2 = RS2 / Ro2; // find the ratio
float ppm2 = pow(10, ((log10(ratio2) - b) / m)); //formula to get exact ppm
// Temperature and humidity display on LCD
// Check if any reads failed and exit early (to try again).
if (isnan(Hum2) || isnan(temp2) || isnan(sensorValue2)) {
lcd.print("Failed");
Serial.println("Failed to read from 1st sensor!");
return;
}
Blynk.virtualWrite(V7, temp2); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V8, Hum2); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V9, ppm2); //Send q value to blynk in V0 virtual pin
Serial.print("Temp 2 : ");
Serial.print(temp2);
Serial.print("C, Humidity 2: ");
Serial.print(Hum2);
Serial.println("%");
Serial.print("Air Quality 2 = ");
Serial.print(ppm2);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm2, DEC); // Print the value read
Serial.println(sensorValue2); // Print the value read
Serial.println();
lcd.setCursor(0, 0);
lcd.print("Temp2 C ");
lcd.setCursor(6, 0);
lcd.print(temp2);
lcd.setCursor(0, 1);
lcd.println("Humi2 % ");
lcd.setCursor(6, 1);
lcd.print(Hum2);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 2 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm2);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
lcd.clear();
//=====================temp humidity ammonia 3=======================================
float temp3 = dht3.readTemperature(); //Temperature reading
float Hum3 = dht3.readHumidity(); //Humidity reading
// get exact value of ammonia NH3
float sensorValue3;
float VRL3;
float RS3;
float ratio3;
sensorValue3 = analogRead(A8);
VRL3 = sensorValue3 * (5 / 1023.0); //analog conversion to tegangan
RS3 = (5.0 / VRL3 - 1) * 10 ; //formula to get RS
ratio3 = RS3 / Ro3; // find the ratio
float ppm3 = pow(10, ((log10(ratio3) - b) / m)); //formula to get exact ppm
// Temperature and humidity display on LCD
// Check if any reads failed and exit early (to try again).
if (isnan(Hum3) || isnan(temp3) || isnan(sensorValue3)) {
lcd.print("Failed");
Serial.println("Failed to read from 1st sensor!");
return;
}
Blynk.virtualWrite(V1, temp3); //Send t value to blynk in V5 virtual pin
Blynk.virtualWrite(V2, Hum3); //Send h value to blynk in V6 virtual pin
Blynk.virtualWrite(V3, ppm3); //Send q value to blynk in V0 virtual pin
Serial.print("Temp 3 : ");
Serial.print(temp3);
Serial.print("C, Humidity 3: ");
Serial.print(Hum3);
Serial.println("%");
Serial.print("Air Quality 3 = ");
Serial.print(ppm3);
Serial.print(" PPM");
Serial.println();
Serial.println(ppm3, DEC); // Print the value read
Serial.println(sensorValue3); // Print the value read
Serial.println();
lcd.setCursor(0, 0);
lcd.print("Temp3 C ");
lcd.setCursor(6, 0);
lcd.print(temp3);
lcd.setCursor(0, 1);
lcd.println("Humi3 % ");
lcd.setCursor(6, 1);
lcd.print(Hum3);
lcd.setCursor(0, 0);
lcd.print(" Air Quality 3 ");
lcd.println(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.print(ppm3);
lcd.print(" PPM");
lcd.println(" ");
lcd.print(" ");
lcd.clear();
//=====================cooling fan & exhaust fan & lamp=======================================
lcd.setCursor(0, 0);
if (Hum > maxHum || temp > maxtemp || Hum2 > maxHum2 || temp2 > maxtemp2 || Hum3 > maxHum3 || temp3 > maxtemp3) {
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY7, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(yelLed, HIGH);
digitalWrite(greLed, HIGH);
//digitalWrite(buzzer, HIGH);
tone(buzzer, 1000, 200);
lcd.write(" CoolFan is ON ");
lcd.setCursor(0, 1);
lcd.write("Temp Humi ALERT!");
Serial.println("Cooling Fan is ON");
Serial.println("Temp Humidity ALERT!");
Serial.println();
} else {
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY7, LOW);
noTone(buzzer);
lcd.write("Temp Humi are OK");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
Serial.println("Temp & Humidity are OK");
Serial.println("NO WORRIES");
Serial.println();
}
lcd.setCursor(0, 0);
if (ppm > maxppm || ppm2 > maxppm2 || ppm3 > maxppm3) {
digitalWrite(RELAY, HIGH);
digitalWrite(RELAY2, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(yelLed, HIGH);
digitalWrite(greLed, HIGH);
//digitalWrite(buzzer, HIGH);
tone(buzzer, 1000, 200);
lcd.write(" ExhaustFan ON ");
lcd.setCursor(0, 1);
lcd.write(" Ammonia ALERT! ");
Serial.println("Exhaust Fan is ON");
Serial.println("Ammonia ALERT!");
Serial.println();
} else {
digitalWrite(RELAY, LOW);
digitalWrite(RELAY2, LOW);
noTone(buzzer);
lcd.write("Ammonia Lvl OKAY");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
Serial.println("Ammonia Level OKAY");
Serial.println("NO WORRIES");
Serial.println();
}
lcd.setCursor(0, 0);
if (Hum < minHum || temp < mintemp || Hum2 < minHum2 || temp2 < mintemp2 || Hum3 < minHum3 || temp3 < mintemp3) {
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
digitalWrite(redLed, HIGH);
digitalWrite(yelLed, HIGH);
digitalWrite(greLed, HIGH);
//digitalWrite(buzzer, HIGH);
tone(buzzer, 1000, 200);
lcd.write(" Light is ON ");
lcd.setCursor(0, 1);
lcd.write("Temp Humi ALERT!");
Serial.println("Light is ON");
Serial.println("Temp Humidity ALERT!");
Serial.println();
} else {
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY6, LOW);
noTone(buzzer);
lcd.write("Condition = OKAY");
lcd.setCursor(0, 1);
lcd.write(" NO WORRIES ");
Serial.println("Condition = OKAY");
Serial.println("NO WORRIES");
Serial.println();
}
}
void loop()
{
realtime();
Blynk.run();
timer.run();
}
...
I have updated my code. is there anything wrong in my coding? and why i still get offline?