ESP8285 Smart thermostat controlled by nextion display & blynk app simultaneously without using Nextion library

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();}

Blynk.begin is a blocking function.
To get around this you need to manage your own WiFi connectivity and use Blynk.config and Blynk.connect.

You also need an if test in your void loop so that Blynk. Run is only executed if Blynk.connected() == true.

Pete.

Thank you, Mr.pete

Dear Mr.Pete,
Just to clarify something, by using Blynk.config in setup and Blynk.connect at the end of code is running without wifi network, do I need to use Blynk.connectWiFi in setup too?

Thanks
Abdulhaq

This is a blocking command too. You need to use the WiFi commands that come as part of the ESP8266 core.

Do a quick forum search and you’ll find some examples.

Pete.

1 Like

Ok thanks
BR,
Abdulhaq

Dear Mr. Pete. Thanks for your valid comments, Now the code is working fantastic either with network or without it no blocking code anymore

Abdulhaq

2 Likes

Abdulhaq, I have a Nextion screen from another project that is similar to yours. I also have a esp8285 with 4 relays board. Is it possible for you to share the completed code and me make a contribution like coffee to your project? Let me know, Rand

Dear Rand,
My pleasure brother no problem. Just give me a week to be near my computer so I can shar the code with you. Now Im in vacation.

BR,
Abdulhaq

/* 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>

#include "time.h"

#include "sntp.h"

///////////////////////////////////////

#define ONE_WIRE_BUS 10 //SW3 GPIO10

#define Y 15           //output4 GPIO15 (Comp relay)

#define G  4           //output3 GPIO04 (Fan relay)

#define HC  5           //output2 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 ;

String Time;

String Date;

char dateBuffer[12];

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);

 WiFi.begin(ssid, pass);

 Blynk.config(BLYNK_AUTH_TOKEN);

 pinMode(Y,OUTPUT);

 digitalWrite(Y, LOW);

 pinMode(G, OUTPUT);

 digitalWrite(G, LOW);

 pinMode(LED_BUILTIN,OUTPUT);

 pinMode(HC, OUTPUT);

 digitalWrite(HC, HIGH);

 sensor.begin();

 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);

 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();}

 BLYNK_CONNECTED(){

   rtc.begin();

 }

/////////////////////*geting the time & date*/////////////////////////////

void gettime(){

  sprintf(dateBuffer, "%02d:%02d", hour(), minute());  

  Time = dateBuffer;

  sprintf(dateBuffer, "%02d/%02d/%04d", day(), month(), year());

  Date = dateBuffer;}

///////////////////*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(){

if (WiFi.status() != WL_CONNECTED) {  

   sync = 1;

   Serial.print("t0.txt=\"OFFLINE\"");

   Serial.write(0xff);

   Serial.write(0xff);

   Serial.write(0xff);

   Serial.print("t0.pco=65535");

   Serial.write(0xff);

   Serial.write(0xff);

   Serial.write(0xff);}

if (WiFi.status() == WL_CONNECTED){                        

   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);}}

//////////////////////*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(Time);

   Serial.print("\"");

   Serial.write(0xff);

   Serial.write(0xff);

   Serial.write(0xff);

   Serial.print("t2.txt=\"");

   Serial.print(Date);

   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);

    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){

    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){

    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);}

/*Cool mode*/

if (pinData== 1){

/*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)){

          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");}

    delay(1000);

    digitalWrite(HC,HIGH);}

/*Heat mode*/          

if (pinData== 0){

/*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)){

          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");}

    delay (1000);

    digitalWrite(HC,LOW);}

/*from here to sync APP with hardware(not from Blynk APP to hareware) when device reconnected again to wifi*/

 isconnected = Blynk.connected();

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 serialonly(){

   sensor.requestTemperatures();

   temp = sensor.getTempCByIndex(0);

   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);

   rec = Serial.readString();

    if (rec == "UP"){

        TempValue++;}

    if (rec == "DOWN"){

        TempValue--;}

    if (rec == "HEAT" ){

        pinData = 0;}

    if(rec == "COOL" ){

        pinData = 1;}

    if (rec == "ON"){

        sON = 1;}

    if (rec == "OFF") {

        sON = 0;}

    if (rec == "AUTO"){

        sAUTO = 0; }

    if (rec == "On"){

        sAUTO = 1;}

/*Cool mode*/

    if (pinData== 1){

/*Comp. ON conditions*/    

        if (temp > TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0)){

               digitalWrite(Y,HIGH);

               delay(1000);}

/*FAN ON conditions*/

        if ((temp > TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0))|| (sAUTO == 1)){

               digitalWrite(G,HIGH);}

/*Comp. OFF conditions*/    

        if ((temp < TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0))|| sON == 0){

               digitalWrite(Y,LOW);

               delay(1000);}

/*FAN OFF conditions*/

        if ((temp < TempValue && sON == 1 && sAUTO == 0) || (sON == 0 && sAUTO == 0)){

               digitalWrite(G,LOW);}

        delay(1000);

        digitalWrite(HC,HIGH);}

/*Heat mode*/    

    if (pinData== 0){

/*Comp. ON conditions*/    

        if (temp < TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0)){

               digitalWrite(Y,HIGH);

               delay(1000);}

/*FAN ON conditions*/

        if ((temp < TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0))|| (sAUTO == 1)){

               digitalWrite(G,HIGH);}

/*Comp. OFF conditions*/    

        if ((temp > TempValue && sON == 1 && (sAUTO == 1 ||sAUTO == 0))|| sON == 0){

               digitalWrite(Y,LOW);

               delay(1000);}

/*FAN OFF conditions*/

        if ((temp > TempValue && sON == 1 && sAUTO == 0) || (sON == 0 && sAUTO == 0)){

               digitalWrite(G,LOW);}

        delay (1000);

        digitalWrite(HC,LOW);}}        

void loop(){

   if (WiFi.status() != WL_CONNECTED) {  

     ledState = HIGH;

     digitalWrite(LED_BUILTIN, ledState);

     serialonly();}

   else {

     ledState = LOW;

     digitalWrite(LED_BUILTIN, ledState);

     Blynk.run();

     mainprogram();}  

     timer.run();

   }