Paso 4: Integración de Pyduino con frasco
Ahora que verificamos que nuestro circuito está configurado correctamente y que nuestra primera página web funciona, es hora de añadir el pyduino comandos que controlan nuestro Arduino en la página web del frasco! Vamos a añadir unos pocos comandos de python a nuestro programa de hello_flask_world.py anterior. Adelante, copie el programa hello_flask_world.py en un nuevo archivo llamado pyduino_website.py
from flask import Flask, render_template,request, redirect, url_forfrom pyduino import * import timeapp = Flask(__name__)# initialize connection to Arduino # if your arduino was running on a serial port other than '/dev/ttyACM0/' # declare: a = Arduino(serial_port='/dev/ttyXXXX') a = Arduino() time.sleep(3)# declare the pins we're using LED_PIN = 3 ANALOG_PIN = 0# initialize the digital pin as output a.set_pin_mode(LED_PIN,'O')print 'Arduino initialized'# we are able to make 2 different requests on our webpage # GET = we just type in the url # POST = some sort of form submission like a button methods = ['POST','GET']) def hello_world(): # variables for template page (templates/index.html) author = "Kyle" # if we make a post request on the webpage aka press button then do stuff if request.method == 'POST': # if we press the turn on button if request.form['submit'] == 'Turn On': print 'TURN ON' # turn on LED on arduino a.digital_write(LED_PIN,1) # if we press the turn off button elif request.form['submit'] == 'Turn Off': print 'TURN OFF' # turn off LED on arduino a.digital_write(LED_PIN,0) else: pass # read in analog value from photoresistor readval = a.analog_read(ANALOG_PIN) # the default page to display will be our template with our template variables return render_template('index.html', author=author, value=100*(readval/1023.))if __name__ == "__main__": # lets launch our webpage! # do 0.0.0.0 so that we can log into this webpage # using another computer on the same network later app.run(host='0.0.0.0')
Vamos a ejecutar el programa y ver lo que conseguimos!!!! ¡ Mira el video de arriba para lo que debe que tienes!