Last active
January 17, 2016 18:10
-
-
Save projectweekend/210a3ac2f540f0cdc357 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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