Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save kelvinninja1/b67947ad6bf6ff934c93c7614d95a148 to your computer and use it in GitHub Desktop.

Select an option

Save kelvinninja1/b67947ad6bf6ff934c93c7614d95a148 to your computer and use it in GitHub Desktop.

Revisions

  1. @otomura otomura created this gist Oct 24, 2015.
    33 changes: 33 additions & 0 deletions arduino_serial_monitor.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import flask
    import serial

    app = flask.Flask(__name__)
    arduino_serial = serial.Serial('/dev/ttyACM0', 9600)

    def event_stream():
    while True:
    yield "data: " + arduino_serial.readline() + "\n\n"

    @app.route('/stream')
    def stream():
    return flask.Response(event_stream(), mimetype="text/event-stream")

    @app.route('/')
    def index():
    return """
    <!doctype html>
    <title>Arduino serial monitor</title>
    <div id="data"/>
    <script>
    (function(){
    var source = new EventSource('/stream');
    var data = document.getElementById('data');
    source.onmessage = function(e) {
    data.innerHTML = e.data + '<br/>' + data.innerHTML;
    };
    })();
    </script>
    """

    if __name__ == '__main__':
    app.run(debug = True)