├── README.md ├── analog_read_test.py ├── hello_flask_world.py ├── pyduino.py ├── pyduino_website.py └── templates └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # pyduino_webapi 2 | This hosts the files needed in the Controlling Arduino with python based web API (No php) 3 | 4 | Visit the instructables tutorial on how to use these codes: http://www.instructables.com/id/Controlling-Arduino-with-python-based-web-API-No-p/ 5 | -------------------------------------------------------------------------------- /analog_read_test.py: -------------------------------------------------------------------------------- 1 | from pyduino import * 2 | import time 3 | 4 | 5 | if __name__ == '__main__': 6 | 7 | print 'Establishing connection to Arduino...' 8 | 9 | # if your arduino was running on a serial port other than '/dev/ttyACM0/' 10 | # declare: a = Arduino(serial_port='/dev/ttyXXXX') 11 | a = Arduino() 12 | 13 | # sleep to ensure ample time for computer to make serial connection 14 | time.sleep(3) 15 | print 'established!' 16 | 17 | # define our LED pin 18 | PIN = 3 19 | 20 | # initialize the digital pin as output 21 | a.set_pin_mode(PIN,'O') 22 | 23 | # allow time to make connection 24 | time.sleep(1) 25 | 26 | # turn LED on 27 | a.digital_write(PIN,1) 28 | 29 | for i in range(0,1000): 30 | 31 | try: 32 | # Read the analog value from analogpin 0 33 | analog_val = a.analog_read(0) 34 | 35 | # print value in range between 0-100 36 | print 'ANALOG READ =',int((analog_val/1023.)*100) 37 | time.sleep(1) 38 | 39 | except KeyboardInterrupt: 40 | break # kill for loop 41 | 42 | # to make sure we turn off the LED and close our serial connection 43 | print 'CLOSING...' 44 | a.digital_write(PIN,0) # turn LED off 45 | a.close() 46 | -------------------------------------------------------------------------------- /hello_flask_world.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template,request, redirect, url_for 2 | 3 | app = Flask(__name__) 4 | 5 | # we are able to make 2 different requests on our webpage 6 | # GET = we just type in the url 7 | # POST = some sort of form submission like a button 8 | @app.route('/', methods = ['POST','GET']) 9 | def hello_world(): 10 | 11 | # variables for template page (templates/index.html) 12 | author = "Kyle" 13 | readval = 10 14 | 15 | # if we make a post request on the webpage aka press button then do stuff 16 | if request.method == 'POST': 17 | 18 | # if we press the turn on button 19 | if request.form['submit'] == 'Turn On': 20 | print 'TURN ON' 21 | 22 | # if we press the turn off button 23 | elif request.form['submit'] == 'Turn Off': 24 | print 'TURN OFF' 25 | 26 | else: 27 | pass 28 | 29 | # the default page to display will be our template with our template variables 30 | return render_template('index.html', author=author, value=100*(readval/1023.)) 31 | 32 | 33 | if __name__ == "__main__": 34 | 35 | # lets launch our webpage! 36 | # do 0.0.0.0 so that we can log into this webpage 37 | # using another computer on the same network later 38 | app.run(host='0.0.0.0') 39 | -------------------------------------------------------------------------------- /pyduino.py: -------------------------------------------------------------------------------- 1 | """ 2 | A library to interface Arduino through serial connection 3 | """ 4 | import serial 5 | 6 | class Arduino(): 7 | """ 8 | Models an Arduino connection 9 | """ 10 | 11 | def __init__(self, serial_port='/dev/ttyACM0', baud_rate=9600, 12 | read_timeout=5): 13 | """ 14 | Initializes the serial connection to the Arduino board 15 | """ 16 | self.conn = serial.Serial(serial_port, baud_rate) 17 | self.conn.timeout = read_timeout # Timeout for readline() 18 | 19 | def set_pin_mode(self, pin_number, mode): 20 | """ 21 | Performs a pinMode() operation on pin_number 22 | Internally sends b'M{mode}{pin_number} where mode could be: 23 | - I for INPUT 24 | - O for OUTPUT 25 | - P for INPUT_PULLUP MO13 26 | """ 27 | command = (''.join(('M',mode,str(pin_number)))).encode() 28 | #print 'set_pin_mode =',command,(''.join(('M',mode,str(pin_number)))) 29 | self.conn.write(command) 30 | 31 | def digital_read(self, pin_number): 32 | """ 33 | Performs a digital read on pin_number and returns the value (1 or 0) 34 | Internally sends b'RD{pin_number}' over the serial connection 35 | """ 36 | command = (''.join(('RD', str(pin_number)))).encode() 37 | self.conn.write(command) 38 | line_received = self.conn.readline().decode().strip() 39 | header, value = line_received.split(':') # e.g. D13:1 40 | if header == ('D'+ str(pin_number)): 41 | # If header matches 42 | return int(value) 43 | 44 | def digital_write(self, pin_number, digital_value): 45 | """ 46 | Writes the digital_value on pin_number 47 | Internally sends b'WD{pin_number}:{digital_value}' over the serial 48 | connection 49 | """ 50 | command = (''.join(('WD', str(pin_number), ':', 51 | str(digital_value)))).encode() 52 | self.conn.write(command) 53 | 54 | def analog_read(self, pin_number): 55 | """ 56 | Performs an analog read on pin_number and returns the value (0 to 1023) 57 | Internally sends b'RA{pin_number}' over the serial connection 58 | """ 59 | command = (''.join(('RA', str(pin_number)))).encode() 60 | self.conn.write(command) 61 | line_received = self.conn.readline().decode().strip() 62 | header, value = line_received.split(':') # e.g. A4:1 63 | if header == ('A'+ str(pin_number)): 64 | # If header matches 65 | return int(value) 66 | 67 | def analog_write(self, pin_number, analog_value): 68 | """ 69 | Writes the analog value (0 to 255) on pin_number 70 | Internally sends b'WA{pin_number}:{analog_value}' over the serial 71 | connection 72 | """ 73 | command = (''.join(('WA', str(pin_number), ':', 74 | str(analog_value)))).encode() 75 | self.conn.write(command) 76 | 77 | 78 | def close(self): 79 | """ 80 | To ensure we are properly closing our connection to the 81 | Arduino device. 82 | """ 83 | self.conn.close() 84 | print 'Connection to Arduino closed' 85 | -------------------------------------------------------------------------------- /pyduino_website.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template,request, redirect, url_for 2 | from pyduino import * 3 | import time 4 | 5 | app = Flask(__name__) 6 | 7 | # initialize connection to Arduino 8 | # if your arduino was running on a serial port other than '/dev/ttyACM0/' 9 | # declare: a = Arduino(serial_port='/dev/ttyXXXX') 10 | a = Arduino() 11 | time.sleep(3) 12 | 13 | # declare the pins we're using 14 | LED_PIN = 3 15 | ANALOG_PIN = 0 16 | 17 | # initialize the digital pin as output 18 | a.set_pin_mode(LED_PIN,'O') 19 | 20 | print 'Arduino initialized' 21 | 22 | 23 | # we are able to make 2 different requests on our webpage 24 | # GET = we just type in the url 25 | # POST = some sort of form submission like a button 26 | @app.route('/', methods = ['POST','GET']) 27 | def hello_world(): 28 | 29 | # variables for template page (templates/index.html) 30 | author = "Kyle" 31 | 32 | # if we make a post request on the webpage aka press button then do stuff 33 | if request.method == 'POST': 34 | 35 | # if we press the turn on button 36 | if request.form['submit'] == 'Turn On': 37 | print 'TURN ON' 38 | 39 | # turn on LED on arduino 40 | a.digital_write(LED_PIN,1) 41 | 42 | # if we press the turn off button 43 | elif request.form['submit'] == 'Turn Off': 44 | print 'TURN OFF' 45 | 46 | # turn off LED on arduino 47 | a.digital_write(LED_PIN,0) 48 | 49 | else: 50 | pass 51 | 52 | # read in analog value from photoresistor 53 | readval = a.analog_read(ANALOG_PIN) 54 | 55 | # the default page to display will be our template with our template variables 56 | return render_template('index.html', author=author, value=100*(readval/1023.)) 57 | 58 | 59 | # unsecure API urls 60 | @app.route('/turnon', methods=['GET'] ) 61 | def turn_on(): 62 | # turn on LED on arduino 63 | a.digital_write(LED_PIN,1) 64 | return redirect( url_for('hello_world') ) 65 | 66 | 67 | @app.route('/turnoff', methods=['GET'] ) 68 | def turn_off(): 69 | # turn off LED on arduino 70 | a.digital_write(LED_PIN,0) 71 | return redirect( url_for('hello_world') ) 72 | 73 | 74 | 75 | if __name__ == "__main__": 76 | 77 | # lets launch our webpage! 78 | # do 0.0.0.0 so that we can log into this webpage 79 | # using another computer on the same network later 80 | app.run(host='0.0.0.0') 81 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {{ author }}'s app 5 | 6 | 7 | 8 |

Light Sensor Reading {{ value }}!

9 | 10 |
11 |

12 |

13 |
14 | 15 | 16 | 17 | --------------------------------------------------------------------------------