قالب وردپرس درنا توس
Home / Tips and Tricks / How to check something with a Wi-Fi relay switch using aRest «Zero Byte :: WonderHowTo

How to check something with a Wi-Fi relay switch using aRest «Zero Byte :: WonderHowTo



A relay is an electrical component that acts as a light switch, where it is switched on or off with an electrical signal. By connecting a relay to a Wi-Fi connected microcontroller like an ESP8266, you can build a connected switch that can be controlled from the browser by a device connected to the same Wi-Fi network – all for just a few dollars. 19659002] How an Electric Relay Works

When you want to turn a device on or off without being present, an electrical relay can take the work off your hands. Designed to turn on and off in response to an electrical signal, a relay can be used to control the power supply to almost any device. This means that a relay can be combined with inputs like a motion sensor or microcontroller to make it react to the environment or commands.

On the inside, a relay has three inputs: a terminal which is normally connected to current, a terminal which is normally disconnected from the current and the "common" terminal where we connect the power we want to change. An additional three inputs are used to connect the relay to a power source and signal used to turn on and off an electromagnet within the relay.

Picture of Kody / Null Byte

When the signal is input, the electromagnet is turned on and terminates the circuit, causing the normally connected current to be switched to the power, as well as the normally connected terminal for disconnecting the power. By choosing which terminal we plug in we can decide if we want our switch to be normally connected or disconnected before sending a signal to change.

Adding an ESP8266 Board

When we have a relay that can respond to a sensor, we can control it even more precisely by communicating with it via a microcontroller. When the microcontroller we connect to it also has the ability to connect to Wi-Fi, it becomes possible to use the relay from a device connected to the same network. The ESP8266 is a popular chipset for prototyping IoT devices in Arduino and happens to be perfect for this application.

Since the ESP8266 chip is so easy to program, we can choose a development table such as the D1 Mini or NodeMCU to combine with a power relay. These ESP8266-based boards are cheap and easy to work with and cost about $ 2.50 when purchased from Chinese online suppliers. If you are willing to wait for overseas delivery, entire home automation projects will be relatively inexpensive to add Wi-Fi functionality to.

At Amazon: D1 Mini V3.1.0 Wi-Fi IoT Development Board

A hacker can get into the fun by programming the relay into power on a hidden device like a Raspberry Pi when needed. They can also disconnect a critical device as a firewall in the future if the hacker gets physical access to the equipment. This feature can even be extended so that the relay can be switched on and off from across the internet, not just a common Wi-Fi connection.

Using aRest for Remote Control

One of the best free platforms for controlling remote Arduino devices is aRest. This project, written in the Arduino for the ESP8266 Wi-Fi chip, hosts a REST API that enables easy communication with any microcontroller. That means you can add a code to your ESP8266 device, find it on your Wi-Fi network, and then send commands to it from any browser connected to the same Wi-Fi network.

While the aRest project is meant to let you do this from anywhere on the internet and also includes a free version with a limited amount of "events" per month. There are several examples of aRest's GitHub that let you control a device on your local network an unlimited amount of times for free. We use the free local network version from GitHub to try this functionality, but if you are curious about controlling your device from all over the world, you can also try their free or paid cloud packages, even if the installation is a little more complicated.

When we have a device with aRest on the network, we can send digital or analog commands to any pin. To control the relay, we connect a signal pin from the Arduino and then connect the Wi-Fi to connect the connected pins by setting it to the initial position with an analog command. When the pin is set to the initial position, we can send the digital commands to enable and disable the relay by setting the pin to 1 or 0.

What you need

Putting this together is surprisingly simple. You have to tie everything together on a bread board, but it can be done on a mini-board with five zippers, a relay and an ESP8266-based unit. For the latter, I use a D1 Mini, but a NodeMCU or other ESP8266-based device will also work. For the relay, it needs a single-channel power relay with a board.

Here's a list of what I stopped using:

Step 1: Install Arduino IDE

Arduino IDE (IDE stands for "Integrated Development Environment") lets you quickly write and upload scripts to Arduino-like microcontroller devices. You can download the free Arduino IDE platform from the official website.

Step 2: Install the CH340G Driver (if needed)

You must install the CH340G driver needed to connect to the D1 Mini's USB interface, but the process is quite simple. To make it easier, use the links below to jump directly to your required version, which are the latest versions of the writing:

Step 3: Add the right board

Once installed and opened, you must click on the "Arduino" drop-down menu, then select "Preferences". Then paste the following URL into the field Additional Control Addresses and click "OK" to continue.

  http://arduino.esp8266.com/stable/package_esp8266com_index.json 

Next, you need to add NodeMCU to Board Manager . To do this you have to click on "Tools" and then turn over the "Board" section to see the drop-down menu with supported boards. At the top, click on "Boards Manager" to open the window that allows us to add more boards.

When Boards Manager ] opens, type "esp8266" in the search field. Select "esp8266" with "ESP8266 Community" and "Install" to add support for the D1 Mini, NodeMCU or other ESP8266 device. Click "Close" to complete here.

When this is done, you should be ready to program your ESP8266-based card. Connect your D1 Mini, NodeMCU or similar microcontroller to your computer with the Micro USB cable. When you click on "Tools", you should see the correct device selected automatically. If not, turn over the "Board", then choose either "WeMod D1 RS & mini" or "NodeMCU 1.0 (ESP-12E module), depending on the board you have.

If you are using a bad cable, the port may not appear, so if you do not see anything after completing the other steps, try another cable first.

Step 4: Get the sample code

In the code box below is the complete code for controlling the Wi-Fi relay. The code is adapted from examples of aRest GitHub. While you cannot find the exact code below on its GitHub you can find it on sent GitHub except below.

  / *
This is a simple example of the aREST library for the ESP8266 WiFi chip.
This example illustrates the part of aREST that makes the board available from anywhere
See the README file for more information.

Written 2015 by Marco Schwartz under a GPL license and proclaimed by Sends
* /

// Import obligatory libraries
#include 
#include 
#include 

// Clients
WiFiClient espClient;
PubSubClient clients (espClient);

// Create aREST instance
aREST rest = aREST (client);

// Unique ID to identify the device for cloud.arest.io
char * device_id = "unique_device_id";

// WiFi parameters
const char * ssid = "NetworkName";
const char * password = "Password";

// Variables to be exposed to the API
int temperature
int humidity
String local_ip = "";

// The port for listening to incoming TCP connections
#define LISTEN_PORT 80

// Create an instance of the server
The WiFiServer server (LISTEN_PORT);

// Functions
void callback (char * subject, byte * payload, unsigned int length);

void setup (void)
{
// Start Serial
Serial.start (115200);

// Enter callback
client.setCallback (callback);

// Init variables and expose them to the REST API
temperature = 24;
humidity = 40;
variable ("temperature" and temperature);
var.variabel ("humidity", and humidity);
rest.variable ("local_ip", & local_ip);

// Enter the name and ID of the device (ID should be 6 characters long)
rest.set_id (DEVICE_ID);
rest.set_name ("esp8266");

// Connect to WiFi
WiFi.start (ssid, password);
while (WiFi.status ()! = WL_CONNECTED) {
delay (500);
Serial.print ("");
}
Serial.println ("");
Serial.println ("WiFi connected");

// Start the server
server.start ();
Serial.println ("Local server started on IP:");

// Print the IP address
Serial.println (WiFi.localIP ());
local_ip = ipToString (WiFi.localIP ());

}

void loop () {

// Connect to the cloud
rest.handle (client);

// Manage local aREST calls
WiFiClient clientLocal = server.available ();
to (! clientLocal) {
return;
}
while (! clientLocal.available ()) {
delay (1);
}
rest.handle (clientLocal);

}

// Handle message arrived on cartoon subject (s)
void callback (char * subject, byte * payload, unsigned int length) {

rest.handle_callback (client, subject, payload, length);

}

// Convert IP address to String
String ipToString (IPAddress address)
{
return string (address [0]) + "." +
String (address [1]) + "." +
String (address [2]) + "." +
String (address [3]);
} 

This creates an aRest server on your ESP8266 device and waits for HTTP commands on port 80 at the IP address assigned by the router after connecting to your Wi-Fi network. In addition, the code will output the IP address through serial connection, so you can easily find it on the network.

Copy and paste this code into a new Arduino sketch ("File", then "New") and select which board you use. Then create a folder with the name you want to save the program and then save the sketch in that folder. Arduino cannot work with the sketch until you put it in a folder of the same name.

Now there is only one thing you need to change in order for the script to work.

Step 5: Change the code with your Wi-Fi references

Take a look at the part of the code that looks like this.

  // WiFi parameters
const char * ssid = "NetworkName";
const char * password = "Password"; 

Change this to replace "NetworkName" and "Password" with the name and password of your Wi-Fi network. When this is done, the code should allow the ESP8266 device to connect to the Wi-Fi network and allow you to send commands.

Step 6: Wire Relay & Esp8266 Board

Now place your D1 Mini or NodeMCU on your breadboard. We need to make some connections to make everything work.

To operate the relay, we connect the ground pin to the ESP8266's control to the ground pin of the relay, as shown below with a black cable connecting the two. In addition, the 3.3-volt pin of the ESP8266 board is the positive pin of the relay, as shown below with a blue wire. To control the relay on and off, we switch to pin D2 to the relay signal pin, as shown below in orange.

Picture of Kody / Null Byte

Now that we can control the switch, let's make a simple circuit. We can also attach something like a lamp or lamps here, but we want to keep this demo simple. We connect pin D3 to ESP8266 to the center, or common contact for the relay, see below with a yellow wire.

If the terminals are turned upside down and the writing on top of the relay facing you terminal to the right of the center should be the "normally closed" terminal. Connect this "normally closed" connector to an LED, seen below with a red wire, and then connect the other side of the LED to the open needle you left next to the ground, below with a black wire.

Picture of Kody / Null Byte

When the signal from D2 turns on the relay, the current from D3 will flow to the LED lamp and back to the ground pin on the ESP8266. With everything wired, it's time to send our code to the device.

Step 7: Press the code and turn on the device

Connect the ESP8266 card with a Micro-USB cable and make sure you select the correct board and port in the Arduino IDE. Click the check box to verify, and if the code is compiled successfully, click the arrow button to send the code to ESP8266.

If you get any warnings If a library is not found, click "Sketch", "Include Library" and "Manage Library". This should open the library search, where you can enter any library that Arduino says is missing. After installing the correct library, the code should be compiled and sent to ESP8266.

With all this, your ESP8266 should connect to the Wi-Fi network and be ready to accept commands. Keep it connected, for the next we will find where to send the commands.

Step 8: Download the IP address from serial

Now that we have printed the code, let's take a look at how things go on serial display. To access this, click "Tools" and then "Serial Display." Set the baud rate to 115200, and you should see a message similar to what is shown in the screenshot below if the device is successfully connected to Wi-Fi.

Now that we have the IP address 192.168.0.87 in our example, we can begin issuing commands to turn on the pins to activate the relay.

Step 9: Arm Output Pin

When the ESP8266 is connected to Wi-Fi, connect to the same network on any device and open a browser window. Type the following command, with the IP address changed to match the one your ESP8266 has received from the network.

  http://192.168.0.87/analog/2/1 

What does this do? We format an HTTP request for the device to listen to 192.168.0.87 and say that we send an analog request to pin number 2 to enter an analog value of 2 of at most 255. This turns the power on to a low level and sets the pin to output mode.

Do this on the pin so that we can send the digital inquiries, which will turn the pin completely or completely. We will get an answer as below.

Now let's set the pin value to 20 so we can see when the lamp lights up. In a browser window, type the following in the browser's IP address.

  http://192.168.0.87/analog/2/20 

The lamp should not be turned on, but if it does, it is because you connected the "normally connected" terminal instead of the "usually connected" " one. Now, let's put on the relay. This time, we send an analog signal to pin D3, which controls the relay.

  http://192.168.0.87/analog/3/1 

We should see an answer like this.

With this, the relay should be ready to change the current we just started from pin D2 to the LED lamp with a last command.

Step 10: Turn off the relay from a browser

Okay, the moment of truth! Let's send a digital, rather than analogue, signal to the relay via pin D3 to turn it all the way. This should go around the relay, making a click sound when the magnet completes the connection. Your LED should be turned on because the current from the D2 pin now flows through the relay and through it.

  http://192.168.0.87/digital/3/1 

If you prefer to stick to analog signals that send an analog value of 255, they will do the same, but to connect a relay to or off, a digital signal tends to be easier.

A Wi-Fi relay allows you to control power to something [19659003] The use of a wireless controlled switch ranges from simple home automation to remotely connecting the power to a critical computer. While Wi-Fi interference may interfere with these requests, the ability to re-install remote-controlled relays anywhere in Wi-Fi is available, which is a useful capability for all hackers. If access to your device from anywhere with Internet access is more your style, aRest offers a dashboard to manage connected devices and automate sending commands over the internet.

While there is a limited amount of 100 free "events" or commands sent to your remote device over the Internet, it is a great way to get started steering a device you do anywhere. Whether you want to use your relay to turn on your lights when your ESP8266 discovers your phone is at home or activates your secret Raspberry Pi hacking computer at that moment, a relay provides a cheap and flexible way to get started. [19659004] I hope you had this guide to control an electrical relay with an ESP8266 device via Wi-Fi! If you have any questions about this guide about connecting relays to the Internet or having a comment, please reach me below in the comments or on Twitter @ KodyKinzie .

Don't miss: Use an ESP8266 Beacon Spammer to track Smartphone Users

Cover photo and screenshots of Kody / Zero Byte




Source link