Load content from a text file

233 views
Skip to first unread message

Kenneth

unread,
Aug 16, 2013, 6:40:29 PM8/16/13
to web...@googlegroups.com
Hello everyone,

I'm planing on running web2py on an Raspberry Pi. On the computer there will also be an app that listens on the serial port and writes the results into an text file.

I'd like to create an extremly simple page where only the content of the file is shown and having the page to reload like once every second. This page will be shown from mobile ones or tablets over GPRS or 3G.

Is it possible to create an page so every reload doesn't re-execute the controller and models just re-reads the text file and shows it.

To controll that app I'd like to sends commands by writing them into an text file that the app reads. This shouldn't be too hard?


Kenneth

Massimo Di Pierro

unread,
Aug 17, 2013, 4:14:19 AM8/17/13
to web...@googlegroups.com
Now many clients need to access this at one time? The issue is the workload on the pi. 

Jim Gregory

unread,
Aug 17, 2013, 6:20:04 AM8/17/13
to web...@googlegroups.com
Do you need to use a text file as an intermediary? I created a similar app for my pedal-powered laptop (described at www.pedalpc.com) where a function is called in the controller that reads/writes the serial port and returns the data as JSON which just updates the necessary data in the page in the browser.

This method assumes the client has the processing power to handle updating the page via JSON...

Kenneth Lundström

unread,
Aug 17, 2013, 1:03:57 PM8/17/13
to web...@googlegroups.com
Hello,

only one max two clients at a time will access this page. I don't even see why two clients would be accesing this page if not a supervisor wants to check on the progress of an worker.

Jim, not quite sure how to do what you suggest. The serial communication is using an special protocol that the app reads and creates an text file from.

The app stores also everything in the database but I thought it would be more efficient for this page to get the info from an text file. There will be many other pages that will be using mobile jquery with an normal menu that reads data from the database, but this one special page will be just a blank page showing the content of the file if I can't find out how to reload just the part of the page that shows the text file. Was thinking using PHP to show that page.


Kenneth


--
 
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jim Gregory

unread,
Aug 18, 2013, 6:59:23 AM8/18/13
to web...@googlegroups.com
I'm not an expert in web2py, but this one approach I think should work
for your application. If others have better ways of doing it, please
share them:

def readfile():
'''
returns the file to be read
'''
response.view = '/path/to/file_to_be_read.txt'
return locals()

def writefile():
'''
writes the values of the form in basic, unescaped CSV format
'''
form = SQLFORM.factory(
Field('name'),
Field('choice', requires=IS_IN_SET(['1','2','3'], zero=None)),
)
if form.validate():
fh = open('path/to/file_to_write_to.txt', 'w')
fh.write(','.join(form.vars.values()))
fh.close()
response.flash = 'Your data has been submitted'
return dict(form=form)

It sounds like in your application, you are reading the serial port at
fixed periodic intervals and writing the data to a file. If you only
need to read or write to it when a user requests a page, this is what I
was suggesting instead:

def readdata():
'''
read data from serial port and return it in a view
see the Python PySerial module documentation for details
'''
import serial
try:
ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
data = ser.readline() # read a '\n' terminated line
ser.close()
except:
data = 'cannot read serial port'
return dict(data=data)

writing to the serial port would be just as easy:

def writedata():
'''
writes form values in basic, unescaped CSV to the serial port
'''
import serial
form = SQLFORM.factory(
Field('name'),
Field('choice', requires=IS_IN_SET(['1','2','3'], zero=None)),
)
if form.validate():
try:
ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
ser.write(','.join(form.vars.values()))
ser.close()
response.flash = 'Your data has been written'
except:
response.flash = 'Cannot write to serial port'
return dict(form=form)

HTH,

-Jim

----- Kenneth Lundstr�m <kenneth.t...@gmail.com> wrote:-----
> --- You received this message because you are subscribed to a topic
> in the Google Groups "web2py-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/_jYbslU46R4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages