Heating system with bridge widget

Hi everybody.

I am developing an heating system control with two units one is base the other is client. Base is sensing temp with DHT22 and on application side “set temp” can be adjusted with step widget. The measured temp is compared with set and if there is 0.5 celcius difference occurs base unit sends on or off signal to client through bridge widget. There is also nokia screen on base side where you can see the relative info.
Base unit is Wemos D1 min, client Nodemcu 12e.
When i start both units every thing works fine but after a while base doesn’t command client. When I check connection both unit is online and also working, since i can reach both units through android app and control them. I couldn’t solve the problem.
What am i doing wrong.

Here is Base side Code

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
//#define BLYNK_DEBUG // Optional, this enables lots of prints
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "xxxxx";  
SimpleTimer timer;

WidgetBridge bridge1(V2); //Bridge



// Software SPI (slower updates, more flexible pin options):
// pin D3 - LCD reset (RST) 0
// pin D4 - LCD chip select (CE) 2
// pin D5 - Data/Command select (DC) 14
// pin D2 - Serial data out (DIN) 4
// pin D0 - Serial clock out (CLK) 16
// pin 3.3 - Serial clock out (VCC)
// pin GND - Serial clock out (LIGHT)
// pin GND - Serial clock out (GND)

Adafruit_PCD8544 display = Adafruit_PCD8544(16, 4, 14, 2, 0);
#include "DHT.h"
#define DHTPIN 5     // D1 pin what pin we're connected to
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);
  float h ; // Humidity
  float t ; // Temperature
  float pin0Value; // Setted Temperature
  
bool isFirstConnect = true;
bool kombist = false;  // Heating System Status
int conn= 2; // Builtin led pin which shows the Connection Status for BLYNK


void Kombi() // This is the Heating System Controling Code
{
    if (t <= pin0Value - 0.5) // Checks the temp with setted temp if temp low switches the heating system
    {
    bridge1.digitalWrite(16, 1);
    bridge1.virtualWrite(V1, 1);
    Serial.println("Yandi");
    kombist = true;
    }
    if (t >= pin0Value + 0.5) 
    {
    bridge1.digitalWrite(16, 0);
    bridge1.virtualWrite(V1, 0);
    Serial.println("Sondu");
    kombist = false;
    }
    if (kombist) // This code for when the temp is between set-0.5 and set+0.5
    {
    bridge1.digitalWrite(16, 1);
    bridge1.virtualWrite(V1, 1);
    Serial.println("Yandi");
    }
    else
    {
    bridge1.digitalWrite(16, 0);
    bridge1.virtualWrite(V1, 0);
    Serial.println("Sondu"); 
    }
  }

BLYNK_CONNECTED()
{
  if (isFirstConnect) {
    // Request Blynk server to re-send latest values for all pins
    Blynk.syncAll();

    // You can also update individual virtual pins like this:
    //Blynk.syncVirtual(V0, V1, V4);

    isFirstConnect = false;

    bridge1.setAuthToken("xxxx"); // Place the AuthToken of the second hardware here

  }
}


BLYNK_WRITE(V0) // This set temp widget
{
    pin0Value = param.asFloat(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
    if (t < pin0Value) // This code is for, controlling the heating system when adjusting temp.
    {
    bridge1.digitalWrite(16, 1);
    bridge1.virtualWrite(V2, 1);
    Serial.println("Yandi");
    kombist = true;
    }
    if (t > pin0Value) 
    {
    bridge1.digitalWrite(16, 0);
    bridge1.virtualWrite(V2, 0);
    Serial.println("Sondu");
    kombist = false;
    }

// Code Below is writing the relative info to screen when seting temp
    display.setCursor(0,0);
    display.setTextSize(2);
    display.clearDisplay();   // clears the screen and buffer
    display.print("ISI");
    display.setTextSize(1);
    display.print("St");
    display.println(pin0Value);
    display.println();
    display.setTextSize(2);
    display.print(t);
    display.println("C");
    display.setTextSize(1);
    display.print("Nem:"); 
    display.print(h);
    display.println(" %");
    display.display();

}





void sendUptime() // This is temp measuring and writting code to virtual pins
{
  h = dht.readHumidity();
  t = dht.readTemperature();
//  t = t - 0.5;
  Blynk.virtualWrite(10, t); // virtual pin Temp 
  Blynk.virtualWrite(11, h); // virtual pin Humidity
  Blynk.virtualWrite(1, pin0Value); // virtual pin Set Temp  

}

void ekranayaz() // This code is screen update code. It was in sendUptime code but i realized that it causes connection drop so i seperated.
{
    h = dht.readHumidity();
    t = dht.readTemperature();
    display.setCursor(0,0);
    if (isnan(t) || isnan(h)) 
    {
    display.println("Failed to read from DHT");
    } 
    else
    {
    display.setTextSize(2);
    display.clearDisplay();   // clears the screen and buffer
    display.print("ISI");
    
    display.setTextSize(1);
    display.print("St");
    display.println(pin0Value);
    display.println();
    display.setTextSize(2);
    
    display.print(t);
    display.println("C");
    display.setTextSize(1);
    display.print("Nem:"); 
    display.print(h);
    display.println(" %");
    display.display();
    }
}

void reconnectBlynk() {                         // reconnect to server if disconnected, timer checks every 60 seconds
  if (!Blynk.connected()) 
  {
    if(Blynk.connect()) 
      {
      BLYNK_LOG("Reconnected");
      } 
    else 
    {
      BLYNK_LOG("Not reconnected");
    }
  }
}

void setup()
{
  Serial.begin(115200);
  pinMode(conn, OUTPUT); // This is builtin led pin that shows connection status
  digitalWrite(conn, HIGH); // Actually this is low on physical side
  display.begin();
  display.setContrast(50);
  display.clearDisplay();   // clears the screen and buffer
  display.setTextColor(BLACK);
  display.setTextSize(1);
  display.clearDisplay();   // clears the screen and buffer
  display.println("Connecting"); 
  display.display();
  Blynk.begin(auth, "xxx", "xxxx"); //insert here your SSID and password
  display.println("Connected"); 
  display.display();

  dht.begin();
  timer.setInterval(5000L, sendUptime); // This updates measurements
  timer.setInterval(60000L, reconnectBlynk);     // check every 60 seconds if we are connected to 
  timer.setInterval(10000L, Kombi); // Sends client on or off
  timer.setInterval(30000L, ekranayaz); // Screen update 
}

void loop() {
  if (Blynk.connected()) 
  {   // to ensure that Blynk.run() function is only called if we are still connected to the server
    Blynk.run();
    digitalWrite(conn,LOW); // If connection ok builtin led on
  }
  else
  {
    digitalWrite(conn,HIGH); // If connection drops builtin led off
  }
  timer.run();
}

On Client Side is so simple and code as follows

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

SimpleTimer timer;
WidgetLED led1(V0); // This for check the status through app
int pinData; // Heating system can be controlled over app also
bool isFirstConnect = true;
int ledpin = 16; // This pin for relay if everthing works fine :)
int conn = 2;  // NodeMCU builtin led pin

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxxx";

void KD() // Heating system check script every 5 sec and update to blynk virtual pins
{
int pinValueRed = digitalRead (ledpin);
if (pinValueRed == 1)
  {
  led1.on();
  Blynk.virtualWrite(10, 100); // virtual pin to see the status through history graph 
  Serial.println("Kombi Acik");
  }
else 
  {
  led1.off();
  Blynk.virtualWrite(10, 0); // virtual pin
  Serial.println("Kombi Kapali");
  }
}

BLYNK_CONNECTED() {
  if (isFirstConnect) {
//    Request Blynk server to re-send latest values for all pins
    Blynk.syncAll();
//
//    You can also update individual virtual pins like this:
//    Blynk.syncVirtual(V0, V1, V4);
    isFirstConnect = false;
  }
}



BLYNK_WRITE(V1) // This is for button widget
        {
        pinData = param.asInt(); // pinData variable will store value that came via Bridge
        digitalWrite(ledpin, pinData);
        }

void AutoOff() // This code for safety. Every hour switch off system
{
digitalWrite(ledpin, 0);
}


void reconnectBlynk() {                         // reconnect to server if disconnected, timer checks every 60 seconds
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}


void setup()
{
  Serial.begin(115200);
  pinMode(ledpin, OUTPUT);
  pinMode(conn, OUTPUT);
  digitalWrite(conn,HIGH);
  Blynk.begin(auth, ssid, pass);
  
  timer.setInterval(3600000L, AutoOff); // Safety Code . Every hour switch off system
  timer.setInterval(5000L, KD);  // Check heating system status every 5 sec and update blynk
  timer.setInterval(60000L, reconnectBlynk);     // check every 60 seconds if we are connected to 
}

void loop()
{
  if (Blynk.connected()) 
  {   // to ensure that Blynk.run() function is only called if we are still connected to the server
    Blynk.run();
    digitalWrite(conn,LOW); // If connection ok builtin led on
  }
  else
  {
    digitalWrite(conn,HIGH);// If connection drops builtin led off
  }
  timer.run();
}

This could be a case study for bridge widget.

Thanx

By the way i am using Blynk_Release_v0.4.4 libraries.
Arduino IDE 1.8.1

ditch the bridge widget, run two codes with same blynk auth code…

1 Like

Is it possible :thinking: to run two unit with same auth code. Virtual pins don’t interfere?

Obviously use different v pins, there’s 120+ to choose from…

This is regularly discussed, did you try the search function?

Thank you for your solution. This definetly solve my problem.
By the way, i made a search before sending post but i am concantrated mostly on bridge widget and i am new to blynk community.
Regards

sorry, yes, you are right, i recall there was more “bridge” discussion in 2015 & 2016… not so much now - so search does not show this up easily, sorry for being presumptuous… my bad :frowning:

1 Like

This also works in IOS App?

Pretty sure it would!