To turn On and Off a Power Relay with Blynk is not a problem and you can find many posts showing this.
@Jamin
“SONOFF Clone” - Mini-ESP8266 Power AC Relay Controller
part of my sketch for a (+ve) activated Relay
sinric/arduino_examples/google_home_switch_example.ino
/*
Version 0.1 - March 17 2018
*/
//#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Arduino.h> // check if working
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h> // get it from https://github.com/Links2004/arduinoWebSockets/releases
#include <ArduinoJson.h> // get it from https://arduinojson.org/ or install via Arduino library manager
#include <StreamString.h>
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
WiFiClient client;
#define MyApiKey "<API KEY>" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
#define WIFI_SSID "<WIFI NAME>" // TODO: Change to your Wifi network SSID
#define WIFI_PASS "<WIFI PASSWORD>" // TODO: Change to your Wifi network password
#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
uint64_t heartbeatTimestamp = 0;
bool isConnected = false;
void turnOn(String deviceId) {
if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of first device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn on device id: ");
Serial.println(deviceId);
}
else {
Serial.print("Turn on for unknown device id: ");
Serial.println(deviceId);
}
}
void turnOff(String deviceId) {
if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of first device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
{
Serial.print("Turn off Device ID: ");
Serial.println(deviceId);
}
else {
Serial.print("Turn off for unknown device id: ");
Serial.println(deviceId);
}
}
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
isConnected = false;
Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
break;
case WStype_CONNECTED: {
isConnected = true;
Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
Serial.printf("Waiting for commands from sinric.com ...\n");
}
break;
case WStype_TEXT: {
Serial.printf("[WSc] get text: %s\n", payload);
// Example payloads
// For Switch types
// {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":true}} // https://developers.google.com/actions/smarthome/traits/onoff
// {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":false}}
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject((char*)payload);
String deviceId = json ["deviceId"];
String action = json ["action"];
/*
//-------------------------------------------------------------------------------
// if you need echo dot + home mini uncomment this part
if(action == "setPowerState") // Switch or Light
{ String value = json ["value"];
if(value == "ON") {
turnOn(deviceId);
} else {
turnOff(deviceId);
}
}
else if(action == "setBrightness") {
}
else if(action == "AdjustBrightness") {
}
// else if (action == "SetTargetTemperature") {
// String deviceId = json ["deviceId"];
// String action = json ["action"];
// String value = json ["value"];
// }
//--------------------------------------------------------------------------------------
*/
if(action == "action.devices.commands.OnOff") { // Switch
String value = json ["value"]["on"];
Serial.println(value);
if(value == "true") {
turnOn(deviceId);
} else {
turnOff(deviceId);
}
}
else if (action == "test") {
Serial.println("[WSc] received test command from sinric.com");
}
}
break;
case WStype_BIN:
Serial.printf("[WSc] get binary length: %u\n", length);
break;
}
}
/*--------------------------------Setup --------------------------------------*/
void setup() {
WiFi.mode(WIFI_STA);
// Serial.begin(115200);// See the connection status in Serial Monitor
#ifdef LOCAL_SERVER
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS, LOCAL_SERVER ,8080);
#else
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS, Geo_DNS_SERVER);
#endif
while (Blynk.connect() == false) {}
WiFiMulti.addAP(WIFI_SSID, WIFI_PASS);
Serial.println();
Serial.print("Connecting to Wifi: ");
Serial.println(WIFI_SSID);
// Waiting for Wifi connect
while(WiFiMulti.run() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if(WiFiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.print("WiFi connected. ");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// server address, port and URL
webSocket.begin("iot.sinric.com", 80, "/"); //"iot.sinric.com", 80
// event handler
webSocket.onEvent(webSocketEvent);
webSocket.setAuthorization("apikey", MyApiKey);
// try again every 5000ms if connection has failed
webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
}
/*----------------------------------Loop--------------------------------------*/
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates Blynk Timer
webSocket.loop();
if(isConnected)
{ uint64_t now = millis();
// Send heartbeat in order to avoid disconnections during ISP
// resetting IPs over night. Thanks @MacSass
if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL)
{ heartbeatTimestamp = now;
webSocket.sendTXT("H");
}
}
}
/*----------------------------------------------------------------------------*/