Introduction: My IoT Device - First Relay

In this instructable we are going to control a relay from Blynk. Turning it on and off from the application.

BEWARE!!!!

Please make sure you know what you are doing if you intend to connect your relay to mains electricity!!!

BEWARE!!!!

Step 1: Open the Example

Go to Files/Examples/My_IoT_Device and select Relay.

Open the Blynk app and take it offline (touch the square icon in the top right corner).

If you haven't bought any extra energy units, delete the current project by touching the nut shaped icon at the top of the screen, scrolling down and selecting delete.

If you have bought extra energy units and want to add the project then touch the back button at the top left of the screen to take the current project out of focus.

Touch the QR code at the top of the screen and point the camera at the QR code above.

Once the project has loaded touch the nut icon at the top of the sceen. Scroll down and select email all.

In a matter of moments you will receive an authorisation code in your email.

Step 2: The Widgets Explained

This project just uses one widget - a push button to switch the relay on and off. We have allocated it to Virtual Slot 0

The widget is set up as a switch, turning the relay on and off. By changing it to Push it becomes a momentary switch.

Step 3: The Code

Despite this being a very sophisticated application - the code is surprisingly simple.

As in all examples you need to input your SSID, password and authentication code.

These are all found on the first tab. This project has an extra field that can be changed in the code:

Relay relay(digital0);// Which output you want the relay to use

On the Blynk tab is the main code for this application.

Step 4: The Blynk Tab

This consists of a single block of code for the push button widget . The BLYNK_WRITE(V0) instruction.

When the button is pressed it sends an integer that is either true or false (param.asInt())

When the button is switched on it sets a variable (boolean On_Off = param.asInt();)

If On_Off is true (if (On_Off) // If the Blynk switch is on)

it switches the relay on

relay.on();

otherwise it switches it off.

else if (!On_Off)// If the Blynk switch is off)
{ relay.off();

Step 5: Finishing Off

Once all that is done, upload the code to the controller and press play on the Blynk application.

Congratulations! You have now created an IoT relay application.