I have used Web2py to create a Home Heating Control application for Raspberry Pi with a web front end to define the and I am using the Web2py Scheduler to perform the control tasks every 30 seconds. I have also designed a radio linked system for temperature measurement and boiler control based around a USB dongle plugged into the Pi.
The problem I have is how to initialise and open the /dev/ttyUSB0 port once at start up, but not reopen it on every subsequent Scheduler run as this resets the dongle via the RTS line as the dongle is based on the Arduino architecture. I have some simple test code running, this section is called by the Scheduler as follows:
In file 'poll.py:'
def ch_scheduler():from utilities import ch_controlimport time
t = time.ctime()
ch_control()
return t
In file 'utilites.py':
from control import *def ch_control():
RelCtrl(1,0)
time.sleep(0.1)RelCtrl(2,1)
time.sleep(0.2)
RelCtrl(2,0)
time.sleep(0.1)
RelCtrl(1,1)return
import serialser=serial.Serial('/dev/ttyUSB0', 57600, timeout=1, xonxoff=0, rtscts=0)
def RelCtrl(in1,in2):
# convert relay parameters to integersrelay = int(in1)
# 1: ON, 0: OFF
relon = int(in2)
if relay > 0 :
if relon == 1 :command = 's1 ' + str(relay - 1) + '\x0d\x0a'
else :
command = 'c1 ' + str(relay - 1) + '\x0d\x0a'
ser.write(command)
# wait for reply, read characters but ignore
time.sleep(0.1)characters = ser.inWaiting()
if characters > 0 :
reply = ser.read(characters)
return
An update to this - I have done some more investigation and discovered that in the 'Not Working' scenario the RTS output is mostly high but goes low whilst the port is open, and in the 'Working' scenario the RTS output has somehow been forced low and so stays low when the port is opened. The Dongle gets reset by a high-to-low transition of RTS, so no falling edge, no Reset and everything works. For reference my Dongle uses an FTDI device (FT230X) and hence the FTDI driver supplied with the latest Raspbian (Jessie) release.The question now, therefore, is how Web2py forces RTS low - either by the saving of a file or when I access the index page of my Site, and How I can replicate this in the Scheduler code! Using 'touch' to update the file does not work, it has to be saved from within Web2py.Richard
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import serial
import time
ser=serial.Serial('/dev/ttyUSB0', 57600, timeout=1, xonxoff=0, rtscts=0)
ser.rts=True
while 1:
time.sleep(0.0001)
dir="/home/www-data/web2py"
user="www-data"
cmd="sudo nohup python applications/Heating_Controller/utilities/OpenPort.py &"
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.