He gents,
I have created this project to replace my AC local thermostat by smart one. The device is working perfect as expected with no issues. I can control the AC either from blynk APP , Blynk web console ,or Nextion touch screen simultaneously. The whole code is function very well while the device is ON and connected to my WIFI. However, if the wifi network is lost for any reason, it still working for a while then it become not responsive to any change from the touch screen. Also, if the device powered up, and there is no WIFI network, it won’t work at all.
Is there any solution for these 2 problems?
BR,
• Smartphone OS (Android) + version 13
• blynk cloud
• Blynk Library version 1.2
• the communication between the ESP8285 and Nextion is serial communication.
• Hardware model: ESP8285 chip and Nextion display
/* This project is a smart thermostat uses Nextion display and ESP8285 chip. The same chip is used with Sonoff modules, so I used Generic ESP8285 Module*/
#define BLYNK_TEMPLATE_ID "************"
#define BLYNK_TEMPLATE_NAME "************************"
#define BLYNK_AUTH_TOKEN "******************************"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WidgetRTC.h>
///////////////////////////////////////
#define ONE_WIRE_BUS 10 // GPIO10
#define Y 15 // GPIO15 (Comp relay)
#define G 4 //GPIO04 (Fan relay)
#define HC 5 // GPIO05 (Heatpump relay ON= COOLING OFF=HEATING)
#define LED_BUILTIN 13
///////////////////////////////////////////////////////////////////////////
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
BlynkTimer timer;
WidgetRTC rtc;
////////////////////////////////////////////////////////////////////////////
char ssid[] = "*********************";
char pass[] = "*****************";
////////////////////////////////////////////////////////////////////////////
int temp = 0;
int TempValue = 24;
int pinData = 1 ;
int HOUR;
String currentTime;
String currentDate;
WidgetLED LED1(V3);
WidgetLED LED2(V10);
int sON =0;
int sAUTO =0;
bool isconnected;
String rec;
int ledState = LOW;
bool sync = 1;
int wakeup = 0;/// Nextion display wakeup (sleep after 5 mins)
////////////////////////////////////////////////////
void setup(){
Serial.begin(115200);
//Serial.setTimeout(2);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pinMode(Y,OUTPUT);
digitalWrite(Y, LOW);
pinMode(G, OUTPUT);
digitalWrite(G, LOW);
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(HC, OUTPUT);
digitalWrite(HC, HIGH);
sensor.begin();
rtc.begin();
timer.setInterval(1000L, mainprogram);
timer.setInterval(5000L, gettime);
timer.setInterval(5000L, gettemperature);
timer.setInterval(3000L, checkconnection);}
//////////////*assigning variables to virtual pins in Blynk App *///////////////////////////////////
BLYNK_WRITE(V2){
TempValue = param.asInt();}
BLYNK_WRITE(V8){
pinData = param.asInt();}
BLYNK_WRITE(V9){
sON = param.asInt();}
BLYNK_WRITE(V7){
sAUTO = param.asInt();}
BLYNK_WRITE(V20){
wakeup = param.asInt();}
/////////////////////*geting the time & date*/////////////////////////////
void gettime(){
HOUR = hour();
if (HOUR > 12){
HOUR = HOUR -12;
currentTime = String(HOUR)+ ":" +minute() + "PM";}
else{
currentTime = String(HOUR)+ ":" +minute() + "AM";}
currentDate = String(day()) + ":" + month() + ":" + year();}
///////////////////*geting temp. from sensor*///////////////////////////////
void gettemperature(){
sensor.requestTemperatures();
temp = sensor.getTempCByIndex(0);
Blynk.virtualWrite(V1, temp); }
/*if wifi is lost, LED OFF,otherwise LED is ON. also update nextion with connection status*/
void checkconnection(){
isconnected = Blynk.connected();
if (isconnected == false){
sync = 1;
Serial.print("t0.txt=\"OFFLINE\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
ledState = HIGH;
Serial.print("t0.pco=65535");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
digitalWrite(LED_BUILTIN, ledState);
if (isconnected == true){
ledState = LOW;
Serial.print("t0.txt=\"ONLINE\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("t0.pco=65535");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
digitalWrite(LED_BUILTIN, ledState);}
//////////////////////*Main program*/////////////////////////////////////////
void mainprogram(){
Blynk.virtualWrite(V20, wakeup);/*wakeup the touch screen*/
if (wakeup == 1){
Serial.print("sleep=0");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
/*update nextion display with variables*/
Serial.print("n0.val=");
Serial.print(temp);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("n1.val=");
Serial.print(TempValue);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("t3.txt=\"");
Serial.print(currentTime);
Serial.print("\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("t2.txt=\"");
Serial.print(currentDate);
Serial.print("\"");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
/*receiving commands from nextion display & update variables simultaneouly with Blynk App*/
rec = Serial.readString();
if (rec == "UP"){
TempValue++;
Blynk.virtualWrite(V2, TempValue);}
if (rec == "DOWN"){
TempValue--;
Blynk.virtualWrite(V2, TempValue);}
if (rec == "HEAT" ){
pinData = 0;
Blynk.virtualWrite(V8, pinData);
digitalWrite(HC,LOW);
Blynk.setProperty(V8, "color", "#D3435C");
Blynk.setProperty(V8, "label", "HEAT");}
if(rec == "COOL" ){
pinData = 1;
Blynk.virtualWrite(V8, pinData);
digitalWrite(HC,HIGH);
Blynk.setProperty(V8, "color", "#5F7CD8");
Blynk.setProperty(V8, "label", "COOL");}
if (pinData == 0){
digitalWrite(HC,LOW);
Blynk.setProperty(V8, "color", "#D3435C");
Blynk.setProperty(V8, "label", "HEAT");
Serial.print("bt1.val=");
Serial.print(0);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("q1.picc=");
Serial.print(0);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
if(pinData == 1){
digitalWrite(HC,HIGH);
Blynk.setProperty(V8, "color", "#5F7CD8");
Blynk.setProperty(V8, "label", "COOL");
Serial.print("bt1.val=");
Serial.print(1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("q1.picc=");
Serial.print(1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
if (rec == "ON"){
sON = 1;
Blynk.virtualWrite(V9, sON);}
if (rec == "OFF") {
sON = 0;
Blynk.virtualWrite(V9, sON);}
if(sON == 1){
Serial.print("bt0.val=");
Serial.print(1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
else{
Serial.print("bt0.val=");
Serial.print(0);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
if (rec == "AUTO"){
sAUTO = 0;
Blynk.virtualWrite(V7, sAUTO);}
if (rec == "On"){
sAUTO = 1;
Blynk.virtualWrite(V7, sAUTO);}
if( sAUTO == 1){
Serial.print("bt3.val=");
Serial.print(1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
else{
Serial.print("bt3.val=");
Serial.print(0);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);}
/*Comp. ON conditions*/
if (temp > TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0)){
digitalWrite(Y,HIGH);
Blynk.setProperty(V3, "color", "#23C48E");
Blynk.setProperty(V3, "label", " Comp On");
delay(1000);}
/*FAN ON conditions*/
if ((temp > TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0))|| (sAUTO == 1 && sON == 0)){
digitalWrite(G,HIGH);
Blynk.setProperty(V10, "color", "#23C48E");
Blynk.setProperty(V10, "label", " FAN On");}
/*Comp. OFF conditions*/
if ((temp < TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0))|| sON == 0){
digitalWrite(Y,LOW);
Blynk.setProperty(V3, "color", "#D3435C");
Blynk.setProperty(V3, "label", " Comp Off");
delay(1000);}
/*FAN OFF conditions*/
if ((temp < TempValue && sON == 1 && sAUTO == 0) || (sON == 0 && sAUTO == 0)){
digitalWrite(G,LOW);
Blynk.setProperty(V10, "color", "#D3435C");
Blynk.setProperty(V10, "label", " FAN Off");}
/*from here to sync APP with hardware(not from Blynk APP to hareware) when device reconnected again to wifi*/
if (sync == 1 && isconnected == true){
if (digitalRead(G)== LOW){
Blynk.setProperty(V10, "color", "#D3435C");
Blynk.setProperty(V10, "label", " FAN Off");}
if (digitalRead(G)== HIGH){
Blynk.setProperty(V10, "color", "#23C48E");
Blynk.setProperty(V10, "label", " FAN On");}
if (digitalRead(Y)== LOW){
Blynk.setProperty(V3, "color", "#D3435C");
Blynk.setProperty(V3, "label", " Comp Off");}
if (digitalRead(Y)== HIGH){
Blynk.setProperty(V3, "color", "#23C48E");
Blynk.setProperty(V3, "label", " Comp On");}
Blynk.virtualWrite(V7, sAUTO);
Blynk.virtualWrite(V9, sON);
Blynk.virtualWrite(V8, pinData);
Blynk.virtualWrite(V2, TempValue);
sync = 0;}}
void loop(){
Blynk.run();
timer.run();}