Project: I have this Chinese 12V dual relay widget with an ESP-01 module strapped onto it. As it arrived, it had some pre-loaded code that only worked with Android (which I don’t have) so I’d like to use it with Blynk.
Now, I’ve got Blynk installed on the ESP-01, along with the virtual terminal and I can do the Marco-Polo thing. What I want to do now and just can’t find anything here in the forums is basically have a button for Open which sends two hex commands down the TX pin to the microcontroller on the relay board and another button that says Close that does the same but with the other hex commands. I want this to close the switch on my garage door controller that triggers the open cycle and the same for the circuit that closes it.
So I guess the first question is if this is possible with the ESP-01? Second is how do I code Blynk to send the relay control commands?
Relay control command(must be hex format):
Open the first relay: A0 01 01 A2
Close the first Relay: A0 01 00 A1
Open the second relay: A0 02 01 A3
Close the second Relay:A0 02 00 A2
I’ve seen quite a few topics about modules (save the word ‘Widget’ for when you’re talking about Blynk app widgets, or it gets confusing) that I think are virtually identical to yours.
No problem.
According to the stats I’ve somehow managed to spend a total of 16 days of my life reading over 36,000 posts on the forum, so it’s easier to know what’s out there and what sort of search terms are likely to return the desired results
For posterity then, here is the working code for the 2 channel version:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
const byte rel1ON[] = {0xA0, 0x01, 0x01, 0xA2}; //Hex command to send to serial for open relay
const byte rel1OFF[] = {0xA0, 0x01, 0x00, 0xA1}; //Hex command to send to serial for close relay
const byte rel2ON[] = {0xA0, 0x02, 0x01, 0xA3}; //Hex command to send to serial for open relay
const byte rel2OFF[] = {0xA0, 0x02, 0x00, 0xA2}; //Hex command to send to serial for close relay
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "yyyy";
// Your WiFi credentials.
char ssid[] = "yyyy";
char pass[] = "yyyy";
BLYNK_WRITE(V1)
{
int pinValue = param.asInt();
if (pinValue == 1) {
Serial.write(rel1ON, sizeof(rel1ON));
} else {
Serial.write(rel1OFF, sizeof(rel1OFF));
}
}
BLYNK_WRITE(V2)
{
int pinValue = param.asInt();
if (pinValue == 1) {
Serial.write(rel2ON, sizeof(rel2ON));
} else {
Serial.write(rel2OFF, sizeof(rel2OFF));
}
}
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
}
void loop() {
Blynk.run();
}
Blynk App set up with two buttons, in my case both set to Push, one on V1, the other on V2
There are two similar BLYNK_WRITE(Vx) functions In icewraithuk’s code. In my project, I have more virtual pins connected to “Time input” widget and I’m going to use your standard example as a base. But I want to create my code more compact and don’t want to copy one long similar code to several BLYNK_WRITE(Vx) function.
Is it possible to use some loop in one function BLYNK_WRITE_DEFAULT instead of writing several BLYNK_WRITE(Vx) functions?
I’ve read many topics in Blynk community and documentation, but that question is still open for me. I will be thankful to you for additional consultation.
You wouldn’t use a loop within the BLYNK_WRITE_DEFAULT() callback.
When BLYNK_WRITE_DEFAULT() fires, you can retrieve the virtual pin number that caused the callback to fire by using int pin = request.pin; and use that to direct your code flow accordingly.