I have a Blynk UI button, talks over virtual pin V1, and toggles a hardware pin on the ESP8266. Works great. I wanted to add Node-Red to control the hardware pin as well. The Blynk button output is either 0 or 1 (default), Node-Red is set up to inject a 0 or 1 into V1.
When I Node-Red inject a 0 or 1 (and it doesn’t matter if it is a string or integer), the Blynk UI button changes to reflect the changed state of V1. However, the hardware pin does not change! I have searched the forum for a similar problem without success.
I must be doing something wrong on the Node-Red end, but I am baffled that the Blynk UI recognizes the changed state of V1, but the hardware does not.
Just using Blynk, no MQTT involved. Since I was focused on V1, chucks of code have been commented out.
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h> // Used to establied serial communication on the I2C bus
#include "SparkFunTMP102.h" // Used to send and recieve specific information from our sensor
#include <ArduinoOTA.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
bool flag = true;
bool buttonFlag = false;
bool LEDflag = true;
bool Waterflag = false;
TMP102 sensor0(0x48);
BlynkTimer timer;
BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
int pinData = param.asInt();
if (pinData == 1)
digitalWrite(D7, 1);
else
digitalWrite(D7, 0);
}
/* not at this time
void checkTemperature()
{
float temperature;
sensor0.wakeup();
temperature = sensor0.readTempF();
Blynk.virtualWrite(V3, temperature);
sensor0.sleep();
if (flag == true)
{ if (temperature <50)
{Blynk.email("@vtext.com", "Subject", "Temperature < 50F");
flag = false;
}
}
if (temperature > 60)
flag = true;
}
*/
/*void checkMotion()
{
if (flag==true)
{if (digitalRead(D5)==1)
Blynk.email("@vtext.com", "Subject", "Garage motion");
flag = false;
}
if (!digitalRead(D5))
flag = true;
WidgetLED led1(V0);
if (digitalRead(D5))
led1.on();
else
led1.off();
}
*/
void checkWater() //borrowing D5 from checkMotion routine
{
if (Waterflag == false)
{
if(digitalRead(D5) == LOW)
{
Waterflag = true; //avoid multiple messages
Blynk.email("strauch1@gmail.com", "Subject", "Water Heater Alert!");
delay(6000); //Blynk allows only one email every five seconds
Blynk.email("@vtext.com", "Subject", "Water Heater Alert!");
}
}
Blynk.virtualWrite(V10,255*digitalRead(D5));
Blynk.virtualWrite(V8,255*Waterflag);
Serial.println(Waterflag);
}
void setup()
{
// Debug console
Serial.begin(9600);
//Wire.begin(D2,D1);
//sensor0.begin();
//sensor0.setConversionRate(2);
//sensor0.setExtendedMode(0);
ArduinoOTA.begin();
pinMode(D5, INPUT);
pinMode(D4, OUTPUT);
pinMode(D7, OUTPUT);
digitalWrite(D7, 0);
//timer.setInterval(200L, checkTemperature);
//timer.setInterval(1000L, checkMotion);
timer.setInterval(5000L, checkWater);
Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,155), 8080);
Blynk.email("strauch1@gmail.com", "Subject", "My Blynk garage project is online.");
delay(1000);
}
void loop()
{
Blynk.run();
timer.run();
ArduinoOTA.handle();
}
Thinking about it a bit more, this issue did ring a vague bell with me. A quick search came up with this thread:
It seems like a similar situation.
I’m currently working on revamping the MQTT code that I use on my devices, and adding-in some direct Blynk communication as well as the MQTT → Node-Red → Blynk method that I currently use. I’ve not shared the code that I run on my devices before (mostly because it needed this revamp first), so that should be too far off.
Perfect! The suggestion by gab.lau was very informative. Instead of the Blynk “write” node, one uses the “bridge” node. The authorization code for the Blynk code on the ESP8266 is what is required for the bridge node configuration.
Glad you remembered that thread, since I did not come across it when searching.
@arcs_n_sparks: I have not seen any part of your code about mqtt. If you do not set it up how can the trigger from node-red to be transferred to hardware as well as Blynk to be updated and vice versa.
No MQTT required (although I like what Pete Knight has done, and will gradually shift in that direction). The Node-Red Blynk library by gal.lau has a number of functions, including a “bridge” node that does what Blynk implemented. That allows a Node-Red message to be sent to the hardware (with appropriate authorization code).
It appears the Blynk server keeps the Blynk UI app synchronized with the current state of virtual pins. So if Node-Red changes the status of a virtual pin, that gets reflected in the UI.
So for the Node-Red diagram below, I have a Blynk UI switch on V1. When I push that switch, it communicates a ‘1’ directly to the ESP8266 hardware (no Node-Red required). That turns on a relay and is reflected in my code above. The “set message” node sends a ‘0’ message that is then delayed 20 minutes. After 20 minutes, Node-Red sends the ‘0’ message to the hardware via the bridge. The status of V1 is then changed on the UI courtesy of the Blynk server.