[Tutor] Trying to post sensor readings to the web.

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Mar 9 12:58:55 EST 2004



On Tue, 9 Mar 2004, Bill Gillespie wrote:

> I'm trying to learn how to post some sensor data, to a web page, by
> running a python cgi program. The objective is that each time I request
> the web page, and run the cgi program, the page would display the
> current readings for the sensors I am trying to monitor via the web.
>
> I have the sensors data in the form of a log file, but I'm not sure how
> to pass those logged sensor variables, to the web page through the cgi
> program.
>
> The log file is called "sensor.txt" and the data in it looks like this:
> --------------------------------------------------------------------
> chan0=125, chan1=32, chan2=255, chan3=003, chan4=103, ...
> chan0=125, chan1=33, chan2=255, chan3=002, chan4=114, ...
> chan0=123, chan1=33, chan2=254, chan3=004, chan4=124, ...
> chan0=125, chan1=30, chan2=255, chan3=000, chan4=136, ...


Hi Bill,


An approach that might help is to create a function that takes your
'sensor.txt' file, and extracts an MxN matrix of its data.



Every line of your program appears to relate to a "row", and you can use
some string manipulation to pull out the channel values out of a line.
One possibility is to use the split() method of a string:

###
>>> '3,4,5,6,7,8,9,10'.split()
['3,4,5,6,7,8,9,10']
>>>
>>>
>>> def extractElements(line):
...     return line.split(', ')
...
>>> extractElements('chan0=125, chan1=30, chan2=255, chan3=000')
['chan0=125', 'chan1=30', 'chan2=255', 'chan3=000']
###

And it should be simple to modify extractElements() to strip off the
'chan' prefix off of every channel value.


Another powerful tool that you can look at is the 're' regular expression
library.

    http://www.python.org/doc/lib/module-re.html

    http://www.amk.ca/python/howto/regex/

Regular expressions give us tools to do sophisticated pattern matching on
strings.



If you have questions on this, please feel free to ask on Tutor.  Good
luck to you!




More information about the Tutor mailing list