RajivCodeLabRajiv Sharma
Published © GPL3+

How to Control LED from Internet using Raspberry Pi Pico W

In this step by step tutorial on how to control LED from Internet we will control from dweet. io website, which is free and very easy to do.

IntermediateProtip2 hours557
How to Control LED from Internet using Raspberry Pi Pico W

Things used in this project

Hardware components

Raspberry Pi Pico W
Raspberry Pi Pico W
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1
LED (generic)
LED (generic)
×1
Resistor 475 ohm
Resistor 475 ohm
It can be 1000 ohm also
×1
Male/Male Jumper Wires
×2

Software apps and online services

Thonny IDE

Story

Read more

Schematics

Connection Diagram

Code

remote_controlled_led.py

Python
import network
import sys
import time
import requests
import json
from machine import Pin

wlan = network.WLAN(network.STA_IF)
led = Pin(0, Pin.OUT)

def connect_to_wifi():
    wlan.active(True)
    for _ in range(10):
        if not wlan.isconnected():
            print("Connecting to the network...")
            wlan.connect("YOUR_SSID", "YOUR_PASSWORD") # Make sure to provide your WiFi credentials here
            time.sleep(2)
            
        if not wlan.isconnected():
            print("Cannot connect to the WiFi, please check your credentials")
            sys.exit(0)
    print("Connected to IP: ", wlan.ifconfig()[0])
    


def call_api():
    if wlan.isconnected() == True :
        response = requests.get("https://dweet.io:443/get/latest/dweet/for/led_control")
        data = json.loads(response.content)
        led_command = data["with"][0]["content"]["value"]
        print(led_command)
        led.value(led_command)
        
connect_to_wifi()
call_api()

while True:
    if wlan.isconnected() == True:
        call_api()
    else:
        print("Something went wrong, please try again")
        sys.exit(0)
    time.sleep(1)
    

Credits

RajivCodeLab
7 projects • 4 followers
Creates YT videos on DIY IoT Projects: Raspberry Pi Pico, Raspberry Pi Zero, Arduino, ESP32,
Contact
Rajiv Sharma
18 projects • 71 followers
Having more than 10 years of experience in IoT and software technology. Founded IoTBoys to share knowledge with IoT enthusiasts.
Contact

Comments

Please log in or sign up to comment.