not the same, 250ms after DHT11 that’s enough to avoid blynk issue
But if I where you I will check DHT11 and Soil sensor only twice per hour
I don’t think your temp change every 10 sec
I’m sorry @PeteKnight because I also got confused with my code Thank you for your suggestion. This is my latest code. Is it like this? All is working now, but the DHT11 and soil moisture sensor change is very late. Is it like that?
#define BLYNK_PRINT Serial
#define ONE_WIRE_BUS 16 //GPIO16 or D0 is for OneWireBus
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
BlynkTimer timer; //Timer object
String apiKey = ""; //API Key from ThingSpeak channel
char auth[] = ""; //Authentication code sent by Blynk
char ssid[] = ""; //WiFi SSID
char pass[] = ""; //WiFi Password
#define DHTPIN 2 //GPIO2 or D4 is for DHT11 sensor
#define pirPin 5 //GPIO5 or D1 is for PIR Motion sensor
#define WATER_PUMP 4 //GPIO4 or D2 is for Water Pump relay
#define sensorPin 12 //GPIO12 or D6 is for Soil moisture sensor
#define rainPin 14 //GPIO14 or D5 is for Rain sensor
#define DHTTYPE DHT11 //DHT11 temperature & Humidity sensor
DHT dht(DHTPIN, DHTTYPE); //DHT11 temperature & Humidity sensor
int pirValue; //For PIR Motion sensor value
int pinValue;
const int buzzerPin = 13; //GPIO13 or D7 is for buzzer connected with PIR Motion sensor
int enable2 = 15; //GPIO15 or D8 is for LED connected with Rain sensor
int sensorState = 0; //For soil sensor value
int rainState = 0; //For rain sensor value
int lastState = 0; //For rain sensor value
int lastRainState = 0; //For rain sensor value
boolean state = false; //For water pump to turn on or off
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode(sensorPin, INPUT); //Soil Moisture sensor
pinMode(rainPin, INPUT); //Rain sensor
pinMode(pirPin, INPUT); //PIR Motion sensor
pinMode(buzzerPin, OUTPUT); //Buzzer
pinMode(WATER_PUMP, OUTPUT); //Water Pump relay
pinMode(enable2, OUTPUT); //LED
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
sensors.begin();
dht.begin();
timer.setInterval(10000L, sendDhtSensor); //main function for DHT11 is called here
timer.setInterval(10250L, soil_temperature); //main function for Soil is called here
timer.setInterval(500L, getPirValue); //main function for PIR is called here
}
void loop()
{
timer.run();
Blynk.run();
}
void sendDhtSensor() //main function for DHT11 sensor
{
float h = dht.readHumidity();
float t = dht.readTemperature();
Blynk.virtualWrite(V6, t); //V6 is for Temperature in Blynk Application
Serial.print("Current temperature = "); //Value is printed on serial monitor
Serial.print(t);
Serial.print(" ");
Blynk.virtualWrite(V5, h); //V5 is for Humidity in Blynk Application
Serial.print("Current humidity = "); //Value is printed on serial monitor
Serial.print(h);
}
int soil_moisture=0; //Variable function for soil moisture
void soil_temperature() //main function for Soil sensor
{
soil_moisture=analogRead(A0); //Get the soil sensor value
sensors.requestTemperatures();
float soil_temperature = sensors.getTempCByIndex(0);
Serial.println(soil_temperature);
Serial.print("Soil Moisture is : ");
Serial.println(soil_moisture);
if (soil_moisture > 900)
{
digitalWrite (WATER_PUMP, HIGH);
}
if (soil_moisture > 900 && soil_moisture < 950)
{
digitalWrite(WATER_PUMP, HIGH);
}
if (soil_moisture < 700)
{
digitalWrite(WATER_PUMP, LOW);
}
Blynk.virtualWrite(V1, soil_temperature); //Value is sent to the Blynk App
Blynk.virtualWrite(V2, soil_moisture); //V2 is for Soil sensor in Blynk App.
}
void getPirValue() //main function for PIR Motion sensor
{
pirValue = digitalRead(pirPin); //Get the PIR motion sensor value
Serial.println(pirValue);
if (pirValue == 0)
{
Serial.print("Motion detected"); //Value is printed on serial monitor
Serial.print(pirValue); //Value is printed on serial monitor
Blynk.notify("Motion detected"); //Send notification to Blynk App
digitalWrite(buzzerPin, HIGH); //Buzzer on when detect motion
Blynk.virtualWrite(V0, pirValue); //V0 is for PIR Motion sensor in Blynk App
}
else
{
digitalWrite(buzzerPin, LOW); //Buzzer off when not detect any motion
}
rainState = digitalRead(rainPin); //Get the Rain sensor value
rainState = !rainState; // Invert the value so 1 = raining
Serial.println(rainState);
if (rainState == 1 && lastRainState == 0) { //detect any rain
Serial.println("Its Raining!"); //Value is printed on serial monitor
Blynk.notify("Its Raining!"); //Send notification to Blynk App
digitalWrite(enable2, HIGH); //LED is on when detect rain
lastRainState = 1;
} else {
Serial.println("No Rains"); //not detect any rain
digitalWrite(enable2, LOW); //LED is off when not detect any rain
lastRainState = 0;
}
}
BLYNK_WRITE(V1) //V1 is for Water Pump button in Blynk App
{
if (state == false) {
state = true;
digitalWrite(WATER_PUMP,HIGH); //Water pump will turn On
Serial.println("You just watered your plant"); //Value is printed on serial monitor
Blynk.notify("You just watered your plant."); //Send notification to Blynk App
}
else {
state = false;
digitalWrite(WATER_PUMP,LOW); //Water pump will turn Off
}
}
The picture below is my current result. Is it like this?
Oh alright, new knowledge. I thought that we must check the the DHT11 every second
Not exactly new knowledge…
This is my latest code. Is it like this?
You’ve ignored most of my advice!
the DHT11 and soil moisture sensor change is very late
I have no idea what this means.
Pete.
Not exactly new knowledge…
It’s new knowledge for me as I have never ever use this arduino or nodemcu things, even once in my life. This is my first time. That is why I said this is new knowledge for me.
I think there’s lots of new information that you’ve ben given in the previous 43 posts, but I have my doubts about how much of that information you’ve absorbed.
Maybe you should go back and re-read from the beginning and update your code accordingly, then if you have further questions try to state them in a clear and understandable way.
Pete.
Hello guys, I already fix my code here. especially for DHT11, soil moisture and pir motion sensor. First question, Is it right like this?
second,where to put the thingspeak code? because from what I know, Void loop must be clean. But I’m not quite sure where to put thingspeak code thus I put it in void loop. Or I have to create another void?
#define BLYNK_PRINT Serial
#define ONE_WIRE_BUS 16
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "ThingSpeak.h"
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define DHTPIN 2
#define DHTTYPE DHT11
#define pirPin 5
char auth[] = "xxxx";
char ssid[] = "xxxx";
char pass[] = "xxxx";
// ThingSpeak credentials.
String apiKey = "xxxxx";
const char* server = "xxxxx";
// Declare variables and constants
int sensorPin=A0;
int t; //temperture
int h; //humidity
int enable2 = 15; //GPIO15 or D8 is for LED connected with Rain sensor
int pirValue; //Motion sensor
int mSensor ; //soil sensor
int soil;
const int RELAY_PIN = 4; //water pump
const int buzzerPin = 13;
boolean state = false;
BlynkTimer timer;
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
dht.begin();
pinMode(sensorPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(enable2, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
digitalWrite(buzzerPin, LOW);
digitalWrite(enable2, LOW);
// Setup a function to be called
timer.setInterval(10000L, TempHum); //10 second
timer.setInterval(1800000L, Moisture); //30 minutes
timer.setInterval(500L, getPirValue); // 500 ms
}
//=============== Humidity and Temperature ===============
void TempHum()
{
// Reads sensor value on Digital Pin 2
// Stores the 2 values in their respective variable. Humidity - hSensor. Temperature - tSensor.
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(h) || isnan(t)) { // In case of abnormal readings from the sensor (not a number)
Serial.println("Failed to read from DHT sensor!"); // Display error message on serial monitor
}
else{
// Display readings on serial monitor
Serial.println (String ("Humidity is : ") + h);
Serial.println (String ("Temperature is : ") + t);
Blynk.virtualWrite(V5, h); /* Sends Humidity reading stored in variable hSensor
to Virtual Pin V4 defined in the Blynk App*/
Blynk.virtualWrite(V6, t); /* Sends Temperature reading stored in variable tSensor
to Virtual Pin V5 defined in the Blynk App*/
}
}
//=============== Soil Moisture ===============
void Moisture(){
mSensor = analogRead(sensorPin); //Read sensor value on analog PIN
mSensor = constrain(mSensor, 400, 900);
soil = map(mSensor, 400, 900, 99, 0);
if (isnan(soil)) { //
Serial.println("Failed to read from Soil Moisture sensor!"); //
}
else{
Serial.println (String ("Moisture is : ") + soil); //
Blynk.virtualWrite(V2, soil); //
}
if(soil <= 38){
Blynk.notify("Plant needs water.. Activating water pump"); //
Serial.println("Plant needs water.. Activating water pump");
digitalWrite(RELAY_PIN, HIGH); // Pump ON
}
else if(soil >= 45){
digitalWrite(RELAY_PIN, LOW); // Pump OFF
}
}
//=============== Motion Detection ===============
void getPirValue() { //main function for IR Motion sensor
pirValue = digitalRead(pirPin);
if (pirValue == 0)
{
Serial.println("Motion detected, buzzer & LED ON");//These value is printed on serial monitor
Blynk.notify("Motion detected, buzzer & LED ON"); //Send notification to Blynk App
digitalWrite(buzzerPin, HIGH); //Buzzer on when detect motion
digitalWrite(enable2, HIGH); //Buzzer on when detect motion
}
else {
digitalWrite(buzzerPin, LOW); //Buzzer off when not detect any motion
digitalWrite(enable2, LOW); //Buzzer on when detect motion
}}
BLYNK_WRITE(V1) //V1 is for Water Pump button in Blynk App
{
if (state == false) {
state = true;
digitalWrite(RELAY_PIN,HIGH); //Water pump will turn On
Serial.println("Manual: You just watered your plant"); //These value is printed on serial monitor
Blynk.notify("Manual : You just watered your plant."); //Send notification to Blynk App
}
else {
state = false;
digitalWrite(RELAY_PIN,LOW); //Water pump will turn Off
}
}
void loop()
{
Blynk.run();
timer.run(); // initiates BlynkTimer
if (client.connect(server,80)) {
String postStr = apiKey;
postStr +="&field1="; //graph 1 in thingspeak
postStr += String(t); //Pull temperature data to graph 1 in thingspeak
postStr +="&field2="; //graph 2 in thingspeak
postStr += String(h); //Pull humidity data to graph 2 in thingspeak
postStr +="&field3="; //graph 1 in thingspeak
postStr += String(soil); //Pull temperature data to graph 1 in thingspeak
postStr +="&field4="; //graph 1 in thingspeak
postStr += String(pirValue); //Pull temperature data to graph 1 in thingspeak
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
}
where to put the thingspeak code? because from what I know, Void loop must be clean. But I’m not quite sure where to put thingspeak code thus I put it in void loop. Or I have to create another void?
It needs to be in its own function, called with another timer at a suitable interval.
Pete.
Is it meaning like that I have to create another void such as ‘void ThingSpeak’? Can DHT be set to 15 minutes reading? or what reading is suitable for dht11 reading?
Is it meaning like that I have to create another void such as ‘void ThingSpeak’?
Yes, what you refer to a “void” is a user defined function.
Can DHT be set to 15 minutes reading? or what reading is suitable for dht11 reading?
Yes, of course. It really depends on your use case. However, remember that if you get an invalid reading from your DHT (the NAN test is true for either the temperature or the humidity) then no values are updated in Blynk, and you have to wait another 15 minutes before the sketch tries again.
Pete.