Controlling Relays Over WiFi

by brlabelectronics in Circuits > Wireless

1038 Views, 2 Favorites, 0 Comments

Controlling Relays Over WiFi

IMG_1358.jpg

Introduction:

In this Instructable you will learn how to control relays over Wifi using the Blynk App on your smart phone.

Thus opening you up to the world of IoT devices.

Supplies

The supplies that you will need are:

- Particle Photon Relay Shield (https://www.tindie.com/products/brlab/particle-pho...

- Particle Photon (https://www.amazon.com/gp/product/B012D6UYTA/ref=p...

- Smart Phone

- Blynk App

- Internet Connection Via Wifi

Place the Photon Onto the Relay Shield

IMG_1366 1.jpg

Place the Photon on the designated headers as shown in the photo.

Power the Relay Shield

IMG_1367 1.jpg

Power the Relay Shield with either a 2.1mm barrel power jack (same as the one on the Arduino Uno). Or it can be powered by wiring a power supply to the 2 position screw terminal next to the barrel jack. A maximum voltage supply of 12Vdc is recommended. This will supply all of the power needed for the board. Once power is supplied to the board a red led will light up on the power indicating that the board has power and the led on the Photon will also light up and flash different colors as it is connecting to your internet. Be sure the Photon is already connected to your WiFi and the Particle Cloud. If not a great tutorial to do so can be found in the link below.

https://docs.particle.io/quickstart/photon/#:~:tex...

Create the Blynk App

IMG_1359.PNG
IMG_1360.PNG
IMG_1363.PNG
IMG_1361.PNG
IMG_1362.PNG
IMG_1365.PNG
IMG_1364.PNG

Open the Blynk App up and create a profile.

To start creating the app click on create new project.

Give the Project a name and select the device, scroll till you find the Particle Photon option. Select WiFi and click on create project. When you create the project an authorization code will be sent to your email. This code will be needed later.

Place 4 buttons on the screen as shown. Open each one to change the settings and to give them a name.

Name them relay 1, relay 2, and so forth. Under each button a pin needs to be assigned so that it can be identified in the code for the Photon later. For each button a virtual pin needs to be used, starting with V0 for relay 1 and so forth. Make sure that for each button the mode is set to switch so that when the button is pressed it stays in that state.

To run the app press the play button. Notice that when the buttons say OFF/ON to indicate the button state, thus will indicate the relay state.

Photon Code

The next step is write the code that will actually run on the Photon and tell the relays to physically switch states.

The source code can be found here:

https://github.com/brlabelectronics/BRPPRSV00/blob...

Login into you Particle Account on your computer and click on Console. Open your Web IDE. The source code can be directly copy and pasted into your Web IDE.

But lets break the code down and see what it all means.

// This #include statement was automatically added by the Particle IDE.

#include<blynk.h>

#define BLYNK_PRINT Serial // Set serial output for debug prints//

#define BLYNK_DEBUG // Uncomment this to see detailed prints

// You should get Auth Token in the Blynk App.

char auth[] = "Enter Your Auth Code Here"; // enter blynk auth code - this is where you will enter the authorization code that was emailed to you.

const int relayPins[] = {4, 5, 6, 7}; - declares which digital i/o pins the relays are connected to

const int relay1 = 4; - tells the photon that relay 1 is connected to digital pin 4

const int relay2 = 5;

const int relay3 = 6;

const int relay4 = 7;

volatile int relayState1 = LOW; // Blynk app pushbutton status; intial state of the relay - sets the initial state of the relay

volatile int relayState2 = LOW; // Blynk app pushbutton status; intial state of the relay

volatile int relayState3 = LOW; // Blynk app pushbutton status; intial state of the relay

volatile int relayState4 = LOW; // Blynk app pushbutton status; intial state of the relay

Next the setup stage of the code runs, this part of the code only runs once.

void setup() {

for(int i = 0; i < 4 ; i++) { - this for loop steps through the code below for each relay

pinMode(relayPins[i], OUTPUT); - sets each digital pin as an output

digitalWrite(relayPins[i], LOW); } - sets each digital pin low, off state

Serial.begin(9600); // start the serial monitor - begins the serial monitor for debugging

delay(1000); - delays for 1 second before moving onto the next section of code

Blynk.begin(auth);

}

The loop function runs continuously

void loop() {

Blynk.run(); - runs the blynk application
}

BLYNK_WRITE(V0){

if ( param.asInt() != relayState1 ) {

- param.asInt() means: assigning incoming value from pin V0 to a variable in this case relayState

relayState1 = !relayState1; // Toggle state. - toggles the state of the relay

digitalWrite( relay1, relayState1 ); // Relay control pin.

}

}
BLYNK_WRITE(V1){

if ( param.asInt() != relayState2 ) {

// param.asInt() means: assigning incoming value from pin V0 to a variable in this case relayState relayState2 = !relayState2; // Toggle state.

digitalWrite( relay2, relayState2 ); // Relay control pin.

}

}

BLYNK_WRITE(V2){

if ( param.asInt() != relayState3 ) {

// param.asInt() means: assigning incoming value from pin V0 to a variable in this case relayState relayState3 = !relayState3; // Toggle state.

digitalWrite( relay3, relayState3 ); // Relay control pin.

}

}

BLYNK_WRITE(V3){

if ( param.asInt() != relayState4 ) {

// param.asInt() means: assigning incoming value from pin V0 to a variable in this case relayState

relayState4 = !relayState4; // Toggle state.

digitalWrite( relay4, relayState4 ); // Relay control pin.

}

}

Have Fun!

You now have a functioning IoT device that you can use to control whatever your heart desires.

For questions please feel free to email brlabelectronics@gmail.com for help, we'll do our best to answer your questions! Also for cool projects and product please follow us on Instagram @brlabelectronics.

Have fun and happy making!