|
Post by raspipeter on Sept 30, 2013 4:12:42 GMT -8
I have factually 2 requests:
1) Is it possible to get some examples how to read digital inputs from the PiFace as well how to read and set digital outputs from the PiFace using the RasPiConnectServer. There is probably a requirement to use additional code to communicate with the piface like the part that can be found in the documentation about it. (http://www.farnell.com/datasheets/1684425.pdf) A few steps that give clear instructions how to set it up would be of great help as such a PiFace make connection to the real word pretty easy.
2) Is the 1-wire temperature sensor DS18S20 or DS18B20 also supported using the RasPiConnectServer? Any example code of that and the way how to add more than one sensor on the system?
Looking forward for some feedback on this excellent looking platform!
|
|
|
Post by SDL on Oct 1, 2013 8:59:37 GMT -8
Hi RaspiPeter, We don't have a PiFace board here at MiloCreek, but the principles are the same. In the documentations for RasPiConnect it shows you how to modify Local.py for your own code. Take a look at the PiFace documentation for Python examples. For example: docs.google.com/folderview?docId=0B-UAZ9CyJCLGaHI2T3NXOWlfLUE&id=0B-UAZ9CyJCLGQjJ3RDlqa2pqaDgThe 1 wire temperature sensor is supported on the RasPiConnect server. If it is supported on the Raspberry Pi, it can work with the RasPiConnect server. Take a look at learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing/overviewfor examples of Python for reading from those sensors. Here is the code for a button that blinks an LED connected to a GPIO pin on the Pi. It is a good example of how to do this on the Pi and it will be similar on PiFace. Add this to your Local.py file as described in the documentation. # object Type match if (objectType == ACTION_BUTTON_UITYPE):
if (Config.debug()): print "ACTION_BUTTON_UTYPE of %s found" % objectServerID
# B-2 - flash an LED on the Pi, located on GPIO 25 if (objectServerID == "B-2"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True)
responseData = "OK" outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
Hope that helps! Best regards, BP
|
|
|
Post by raspipeter on Oct 1, 2013 13:47:06 GMT -8
Hi Raspiardsupport,
Thanks for the quick reply. I have succesfull got the PiFace working to switch ON an output, however I did not succeed to switch it back OFF with the same button. Can I use the FEEDBACK_ACTION_BUTTON_UITYPE button like the "FB-1" type of button to achieve this? How does the code for that button looks like if you have two different stages (ON and OFF) in order to switch the digital output also ON and OFF with the same button? Thanks in advance for your support.
Regards, Peter
|
|
|
Post by SDL on Oct 2, 2013 8:31:15 GMT -8
Hi Peter,
There are examples of how to use each of the controls in the Execute directory in the RasPiConnectServer directory. It sounds like you are very close to getting things up and working.
Following is an example from ExecuteActionButton.py
# FB-1 just does a toggle from on to off from the button name
if (objectServerID == "FB-1"): # do a toggle
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
responseData = "XXX"
lowername = objectName.lower()
if (lowername.count(" off") > 0):
lowername = lowername.replace(" off", " on") responseData = lowername.title()
elif (lowername.count(" on") > 0):
lowername = lowername.replace(" on", " off") responseData = lowername.title()
else: responseData = objectName
outgoingXMLData += BuildResponse.buildResponse(responseData)
Another example from a customers code.
Here he is storing the state information of the button in a file and changing the button title appropriately.
# object Type match if (objectType == FEEDBACK_ACTION_BUTTON_UITYPE):
if (Config.debug()): print "FEEDBACK_ACTION_BUTTON_UTYPE of %s found" % objectServerID
# FB-11 - change the graph display if (objectServerID == "FB-11"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX"
lowername = objectName.lower()
if (lowername == "display voltages"):
responseData = "display currents" responseData = responseData.title()
f = open("./local/GraphSelect.txt", "w") f.write(lowername) f.close()
elif (lowername == "display currents"):
responseData = "display voltages" responseData = responseData.title()
f = open("./local/GraphSelect.txt", "w") f.write(lowername) f.close()
# defaults to display currents else: lowername = "display currents" f = open("./local/GraphSelect.txt", "w") f.write(lowername) f.close()
responseData = "display voltages" responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
Hope this helps.
BP
|
|
|
Post by raspipeter on Oct 22, 2013 13:10:49 GMT -8
Hi BP,
Correct me if I'm wrong, but I understood that ALL user defined hardware have to be added in the "Local.py" file and not in any other file. Is that correct? The directory called "ExecuteFiles" does contain a couple of files, but is this some kind of example code and not realy used, or something else. Excuse me, I'm new with this stuff, so this might sound stupid.
I tried a lot, succeeded to nicely switch ON and OFF the individual outputs of the PiFace using B-0 to switch "ON" and B-00 to switch "OFF" the digital output #0 (same for B-1 and B10 for output #1; B-2 and B-20 for output #2, B-3 and B-30 for output #3 etc.) All works fine, but I cannot get the ActionFeedback button to work.
I tried to switch the output #1 with FB-1, but it does not work. Hopefully you can have a look (and a fix) on the code I used for it.
#!/usr/bin/python # Filename: Local.py # MiloCreek JS MiloCreek # Version 2.8 8/12/13 # # Local Execute Objects for RasPiConnect # to add Execute objects, modify this file # # # # RasPiConnectServer interface constants
REMOTE_WEBVIEW_UITYPE = 1 ACTION_BUTTON_UITYPE = 16 FEEDBACK_ACTION_BUTTON_UITYPE = 17 SINGLE_LED_DISPLAY_UITYPE = 32 SPEEDOMETER_UITYPE = 64 VOLTMETER_UITYPE = 128 BARMETER_UITYPE = 129 SERVER_STATUS_UITYPE = 256 PICTURE_REMOTE_WEBVIEW_UITYPE = 512 LABEL_UITYPE = 1024 FM_BLINK_LED_UITYPE = 2048 TEXT_DISPLAY_UITYPE = 4096 TOGGLE_SWITCH_UITYPE = 33 SEND_TEXT_UITYPE = 34
# system imports import sys import subprocess
# PiFace import import piface.pfio as pfio pfio.init()
# RasPiConnectImports
import Config import Validate import BuildResponse
def ExecuteUserObjects(objectType, element):
# Example Objects
# fetch information from XML for use in user elements
#objectServerID is the RasPiConnect ID from the RasPiConnect App
objectServerID = element.find("./OBJECTSERVERID").text objectID = element.find("./OBJECTID").text
if (Config.debug()): print("objectServerID = %s" % objectServerID) # # check to see if this is a Validate request # validate = Validate.checkForValidate(element)
if (Config.debug()): print "VALIDATE=%s" % validate
# Build the header for the response
outgoingXMLData = BuildResponse.buildHeader(element)
# objects are split up by object types by Interface Constants # # # # search for matches to object Type
# object Type match if (objectType == ACTION_BUTTON_UITYPE):
if (Config.debug()): print "ACTION_BUTTON_UTYPE of %s found" % objectServerID
# B-0 - activate relay PiFace if (objectServerID == "B-0"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(0,1)
# B-00 - activate relay PiFace if (objectServerID == "B-00"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(0,0) # B-1 - activate relay PiFace if (objectServerID == "B-1"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(1,1)
# B-10 - activate relay PiFace if (objectServerID == "B-10"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(1,0) # B-2 - activate relay PiFace if (objectServerID == "B-2"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(2,1) # B-20 - de-activate relay PiFace if (objectServerID == "B-20"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(2,0)
# B-3 - activate relay PiFace if (objectServerID == "B-3"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(3,1)
# B-30 - de-activate relay PiFace if (objectServerID == "B-30"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(3,0) # B-4 - activate relay PiFace if (objectServerID == "B-4"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(4,1)
# B-40 - de-activate relay PiFace if (objectServerID == "B-40"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(4,0) # B-5 - activate relay PiFace if (objectServerID == "B-5"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(5,1)
# B-50 - activate relay PiFace if (objectServerID == "B-50"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(5,0) # B-6 - activate relay PiFace if (objectServerID == "B-6"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(6,1)
# B-60 - activate relay PiFace if (objectServerID == "B-60"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(6,0) # B-7 - activate relay PiFace if (objectServerID == "B-7"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(7,1)
# B-70 - activate relay PiFace if (objectServerID == "B-70"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(7,0)
# B-8 - activate relay PiFace if (objectServerID == "B-8"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(8,1)
# B-80 - activate relay PiFace if (objectServerID == "B-80"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(8,0) # outgoingXMLData += BuildResponse.buildResponse(responseData) # outgoingXMLData += BuildResponse.buildFooter() # return outgoingXMLData # Up to hear all worked fine for just switching ON and OFF the individual outputs. I disabled the 3 lines above # since I added the code below for the toggle switch (Action Feedback switch). # At the end of that section, the 3 lines from above also have been added again.
# FB-1 just does a toggle from on to off from the button name
if (objectServerID == "FB-1"): # do a toggle #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
responseData = "XXX"
lowername = objectName.lower()
if (lowername.count(" off") > 0): lowername = lowername.replace(" off", " on") responseData = lowername.title() pfio.digital_write(1,1) elif (lowername.count(" on") > 0): lowername = lowername.replace(" on", " off") responseData = lowername.title() pfio.digital_write(1,0) else: responseData = objectName outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData else: # invalid RaspiConnect Code outgoingXMLData += Validate.buildValidateResponse("NO")
outgoingXMLData += BuildResponse.buildFooter() if (Config.debug()): print outgoingXMLData
return outgoingXMLData
The next thing I would like to add to the code is a simple indication if a digital input is ON or OFF. Maybe easy to implement, but if the above is already giving me a hard time, I guess this "nice to have digital input indication" might be the second hard time I would get...
Hope to get some help on this... Peter
|
|
|
Post by raspipeter on Oct 22, 2013 13:12:04 GMT -8
Just noticed that the indents are meshed up when publishing the code.... Sorry for that.
|
|
|
Post by kincade on Oct 22, 2013 14:01:03 GMT -8
RaspiPeter,
Python is so particular about indenting it's almost impossible to read your code and figure out what you did.
Please repost your code (using reply instead of post quick replay) and embed your code inside the code tag (square with a C in it above).
This Works Better!
|
|
|
Post by raspipeter on Oct 22, 2013 22:26:04 GMT -8
Sorry for the plain text above. Please find the code I used below:
#!/usr/bin/python # Filename: Local.py # MiloCreek JS MiloCreek # Version 2.8 8/12/13 # # Local Execute Objects for RasPiConnect # to add Execute objects, modify this file # # # # RasPiConnectServer interface constants
REMOTE_WEBVIEW_UITYPE = 1 ACTION_BUTTON_UITYPE = 16 FEEDBACK_ACTION_BUTTON_UITYPE = 17 SINGLE_LED_DISPLAY_UITYPE = 32 SPEEDOMETER_UITYPE = 64 VOLTMETER_UITYPE = 128 BARMETER_UITYPE = 129 SERVER_STATUS_UITYPE = 256 PICTURE_REMOTE_WEBVIEW_UITYPE = 512 LABEL_UITYPE = 1024 FM_BLINK_LED_UITYPE = 2048 TEXT_DISPLAY_UITYPE = 4096 TOGGLE_SWITCH_UITYPE = 33 SEND_TEXT_UITYPE = 34
# system imports import sys import subprocess
# PiFace import import piface.pfio as pfio pfio.init()
# RasPiConnectImports
import Config import Validate import BuildResponse
def ExecuteUserObjects(objectType, element):
# Example Objects
# fetch information from XML for use in user elements
#objectServerID is the RasPiConnect ID from the RasPiConnect App
objectServerID = element.find("./OBJECTSERVERID").text objectID = element.find("./OBJECTID").text
if (Config.debug()): print("objectServerID = %s" % objectServerID) # # check to see if this is a Validate request # validate = Validate.checkForValidate(element)
if (Config.debug()): print "VALIDATE=%s" % validate
# Build the header for the response
outgoingXMLData = BuildResponse.buildHeader(element)
# objects are split up by object types by Interface Constants # # # # search for matches to object Type
# object Type match if (objectType == ACTION_BUTTON_UITYPE):
if (Config.debug()): print "ACTION_BUTTON_UTYPE of %s found" % objectServerID
# B-0 - activate relay #0 on PiFace if (objectServerID == "B-0"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(0,1)
# B-00 - activate relay #0 on PiFace if (objectServerID == "B-00"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(0,0) # B-1 - activate relay #1 on PiFace if (objectServerID == "B-1"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(1,1)
# B-10 - de-activate relay #1 on PiFace if (objectServerID == "B-10"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(1,0) # B-2 - activate relay #2 on PiFace if (objectServerID == "B-2"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(2,1) # B-20 - de-activate relay #2 on PiFace if (objectServerID == "B-20"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(2,0)
# B-3 - activate relay #3 on PiFace if (objectServerID == "B-3"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(3,1)
# B-30 - de-activate relay #3 on PiFace if (objectServerID == "B-30"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(3,0) # B-4 - activate relay #4 on PiFace if (objectServerID == "B-4"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(4,1)
# B-40 - de-activate relay #4 on PiFace if (objectServerID == "B-40"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(4,0) # B-5 - activate relay #5 on PiFace if (objectServerID == "B-5"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(5,1)
# B-50 - de-activate relay #5 on PiFace if (objectServerID == "B-50"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(5,0) # B-6 - activate relay #6 on PiFace if (objectServerID == "B-6"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(6,1)
# B-60 - de-activate relay #6 on PiFace if (objectServerID == "B-60"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(6,0) # B-7 - activate relay #7 on PiFace if (objectServerID == "B-7"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(7,1)
# B-70 - de-activate relay #7 on PiFace if (objectServerID == "B-70"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(7,0)
# B-8 - activate relay #8 on PiFace (PiFace only has 8 outputs, #0 up to #7) # so #8 and higher numbers very likely can be used for the second or # third PiFace module. Have not checked this as I only have 1 PiFace module. if (objectServerID == "B-8"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(8,1)
# B-80 - de-activate relay #8 on PiFace if (objectServerID == "B-80"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(8,0) # outgoingXMLData += BuildResponse.buildResponse(responseData) # outgoingXMLData += BuildResponse.buildFooter() # return outgoingXMLData # # The above 3 lines were part of the working system with only the ON and OFF buttons. # I have disabled them now since the same 3 lines will be at the end of the next module # where the "toggle switch" is used to switch ON and OFF a digital output #1 of the PiFace # # FB-1 just does a toggle from ON to OFF from the button name # and it should toggle the digital output #1 of the PiFace from ON to OFF
if (objectServerID == "FB-1"): # do a toggle #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
responseData = "XXX"
lowername = objectName.lower()
if (lowername.count(" off") > 0): lowername = lowername.replace(" off", " on") responseData = lowername.title() pfio.digital_write(1,1) elif (lowername.count(" on") > 0): lowername = lowername.replace(" on", " off") responseData = lowername.title() pfio.digital_write(1,0) else: responseData = objectName
# # Below are the 3 lines again that I disableed in the upper section as mentioned above # outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData else: # invalid RaspiConnect Code outgoingXMLData += Validate.buildValidateResponse("NO")
outgoingXMLData += BuildResponse.buildFooter() if (Config.debug()): print outgoingXMLData
return outgoingXMLData
I hope this will make it more clear.
Regards, Peter
|
|
|
Post by SDL on Oct 23, 2013 7:39:35 GMT -8
Shouldn't
# # Below are the 3 lines again that I disableed in the upper section as mentioned above # outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
be indented to the left one more level?
BP
|
|
|
Post by kincade on Oct 24, 2013 7:40:30 GMT -8
Peter,
I agree. That looks like your problem!
|
|
|
Post by raspipeter on Mar 12, 2014 14:12:48 GMT -8
After a long time I could make some time again for the PiFace to work with the Pi using RasPiConnectServer. I succeeded to make a universal file that is able to monitor all 8 digital inputs and to control each of the 8 digital outputs of the PiFace. Some details have been added in the header as well as in the project to have a better view on the individual controls that are available. Hope this might be helpful for more people that are struggling to get access to the hardware like the PiFace module. Here is the code (and thanks to the contribution of the nice code of the Curacao project!). #!/usr/bin/python # Filename: Local.py # MiloCreek JS MiloCreek # RaspiPeter (PiFace section) # Version 1.0 March 12, 2014 # # Local Execute Objects for RasPiConnect # to add Execute objects, modify this file # # # This Local.py file will allow you to monitor and control each Input and Output of the PiFace module fitted on the Rasberry Pi. # Use L-000 up to L-007 as LED indicator to monitor each of the digital inputs 0 - 7. # Use B-0 up to B-7 as Action Button to switch ON output 0 - 7 # Use B-00 up to B-70 as Action Button to switch OFF output 0 - 7 # Use FB-00 up to FB-07 as Feedback Action Button to control output 0 - 7. # Use FB-99 as Feedback Action Button to toggle all outputs at once. # Use L-00 up to L-07 as LED indicator to monitor output 0 - 7 (note: you need to create a text file for each output!). # Simply create a text file here: "/home/pi/RasPiConnectServer/relay0state.txt" for relay 0; "relay1state.txt" for relay 1; etc. # This file only should have a number in it "1" or "0". Does not matter what you start with. # It will take the status of the Digital Output when it changes. #
# RasPiConnectServer interface constants REMOTE_WEBVIEW_UITYPE = 1 ACTIon_BUTTon_UITYPE = 16 FEEDBACK_ACTIon_BUTTon_UITYPE = 17 SINGLE_LED_DISPLAY_UITYPE = 32 SPEEDOMETER_UITYPE = 64 VOLTMETER_UITYPE = 128 BARMETER_UITYPE = 129 SERVER_STATUS_UITYPE = 256 PICTURE_REMOTE_WEBVIEW_UITYPE = 512 LABEL_UITYPE = 1024 FM_BLINK_LED_UITYPE = 2048 TEXT_DISPLAY_UITYPE = 4096 TOGGLE_SWITCH_UITYPE = 33 SEND_TEXT_UITYPE = 34
# system imports import sys import subprocess
# PiFace import import piface.pfio as pfio pfio.init()
# RasPiConnectImports
import Config import Validate import BuildResponse
def ExecuteUserObjects(objectType, element):
# Example Objects
# fetch information from XML for use in user elements
#objectServerID is the RasPiConnect ID from the RasPiConnect App objectServerID = element.find("./OBJECTSERVERID").text objectID = element.find("./OBJECTID").text #this "objectName" line below is important for the Feedback Button functionality! objectName = element.find("./OBJECTNAME").text objectFlags = element.find("./OBJECTFLAGS").text
if (Config.debug()): print("objectServerID = %s" % objectServerID) # # check to see if this is a Validate request # validate = Validate.checkForValidate(element)
if (Config.debug()): print "VALIDATE=%s" % validate
# Build the header for the response
outgoingXMLData = BuildResponse.buildHeader(element)
# objects are split up by object types by Interface Constants # # search for matches to object Type # object Type match if (objectType == FEEDBACK_ACTIon_BUTTon_UITYPE):
if (Config.debug()): print "FEEDBACK_ACTIon_BUTTon_UTYPE of %s found" % objectServerID #**************************************************************************************** # FB-99 - toggle all relay) in case you want a button that turns ON/OFF all digital outputs if (objectServerID == "FB-99"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "allrelay on"):
pfio.digital_write(0,1) #turn on f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") f.write("1") f.close() pfio.digital_write(1,1) #turn on f = open("/home/pi/RasPiConnectServer/relay1state.txt", "w") f.write("1") f.close() pfio.digital_write(2,1) #turn on f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") f.write("1") f.close() pfio.digital_write(3,1) #turn on f = open("/home/pi/RasPiConnectServer/relay3state.txt", "w") f.write("1") f.close() pfio.digital_write(4,1) #turn on f = open("/home/pi/RasPiConnectServer/relay4state.txt", "w") f.write("1") f.close() pfio.digital_write(5,1) #turn on f = open("/home/pi/RasPiConnectServer/relay5state.txt", "w") f.write("1") f.close() pfio.digital_write(6,1) #turn on f = open("/home/pi/RasPiConnectServer/relay6state.txt", "w") f.write("1") f.close() pfio.digital_write(7,1) #turn on f = open("/home/pi/RasPiConnectServer/relay7state.txt", "w") f.write("1") f.close()
responseData = "allrelay off" responseData = responseData.title()
elif (lowername == "allrelay off"):
pfio.digital_write(0,0) #turn off f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") f.write("0") f.close() pfio.digital_write(1,0) #turn off f = open("/home/pi/RasPiConnectServer/relay1state.txt", "w") f.write("0") f.close() pfio.digital_write(2,0) #turn off f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") f.write("0") f.close() pfio.digital_write(3,0) #turn off f = open("/home/pi/RasPiConnectServer/relay3state.txt", "w") f.write("0") f.close() pfio.digital_write(4,0) #turn off f = open("/home/pi/RasPiConnectServer/relay4state.txt", "w") f.write("0") f.close() pfio.digital_write(5,0) #turn off f = open("/home/pi/RasPiConnectServer/relay5state.txt", "w") f.write("0") f.close() pfio.digital_write(6,0) #turn off f = open("/home/pi/RasPiConnectServer/relay6state.txt", "w") f.write("0") f.close() pfio.digital_write(7,0) #turn off f = open("/home/pi/RasPiConnectServer/relay7state.txt", "w") f.write("0") f.close()
responseData = "allrelay on" responseData = responseData.title()
# defaults to relay on (meaning relay is off) else: lowername = "allrelay on" responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # FB-00 - turn on relay0) if (objectServerID == "FB-00"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay0 on"): pfio.digital_write(0,1) #turn on # create some textfiles that only contain a "0" or a "1" and place them in a folder referring to. f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") # the file will be opened f.write("1") # the value "1" will be written to match the output state of the "relay0" f.close() # the file will be closed again
responseData = "relay0 off" responseData = responseData.title()
elif (lowername == "relay0 off"):
pfio.digital_write(0,0) #turn off f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") f.write("0") f.close()
responseData = "relay0 on" responseData = responseData.title() else: lowername = "relay0 on" pfio.digital_write(0,1) #turn on f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") f.write("0") f.close()
responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # FB-01 - turn on relay1) if (objectServerID == "FB-01"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay1 on"):
pfio.digital_write(1,1) #turn on f = open("/home/pi/RasPiConnectServer/relay1state.txt", "w") f.write("1") f.close()
responseData = "relay1 off" responseData = responseData.title()
elif (lowername == "relay1 off"):
pfio.digital_write(1,0) #turn off f = open("/home/pi/RasPiConnectServer/relay1state.txt", "w") f.write("0") f.close()
responseData = "relay1 on" responseData = responseData.title()
# defaults to relay on (meaning relay is off) else: lowername = "relay1 on" pfio.digital_write(1,0) #turn on f = open("/home/pi/RasPiConnectServer/relay1state.txt", "w") f.write("0") f.close()
responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData #****************************************************************************************
# FB-02 - turn on relay2) if (objectServerID == "FB-02"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay2 on"): pfio.digital_write(2,1) #turn on f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") f.write("1") f.close() responseData = "relay2 off" responseData = responseData.title()
elif (lowername == "relay2 off"):
pfio.digital_write(2,0) #turn off f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") f.write("0") f.close() responseData = "relay2 on" responseData = responseData.title() else: lowername = "relay2 on" pfio.digital_write(2,0) #turn on f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") f.write("0") f.close() responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # FB-03 - turn on relay3) if (objectServerID == "FB-03"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay3 on"): pfio.digital_write(3,1) #turn on f = open("/home/pi/RasPiConnectServer/relay3state.txt", "w") f.write("1") f.close() responseData = "relay3 off" responseData = responseData.title()
elif (lowername == "relay3 off"):
pfio.digital_write(3,0) #turn off f = open("/home/pi/RasPiConnectServer/relay3state.txt", "w") f.write("0") f.close() responseData = "relay3 on" responseData = responseData.title() else: lowername = "relay3 on" pfio.digital_write(3,0) #turn on f = open("/home/pi/RasPiConnectServer/relay3state.txt", "w") f.write("0") f.close() responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # FB-04 - turn on relay4) if (objectServerID == "FB-04"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay4 on"): pfio.digital_write(4,1) #turn on f = open("/home/pi/RasPiConnectServer/relay4state.txt", "w") f.write("1") f.close() responseData = "relay4 off" responseData = responseData.title()
elif (lowername == "relay4 off"):
pfio.digital_write(4,0) #turn off f = open("/home/pi/RasPiConnectServer/relay4state.txt", "w") f.write("0") f.close() responseData = "relay4 on" responseData = responseData.title() else: lowername = "relay4 on" pfio.digital_write(4,0) #turn on f = open("/home/pi/RasPiConnectServer/relay4state.txt", "w") f.write("0") f.close() responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData #****************************************************************************************
# FB-05 - turn on relay5) if (objectServerID == "FB-05"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay5 on"): pfio.digital_write(5,1) #turn on f = open("/home/pi/RasPiConnectServer/relay5state.txt", "w") f.write("1") f.close() responseData = "relay5 off" responseData = responseData.title()
elif (lowername == "relay5 off"):
pfio.digital_write(5,0) #turn off f = open("/home/pi/RasPiConnectServer/relay5state.txt", "w") f.write("0") f.close() responseData = "relay5 on" responseData = responseData.title() else: lowername = "relay5 on" pfio.digital_write(5,0) #turn on f = open("/home/pi/RasPiConnectServer/relay5state.txt", "w") f.write("0") f.close() responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # FB-06 - turn on relay6) if (objectServerID == "FB-06"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay6 on"): pfio.digital_write(6,1) #turn on f = open("/home/pi/RasPiConnectServer/relay6state.txt", "w") f.write("1") f.close() responseData = "relay6 off" responseData = responseData.title()
elif (lowername == "relay6 off"):
pfio.digital_write(6,0) #turn off f = open("/home/pi/RasPiConnectServer/relay6state.txt", "w") f.write("0") f.close() responseData = "relay6 on" responseData = responseData.title() else: lowername = "relay6 on" pfio.digital_write(6,0) #turn on f = open("/home/pi/RasPiConnectServer/relay6state.txt", "w") f.write("0") f.close() responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData #**************************************************************************************** # FB-07 - turn on relay7) if (objectServerID == "FB-07"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay7 on"): pfio.digital_write(7,1) #turn on f = open("/home/pi/RasPiConnectServer/relay7state.txt", "w") f.write("1") f.close() responseData = "relay7 off" responseData = responseData.title()
elif (lowername == "relay7 off"):
pfio.digital_write(7,0) #turn off f = open("/home/pi/RasPiConnectServer/relay7state.txt", "w") f.write("0") f.close() responseData = "relay7 on" responseData = responseData.title() else: lowername = "relay7 on" pfio.digital_write(7,0) #turn on f = open("/home/pi/RasPiConnectServer/relay7state.txt", "w") f.write("0") f.close() responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData #**************************************************************************************** # object Type match if (objectType == SINGLE_LED_DISPLAY_UITYPE): if (Config.debug()): print "SINGLE_LED_DISPLAY_UITYPE of %s found" % objectServerID #**************************************************************************************** # L-000 sends back the current state of the PiFace digital input "0" if (objectServerID == "L-000"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # read PiFace digital input try: inputstate= pfio.digital_read(0) #relaystate = f except IOError as e: inputstate = 0
if (inputstate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # L-001 sends back the current state of the PiFace digital input "1" if (objectServerID == "L-001"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # read PiFace digital input try: inputstate= pfio.digital_read(1) #relaystate = f except IOError as e: inputstate = 0
if (inputstate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # L-002 sends back the current state of the PiFace digital input "2" if (objectServerID == "L-002"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # read PiFace digital input try: inputstate= pfio.digital_read(2) #relaystate = f except IOError as e: inputstate = 0
if (inputstate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # L-003 sends back the current state of the PiFace digital input "3" if (objectServerID == "L-003"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # read PiFace digital input try: inputstate= pfio.digital_read(3) #relaystate = f except IOError as e: inputstate = 0
if (inputstate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # L-004 sends back the current state of the PiFace digital input "4" if (objectServerID == "L-004"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # read PiFace digital input try: inputstate= pfio.digital_read(4) #relaystate = f except IOError as e: inputstate = 0
if (inputstate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # L-005 sends back the current state of the PiFace digital input "5" if (objectServerID == "L-005"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # read PiFace digital input try: inputstate= pfio.digital_read(5) #relaystate = f except IOError as e: inputstate = 0
if (inputstate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** # L-006 sends back the current state of the PiFace digital input "6" if (objectServerID == "L-006"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # read PiFace digital input try: inputstate= pfio.digital_read(6) #relaystate = f except IOError as e: inputstate = 0
if (inputstate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData #**************************************************************************************** # L-007 sends back the current state of the PiFace digital input "7" if (objectServerID == "L-007"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData # read PiFace digital input try: inputstate= pfio.digital_read(7) #relaystate = f except IOError as e: inputstate = 0
if (inputstate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData #**************************************************************************************** # L-00 sends back the current state of the relay0 if (objectServerID == "L-00"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# read from relay0 state file try: f = open("/home/pi/RasPiConnectServer/relay0state.txt", "r") tempString = f.read() f.close() relaystate = int(tempString) except IOError as e: relaystate = 0
if (relaystate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#****************************************************************************************
# L-01 sends back the current state of the relay1 if (objectServerID == "L-01"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# read from relay1 state file try: f = open("/home/pi/RasPiConnectServer/relay1state.txt", "r") tempString = f.read() f.close() relaystate = int(tempString) except IOError as e: relaystate = 0
if (relaystate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** #**************************************************************************************** # L-02 sends back the current state of the relay2 if (objectServerID == "L-02"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# read from relay2 state file try: f = open("/home/pi/RasPiConnectServer/relay2state.txt", "r") tempString = f.read() f.close() relaystate = int(tempString) except IOError as e: relaystate = 0
if (relaystate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** #**************************************************************************************** # L-03 sends back the current state of the relay13 if (objectServerID == "L-03"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# read from relay3 state file try: f = open("/home/pi/RasPiConnectServer/relay3state.txt", "r") tempString = f.read() f.close() relaystate = int(tempString) except IOError as e: relaystate = 0
if (relaystate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** #**************************************************************************************** # L-04 sends back the current state of the relay4 if (objectServerID == "L-04"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# read from relay4 state file try: f = open("/home/pi/RasPiConnectServer/relay4state.txt", "r") tempString = f.read() f.close() relaystate = int(tempString) except IOError as e: relaystate = 0
if (relaystate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** #**************************************************************************************** # L-05 sends back the current state of the relay5 if (objectServerID == "L-05"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# read from relay5 state file try: f = open("/home/pi/RasPiConnectServer/relay5state.txt", "r") tempString = f.read() f.close() relaystate = int(tempString) except IOError as e: relaystate = 0
if (relaystate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** #**************************************************************************************** # L-06 sends back the current state of the relay6 if (objectServerID == "L-06"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# read from relay6 state file try: f = open("/home/pi/RasPiConnectServer/relay6state.txt", "r") tempString = f.read() f.close() relaystate = int(tempString) except IOError as e: relaystate = 0
if (relaystate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#**************************************************************************************** #**************************************************************************************** # L-07 sends back the current state of the relay7 if (objectServerID == "L-07"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# read from relay7 state file try: f = open("/home/pi/RasPiConnectServer/relay7state.txt", "r") tempString = f.read() f.close() relaystate = int(tempString) except IOError as e: relaystate = 0
if (relaystate == 0): ledoutput = 5 else: ledoutput = 2
responseData = "%i" % ledoutput
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#****************************************************************************************
if (objectType == ACTIon_BUTTon_UITYPE):
if (Config.debug()): print "ACTIon_BUTTon_UTYPE of %s found" % objectServerID
# B-0 - activate relay PiFace if (objectServerID == "B-0"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(0,1) f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") f.write("1") f.close()
# B-00 - activate relay PiFace if (objectServerID == "B-00"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(0,0) f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") f.write("0") f.close() # B-1 - activate relay PiFace if (objectServerID == "B-1"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(1,1) f = open("/home/pi/RasPiConnectServer/relay1state.txt", "w") f.write("1") f.close()
# B-10 - activate relay PiFace if (objectServerID == "B-10"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(1,0) f = open("/home/pi/RasPiConnectServer/relay1state.txt", "w") f.write("0") f.close() # B-2 - activate relay PiFace if (objectServerID == "B-2"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(2,1) f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") f.write("1") f.close() # B-20 - de-activate relay PiFace if (objectServerID == "B-20"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(2,0) f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") f.write("0") f.close()
# B-3 - activate relay PiFace if (objectServerID == "B-3"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(3,1) f = open("/home/pi/RasPiConnectServer/relay3state.txt", "w") f.write("1") f.close()
# B-30 - de-activate relay PiFace if (objectServerID == "B-30"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(3,0) f = open("/home/pi/RasPiConnectServer/relay3state.txt", "w") f.write("0") f.close() # B-4 - activate relay PiFace if (objectServerID == "B-4"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(4,1) f = open("/home/pi/RasPiConnectServer/relay4state.txt", "w") f.write("1") f.close()
# B-40 - de-activate relay PiFace if (objectServerID == "B-40"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(4,0) f = open("/home/pi/RasPiConnectServer/relay4state.txt", "w") f.write("0") f.close() # B-5 - activate relay PiFace if (objectServerID == "B-5"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(5,1) f = open("/home/pi/RasPiConnectServer/relay5state.txt", "w") f.write("1") f.close()
# B-50 - activate relay PiFace if (objectServerID == "B-50"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(5,0) f = open("/home/pi/RasPiConnectServer/relay5state.txt", "w") f.write("0") f.close() # B-6 - activate relay PiFace if (objectServerID == "B-6"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(6,1) f = open("/home/pi/RasPiConnectServer/relay6state.txt", "w") f.write("1") f.close()
# B-60 - activate relay PiFace if (objectServerID == "B-60"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(6,0) f = open("/home/pi/RasPiConnectServer/relay6state.txt", "w") f.write("0") f.close() # B-7 - activate relay PiFace if (objectServerID == "B-7"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(7,1) f = open("/home/pi/RasPiConnectServer/relay7state.txt", "w") f.write("1") f.close()
# B-70 - activate relay PiFace if (objectServerID == "B-70"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(7,0) f = open("/home/pi/RasPiConnectServer/relay7state.txt", "w") f.write("0") f.close()
# B-8 - activate relay PiFace if (objectServerID == "B-8"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(8,1)
# B-80 - activate relay PiFace if (objectServerID == "B-80"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
pfio.digital_write(8,0) outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
else: # invalid RaspiConnect Code outgoingXMLData += Validate.buildValidateResponse("NO")
outgoingXMLData += BuildResponse.buildFooter() if (Config.debug()): print outgoingXMLData
return outgoingXMLData
Attachments:Local.py (48.55 KB)
|
|
|
Post by raspipeter on Mar 12, 2014 14:24:23 GMT -8
Oh, almost forgot the layout on the Ipad.
|
|
grandad
New Member
No Quite So Confused.
Posts: 30
Raspberry Pi: Yes
Other Device: 2nd Pi, PCs, iPad, Android Tablet and many, many gadgets!
|
Post by grandad on Mar 26, 2014 12:57:17 GMT -8
Hi Peter,
Impressive looking interface!
I'm trying to implement something similar, and I have "stolen" your code in an attempt to get things going - hope you don't mind.
Just as a trial I'm trying to turn an LED on and off using the feedback button code FB-00. I can achieve this fine using separate action buttons. I have commented out your file write/read lines 'cos I don't need 'em yet. The FB-00 validates fine, but the code always returns the else part of the function, and I cannot work out why. I've added the objectName = element.find("./OBJECTNAME").text to the head of Local.py.
I notice in your code that you have changed the ID of the feedback button to FEEDBACK_ACTIon_BUTTon_UITYPE = 17 with lower case "on" Is there a reason for that?
The snippet of my code is below; any ideas?
if (objectType == FEEDBACK_ACTION_BUTTON_UITYPE):
if (Config.debug()): print "FEEDBACK_ACTION_BUTTON_UTYPE of %s found" % objectServerID
# FB-00 - turn on GPIOA bit 0) if (objectServerID == "FB-00"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "LED on"): bus.write_byte_data(address, GPIOA, 0x01) #turn on # create some textfiles that only contain a "0" or a "1" and place them in a folder referring to. # f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") # the file will be opened # f.write("1") # the value "1" will be written to match the output state of the "relay0" # f.close() # the file will be closed again
responseData = "LED off" responseData = responseData.title()
elif (lowername == "LED off"):
bus.write_byte_data(address, GPIOA, 0x00) #turn off # f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") # f.write("0") # f.close()
responseData = "LED on" responseData = responseData.title() else: lowername = "LED on" bus.write_byte_data(address, GPIOA, 0x01) #turn on # f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w") # f.write("0") # f.close()
responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData TIA Martin
|
|
|
Post by raspipeter on Mar 27, 2014 13:02:58 GMT -8
Hi Martin,
First of all, I'm also completely NEW to this whole programming of the RPi. I have also simply searched for various examples of codes and copied and pasted something to try a single output and worked a little futher to include all the 8 inputs and 8 outputs of the PiFace module.
I'm not sure if you want to activate directly one of the available I/O's from the RPi itself, or from an additional module fitted on it (like the PiFace board that I used). If you are using the standard available I/O's, I have the impression that you simply can use the code that BP was mentioning as first reply on my initial request.
# object Type match
if (objectType == ACTION_BUTTON_UITYPE):
if (Config.debug()):
print "ACTION_BUTTON_UTYPE of %s found" % objectServerID
# B-2 - flash an LED on the Pi, located on GPIO 25
if (objectServerID == "B-2"):
#check for validate request
# validate allows RasPiConnect to verify this object is here
if (validate == "YES"):
outgoingXMLData += Validate.buildValidateResponse("YES")
outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# not validate request, so execute
# ******************************************************************************
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, False)
time.sleep(0.5)
GPIO.output(22, True)
#******************************************************************************
responseData = "OK"
outgoingXMLData += BuildResponse.buildResponse(responseData)
outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
The part between "comment lines" is her producing a blinking output, but of course you can change that simply to only switching "ON" the output 22 in the code I used for the FeedBack button.
So I would expect it would more or less look like this:
#****************************************************************************************
# FB-02 - turn on relay2) if (objectServerID == "FB-02"):
#check for validate request # validate allows RasPiConnect to verify this object is here if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# not validate request, so execute
responseData = "XXX" if (objectName is None): objectName = "XXX"
lowername = objectName.lower()
if (lowername == "relay2 on"): GPIO.setup(22, GPIO.OUT)
GPIO.output(22, True) #turn on #pfio.digital_write(2,1) #turn on #f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") #f.write("1") #f.close() responseData = "relay2 off" responseData = responseData.title()
elif (lowername == "relay2 off"): GPIO.setup(22, GPIO.OUT)
GPIO.output(22, False) #turn off #pfio.digital_write(2,0) #turn off #f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") #f.write("0") #f.close() responseData = "relay2 on" responseData = responseData.title() else: lowername = "relay2 on" GPIO.setup(22, GPIO.OUT)
GPIO.output(22, False) #turn off #pfio.digital_write(2,0) #turn off #f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") #f.write("0") #f.close() responseData = lowername.title()
outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
#****************************************************************************************
Sorry, I have not tried it on my RPi, but I'm pretty convinced it should work. Maybe BP can comment on it.
I also expect that I know what went wrong in your code and this is likely caused by a typo in my code :-). I wrote as text "turn on" where I factually turned OFF the digital output. Output 2 in this example turned OFF (= "0"), but the comment behind it says it is "turn on". Sorry my fault :-(
else: lowername = "relay2 on" pfio.digital_write(2,0) #turn on f = open("/home/pi/RasPiConnectServer/relay2state.txt", "w") f.write("0") f.close() responseData = lowername.title()
You used the code that really turns ON the output.
else:
lowername = "LED on"
bus.write_byte_data(address, GPIOA, 0x01) #turn on
# f = open("/home/pi/RasPiConnectServer/relay0state.txt", "w")
# f.write("0")
# f.close()
responseData = lowername.title()
So I would think you simply have to change your code to this:
bus.write_byte_data(address, GPIOA, 0x00) #turn off
With respect to your comment: "I notice in your code that you have changed the ID of the feedback button to FEEDBACK_ACTIon_BUTTon_UITYPE = 17 with lower case "on" Is there a reason for that? "
The answer is NO, it was very likely used in the code I "copied" from a source on the web. But I think as long as it is exactly the same written under the RasPiConnectServer interface constants in the header of the file as well as in the part of the code where you are calling for the "if (objectType == xxxxx_UITYPE): it will very likely be fine.
Hope you get it back to work.
Regards, Peter
|
|
grandad
New Member
No Quite So Confused.
Posts: 30
Raspberry Pi: Yes
Other Device: 2nd Pi, PCs, iPad, Android Tablet and many, many gadgets!
|
Post by grandad on Apr 2, 2014 10:53:43 GMT -8
Hello Peter, Thank you for your reply. I had picked up the "#Turn ON" error, but hadn't pasted that correction when posting. But that made no difference unfortunately. I've been away from the Pi for a few days now, so I must get back to it again. BTW, I am not driving the GPIO pins directly to light the LED, I am using the i2c bus, and an MCP23017 chip as a buffer / expander. As I said earlier, I can illuminate / extinguish an LED connected to the MCP23017 using individual ACTION buttons, just not a single FEEDBACK button - Ho-Hum, must try harder ) MArtin
|
|