Tim Mikkelsen's Raspberry Pi Projects
2016/06 RPi Python web LED
Description:
This project was to turn off and on an LED via the web.
A few comments:
1. This was not done with any sort of fancy web UI:
- to turn the LED on, enter the URL 10.0.1.7:1234/ledon in a web browser
- to turn the LED off, enter the URL 10.0.1.7:1234/ledoff in a web browser
2. The tools on the Raspberry Pi are IDLE and Python 2.7.
Software:
- NOOB software for the Raspberry Pi
- netatalk for the Raspberry Pi
- afpd.service for the Raspberry Pi
- IDLE on the Raspberry Pi
- Python 2.7 on the Raspberry Pi
- PyCharm for the Mac
Hardware:
- Raspberry Pi
- 330 ohm resistor
- LED
- another device with a web browser on your network
webled.py code:
import socket
import sys
import RPi.GPIO as GPIO
mysock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
try:
mysock.bind(("",1234))
except socket.error:
print("failed to bind")
sys.exit()
mysock.listen(4)
while True:
conn, addr = mysock.accept()
data = conn.recv(1000)
print(data)
if not data:
break
if 'ledon' in data:
print("LED on")
GPIO.output(18,GPIO.HIGH)
if 'ledoff' in data:
print("LED off")
GPIO.output(18,GPIO.LOW)
conn.sendall(data)
conn.close()
mysock.close()