Skip to content

Instantly share code, notes, and snippets.

@wybiral
Created September 21, 2018 21:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wybiral/ec29670abbe61a3474e4da07ecf4d92f to your computer and use it in GitHub Desktop.
Save wybiral/ec29670abbe61a3474e4da07ecf4d92f to your computer and use it in GitHub Desktop.
import pycom
import socket
pycom.heartbeat(False)
html = b'''<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Change Color</title>
<style>
*{margin:0;padding:0;border:0;width:100%;height:100%}
</style>
<script>
window.onload = () => {
const el = document.getElementById('color');
el.onchange = evt => fetch('/' + el.value.slice(1));
};
</script>
</head>
<body>
<input type="color" id="color" value="#000000">
</body>
</html>'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 80))
s.listen()
while True:
conn, addr = s.accept()
try:
request = conn.recv(1024)
lines = request.split(b'\r\n')
method, uri, version = lines[0].split(b' ')
if uri == b'/':
conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
conn.send(html)
elif uri == b'/favicon.ico':
conn.send(b'HTTP/1.1 404 Not Found\r\n\r\n')
else:
rgb = uri[1:]
r = int(rgb[:2], 16)
g = int(rgb[2:4], 16)
b = int(rgb[4:], 16)
pycom.rgbled(r << 16 | g << 8 | b)
conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
except:
conn.send(b'HTTP/1.1 500 Internal Server Error')
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment