Skip to content

Instantly share code, notes, and snippets.

@itsjfx
Created April 18, 2025 13:54
Show Gist options
  • Save itsjfx/647cb9cb142cb4eb3233214ec6a50229 to your computer and use it in GitHub Desktop.
Save itsjfx/647cb9cb142cb4eb3233214ec6a50229 to your computer and use it in GitHub Desktop.
pico micropython HTTP server to ground a pin on request
import network
import socket
from picozero import pico_led
import machine
import utime
# GPIO2
switch_pin = machine.Pin(2, machine.Pin.OUT, value=1)
ssid = 'ssid'
password = ''
def trigger_switch():
# "press" the button (short to ground)
switch_pin.value(0)
utime.sleep(0.1)
# "release" the button (disconnect)
switch_pin.value(1)
def open_socket(ip):
# Open a socket
address = (ip, 80)
s = socket.socket()
s.bind(address)
s.listen(1)
return s
def connect():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
pico_led.on()
utime.sleep(0.2)
pico_led.off()
utime.sleep(0.2)
ip = wlan.ifconfig()[0]
pico_led.on()
return ip
def serve(s):
while True:
conn, addr = s.accept()
# request = conn.recv(1024).decode()
trigger_switch()
response = ['HTTP/1.1 200 OK', 'Content-Type: text/plain', '', 'yo yo']
conn.send('\r\n'.join(response))
conn.close()
utime.sleep(0.1)
ip = connect()
s = open_socket(ip)
serve(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment