Hi guys
I have a relay module problem.
I wanna run a fan through relay module.
It works when I use without the code about PM things. (The part related to 'Timer.run')
but with whole code, it only run just short time at first and stop soon.
Maybe its about Voltage source? I don't know
Plz help me....
I use wemos d1 r1
Here is the whole code.
#define BLYNK_PRINT Serial
#include <pm2008_i2c.h>
#include <DHT.h>
#include "ThingSpeak.h"
PM2008_I2C pm2008_i2c;
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth []="*";
char ssid []="*";
char pass []="*";
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = *;
const char* myWriteAPIKey = "*";
const char* myReadAPIKey = "*";
float t,h;
WidgetLCD lcd(V1);
WidgetLED led1(V2);
BlynkTimer timer;
bool ledStatus = false;
#define BLYNK_GREEN "#23C48E"
#define BLYNK_BLUE "#04C0F8"
#define BLYNK_YELLOW "#ED9D00"
#define BLYNK_RED "#D3435C"
int R=5;
uint8_t pm2p5_grade=0;
uint8_t pm10_grade=0;
uint8_t total_grade=0;
void LCD(){
lcd.clear();
}
void setup() {
Serial.begin (9600);
// Serial.begin (115200);
pinMode(R,OUTPUT);
//digitalWrite(R,LOW);
Blynk.begin(auth,ssid,pass);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
pm2008_i2c.begin();
pm2008_i2c.command();
delay(1000);
led1.on();
dht.begin();
uint8_t ret = pm2008_i2c.read();
timer.setInterval(1000L, myTimerEvent);
timer.setInterval(1000L, blinkLedWidget);
timer.setInterval(16000L, TempHumEvent);
}
void loop() {
Blynk.run();
timer.run();
}
BLYNK_WRITE(V3){
int pinValue=param.asInt();
if(pinValue==1){
digitalWrite(R, HIGH);
} else {
//pinMode(R,OUTPUT);
digitalWrite(R, LOW);
}
}
void myTimerEvent()
{
uint8_t ret = pm2008_i2c.read();
if(ret==0||ret==1){
Serial.print("PM2.5: ");
Serial.println(pm2008_i2c.pm2p5_grimm);
Blynk.virtualWrite(V4, pm2008_i2c.pm2p5_grimm);
Serial.print("PM10: ");
Serial.println(pm2008_i2c.pm10_grimm);
Blynk.virtualWrite(V5, pm2008_i2c.pm10_grimm);
char pm2p5[10];
lcd.print(0, 0, "PM2.5: ");
sprintf(pm2p5, "%3d", pm2008_i2c.pm2p5_grimm);
lcd.print(6, 0, pm2p5);
char pm10[10];
lcd.print(0, 1, "PM10: ");
sprintf(pm10, "%3d", pm2008_i2c.pm10_grimm);
lcd.print(5, 1, pm10);
}
}
void blinkLedWidget()
{
uint8_t ret = pm2008_i2c.read();
if(ret==0||ret==1){
if(pm2008_i2c.pm10_grimm<31){
pm10_grade=1;
} else if(pm2008_i2c.pm10_grimm<81){
pm10_grade=2;
} else if(pm2008_i2c.pm10_grimm<151){
pm10_grade=3;
} else {
pm10_grade=4;
}
if(pm2008_i2c.pm2p5_grimm<16){
pm2p5_grade=1;
} else if(pm2008_i2c.pm2p5_grimm<36){
pm2p5_grade=2;
} else if(pm2008_i2c.pm2p5_grimm<76){
pm2p5_grade=3;
} else {
pm2p5_grade=4;
}
}
total_grade=max(pm2p5_grade,pm10_grade);
if (total_grade==1) {
led1.setColor(BLYNK_BLUE);
Serial.println("GOOD");
ledStatus = true;
} else if(total_grade==2){
led1.setColor(BLYNK_GREEN);
Serial.println("NORMAL");
ledStatus = true;
} else if(total_grade==3){
led1.setColor(BLYNK_YELLOW);
Serial.println("BAD");
ledStatus = true;
} else{
led1.setColor(BLYNK_RED);
Serial.println("WORST");
ledStatus = true;
}
if ( total_grade>=3 ){
Blynk.notify("ventilation start");
digitalWrite(R,HIGH);
}
}
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
}