# Appunti Raspberry/Python <abstract> Esempi di codice Python per Raspberry Pi </abstract> ## Pinout Raspberry Pi 2 <img src="./rp2_pinout.png"> ## blinker.py import RPi.GPIO as GPIO import time led=2 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(led, GPIO.OUT) while True: GPIO.output(led, True) time.sleep(1) GPIO.output(led, False) time.sleep(1) ## blinker2.py import RPi.GPIO as GPIO import time led=2 switch=3 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(led, GPIO.OUT) GPIO.setup(switch, GPIO.IN,pull_up_down=GPIO.PUD_UP) while True: if GPIO.input(switch): GPIO.output(led, True) time.sleep(1) GPIO.output(led, False) time.sleep(1) else: GPIO.output(led, False) time.sleep(0.1) ## event.py import RPi.GPIO as GPIO import time led=2 switch=3 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(led, GPIO.OUT) GPIO.setup(switch, GPIO.IN,pull_up_down=GPIO.PUD_UP) def my_callback(channel): if GPIO.input(channel): print "Rising edge detected" else: print "Falling edge detected" GPIO.add_event_detect(switch, GPIO.BOTH, callback=my_callback) while True: GPIO.output(led, True) time.sleep(1) GPIO.output(led, False) time.sleep(1) ## pulse.py import RPi.GPIO as GPIO import time led=2 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(led, GPIO.OUT) pwm = GPIO.PWM(led, 50) while True: for i in range(100): time.sleep(0.01) pwm.start(i) for i in range(100,0,-1): time.sleep(0.01) pwm.start(i) ## webserver.py from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer PORT_NUMBER = 80 class myHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write("Hello World !") return try: server = HTTPServer(('', PORT_NUMBER), myHandler) print 'Started httpserver on port ' , PORT_NUMBER server.serve_forever() except KeyboardInterrupt: server.socket.close() ## remote.py from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer import RPi.GPIO as GPIO import urlparse PORT_NUMBER = 80 led=2 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(led, GPIO.OUT) pwm = GPIO.PWM(led, 50) class myHandler(BaseHTTPRequestHandler): def do_GET(self): parsed_path = urlparse.urlparse(self.path) if parsed_path.path=="/": self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write(""" <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <h1>Controllo luce</h1> <br/> <input type="range" min="0" max="100" value="0" class="slider" id="myRange"> <script> var slider = document.getElementById("myRange"); var xhttp = new XMLHttpRequest(); slider.oninput = function() { xhttp.open("GET", "/send?value=" + this.value, true); xhttp.send(); } </script> </body> </html> """) return if parsed_path.path=="/send": value=urlparse.parse_qs(parsed_path.query).get('value',None) pwm.start(float(value[0])) self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write("Ok"); return try: server = HTTPServer(('', PORT_NUMBER), myHandler) print 'Started httpserver on port ' , PORT_NUMBER server.serve_forever() except KeyboardInterrupt: server.socket.close() @include='bio_sergio_tanzilli'
2018 Ⓒ TanzoLab