Arduino Uno R4 WiFi control Relay via Web
In this tutorial, we'll learn how to control a relay using a web interface accessible through a browser on a PC or smartphone. We'll be using the Arduino Uno R4 WiFi board, which will be programmed to function as a web server. Let's assume that the IP address of the Arduino Uno R4 WiFi is 192.168.0.2. Here's how it works:
- To access the web interface, simply enter 192.168.0.2 in the address bar of your web browser. This will send a request to the Arduino, and in response, the Arduino will provide a web page containing an on/off button for controlling the relay.
- Similarly, if you click the RELAY ON button on the web page or enter 192.168.0.2/relay1/on in the web browser, the Arduino will activate the relay and respond with the updated web page.
- Likewise, if you click the RELAY OFF button on the web page or enter 192.168.0.2/relay1/off in the web browser, the Arduino will deactivate the relay and respond with the updated web page.
By connecting a relay to devices like a solenoid lock, light bulb, LED strip, motor, or actuator, we can control them through a web interface.
The tutorial provides a foundation that you can easily and creatively customize to accomplish the following:
- Control multiple relays through a web interface.
- Redesign the web user interface (UI) to suit your preferences.
Hardware Required
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some links direct to products from our own brand, DIYables.
About Relay and Arduino Uno R4
If you do not know about Arduino Uno R4 and relay (pinout, how it works, how to program ...), learn about them in the following tutorials:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino Code
Quick Steps
- If this is the first time you use Arduino Uno R4, see how to setup environment for Arduino Uno R4 on Arduino IDE.
- Copy the above code and open with Arduino IDE
- Change the wifi information (SSID and password) in the code to yours
- Click Upload button on Arduino IDE to upload code to Arduino
- Open the Serial Monitor
- Check out the result on Serial Monitor.
- You will see an IP address, for example: 192.168.0.2. This is the IP address of the Arduino Web Server
- Open a web browser and enter one of the three formats below into the address bar:
- Please note that the IP address may be different. Make sure to check the current value on the Serial Monitor.
- Additionally, you will observe the following output on the Serial Monitor.
- Check the relay state
- You will see the web page of Arduino board on the web browser as below:
Now, you have the capability to control the relay's on/off state through the web interface. You can also easily and creatively customize the code to accomplish the following:
- Control multiple relays through a web interface.
- Redesign the web user interface (UI) to suit your preferences.
If you desire to enhance the web page's appearance with an impressive graphic user interface (UI), you can refer to the Arduino - Web Server tutorial for inspiration and guidance.