Skip to content

Instantly share code, notes, and snippets.

@projectweekend
Last active January 17, 2016 18:10
Show Gist options
  • Save projectweekend/210a3ac2f540f0cdc357 to your computer and use it in GitHub Desktop.
Save projectweekend/210a3ac2f540f0cdc357 to your computer and use it in GitHub Desktop.
import json
import serial
# this port address is for the serial tx/rx pins on the GPIO header
SERIAL_PORT = '/dev/ttyAMA0'
# be sure to set this to the same rate used on the Arduino
SERIAL_RATE = 9600
def main():
ser = serial.Serial(SERIAL_PORT, SERIAL_RATE)
while True:
# using ser.readline() assumes each line contains a single reading
# sent using Serial.println() on the Arduino
reading = ser.readline().decode('utf-8')
# reading is a string...and if it's JSON formatted...
data = json.loads(reading)
# data will be a list or a dictionary depending on whether
# the 'reading' string was a JSON array or object
print(data)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment