[Tutor] making use of input

richard kappler richkappler at gmail.com
Sun Nov 4 21:52:32 CET 2012


Me again. :-)

Today was a good day, I accomplished a lot, but I'm stumbling now. I
assembled the sensor array, coded the Arduino board to read the sensors and
send the data out to serial, played with formatting the data, learned
enough about the python serial library to be a little dangerous.

The serial out from the Arduino sends in some form (depending on how I
format it in the Arduino code) Sonar1, Sonar2, Sonar3, Sonar4, Temperature,
Humidity, Dewpoint, and Light.

I can get these into Python in a continuous read by doing:

import serial
arduino = serial.Serial('/dev/ttyACM0', 9600)
while 1:
    arduino.readline()

/dev/ttyACM0 is the serial port being read and 9600 is the baud rate I set
for now.

This little bit of code gives me data in the form of 'Sensor1:120\r\n' one
line for each sensor in a continuous loop.
I can also get it to python in the following form {Sensor1:120, Sensor2:89,
etc...}
Right. Like a dictionary. but a continuous read, in other words, python
prints a new dictionary with the same keys but different data each time the
Arduino loops which is what Arduino's do.

I tried the following:

import serial
arduino = serial.Serial('/dev/ttyACM0', 9600)
sensor = arduino.readline()

and then ran

print sensor

I got only part of it, eg something like {,Sonar4:120,temperature:71, etc
on to the end of the sensor list} so python just captured whatever was
scrolling through serial at the moment the readline was executed, not the
full set of data.

What I NEED to do is have each parameter incoming to python through serial
be assigned to a name and be available within the python program to use in
other parts of the program. For example if temperature >70 print "It's
bloody well getting warm in here!" type thing.

The use of dictionary form was kind of a whim, and within limits I have not
yet found, I can format the output pretty much any way I'd like from the
Arduino, including just sending the numbers.

I'm pretty happy with the accomplishments of the day, but am at a bit of a
loss as to how to proceed next. I hope that all made sense. I might be
suffering a bit from "forest for trees" syndrome.

Does anyone have any guidance?

regards, Richard

-- 

quando omni flunkus moritati
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121104/baad8424/attachment.html>


More information about the Tutor mailing list