7.6 IoT Controlled Relay ================================== In this project, we will build an IoT Relay using Raspberry Pi Pico W WiFi and control the Relay via a Web Server. We will use Thonny IDE to program the Raspberry Pi Pico W using MicroPython Code. It’s a great way to build a Wi-Fi switch to turn on and off any electrical device spending very little money. This DIY IoT Relay based on the Raspberry Pi Pico W Wi Fi + Bluetooth module can be used to control High Power Devices like Water Pumps, Air Conditioners, Heaters, and other High Ampere loads. The best thing about this IoT Relay is that it can be controlled remotely in a local network using WiFi technology. The cell phone or any web application can be used to access the Web Server. Using the web page, you can turn ON/OFF any appliances at your home. Component List ^^^^^^^^^^^^^^^ - Raspberry Pi Pico W x1 - MicroUSB cable x1 - 830 Tie-Points Breadboard x1 - Resistor 220Ω x1 - LED x1 - Relay Module x1 - Jumper Wire Several .. warning:: Modification of electrical appliances comes with great danger, do not try it lightly, please do it under the guidance of professionals. Connect ^^^^^^^^^ .. image:: img/3.connect/1.16.png This circuit is based on 1.16 Circuit Control with Relay. Code ^^^^^^^ .. note:: * Open the ``6.IOT_relay.py`` file under the path of ``Ultimate-Starter-Kit-for-Pico-W\Python\1.Project\iot`` or copy this code into Thonny, then click "Run Current Script" or simply press F5 to run it. * Don't forget to click on the "MicroPython (Raspberry Pi Pico)" interpreter in the bottom right corner. * Before running the code, you need to make sure you have do_connect.py and secrets.py scripts in your Pico W, if not please refer to 8.1 Access to the Network to create them. .. image:: img/4.software/7.6.png Click 'Run current script'. After a successful connection, the shell will display the IP address of the Pico W. You can then enter the IP address in your browser to see the webpage, where you can use the switch above to control the relay and thus turn the LED light on and off. If you want this script to be able to boot up, you can save it to the Raspberry Pi Pico W as ``main.py``. The following is the program code: .. code-block:: python from machine import Pin import network import time from secrets import * from do_connect import * import usocket as socket # Initialize network connection do_connect() # Define relay pin configuration RELAY_PIN = 15 relay = Pin(RELAY_PIN, Pin.OUT) relay.value(0) # Set initial state to OFF (0) # HTML template for web interface # Contains CSS for switch styling and JavaScript for AJAX requests HTML_TEMPLATE = """