Thank you everyone helping me. This is a code that can be work successfully by control the pin 8 with a blynk switch. I just hide those #define in hi.h. However, I would like to change a little bit to the code by adding a if statement. “If (temp > 25) then pin 8 on else off” But I not sure where I can add it.
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""
#include "BlynkSimpleShieldEsp8266.h"
#include "ESP8266_Lib.h"
#include "DHT.h"
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3);
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
char ssid[] = "***";
char pass[] = "***";
char auth[] = BLYNK_AUTH_TOKEN;
#define DHTPIN 10
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
BLYNK_WRITE(V0)
{
if (param.asInt() == 1 )
{
digitalWrite(8,1);
}
if (param.asInt() == 0)
{
digitalWrite(8,0);
}
}
BlynkTimer timer;
void sendSensor()
{
Blynk.virtualWrite(V5, dht.readHumidity());
Blynk.virtualWrite(V6, dht.readTemperature());
}
void setup() {
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
pinMode(8,OUTPUT);
timer.setInterval(1000L, sendSensor);
dht.begin();
}
void loop() {
Blynk.run();
timer.run();
}
I have tried to add it in
void setup(){
...
dht.begin();
if(dht.readTemperature()>25)
{
digitalWrite(8,1);
}else{
digitalWrite(8,1);
}
but this seems violate the BLYNK_WRITE(V0) function and it cannot run the switch. then i tried to add a or and and statement in the BLYNK_WRITE(V0)
BLYNK_WRITE(V0)
{
if (param.asInt() == 1|| dht.readTemperature()>25)
{
digitalWrite(8,1);
}
if (param.asInt() == 0||dht.readTemperature()<25)
{
digitalWrite(8,0);
}
}
this seems useless as the temperature is higher and nth change. only the switch can change its on off.
for and statement
BLYNK_WRITE(V0)
{
if (param.asInt() == 1&& dht.readTemperature()>25)
{
digitalWrite(8,1);
}
if (param.asInt() == 0)
{
digitalWrite(8,0);
}
}
this can be run when i switch on it and temperature is higher than 25 but when it is lower, the switch still on unless i switch it off. if I switch it on & temp<25 it wont on. that is not what i want. how can i do it? thank you