Created
April 6, 2024 10:15
-
-
Save RajivCodeLab/4972b82cb96fa7c27b1e24d6a22ce079 to your computer and use it in GitHub Desktop.
To control the LED remotely from anywhere in the world using Raspberry Pi Pico W and DWEET.IO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment