Arduino Python Serial Mysql Update
First I'll point you in the right direction for installing the required Python libraries. First you'll need to install the pySerial library. Simply put, the pySerial library allows your Python script to talk with the serial port in which the Arduino is connected. You can kind of think of it as a stream connecting the Arduino code to the Python code (insert other silly analogies here). You can download the pySerial library here: 2. For mac or linux users, download the file and extract it. Open terminal and cd into the extracted folder and run the following command: python setup.py install This will install the pySerial package.
I want to continuously insert arduino serial Data in MySQL using Python. I´ve already wrote a code. But only the first value is writing in mysql, by the second value. Interface Arduino to MySQL using Python. The pySerial library allows your Python script to talk with the serial port in which the Arduino is.
(screen shot below) Next, we will install the library to allow Python to talk with MySQL called MySQLdb. I just want to note, this step can be very annoying, but very rewarding once completed. I have included a guide for you to follow, but I recommend you have MySQL, python, and XCode(or the latest GCC) installed before you try and install MySQLdb. Download the library from source forge: 2.
If you're lucky enough, you should just be able to download it, extract it, open Terminal, cd into the folder and run python setup.py install, just as you did before. If this works, you're awesome and you should awesome, but if not, this guide should help. Note, I had to do step 6 before step 3. Now that we have the code running on our Arduino, we need some Python code to talk to it, but first we need a MySQL database and table to store this data. Our Arduino is reading the temp/humidity data every second and writing it with Serial.print.
So we're going to write some Python to grab this data and insert it into some MySQL. First, I'll create a simple MySQL table to store this data. Create table weatherData ( weatherDataID int(11) AUTOINCREMENT NOT NULL, humidity decimal(4,2) NOT NULL, tempC decimal(4,2) NOT NULL, constraint weatherDataPK primary key (weatherDataID) ); This table is simple enough, just going to store the humidity and temperature reading that I'm getting from the Arduino.
Attached is a screen shot of me setting up this database using the mysql command line. Here's a wonderful guide to refresh your memory on the process if need be (I know I reference it monthly). Alright, fewf, now we've got our Arduino ready and a database all prepared for our data. Last step is to write the Python to get this data and insert it into our database. #!/usr/bin/python import serial import MySQLdb #establish connection to MySQL. You'll have to change this for your database.
DbConn = MySQLdb.connect('localhost','databaseusername','password','databasename') or die ('could not connect to database') #open a cursor to the database cursor = dbConn.cursor device = '/dev/tty.usbmodem1411' #this will have to be changed to the serial port you are using try: print 'Trying.' ,device arduino = serial.Serial(device, 9600) except: print 'Failed to connect on',device try: data = arduino.readline #read the data from the arduino pieces = data.split(' t') #split the data by the tab #Here we are going to insert the data into the Database try: cursor.execute('INSERT INTO weatherData (humidity,tempC) VALUES (%s,%s)', (pieces0,pieces1)) dbConn.commit #commit the insert cursor.close #close the cursor except MySQLdb.IntegrityError: print 'failed to insert data' finally: cursor.close #close just incase it failed except: print 'Failed to get data from Arduino!' Okay, so hopefully this is relatively understandable from the comments. The real important parts to note are to make sure you configure the connection to be specific to your data for your database (i.e. Username/password/database name). Secondly, you're going to want to change the device=' line to point to the usb serial port that you are using.
Once you configure this script as you needed, you should see the data being populated in your MySQL table when you run the script. Here's an example below of what mine is populating like (see image).
Arduino /python Serial.com Handshake
Well that's about it! Hopefully you're all set up an good to go now. You should be able to do a number of cool things now with this basis, and I hope you have some fun with it. Go put this data on your website or do whatever your heart desires! Thanks for reading, and please please please feel free to let me know if you have any suggestions to improve this tutorial, or have suggestions for any tutorials you'd like to see in the future. Hello, i followed this instructable but i am getting an error This is my python code import serial import pgdb import psycopg2 #establish connection to MySQL.
You'll have to change this for your database. Hey, At first glance I would imagine it has to do with not reading successfully from the USB port.

Try to log what the value of the line you read from the Arduino is to quickly check if it's actually getting data. I'd guess the index error comes from the pieces array at your insert line. You could simply check your split line result was an array with two array elements before trying the insert. I'm not familiar with the postgres library so I can't confirm it's not potentially something there. Hopefully that helps, let me know what you find. Best, Tom 2 years ago. Hey there, I haven't testing this, so just a warning, but a straightforward solution would be to put the the insertion in a loop and do an insert each time some data was read.
Arduino Python Serial
Something along the lines of (just restart the loop if you don't have any input, so you don't try and insert null values into your database): while True: try: data = arduino.readline #read the data from the arduino if not data.strip: #did we get an empty line? #could put a sleep(1) or something if you wanted to wait before trying to read again continue #start the loop over pieces = data.split(' t') #split the data by the tab #Here we are going to insert the data into the Database try: cursor.execute('INSERT INTO weatherData (humidity,tempC) VALUES (%s,%s)', (pieces0,pieces1)) dbConn.commit #commit the insert cursor.close #close the cursor except MySQLdb.IntegrityError: print 'failed to insert data' finally: cursor.close #close just incase it failed except: print 'Failed to get data from Arduino!'
Arduino Python Serial
For this project, you need: Arduino UNO or similar board ($29.95) WiFly Shield ( $89.95) Grove - Moisture sensor ($4.99) Six Jumper Wires ($4.50) DS18B20 Temperature Sensor with metal tip ( $10.59) If only need, One protoboar ( $9.58) AND. You need a server WEB, with PHP and database MySQL. I used the NAS of QNAP TS-110, is perfect for this project.