Skip to content

Instantly share code, notes, and snippets.

@hrafnkelle
Created December 1, 2014 12:05
Show Gist options
  • Save hrafnkelle/c6d7c56883f93aa3d5d7 to your computer and use it in GitHub Desktop.
Save hrafnkelle/c6d7c56883f93aa3d5d7 to your computer and use it in GitHub Desktop.
HTTP server for Raspberry Pi Camera
import time
import http.server
import picamera
import datetime as dt
import shutil
from fractions import Fraction
HOST_NAME = 'raspiplus.lan'
PORT_NUMBER = 9000
class MyHandler(http.server.BaseHTTPRequestHandler):
camera = picamera.PiCamera()
camera.hflip = True
camera.vflip = True
camera.resolution = (1280, 720)
#camera.resolution = (640,480)
# camera.iso = 800
# camera.framerate = 2
# camera.meter_mode = 'spot'
camera.exposure_mode= 'auto'
def do_GET(s):
if s.path=="/img":
s.send_response(200)
s.send_header("Content-type", "image/jpeg")
s.send_header("Cache-Control", "no-cache, must-revalidate, no-store")
s.end_headers()
s.camera.capture(s.wfile, 'jpeg')
elif s.path=="/":
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write(bytes(htmldoc,'utf-8'))
else:
s.send_error(401)
htmldoc = """<!DOCTYPE HTML>
<html>
<head>
<script>
function refresh(node)
{
var times = 4000; // gap in Milli Seconds;
(function startRefresh() {
node.src = node.src
setTimeout(startRefresh,times);
})();
}
window.onload = function() {
var node = document.getElementById('img');
refresh(node);
}
</script>
</head>
<body>
<img id="img" src="/img" />
</body>
</html>"""
if __name__ == '__main__':
httpd = http.server.HTTPServer((HOST_NAME, PORT_NUMBER), MyHandler)
print("%s: Server Starts - %s:%s" % (time.asctime(),HOST_NAME, PORT_NUMBER))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print("%s: Server Stops - %s:%s" % (time.asctime(), HOST_NAME, PORT_NUMBER))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment