BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Bridge Widget Communication

#1

I have been searching everywhere for good examples in using this widget but can only find one where a relay switch is turned on or off. What i am trying to achieve is a bit different. I have 2 nodeMCU’s, Master & Slave. The idea is for an aquarium led fixture where the master will send a set of values to the Slave where the Slave will take these values and implement them into a led fading code flashed on the Slave.
The approach i have taken is

  1. List item The intensity of the led, the fadeintime and fadeouttime and the time input send values to the Slave.

  2. The Slave is supposed to take these values and implement them into to fade code.

What is happening is…well nothing really, both Master and Slave connect up but there does not seem to be communication between them. I would appreciate any help with this. I have left the auth codes in to show that they are linked correctly.

MASTER CODE
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
//#include <FadeLed.h>
WidgetRTC rtc;
BlynkTimer timer;

//WIFI Variables
char auth[] = "79498609bb2e47ae95248239105346b4";   //Master auth
char ssid[] = "";
char pass[] = "";

//BRIDGE WIDGET//
WidgetBridge bridge1(V0);


//RTC/Time Input Widget Variables
char currentTime[9];
char currentDate[11];
char startTime[9];
char stopTime[9];
int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;
int dayNumber;
bool activeToday = true;
bool scheduler = true;
bool schedulerStatusChange = false;

//LED FADE ATTRIBUTES & VARIABLES
//FadeLed CH1(V10);
int FadeIN = 0;
int FadeOUT = 0;
int maxPWM;
unsigned long FadeInTime;
unsigned long FadeOutTime;

BLYNK_CONNECTED()   //Connect to Blynk server
{
  bridge1.setAuthToken("27796ba8ad5c4c649f73c7592ca221f2");   //Slave auth
  Blynk.syncAll();
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  //CH1.set(0);
  rtc.begin();
  setSyncInterval(30);
  timer.setInterval(1000L, ClockDisplay);
  timer.setInterval(500L, TimeCheck);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

void ClockDisplay()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
  sprintf(currentDate, "%02d/%02d/%04d", day(), month(), year());

  Blynk.virtualWrite(V6, currentTime);    //Send time to TIME widget
  Blynk.virtualWrite(V7, currentDate);    //Send date to DATE widget
}

BLYNK_WRITE(V1)  //CH1 TIME IMPUT WIDGET
{
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
  dayNumber = weekday();
  activeToday = false;
  unsigned int OnDays = 0;
  for (int i = 1; i <= 7; i++)    // Process weekdays (1-Mon, 2-Tues, 3-Wed, 4-Thurs, 5-Fri, 6-Sat, 7-Sun)
  {
    if (t.isWeekdaySelected(i))
    {
      OnDays++;
      if(i==dayNumber)
      {
        activeToday = true;   //Active today
      }
      Serial.println(String("Day ") + i + " is selected");
    }
  }
  if (OnDays == 0)    //Not active today
  {
    activeToday = false;
  }
}

BLYNK_WRITE(V2)   //CH1 FADEIN TIME WIDGET
{
  FadeIN = param.asInt();
  FadeInTime = (FadeIN * 60000);   //Fadein param is in minutes as set by widget
  bridge1.virtualWrite(V11, FadeInTime);   //Send fadein time to Slave
  Serial.print("LedFadeIn Time: ");
  Serial.println(FadeInTime);
}

BLYNK_WRITE(V3)   //CH1 FADEOUT TIME WIDGET
{
  FadeOUT = param.asInt();
  FadeOutTime = (FadeOUT * 60000);      //Fadeout param is in minutes as set by widget
  bridge1.virtualWrite(V12, FadeOutTime);   //Send fadeout time to Slave
  Serial.print("LedFadeOut Time: ");
  Serial.println(FadeOutTime);
}

BLYNK_WRITE(V4)   //CH1 UV INTENSITY WIDGET
{
  int PWMinput = param.asInt();
  maxPWM = map (PWMinput, 0, 100, 0, 1023);   //Intensity param set with widget
  bridge1.virtualWrite(V13, maxPWM);          //Send intensity value to Slave
  Serial.print("Intensity: ");
  Serial.println(maxPWM);
}

/*void LedFadeIn ()
{
  CH1.setTime(FadeInTime);
  CH1.set(maxPWM);
}

void LedFadeOut ()
{
  CH1.setTime(FadeOutTime);
  CH1.set(0);
}

void ProgressBar()
{
  CH1.getCurrent();
  Serial.print("Progress: ");
  Serial.println(CH1.getCurrent());
  Blynk.virtualWrite(V5, CH1.getCurrent());
}*/

void TimeCheck()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second()); //Get RTC time
  Serial.print("Current Time: ");
  Serial.println(currentTime);
  sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec); //Get start time from widget
  Serial.print("Start Time: ");
  Serial.println(startTime);  
  sprintf(stopTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec); //Get stop time from widget
  Serial.print("Stop Time: ");
  Serial.println(stopTime);
  
  if (activeToday == true)
  {
    if (hour() == SThour)
    {
      if (minute() == STmin)
      {
        //LedFadeIn();
        bridge1.virtualWrite(V1, 1);      //Send a value of 1 to Slave as time to dim up
        }
        else if (hour() == SPhour)
        {
          if (minute() == SPmin)
          {
            //LedFadeOut();
            bridge1.virtualWrite(V1, 0);    //Send a value of 0 to Slave as time to dim down
          }
        }
    }
  }
}
SLAVE CODE#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <FadeLed.h>
WidgetRTC rtc;
BlynkTimer timer;

char auth[] = "27796ba8ad5c4c649f73c7592ca221f2"; //Slave auth
char ssid[] = "";
char pass[] = "";

//BRIDGE WIDGET//
WidgetBridge bridge1(V0);
static bool bridge = true;

//LED FADE ATTRIBUTES & VARIABLES
FadeLed CH1(D4);
int FadeIN = 0;
int FadeOUT = 0;
int maxPWM;
unsigned long FadeInTime;
unsigned long FadeOutTime;

BLYNK_CONNECTED()   //Connect to Blynk server
{
  bridge1.setAuthToken("79498609bb2e47ae95248239105346b4"); //Master auth
  Blynk.syncAll();
}

void SyncChannels()
{
  FadeLed::update();
}

void setup()
{
  // Debug console

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  analogWrite (D4, 0);
  CH1.set(0);
  setSyncInterval(30);
  timer.setInterval(10L, SyncChannels);
  timer.setInterval(500L, ProgressBar);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}
BLYNK_WRITE(V10)  //Function to determine to fade up or fade down
{
  int pinData = param.asInt();
  if (pinData == 1)           //Value of 1 received from Master = fade up
  {
    LedFadeIn();
  }
  else if (pinData == 0)      //Value of 0 received from Master= Fade down
  {
    LedFadeOut();
  }
}

BLYNK_WRITE(V11)
{
  FadeInTime = param.asInt();   //Fadein time received from Master
}

BLYNK_WRITE(V12)
{
  FadeOutTime = param.asInt();    //Fadeout time received from Master
}

BLYNK_WRITE(V13)
{
  maxPWM = param.asInt();   //Intensity value received from Master
}
  
void LedFadeIn ()
{
  CH1.setTime(FadeInTime);
  CH1.set(maxPWM);
}

void LedFadeOut ()
{
  CH1.setTime(FadeOutTime);
  CH1.set(0);
}

void ProgressBar()
{
  CH1.getCurrent();
  Blynk.virtualWrite(V5, CH1.getCurrent());
}
0 Likes

#2

Found my mistake!!! I could slap myself for this…sometimes looking at a screen for too long full of numbers and code you start to not be able to see stupid typos!!!

2 Likes