Go Down

Topic: WebSockets with Yun? (Read 32107 times) previous topic - next topic

alnitak

It would be great if someone would post a link to a blog about how to get pin data communicated to a websocket running on the linino in near real time and then show us how to see the pin data on a web page that communicates with the websocket running on the linino. I've been trying to figure out how to modify the autobahn websocket version of the serial2ws project, but can't figure out how to change from querying the serial port to querying the bridge data.  Ultimately, I'd like to duplicate this sort of thing:

http://yopero-tech.blogspot.com/2012/02/arduino-websocket.html

robbyar

Hi.
I modified the code of ElArtista in order to send back data from Arduino to web page through websocket.
My application uses a wab page and websocket in order to send controls to a rover and to receive data from sensors mounted on the rover itself.

Here the Html code:
Code: [Select]

var ws = new WebSocket ("ws:urlarduino:8888/w/");
ws.onopen = function()
{
alert("WebSocket Opened");
}
ws.onmessage = function(evt)
                {
        alert(evt.data); //Displays received sensor data
                }
}
ws.onclose = function()
{
alert("WebSocket Closed");
}

var intervalID = setInterval(function readVoltage(){ws.send("X");}, 5000); //polling from the web page: it sends to Arduino char "X" that forces it to send back sensor data


Here the Arduino code:
Code: [Select]

void setup() {
Serial1.begin(57600); // Set the baud.
while (!Serial1) { }
}

void loop() {
if (Serial1.available() > 0)
  { 
    byte command = Serial1.read();
    if (command=='X'){
    delay(1);
    long r = readVcc(); //reads sensor data
    Serial1.println(r);    // sends sensor data
    }
  }
}


Here python code:
Code: [Select]

import tornado.ioloop
import tornado.web
import tornado.websocket
import serial

from tornado.options import define, options, parse_command_line
ser = serial.Serial(port='/dev/ttyATH0', baudrate=57600, timeout=2)
define("port", default=8888, help="", type=int)
class WebSocketHandler(tornado.websocket.WebSocketHandler):

    def open(self, *args):
        print "Connected"
    def on_message(self, message):
        ser.open()
        while not ser.isOpen():
              pass
        dato_1 = str(message)
        ser.write(dato_1)
        print (message)   
        if message == 'X':
           try:
             byteData = ser.read(4) #I expect 4 byte of data from Arduino
             self.write_message(byteData) #Send data to webpage
           except Exception:
             pass
        ser.close()
    def on_close(self):
        print "Disconnected"
app = tornado.web.Application([
    (r'/w/', WebSocketHandler),
])
if __name__ == '__main__':
    parse_command_line()
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()


Note that in python I used try statement to handle exception. Sometimes serial port is not available and the program tends to stop. With the try statement the program continues execution even if there is something wrong in serial communication.
This part should be improved, but I am novice in python and serial.
If someone can improve it, I will be grateful.

I hope this help.

Bye

dfronin

Hi

I'm trying to make a robot controlled by a web page served from the yun. I have installed Tornado on the linux side. I understand how to manipulate the arduino scripts and the HTML code. I have difficulty setting up the Tornado and linux side of the house to make this work.

Is anyone willing to give me a hand and teach me a few things. I've attempted autobahn and crossbar.io but have difficulties setting those up correctly.


Thanks in advance for the help.

sonnyyu


jessemonroy650

Hi

I'm trying to make a robot controlled by a web page served from the yun. I have installed Tornado on the linux side. I understand how to manipulate the arduino scripts and the HTML code. I have difficulty setting up the Tornado and linux side of the house to make this work.

Is anyone willing to give me a hand and teach me a few things. I've attempted autobahn and crossbar.io but have difficulties setting those up correctly.


Thanks in advance for the help.
@dfronin,
please do not restart old threads, especially if they are off-topic and over one year old. Please start a new thread with your request.

TIA
Jesse

Go Up