Hey guys! I have a problem with my project. I have an indoor control system. Lights turn on and off depending on the time set.
The program works almost perfectly except for one thing:
When i power up the device the only way the light turn on as it should is if I add a button with for that pin in the app and then erase it. After that the program works ok, until it reboots for whatever reason.
I don’t know what it could be happening.
Im using NodeMCU ESP8266 (via wifi) with blynk library v 0.6.1.
Blynk server, and app on MotoG5 with android 10.
Here’s my code:
#include <Wire.h>
#include "BlueDot_BME280.h"
#define builtLed0 2
#define builtLed1 16
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
char auth[] = "HozCiU_f-EmZ92-Esnux3U-GoAwDlZ_b";
String currentTime;
char ssid[] = "Nahuel's Fiber";
char pass[] = "fernandez1";
float lastTemp;
float currentTemp;
float lasthum;
float currenthum;
long lightoffT;
int fotoperiodo;
long secs;
long minutooff;
long horaoff;
long horaon;
long minutoon;
String fp;
BlynkTimer timer;
WidgetRTC rtc;
WidgetTerminal term(V6);
BlueDot_BME280 bme280 = BlueDot_BME280();
void get_temphum(){
lastTemp = currentTemp;
lasthum = currenthum;
currentTime = String(hour()) + ":" + minute();
Blynk.virtualWrite(V1, currentTime); //updates current ESP time in the app.
float temp = bme280.readTempC();
float hum = bme280.readHumidity();
float presion = bme280.readPressure();
currentTemp = temp;
currenthum = hum;
Serial.println(temp);
Serial.println(bme280.readHumidity());
Serial.println(presion);
Blynk.virtualWrite(V2, temp);
Blynk.virtualWrite(V3, hum);
Blynk.virtualWrite(V4, presion);
if (currentTemp > 27 and lastTemp<=27) {
Blynk.notify("Floración: Temperatura alta. >27°C");
term.println("HIGH TEMP!");
term.flush();
}
else if (currentTemp < 16 and lastTemp>=16) {
Blynk.notify("Floración: Temperatura baja. <16°C");
term.println("LOW TEMP!");
term.flush();
}
if (currenthum > 60 and lasthum<=60){
Blynk.notify("Floración: Humedad alta. >60%");
term.println("HIGH HUM!");
term.flush();
}
else if (currenthum < 40 and lasthum>=40){
Blynk.notify("Floración: Humedad baja. <40%");
term.println("LOW HUM!");
term.flush();
}
}
void light_stat(){ //Status indicator
if (digitalRead(5)== HIGH){
Blynk.virtualWrite(V0, 0);
}
else if(digitalRead(5)== LOW ){
Blynk.virtualWrite(V0, 255);
}
}
void blk_sync(){
Blynk.syncAll();
currentTime = String(hour()) + ":" + String(minute());
}
//Enciende o apaga las luces segun fotoperiodo.
void light_time(){
//Fotoperiodo 20/4.
if (fotoperiodo == 1){
fp = "Fotoperiodo: 20/4";
secs = lightoffT;
minutooff = ( secs %3600) /60;
horaoff = secs / 3600;
horaon = horaoff + 4;
minutoon= minutooff;
if (horaon >= 24){
horaon= horaon - 24;
}
if (horaon == hour() and minutoon <= minute()){
digitalWrite(5, LOW);
term.println(F("Luces encendidas. FP: 20/4"));
term.flush();
}
if (horaoff == hour() and minutooff<= minute()){
digitalWrite(5, HIGH);
term.println(F("Luces apagadas. FP: 20/4"));
term.flush();
}
}
if (fotoperiodo == 2){
fp = "Fotoperiodo: 18/6";
secs = lightoffT;
minutooff = ( secs %3600) /60;
horaoff = secs / 3600;
horaon = horaoff + 6;
minutoon= minutooff;
if (horaon >= 24){
horaon= horaon - 24;
}
if (horaon == hour() and minutoon <= minute()){
digitalWrite(5, LOW);
term.println(F("Luces encendidas. FP: 18/6"));
term.flush();
}
if (horaoff == hour() and minutooff <= minute()){
digitalWrite(5, HIGH);
term.println(F("Luces apagadas. FP: 18/6"));
term.flush();
}
}
if (fotoperiodo == 3){
fp = "Fotoperiodo: 12/12";
secs = lightoffT;
minutooff = ( secs %3600) /60;
horaoff = secs / 3600;
horaon = horaoff + 12;
minutoon= minutooff;
if (horaon >= 24){
horaon = horaon - 24;
}
if (horaon == hour() and minutoon <= minute()){
digitalWrite(5, LOW);
term.println(F("Luces encendidas. FP: 12/12"));
term.flush();
}
if (horaoff == hour() and minutooff <= minute()){
digitalWrite(5, HIGH);
term.println(F("Luces apagadas. FP: 12/12"));
term.flush();
}
}
}
BLYNK_CONNECTED(){
rtc.begin();
Blynk.syncAll();
}
BLYNK_APP_CONNECTED(){
Blynk.syncAll();
}
BLYNK_WRITE(V6){ //terminal widget
String command = (param.asString());
if (command == "getTime(on)"){
term.println("Hora encendido: " + String(horaon) + ":" + String(minutoon));
term.flush();
}
else if (command == "getTime(off)"){
term.println("Hora apagado: " + String(horaoff) + ":" + String(minutooff));
term.flush();
}
else if (command == "clear"){
term.clear();
}
else if (command == "fp"){
term.println(fp);
term.flush();
}
else if(command == "luz.on"){
digitalWrite(5, LOW);
term.println(F("Luz Encendida."));
term.flush();
}
else if(command == "luz.off"){
digitalWrite(5, HIGH);
term.println(F("Luz Apagada."));
term.flush();
}
}
BLYNK_WRITE(V8){//time input widget. sets lights off time.
lightoffT = param.asLong();
secs = lightoffT;
minutooff = ( secs %3600) /60;
horaoff = secs / 3600;
light_time();
}
BLYNK_WRITE(V10){//photoperiod menu widget
switch (param.asInt()){
case 1:
fotoperiodo = 1;
break;
case 2:
fotoperiodo = 2;
break;
case 3:
fotoperiodo = 3;
break;
}
}
void setup()
{
// Debug console
Serial.begin(9600);
//BME280 config.
bme280.parameter.communication = 0;
bme280.parameter.I2CAddress = 0x76;
bme280.parameter.sensorMode = 0b11;
bme280.parameter.IIRfilter = 0b000;
bme280.parameter.humidOversampling = 0b001;
bme280.parameter.tempOversampling = 0b001;
bme280.parameter.pressOversampling = 0b001;
bme280.parameter.pressureSeaLevel = 1013.3;
bme280.parameter.tempOutsideCelsius = 15;
bme280.init();
Blynk.begin(auth, ssid, pass);
setSyncInterval(600);
timer.setInterval(5000L, get_temphum);
timer.setInterval(1000L, light_stat);
timer.setInterval(1000L, blk_sync);
timer.setInterval(60000L, light_time);
digitalWrite(builtLed1, HIGH);
digitalWrite(builtLed0, HIGH);
}
void loop()
{
Blynk.run();
timer.run();
}