Session notes, 27th April 2013 (part 1)

Arduino + Pi + Python

  1. We will connect an Arduino to the Raspberry Pi. There will be a program running on the Arduino that generates random numbers and sends these to the serial port.
  2. Use the sample Python program below to read in values from the serial port and display these.
  3. We when then add some logic to compare the values and print different messages depending on values.
  4. We will also install MySQL database and create a table to store the values from serial port.
  5. Finally we will update the Python program to use MySql and write vales read from serial port to a database table.

Sample code

# Import the Serial port library
import serial

# x will be used as counter in the while loop
x = 1

# InChar will be used to store the values read from serial port
InChar = 0

# Open serial port on Raspberry PI
ser = serial.Serial('/dev/ttyACM0', 9600)

# Remove any characters in the serial port buffer

while x < 100:
    # Read value from serial port into InChar variable
    InChar = ser.readline()

    # Print loop counter variable and value read from serial port       
    print x, InChar
    # Increment x by 1
    x =x + 1

# Close serial port before exiting