Blynk + Homekit + Node-Red (on Raspberry or Synology)

My idea was to connect Blynk projects to the rest of the world. As I have iOS with embedded app Home and also RaspberryPi (or Synology NAS) as a server I finaly found awesome Node-Red.

Embeded relay

1. HW

from TME.eu

USB power adapter - 5V, 1A output
WeMos D1 mini V2
WeMos relay shield

How to combine I let on You :slight_smile:

2. SW

I run everything on my RaspberryPi but it can be also run on other platforms where can be installed Node.js (and Java for local Blynk server - not needed). After that a Node-Red needs to be installed. (If You have Raspbian Jessie there is Node-Red preinstalled).

Next plugins node-red-contrib-homekit and node-red-contrib-blynk-websockets needs to be installed.

Restart Node-Red daemon to allow newly installed package to tak effect.

3. Blynk application + ESP scetch

setup Blynk project:

See WeMos and Use Arduino:

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

const int relayPin = D1;
SimpleTimer timer; // the timer object
char auth[] = "BLYNK_AUTH_TOKEN";
char localserver[16] = "IP_OF_SERVER";          // Enter your IP details for the local server
char ssid[] = "ESSID";
char pass[] = "ESSID_PASSWORD";

void setup()
{
  pinMode(relayPin, OUTPUT);
  Blynk.begin(auth, ssid, pass, localserver);
  timer.setInterval(1000, repeatMe);
}

// This function will be called every time V1 will be chaged by application
// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  digitalWrite(relayPin, pinValue);
}

//Get data stored on server
BLYNK_CONNECTED() {
      Blynk.syncAll();
}

// A function to be executed periodically
void repeatMe() {
    Blynk.syncVirtual(V1); //Get V1 from server by force - due to websocket
}

void loop()
{
  Blynk.run();
  timer.run();
}

4. Set up flow in Node-Red

I’ve created flow which read changes from Blynk (from app) and transfer data to HomeKit accesory. If Accesory changes, data are transfered back to Blynk (app and HW). This ensure that both, Blynk and Home, updates state of button and HW will be triggered.

You can import this flow directly to Node-Red (try YOUR_SERVER:1880 in web browser):

[{"id":"cafa6165.75fb2","type":"blynk-websockets-in-write","z":"22ab1451.c65d5c","name":"","pin":"1","client":"2fa4f650.84b19a","x":110,"y":120,"wires":[["a2719851.de8bb8","12fc129c.477855"]]},{"id":"a2719851.de8bb8","type":"template","z":"22ab1451.c65d5c","name":"\"on\":value","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\"On\":{{payload}}}","x":290,"y":120,"wires":[["81a07c29.0f3c7"]]},{"id":"81a07c29.0f3c7","type":"json","z":"22ab1451.c65d5c","name":"","x":430,"y":120,"wires":[["d6a28a0e.b209c8"]]},{"id":"d6a28a0e.b209c8","type":"homekit-service","z":"22ab1451.c65d5c","accessory":"5c1a3f3.55c7fc","name":"","serviceName":"Outlet","x":550,"y":120,"wires":[["dd481e95.f5fa5"]]},{"id":"c00e818b.3a24d","type":"blynk-websockets-out-write","z":"22ab1451.c65d5c","name":"","pin":"1","client":"2fa4f650.84b19a","x":940,"y":120,"wires":[]},{"id":"dd481e95.f5fa5","type":"function","z":"22ab1451.c65d5c","name":"On toString","func":"msg.payload = msg.payload.On.toString();\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":120,"wires":[["c00e818b.3a24d","8b839cf0.44b4d"]]},{"id":"2fa4f650.84b19a","type":"blynk-websockets-client","z":"","name":"Outles","path":"ws://BLYNK_SERVER:8082/websockets","key":"BLYNK_TOKEN "},{"id":"5c1a3f3.55c7fc","type":"homekit-accessory","z":"","accessoryName":"zasuvka","pinCode":"111-11-111","port":"","manufacturer":"fisero","model":"Blynk relay","serialNo":"001","accessoryType":"7"}]

##Result
###Perform click in Blynk app:


as a result, WeMos relay will be switched ON.

###Perform click in Home app:


as a result, WeMos relay will be switched OFF.

7 Likes

Nice setup!

I have one problem which is not resolved yet. String sended from Node-Red flow (via websockets) is routed to application on phone but not to the hardware in any form. I do not know what is wrong yet.

Finally I’ve solved problem that data pushed to server from node-red was not received on HW side. So solution is force HW to read actual data from server. See new code for HW updated on top.

1 Like