قالب وردپرس درنا توس
Home / Tips and Tricks / How to Control Anything with a Wi-Fi Relay Switch Using aRest «Null Byte :: WonderHowTo

How to Control Anything with a Wi-Fi Relay Switch Using aRest «Null Byte :: WonderHowTo



A relay is connected to an electrical component that works like a light switch, where it turns on or off with an electrical signal. Wi-Fi connected microcontroller like to ESP8266, you can build a connected network with Wi-Fi network 19659002] How to Electrical Relay Works

When you're ready to start working on your hands. Designed to switch on and off in response to an electrical signal.

On the inside, a relay has three power inputs: a terminal that is normally connected to power, a terminal that is normally disconnected from power, and the "common" terminal where we plug in the power we want to switch.

Image by Kody / Null Byte

When the signal input gets power, the electromagnet turns on and completes the circuit, causing the normally disconnected circuit to connect to power, as well as the normally connected terminal to disconnect from power.

Adding to ESP8266 Board

Once you have a relay ready to switch to respond to a sensor, we can control it even more precisely by communicating with it through a microcontroller. When it comes to Wi-Fi, it becomes remotely connected to the same network. The ESP8266 is a popular chipset for prototyping IoT devices in Arduino and makes it perfect for this application.

Because the ESP8266 chip is so easy to program, it can be used as a development board like the D1 Mini or the NodeMCU to combine with a power relay. These ESP8266-based boards are cheap and easy to work with, costing around $ 2.50 if purchased from Chinese suppliers online. Wi-Fi functionality to become Wi-Fi functionality to.

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

A hacker can get into the fun by programming the relay to power on a hidden device like a Raspberry Pi when needed. They could be disconnected as a firewall in the future if the hacker gets physical access to the equipment.

Using aRest for Remote Control

One of the best free platforms for controlling Arduino devices remotely is aRest. This project, written in Arduino for the ESP8266 Wi-Fi chip, hosts a REST API that allows easy communication with any pin of the microcontroller.

While the aRest project is intended to let you know that you are using the Wi-Fi network per month, there are several examples on aRest's GitHub that allow you to control a device on an unlimited amount of times for free , We'll be using the free, local network version from GitHub to try this functionality, but if you're curious about controlling your device from anywhere in the world, you can try out their free or paid cloud packages as well setup is a bit more complicated.

Once we have a device with a rest on the network, we can send digital or analog commands to any pin. To control the relay, we'll connect a signal pin from the Arduino, and then connect Wi-Fi to the connected pin. Once the pin is set to output mode, it can be used to send the pin to the pin on or off.

What you'll need

Putting this all together is surprisingly easy , You can get everything on a breadboard, but this can be done on a mini breadboard with five jumper wires, a relay, and an ESP8266-based device. For the latter, I'll use a D1 Mini, but a NodeMCU or other ESP8266-based device will work too. For the relay, it needs to be a one-channel power relay with a board.

Good Deal: Breadboard Kit with Solderless Jumper Wires, RGB LED, Resistors & More

Step 1: Install Arduino IDE

Arduino IDE (the IDE stands for "integrated development environment") allows you to quickly write and upload scripts to Arduino-like microcontroller devices. Arduino IDE from the official website

Step 2: Install the CH340G Driver (If Needed)

You need to install the CH340G driver needed to connect to the D1 Mini's USB interface, but the process is simple.

Step 3: Add the Correct Board

Once you've installed and opened, you 'will make it easier to use it. You'll need to click on the "Arduino" drop-down menu, then select "Preferences." Next, paste the following URL into the Additional Board Manager URLs field, and click "OK" to continue.

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

Next , you'll need to add the NodeMCU to the Boards Manager . To do this, you'll need to click on "Tools," then hover over the "Board" section to see the drop-down list of supported boards.

When the Boards Manager window opens, type "esp8266" into the search bar. Select "esp8266" by "ESP8266 Community," and "Install" it to add support for the D1 Mini, NodeMCU, or another ESP8266 device. Clicking "Close" to finish here.

Once this is done, you should be ready to program your ESP8266-based board. Plug your D1 Mini, NodeMCU, or similar microcontroller into your computer with the Micro-USB cable. When you click on "Tools," you should see the correct device auto-selected. If not, hover over "boards," then select either "WeMod D1 RS & mini" or "NodeMCU 1.0 (ESP-12E modules), depending on the board you have.

If you're using a bad cable, the port may not show up, so if you do not see anything after you've completed the other steps, try another cable first.

Step 4: Download the Example Code

Wi-Fi relay is the complete code to control it on its GitHub, you can find it on skickar's GitHub aside from shown below.

 / *
  This is a simple example of the aREST Library for the ESP8266 WiFi chip.
  This example illustrates the cloud part of aREST that makes the board accessible from anywhere
  See the README file for more details.

  Written in 2015 by Marco Schwartz under a GPL license and forked by Skickar
* /

// Import required libraries
#include 
#include 
#include 

// clients
WiFi client espClient;
PubSubClient client (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 to listen for incoming TCP connections
#define LISTEN_PORT 80

// Create an instance of the server
WiFi server server (LISTEN_PORT);

// functions
void callback (char * topic, byte * payload, unsigned int length);

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

  // set callback
  client.setCallback (callback);

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

  // Give name & ID to the device (ID should be 6 characters long)
  rest.set_id (device_id);
  rest.set_name ( "esp8266");

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

  // start the server
  server.begin ();
  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);

  // Handle Local aREST calls
  WiFiClient clientLocal = server.available ();
  if (! clientLocal) {
    return;
  }
  while (! clientLocal.available ()) {
    delay (1);
  }
  rest.handle (client local);

}

// Handles message arrived on subscribed topic (s)
void callback (char * topic, byte * payload, unsigned int length) {

  rest.handle_callback (client, topic, 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 will create an aRest server on your ESP8266 device, waiting 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 via the serial connection, allowing you to easily find it on the network.

Copy and paste this code into a new Arduino sketch ("File," then "New"), and select whichever board you are using. Next, create a folder with the name you want to save the program, and then save the sketch into that folder.

Now, there's only one thing you need to modify to get the script working.

Step 5: Arduino does not want to be in a folder of the same name : Modify the Code with Your Wi-Fi Credentials

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

Modify this to replace "NetworkName" and "Password" with the name and password of your Wi-Fi network. Step 6: Wire the Relay & the Esp8266 Board

Now, place your D1 Mini or NodeMCU onto your breadboard.

ESP8266's board to the ground pin of the ESP8266's board, shown below with a black wire connecting the two , So, the 3.3-volt pin of the ESP8266's board to the positive pin of the relay, shown below with a blue wire.

Image by Kody / Null byte

Now that we can control the switch, let's make a simple circuit. We can attach something like a lamp or lights here as well, but we want to keep this demo simple. We'll connect the D3 of the ESP8266 to the center, or common terminal of the relay, located below a yellow wire.

If the terminals are facing one another, the terminal to the right of center should be the "normally closed" terminal. Connect this "normally closed" terminal to LED, shown below with a red wire, and then connect the other side of the LED to the left side to ground, shown below with a black wire.

Image by Kody / Zero Byte

Now, when the signal from D2 turns on the relay, power from D3 wants to go back to the ground esp8266.

Step 7: Push the Code & Power the Device

Connect the ESP8266 board with a Micro-USB cable, and make sure you select the correct board and port in Arduino IDE.

If you get any warnings, click the check button to verify that the ESP8266 is correct about a library not being found, click on "Sketch," then "Include Library," and "Manage Libraries." Arduino says is missing.

With all this done, your ESP8266 should connect to the Wi-Fi network and ready to accept commands. Keep it Connected, because next we'll find where to send the commands.

Step 8: Get the IP Address from Serial

Now that we've pushed the code, let's take a look at how Things are going on the serial monitor. To access this, click on "Tools," then "Serial Monitor." How to Control Anything with a Wi-Fi Relay Switch Using aRest ” width=”532″ height=”532″ style=”max-width:532px;height:auto;”/>

How to Control Anything with a Wi-Fi Relay Switch Using aRest ” width=”532″ height=”532″ style=”max-width:532px;height:auto;”/>

Now that we have the IP address, 192.168.0.87 in our example,

Step 9: Arm the Output Pins

Once the ESP8266 is connected to Wi-Fi, connect to the same network on any device and open a browser window. ESP8266 has been given by the network.

 http://192.168.0.87/analog/2/1 

What is this doing? We are formatting an HTTP request for the device listening at 192.168.0.87, telling it to send an analog request to a PIN number 2 to set to an analog value of 2 out of a maximum of 255

Do you want to post a request for a request for a quote?

19659070] How to Control Anything with a Wi-Fi Relay Switch Using aRest ” width=”532″ height=”532″ style=”max-width:532px;height:auto;”/>

Now, let's put the pin value to 20 so we can tell when the light turns on.

 http://192.168.0.87/analog/2/20 

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

 http://192.168.0.87/analog/3/1 

Step 10 .This is the best way to switch the power on : Fire the Relay from a Web Browser

Alright, the moment of truth! Let's send a digital, rather than analogue, signal to the relay via pin D3 This should be the relay, making a sound when the magnet completes the connection. Your LED should turn on, because the power from the D2 pin is flowing through the relay and through it.

 http://192.168.0.87/digital/3/1 

If you would prefer to stick with analog A signal is sent to analogue value of 255 A Wi-Fi Relay Lets You Control Power to Anything

19659003] The uses for a wirelessly controlled switch range from simple home automation to remotely cut the power to a critical computer at a key moment. While wi fi jamming can interfere with these requests, the ability to make rapidey remotely operated relays anywhere Wi-Fi is available is a useful skill for any hacker. offers a dashboard to manage connected devices and automates sending commands across the Internet.

While there is a limited amount of 100 "events," or commands sent to your remote device over the internet, it's a great way to get started. Raspberry Pi hacking computer at the right moment, a relay gives you a cheap and flexible way to get started. [19659004] I hope you enjoyed this guide to controlling ESP8266 device over Wi-Fi! @KodyKinzie

Do not Miss: [PleaseNotice:Thisisanautomaticallytranslatedarticle! Using ESP8266 Beacon Spammers to Track Smartphone Users

Cover photo and screenshots by Kody / Null Byte




Source link

Share