This post will cover how to control two relays remotely from the Internet. This kind of project will allow you to easily turn on and off a light from the internet, open a garage door, open/close blinds, and so on. This This is a very easy project that can be done with a few lines of codes. Will help to know how to define input parameters to the device.
Hardware
In this sample project it is used a Texas Instrument CC3200 board for controlling the relays from the Internet. This chip integrates both the MCU and the Wireless capabilities, similar to the widely known ESP8266. You can use any other MCU as the code for controlling the relays will be exactly the same. The relays are generic ones that can switch up to 10A 250VAC
. The relays are connected to pin 5
and 6
of the board, that appears as P61
and P59
in the silk screen. VCC from relays board is connected to 5V
near to SW3
button. Check this picture for the pinout reference.
Software
For programming the CC3200 board easily it is required the Energia IDE (0101E0016 as of this writing). Notice that you need to set some jumpers in the board to be able to program it directly from the IDE. Checkout this page for reference. For more information about using this board with the Energia IDE, please visit this other page.
It is also required to install the thinger.io libraries in the Energia libraries folder (normally located under your Documents folder). You can take a look on how to install the libraries for Arduino in this link, but Energia is a bit outdated, so you will need to do it manually. Please, download the source code library from Github, after decompressed rename the library folder name to thinger
. Then enter in the thinger
folder and copy all the contents located in the subfolder src
into the parent directory. You can delete the src
subfolder. Now copy the whole thinger
folder in your Energia libraries. These steps are required as Energia does not support the latest Arduino library structures.
Picture
Energia Sketch
#include <WiFi.h>
#include <ThingerWifi.h>
#define USERNAME "your_username"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"
#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_password"
#define RELAY_1 5
#define RELAY_2 6
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
thing.add_wifi(SSID, SSID_PASSWORD);
thing["relay_1"] << [](pson& in){
digitalWrite(RELAY_1, in ? HIGH : LOW);
};
thing["relay_2"] << [](pson& in){
digitalWrite(RELAY_2, in ? HIGH : LOW);
};
}
void loop() {
thing.handle();
}
Web Control
Once you enter in your device API you will see two different resources for controlling your relays, which are relay_1
and relay_2
. You can open this resources and swith the on/off button and click on Run
to set the relays states