Earlier I wrote about the mechanism of starting a server with Python and operating Arduino from a browser.
http://qiita.com/drafts/9abfa1a4b6bf66a5f8fa/edit
At this time, I was using a framework called Bottle, but since the interface that handles images with JavaScript such as getUserMedia can only be used with HTTPS, it is difficult to make Bottle compatible with HTTPS, so I changed it to a framework called CherryPy. It was.
First of all, as a preliminary work, make an oleore certificate referring to the following. http://docs.cherrypy.org/en/latest/deploy.html#ssl
$ openssl genrsa -out privkey.pem 2048
$ openssl req -new -x509 -days 365 -key privkey.pem -out cert.pem
Here is a sample to display Hello world by specifying this pem. Please check the operation here before connecting to Arduino.
import cherrypy
cherrypy.server.ssl_module = 'builtin'
cherrypy.server.ssl_certificate = "cert.pem"
cherrypy.server.ssl_private_key = "privkey.pem"
class HelloWorld(object):
@cherrypy.expose
def index(self):
return "Hello world!"
if __name__ == '__main__':
cherrypy.quickstart(HelloWorld())
Your browser gets angry, but don't worry. ~~ I'm wearing it. ~~ Normal operation.
Here is the addition of serial transmission by pyserial and CORS support (setting to allow access across domains in the HTTP header).
import cherrypy
cherrypy.server.ssl_module = 'builtin'
cherrypy.server.ssl_certificate = "cert.pem"
cherrypy.server.ssl_private_key = "privkey.pem"
import serial
ser = serial.Serial('/dev/tty.usbmodem1411',9600)
def multi_headers():
cherrypy.response.header_list.extend(
cherrypy.response.headers.encode_header_items(
cherrypy.response.multiheaders))
cherrypy.tools.multiheaders = cherrypy.Tool('on_end_resource', multi_headers)
class CherryArduino(object):
@cherrypy.expose
@cherrypy.tools.multiheaders()
def index(self,value='Hello'):
cherrypy.response.multiheaders = [('Access-Control-Allow-Origin', '*')]
ser.write(str(value))
return value
if __name__ == '__main__':
cherrypy.quickstart(CherryArduino())
If you do this and access https://127.0.0.1:8080/?value=1
, the value" 1 "will be sent to Arduino.
Please refer to here for the script on the Arduino side.
http://qiita.com/usopyon/items/aada42d44c6f2cd0a3ff
I also put it on Github, so please click here as well.
https://github.com/usopyon/cherrypy_serial
Recommended Posts