Go Down

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

mamu

Do you think one can make node.js Serialport to point to the matching tty?
Which tty is the one used by the sketch? ttyATH0?

mundosimaginados

I'm working with tornado python web server... it's ok for me.

mamu

ok and on which serial port are you listening?

mundosimaginados

For linux side check the tty trhead in forum. The name isis some think like ttyATH....: in arduino side use serial1. Excuse me I dont have yun here with me. Yo can find the names in the sources of bridge library & in the source of bridge.py in linux side.

mamu


Yo can find the names in the sources of bridge library & in the source of bridge.py in linux side.

Hm, sorry, I was not able to find the serial port names in any of the two files.

federicofissore

The name of the serial device used by the ar9331 and 32u4 to communicate is /dev/ttyATH0

mamu


The name of the serial device used by the ar9331 and 32u4 to communicate is /dev/ttyATH0

Thank you very much.

Do you think that the following will work?
* Install node.js on Arduino Linino
* Make the SerialPort library listen on /dev/ttyATH0
* Communicate from an Arduino sketch using Serial1

federicofissore

I'm not sure, because on that device there is the linux terminal, so if you just write something it will be interpreted as a command (and probably issue a "command not found")
But it may depend on the way node takes control (or not) of the serial (maybe it consumes the output before it reaches the linux prompt?). It definitely something worth experimenting with

mamu

Okay I will try and report here.

mundosimaginados

I confirm you that it work. I achieved to communicate the environments through the serial1 port at 57000 ... installing pyserial in python.

mamu

Sounds great.
Did you establish a two way communication ?
Would you mind to post your server configs?

mundosimaginados

At this moment I worked in sending from linino to arduino... I'm still working on.

mundosimaginados

Hello here are my codes:

In the html, js or the enviroment from where you want to connect to the socket we must open the socket and send the message (in this case a letter, one byte) this html can be hosted in YUN or in other sever. You can use other languaje or environment that support sockets for interact...
Code: [Select]

weso = new WebSocket("weso://ARDUINO.YUN.IP.ADRESS:8888/w/");
weso.send('A');


We must install the packages for python in the yun linux enviroment: pyserial, pyopenssl, python-openssl.
Download tornado: http://www.tornadoweb.org/en/stable/ and install with python setup.py install

Run this python script-server:
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('/dev/ttyATH0', 57600)
define("port", default=8888, help="", type=int)
class WebSocketHandler(tornado.websocket.WebSocketHandler):
    def open(self, *args):
        print "Connected"
    def on_message(self, message):
        dato_1 = str(message)
        print (message)
ser.write(dato_1)
    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()


Finally load this scketch in the arduino side:

Code: [Select]

byte data_readed = 0;
void setup() {
  pinMode(13, OUTPUT);
  Serial1.begin(57600); // Set the baud.
  while (!Serial1)
  {
  }
}

void loop()
{
  if (Serial1.available() > 0)
  { 
    data_readed = Serial1.read();
    if (data_readed == 'A')
    {
      pin_on_off();
    }
  }       
}
void pin_on_off()
{
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(200);               // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(200);               
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(200);               // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
}


When the "A" arrives to the Arduino it will do an blinking effect in pin 13.

There is an improvement that can be made. The tornado server can serve the html page. I can't achieve to do. If some on can help please modify file and post.

It's important too t to ake care of the kind of info you are sending. If the info are character, int ... bytes... I'm working on that.

In my case I don't need to send info from Arduino to linux at this moment. My main code will be in the js program. If some one modify and improve, please, share. If not I'll work in that in near future.



mamu

Thanks for sharing. I will try this approach using node.js.
Is there a reason why you use port 8888 for WrbSocket communication?
I think that this could be the reason why your server does not serve HTML. Have you tried to load your HTML page from 8888?

mundosimaginados

I used 8888 as is like whatever-port... itsn't mather... And it's not confliting with the html server because in tornado (I'm not an expert) you can setup the response for each service. If you want to add the server web for html feature you must change the definition os services like that:

Code: [Select]
app = tornado.web.Application([
    (r'/', IndexHandler),
    (r'/w/', WebSocketHandler),
])


And implemet the IndexHandler class with your response to html server configuration. You can do ussing html pages stored in local with a wide array of options based on templates: http://www.tornadoweb.org/en/stable/template.html

Inform us, please, of your advances and share it here, thanks.


Go Up