Ok here goes. First you need to install the web.py application from
https://webpy.org/
(I still use python 2 and the code below is in python 2 so I think for python 2 it is......
pip install web.py
you also then need to ensure you have fswebcam installed which will run your USB camera
sudo apt install fswebcam
copy the code below into the web.py folder and name it something like "website.py"
finally you need to start the web.py webserver by CDing to the web.py directory (think it is web.py_0.38 by default, I renamed mine to web.py which is why my code refers to /home/pi/web.py/)
python website.py xxxx
where xxxx is the port you want the webserver to run on (e.g. 8888)
then in a browser navigate to the IP address of the PI and the port e.g. if you are on the same local network:
192.168.0.7:8888
If everything works you should after a few seconds see a still webcam image in a webpage.
DISCLAIMER: I have cut my webserver script down a lot to make it clearer, I may have cut something important out accidentally...
DISCLAIMER 2: other python based webservers are available, e.g. simpleHTTPserver is another possibility.
DISCLAIMER 3: if something is wrong in web.py its error messages are super-cryptic. If you have problems let me know....
Code: Select all
import web
import time
import commands
import urllib
urls = (
'/', 'index'
)
data=""
class index:
def GET(self):
a=commands.getoutput("sudo fswebcam -d /dev/video0 --fps 3 -S 3 --subtitle CAMERA -r 640x480 /home/pi/web.py/static/camera.jpg")
data= '<!DOCTYPE html>'
data=data+'<html>'
data=data+'<body>'
data=data+'<b> CAMERA OUTPUT </b>'
data=data+'<img src="/static/camera.jpg" alt="HTML5 Icon">'
data=data+'<body>'
data=data+'<html>'
return data
if __name__ == "__main__":
app = web.application(urls, globals())
time.sleep(1)
app.run()