Blynk Control of NCD Devices
In this article we will introduce you to controlling real world devices using the Blynk system.
Blynk is a really cool and easy way to interface to IoT devices of all sorts. It provides an app you can install right on your Android or iOS device and start interacting with your IoT project. ControlEverything provides a wide array of devices for your control, monitoring, or automation projects. Everything from relays, to current monitoring, to environmental sensing, etc. With the wide range of off the shelf products from ControlEverything you will find it difficult to think of anything we don’t have covered. Oh and did I mention no soldering or breadboards? Make you project look professional with purpose built solutions and impress your co-workers, friends, and family. These devices can be used for home/hobby projects right up to industrial control/monitoring and everything in between. Blynk and ControlEverything really are a match made in IoT heaven. So let’s get to it.
We will begin our journey with Blynk/NCD by controlling 2 relays, monitoring 6 inputs, as well as a temperature sensor for environmental monitoring. The range of projects these devices can fulfill are vast, so it’s a good starter project.
For our on device processor we will be using the Particle Photon module which is a WiFi connected device which will provide internet access/connection between our hardware and the Blynk Cloud server. Of course you could go another route here but I have found this is the simplest place to start.
Things you will need for this project
- 2 channel Photon or Electron interface relay controller (Either will work with the Photon module)
- SI7020 temperature sensor mini module from ncd.io
- 12VDC power supply.
- Particle Photon module (available from ncd.io).
- iOS or Android device.
- Blynk app installed on your iOS or Android device(Search the app store for Blynk).
- Particle account. You can set one up at http://build.particle.io
- Blynk account. You can set up an account through the Blynk app installed on your Android or iOS device
The prudent first step is to set up your Particle account. If you do not have one already the head over to http://build.particle.io and do that now.
Once your account is setup you should now install the Photon module into the relay controller. While we’re at it go ahead and connect the SI7020 temperature sensor to the relay controller using the included 4 conductor cable. Finally apply your 12VDC power supply to the board to power everything up.
At this point you need to associate your Particle Photon module with your Particle account. This is very well documented by Particle here:
Go to the app store on your smartphone/tablet and install the Blynk app, then open it up. If this is your first time using Blynk it will prompt you to login, you can also create a new account here so do that now.
Once you are logged in create a new project. Projects in Blynk are essentially dashboards for a particular setup. So this project will be a dashboard for interfacing to 2 relays, 6 inputs, and a temperature sensor. You can name the project anything you like.
When prompted for Hardware Model select Particle Photon.
For connection type select WiFi.
Finally press Create Project.
You will now see a dot grid screen. This is where we will drag widgets like buttons, input indicator LEDs, and a view for our temperature. We have some other stuff to do first though.
Click on the Settings button in the upper right corner of your project(looks like a hex nut). Here we can edit all settings for this particular Project. Projects can interface with several devices but for this simple example we will only use one device which is the Particle Photon module installed in the relay board. We already set that up when we created the project so no need to do anything here now unless you want to rename the module to something different from the Project name.
Scroll down to the Auth Tokens section. Here there is an email all button. Click that to email all auth tokens for the project and the devices to the email address you used to set up the Blynk app. We will need to insert the project Auth token into the code running on the Photon module.
We are finished with the Blynk app for now so you can close it at this time.
Code on Module
Now it’s time to flash the code onto the Photon module which will interact with the relays, the inputs, the temperature sensor, and the Blynk app. The code as well as the Photon module really are the glue that stick your control interface and the hardware together.
Head over to http://build.particle.io and sign into your Particle account if you have not already.
Create a new code project and give it a name.
Now go over to our github repo here and copy the code:
Go back to Build and paste that code into your .ino file.
Finally we need to add the dependency libraries which this code requires. On build click the community Libraries icon on the left(looks like a ribbon). Under community Libraries search for NCD2Relay. Once you find it click the Include in App button, then select the app you just created. That should add an include statement to the top of the code.
Now search Community Libraries for SI7020-A20_CE Once you find it click the include in app button and select your app again. This will add another include statement to the top. One more, search community libraries for Blynk(make sure you select the one with the most includes as there are a few on there) click include in app and select your app. This will add a final 3rd include statement to the top.
Now go ahead and click the Verify icon at the top left(looks like a check) just to make sure everything jives. If so Great!
Now we have one final step to complete before we can flash the code to the Photon module. We need to insert the Auth code for the device in the project in the code. Remember that email or group of emails you sent yourself from the Blynk app? Open those up. You are looking for Auth Token for Device ‘Name of device’ : bunchOfLettersAndNumbers Copy that long Auth code. Now go back to build and look at the code for your project. There is a line there that says:
char blynkAuth = “”;
Paste that code you copied between the double quotes.
You’re done. You are officially a code super star. Now click that lighting bolt in the upper left corner and watch as your super spectacular code is flashed to the Photon module. You can watch the LED on the photon module flash magenta for fun letting you know the code is being flashed.
That is literally it for the hardware. From here it’s all about having fun in the Blynk app.
Blynk App Setup
Ok so open the Blynk app back up on your phone/tablet. You will want to navigate to the view of your project where you see the dot grid. Now it’s time to add some interface widgets.
For relay control it makes sense to add a button.
- Click anywhere on the dot grid and a menu will appear where you can select a widget.
- Click on the Button widget.
- New Button will appear on the interface, move it to where you see fit but generally I wait until I have all widgets added to arrange things.
- Once you have it where you want it press it to edit settings.
- Give the button a name in the top text box. Set it’s color as you desire.
- Now click the option for OUTPUT. Set it to Virtual V0(this corresponds to code in the Photon module so be sure you get that right).
- Now you can set it to PUSH or SWITCH. A switch will toggle when you press the button and Push will only turn the relay on while you have your finger on the button.
- Enter On/Off labels. The default of on and off makes sense here for a relay but enter anything you like.
- Make sure the Target is set for your device correctly which it should anyway.
- Click OK at the top
Now if you click the button at this point it will just take you back to the buttons settings because your project is currently in editing mode. Click the play button in the upper corner and the app should connect to your Photon module. You can give the button a press now to make sure everything works, but be careful not to bump your knee into the desk when you jump with excitement as the relay springs to life.
Once you’re done clicking the button on and off like a child(I know we all do it) press the stop button in the corner to put the project back to editing mode so we can get back to business.
For Relay two we follow the exact same procedure we did before for relay 1. Only this time give it an appropriate different name so you can tell the difference between the two buttons and set the OUTPUT to Virtual V1.
That’s it for the relays.
The relay board has 6 digital contact closure inputs so why not monitor them in the Blynk app? I can’t think of a good reason not to and it’s good practice for pushing information from the hardware to the Blynk app.
- Click anywhere on the dot grid on your project to bring up the widget menu.
- Scroll until you find LED and select that.
- A new LED indicator will appear on your Project dashboard. Click on it to edit settings.
- Give it a name at the top and select the color you would like for it.
- For the INPUT option select Virtual V2. Once again this corresponds to code on the board so be sure you get that right. This is for the first input on the board which is actually physically labeled there as GP2, this is because GP0 and GP1 are used to control the relays. Neat huh.
- Then click OK to go back to the project.
You can now click play on the project if you would like to test out the first input. On the board you can use a button, a switch, or just a simple jumper wire to connect one of the GND terminals on that terminal strip to GP2. Then watch as the LED indicator on Blynk lights up a beautiful color. Pretty neat huh. Ok after you’re all done playing come back and we will finish up the rest of the inputs.
Press the stop button on the Blynk project to take it back into editing mode.
Follow the exact same procedure we did for the first input 5 more times to add LED indicators for all 6 inputs.
The next inputs on the board in order will use:
In that order.
That’s it for inputs.
I added a nice little annoying part to the Particle code so any time you trip input 4 on the board it will pop a notification right to your SmartPhone. That’s super fun to play with. All you need is to add a notification widget to the project. So make sure the project is in editing mode by pressing the stop button. Now click anywhere on the grid to add a new widget. Scroll down until you find the Notification widget and click that to add it to the interface. You technically do not have to do anything at all to the notification widget settings, all the work for notifying on input trigger is done in the Photon code so go edit that if you like. I do not recommend notify when hardware goes offline as I have found that Blynk shows the device going offline quite often when it’s just not the case.
Now trip input 4 and watch as almost by magic a notification pops up on your phone. This will pop up whether you have the Blynk app open or not. The uses for this notification ability are endless.
That pretty well wraps things up. Controlling and monitoring NCD devices through the Blynk app really couldn’t get much simpler or fun. Just think of the endless possibilities when you couple them together and glue them with a Photon module. Home self monitoring security, monitoring, and control. The possibilities are endless. So have fun and be sure to support Blynk, ControlEverything, and Particle. We are all working very hard to bring control, automation, and monitoring to the masses in a simple and fun way. We also all have business level support if you decide to build the next big thing on our infrastructure/hardware.