ve7mkf
New Member
Posts: 13
Raspberry Pi: Yes
|
Post by ve7mkf on Feb 24, 2014 15:15:06 GMT -8
Martin,
Not sure if it was the best place to do it, but I just modified the W-2 section of the ExecuteRemoteWebView.py file (in ExecuteFiles directory). Here is what I have in there now. Note that I have commented out the original lines and also some of the things I tried to make it work, so there is a little extra in there that needn't be. I also have it print out the response data to see how it looked. Here is the W-2 section I have:
Mike
elif (objectServerID == "W-2"): #check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData # normal response requested
#imageName = "RovioImage.jpg" imageName = "picamera.jpg"
responseData = "<html>" #responseData += '<meta http-equiv="Pragma" content="no-cache">' responseData += '<meta http-equiv="cache-control" content="no-cache">' #responseData += '<meta name="Expires" content="0">' responseData += "<head>" responseData += "<title></title><style>body,html,iframe{margin:0;padding:0;}</style>" responseData += "</head>" responseData += "<body><img src=\"" responseData += LOCALURL responseData += "static/" responseData += imageName responseData += "\" type=\"jpg\" width=\"723\" height=\"508\">" responseData += "<BR>Picture<BR>" responseData +="</body>"
responseData += "</html>" print responseData
outgoingXMLData += BuildResponse.buildResponse(responseData)
if (Config.debug()): print outgoingXMLData
else: # invalid RaspiConnect Code outgoingXMLData += Validate.buildValidateResponse("NO")
outgoingXMLData += BuildResponse.buildFooter()
return outgoingXMLData
# End of ExecuteWebView.py
|
|
|
Post by SDL on Feb 24, 2014 18:12:04 GMT -8
Mike / Martin,
No problem at all modifying the W-2 ExecuteRemoteWebView.py to make it work, but that file will be written over if you install a new version of RasPiConnectServer.
Only the Local.py file is guarenteed to be not overwritten. I'd suggest you copy the W-2 section to Local.py and change it to, say, W-10 and change RasPiConnect to reflect that.
Oh and Mike, great response to Martin!
Best regards,
BP
|
|
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 Feb 25, 2014 3:56:38 GMT -8
Mike, Thanks again for the code. I seem to have an issue with modprobe w1-gpio and modprobe w1-therm killing the raspistill camera now! I'm running Raspian v3.10.25 This URL refers: github.com/raspberrypi/linux/issues/435DOH! Is your raspicam/python-picamera working OK Mike? Oh, and I had to add "sudo" in front of the "modprobe w1" os.system entries within your Local.py code to get the DS18B20 meter working. BP, thanks for your help also. Martin [edit] OK, got both camera and DS18B20 working again by following advice on the previous URL I mentioned, as follows:- /etc/modules
w1-therm w1-gpio pullup=1 i2c-dev i2c-bcm2708 spi-bcm2708 snd-bcm2835
/etc/modprobe.d/raspi-blacklist.conf
# blacklist spi and i2c by default (many users don't need them) #blacklist spi-bcm2708 blacklist i2c-bcm2708
Still running the Pi camera module with a crontab entry for a pic every minute, so now to try and find my way around placing a button within Local.py to enable me to take a pic whenever I choose. Wish me luck! [/edit]
|
|
|
Post by nolasafari on Feb 25, 2014 5:17:37 GMT -8
I want to let everyone know I am working thru the same problems and this thread is a great help. Also a big thanks to Mike who sent me some sample code early on.
Jim
|
|
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 Feb 25, 2014 5:40:15 GMT -8
Hi Jim, Welcome to the forum. Mike is a hero in my book ) Martin
|
|
|
Post by SDL on Feb 25, 2014 13:24:59 GMT -8
Martin,
Here is the Python code that we use to generate a picture. We didn't use the picamera library because it interferes with the RPi.GPIO library.
We run it via a cron (actually from apscheduler.py, but it is like a cron entry) entry, although you could embed calls to this file into a Local.py Webview command (W-10 for example).
Note you HAVE to put the camera.jpg file in the static directory or web.py will ignore it.
# Takes a single picture on System # filename: takeSinglePicture.py # Version 1.0 10/31/13 # # takes a picture using the camera # #
import sys import time import RPi.GPIO as GPIO import serial import picamera import subprocess import MySQLdb as mdb sys.path.append('/home/pi/ProjectCuracao/main/hardware') sys.path.append('/home/pi/ProjectCuracao/main/pclogging') sys.path.append('/home/pi/ProjectCuracao/main/util') import pclogging import util import hardwareactions
def sweepShutter(source, delay):
print("sweepShutter source:%s" % source) GPIO.setmode(GPIO.BOARD) time.sleep(delay) # blink GPIO LED when it's run GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5)
hardwareactions.sweepshutter()
time.sleep(3.0)
pclogging.log(pclogging.INFO, __name__, "Sweep Shutter")
def takePicture(source):
try: f = open("/home/pi/ProjectCuracao/main/state/exposure.txt", "r") tempString = f.read() f.close() lowername = tempString
except IOError as e: lowername = "auto"
exposuremode = lowername # take picture print "taking picture" cameracommand = "raspistill -o /home/pi/RasPiConnectServer/static/picameraraw.jpg -rot 180 -t 750 -ex " + exposuremode print cameracommand output = subprocess.check_output (cameracommand,shell=True, stderr=subprocess.STDOUT ) output = subprocess.check_output("convert '/home/pi/RasPiConnectServer/static/picameraraw.jpg' -pointsize 72 -fill white -gravity SouthWest -annotate +50+100 'ProjectCuracao %[exif:DateTimeOriginal]' '/home/pi/RasPiConnectServer/static/picamera.jpg'", shell=True, stderr=subprocess.STDOUT)
pclogging.log(pclogging.INFO, __name__, source )
print "finished taking picture" return
def takeSinglePicture(source, delay):
print("takeSinglePicture source:%s" % source)
GPIO.setmode(GPIO.BOARD) time.sleep(delay) # blink GPIO LED when it's run GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5)
print GPIO.VERSION
hardwareactions.openshutter()
time.sleep(3.0) takePicture("Single Picture Taken With Shutter")
hardwareactions.closeshutter()
return
Best regards,
BP
|
|
ve7mkf
New Member
Posts: 13
Raspberry Pi: Yes
|
Post by ve7mkf on Feb 25, 2014 16:04:27 GMT -8
Martin, I do have my camera working with the python picamera module as well as using the GPIO module for LED blink. I use an action button to take a picture. Maybe I shouldn't be doing it this way (security risk?), but I do run the RasPiConnectServer as root: "sudo python RasPiConnectServer.py" I also see that I did copy the remote picture web view code there, but the app is still using the W-2 code in the ExecuteRemoteWebView.py file (I also see I don't have the meta tags in the Local.py file needed to force a refresh). Will have to get that all fixed up as BP suggested to keep all changes to Local.py. Anyway, here is a complete copy of my Local.py file. I have an action button there to take a picture B-3, a button to blink an LED B-12, and a voltmeter M-1 to get the temperature, etc. I'm not much of a python programmer, so maybe not too pretty, but seems to work OK Hope this helps. I am out of town on business (1 more day), so not too much time to work on this stuff. At least my hotel has free internet so I can ssh into my system at home. Also had to leave the iPad at home for the kids and besides I haven't yet got a port opened up in my router to allow RasPiConnect to work outside of my local network at home. Mike #!/usr/bin/python # Filename: LocalExample.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 import RPi.GPIO as GPIO import time import os import glob import picamera
# 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-2 - play a beep on the Raspberry Pi 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
# note that python is in the main directory for this call, not the local directory
output = subprocess.call(["aplay", "sounds/match1.wav"]) responseData = "OK" outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# B-3 Take a photo with the RasPi Camera 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
# not validate request, so execute
# note that python is in the main directory for this call, not the local directory
# output = subprocess.call(["raspistill", "-o", "static/picamera.jpg"]) with picamera.PiCamera() as camera: camera.resolution = (1024, 768) camera.start_preview() time.sleep(2) camera.capture('static/picamera.jpg')
responseData = "OK" outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# B-12 - turn on GPIO 7 (LED) if (objectServerID == "B-12"):
#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.setmode(GPIO.BCM) GPIO.setup(7,GPIO.OUT) GPIO.output(7,GPIO.HIGH) time.sleep(2) GPIO.output(7,GPIO.LOW) GPIO.cleanup ()
responseData = "OK" outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData if (objectType == PICTURE_REMOTE_WEBVIEW_UITYPE):
if (Config.debug()): print "PICTURE_REMOTE_WEBVIEW_UITYPE of %s found" % objectServerID
#W-15 is latest pi camera picture if (objectServerID == "W-15"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# normal response requested
imageName = "picamera.jpg"
responseData = "<html><head>" responseData += "<title></title><style>body,html,iframe{margin:0;padding:0;}</style>" responseData += "</head>" responseData += "<body><img src=\"" responseData += Config.localURL() responseData += "static/" responseData += imageName responseData += "\" type=\"jpg\" width=\"585\" height=\"300\">" responseData += "</body>" responseData += "</html>" outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
if (objectType == VOLTMETER_UITYPE):
if (Config.debug()): print "VOLTMETER_UITYPE of %s found" % objectServerID
#M-1 is DS18B20 temperature if (objectServerID == "M-1"):
#check for validate request if (validate == "YES"): outgoingXMLData += Validate.buildValidateResponse("YES") outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
# normal response requested os.system('modprobe w1-gpio') os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*')[0] device_file = device_folder + '/w1_slave'
def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines
lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0
responseData = "%3.2f" % temp_c outgoingXMLData += BuildResponse.buildResponse(responseData) outgoingXMLData += BuildResponse.buildFooter() return outgoingXMLData
else: return "" # returning a zero length string tells the server that you have not matched # the object and server return ""
|
|
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 Feb 26, 2014 7:25:33 GMT -8
Martin, Here is the Python code that we use to generate a picture. We didn't use the picamera library because it interferes with the RPi.GPIO library. We run it via a cron (actually from apscheduler.py, but it is like a cron entry) entry, although you could embed calls to this file into a Local.py Webview command (W-10 for example). Note you HAVE to put the camera.jpg file in the static directory or web.py will ignore it. <snip> Best regards, BP BP, Thank you for your code, a little complex for me! I notice that it appears similar to the ProjectCuracao code which you have referenced previously. Is that project your own? Also, I see that you appear to use Raspistill to capture the image. Which kernel version do you run with that script, and/or have you required the workaround to get both the camera module, and GPIO working together happily? ==== Mike, Thanks for your Local.py file, that really is a great help. I think I am slowly beginning to see a little light at the end of the tunnel
Again, I thank you both for your invaluable help, patience and support. Best regards, Martin PS: how do you get the "whitespace" into your code postings Mike? When I copy and paste any code into or out of a forum page, that "whitespace" disappears! Even though I'm using the "code" tag.
|
|
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 Feb 26, 2014 10:02:06 GMT -8
Yay! Now have the app taking pics with the mods to Local.py which you supplied Mike - great! shutter.wav (12.02 KB) Here is the shutter sound I used along with the button (B-3) to alert that the pic has been taken!! This is the camera section with the "aplay" section added:- with picamera.PiCamera() as camera: camera.resolution = (320, 240) camera.start_preview() time.sleep(2) camera.capture('static/picam.jpg') output = subprocess.call(["aplay", "sounds/shutter.wav"]) It is interesting that the shutter sound only plays once using the B-3 button which you set up Mike, however the sounds produced by B-2 button plays twice. I cannot see why that is; there is only a single "aplay" call under that button heading. Many, many thanks Mike. Regards, Martin
|
|
|
Post by SDL on Feb 26, 2014 20:43:09 GMT -8
Martin,
I answered this question yesterday, but the forum gods did not publish my reply! So here it is again.
Regarding: "The server crash (internal server error 504) must have been entirely due to my malformed LocalExample.py file. Below is the LocalExample.py file I am currently using. It is no more than the "default" plus Mike's code, and the sound changed to one of my own. One thing - does the server require LocalExample.py to be in the local directory, or can there just be a local.py file constructed by me? I notice that the server generates a .pyc file when started."
You can change the LocalExample file or any of the other files, but you have to remember that when the server is updated it will OVERWRITE ALL FILES BUT THE Local.py file, so please copy LocalExample.py to Local.py and make your changes in the Local.py file.
The next version will have changes in the server, so be aware of this.
The picamera module interferes with RPi.GPIO so I have not used this module. I posted a bug but the author (to my knowledge) has not addressed the problem.
Here is we are using (and faraday over at ProjectCuracao) is using to do pi camera shots in python. You can import and call these routines in your Local.py.
Post your current Local.py and I will show you how to put it into a W-10 web view (note: You have to point RasPiconnect App to W-10!!!!!).
# Takes a single picture on System # filename: takeSinglePicture.py # Version 1.0 10/31/13 # # takes a picture using the camera # #
import sys import time import RPi.GPIO as GPIO import serial import picamera import subprocess import MySQLdb as mdb sys.path.append('/home/pi/ProjectCuracao/main/hardware') sys.path.append('/home/pi/ProjectCuracao/main/pclogging') sys.path.append('/home/pi/ProjectCuracao/main/util') import pclogging import util import hardwareactions
def sweepShutter(source, delay):
print("sweepShutter source:%s" % source) GPIO.setmode(GPIO.BOARD) time.sleep(delay) # blink GPIO LED when it's run GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5)
hardwareactions.sweepshutter()
time.sleep(3.0)
pclogging.log(pclogging.INFO, __name__, "Sweep Shutter")
def takePicture(source):
try: f = open("/home/pi/ProjectCuracao/main/state/exposure.txt", "r") tempString = f.read() f.close() lowername = tempString
except IOError as e: lowername = "auto"
exposuremode = lowername # take picture print "taking picture" cameracommand = "raspistill -o /home/pi/RasPiConnectServer/static/picameraraw.jpg -rot 180 -t 750 -ex " + exposuremode print cameracommand output = subprocess.check_output (cameracommand,shell=True, stderr=subprocess.STDOUT ) output = subprocess.check_output("convert '/home/pi/RasPiConnectServer/static/picameraraw.jpg' -pointsize 72 -fill white -gravity SouthWest -annotate +50+100 'ProjectCuracao %[exif:DateTimeOriginal]' '/home/pi/RasPiConnectServer/static/picamera.jpg'", shell=True, stderr=subprocess.STDOUT)
pclogging.log(pclogging.INFO, __name__, source )
print "finished taking picture" return
def takeSinglePicture(source, delay):
print("takeSinglePicture source:%s" % source)
GPIO.setmode(GPIO.BOARD) time.sleep(delay) # blink GPIO LED when it's run GPIO.setup(22, GPIO.OUT) GPIO.output(22, False) time.sleep(0.5) GPIO.output(22, True) time.sleep(0.5)
print GPIO.VERSION
hardwareactions.openshutter()
time.sleep(3.0) takePicture("Single Picture Taken With Shutter")
hardwareactions.closeshutter()
return
Best regards,
BP
|
|
|
Post by SDL on Feb 26, 2014 20:47:52 GMT -8
Martin,
No, the Project Curacao project is not my own, but we do use a lot of software from it. Faraday has done a great job.
Regarding your comment about "sudo python RasPiConnectServer.py" you do need to use sudo if your Pi has limited rights to GPIOs, pi cameras or I2C bus access.
Best regards,
BP
|
|
|
Post by SDL on Feb 26, 2014 20:54:53 GMT -8
Martin,
Just noticed another question in your post. You asked "Also, I see that you appear to use Raspistill to capture the image. Which kernel version do you run with that script, and/or have you required the workaround to get both the camera module, and GPIO working together happily?"
Kernel version? Linux Kernel version? We are using the standard "Wheezy" distribution.
By using raspistill instead of picamera, the GPIO and camera play well enough together. Including the PWM code, which is how we found the issue.
The pi camera LED is connected to GPIO 5.
Best regards,
BP
|
|
|
Post by nolasafari on Feb 27, 2014 17:06:26 GMT -8
Success, finally. I have managed to get 2 voltmeters reading my temp probes on the screen. Thanks for all the code samples. Will start moving on to some warnings for over heat and the such. Please keep posting in this thread I am learning a lot.
Thanks to all.
Jim
|
|
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 Feb 28, 2014 1:53:09 GMT -8
Martin, <snip> By using raspistill instead of picamera, the GPIO and camera play well enough together. Including the PWM code, which is how we found the issue. <snip> BP BP, Thanks for the extra input. I was puzzled by the fact that when I attempted to use raspistill command, after I had entered the modprobe commands, that the Pi failed to take a picture, throwing up an error message ending with something like " out of resources (other than memory)" Changing the entries in /etc/modules cured that ( my raspi-blacklist.conf had already been altered) and I was able to use raspistill, and access the DS18B20 sensor sucessfully. Now, however I have deleted the crontab entry, and use Mike's code with the python-picamera instead of raspistill. I thought I had solved my no refresh problem with the remote web picture by adjusting the refresh options under the B-3 customize button. All worked fine yesterday, picture was updated as soon as I took a snap; today it didn't work at first, but after a couple of tries voila! it works again. I guess I'll have to include the no-cache meta entries until a permanent fix is issued by you guys. Thanks again BP. Keep up the good work Martin
|
|
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 Feb 28, 2014 2:01:21 GMT -8
Success, finally. I have managed to get 2 voltmeters reading my temp probes on the screen. Thanks for all the code samples. Will start moving on to some warnings for over heat and the such. Please keep posting in this thread I am learning a lot. Thanks to all. Jim Hi Jim, That's great news! I have several DS18B20 devices that I am hoping to use, and not being any good at this stuff I wondered if you would consider sharing your method of displaying more than one device? Mike's code is for one device, and I am not proficient enough to work out how to add more. All of my DS18B20s ID numbers begin with 28-xxxxx etc. Would I have to expand device_folder = glob.glob(base_dir + '28*')[0] to include more of the ID number to identify each device, or is it a case of generating a new "meter" with associated ID (which is required anyway) and duplicating the Local.py entry to reflect the other device IDs? I'm learning, albeit extremely slowly Regards, Martin
|
|