Go Down

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

mundosimaginados

Here you can find examples of how to run tornado in a trhead...
http://stackoverflow.com/questions/5375220/how-do-i-stop-tornado-web-server
I think the solution will be launch the serial listen in other trhead.


mamu

Hello ElArtista!
I will go the tornado road and give it a try.
Your wrote about installing several python packages.
Could you give me some more and detailed advice on how to do this. I'm no Linux or Python expert.
Why did you install the OpenSSL packages? Does tornado require these?
Many thanks in advance.

mundosimaginados

The necesary packages will be: pyserial, pyopenssl, python-openssl. One way is install tornado and looking for the requested dependencies.

These packages are availables from luci interface or via opkg installer. If you are not really expert try with web interface luci. You have the search feature that will help you to find the packages.


mamu

Thanks for your reply.
I already figured out on how to use opkg update and installed serial.io.
Can you tell me if the tornado installation is permanent? Will I survive a power off or reset?

mundosimaginados

If you are worried because you want to be sure you can do a complete uninstall I read that it's pure Python and you can be safe deleting the folder that contain the py files... I haven't tested.

http://serverfault.com/questions/383693/uninstall-tornado-web-server

mamu

In the meantime have you optimized your tornado that it uses threads and have you established a two way communication ?

mamu

#36
Nov 13, 2013, 07:19 pm Last Edit: Nov 13, 2013, 07:28 pm by mamu Reason: 1
ElArtisa,

hurrah it works!
Got your sample up and running.
Will now try to get a bidirectional communication and  enable WebSocketHandler on a thread.

mamu

#37
Nov 16, 2013, 03:42 pm Last Edit: Nov 16, 2013, 03:46 pm by mamu Reason: 1
Hi there!
I'm playing around with Serial1 to get a two way communication with my python script.
But I'm totally out of luck. Tried so many things but I'm only able to get it one way talking from python to Arduino.
Maybe someone can give me some help?

Here's my Arduino sketch:
Code: [Select]

volatile bool sendData = false;
volatile bool bootDone = false;

void setup() {
 pinMode(13, OUTPUT);  
 Serial.begin(9600);  
 Serial1.begin(115200); // Set the baud.
 while (!Serial1)
 {
 }
}

void loop()
{
 if (!bootDone) {
    // Wait for U-boot to finish startup.  Consume all bytes until we are done.
   do {
      while (Serial1.available() > 0) {
         Serial1.read();
      }      
     delay(1000);
   } while (Serial1.available() > 0);
   
   bootDone = true;
 }
 
 if (sendData) {
   Serial1.println("test=[[1,2],[3,4]]");
   Serial1.flush();
   sendData = false;
 }
}

void serialEvent1() {
 if (!bootDone)
   return;

 char buffer[50];
 int result = Serial1.readBytesUntil('\n', buffer, 50);
 if (result <= 0) {
   Serial.println("error");
   return;
 }

 buffer[result] = '\0';  
 Serial.println(buffer);  
 sendData = true;
}


And on the python site I use this script which runs using tornado:
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', 115200, timeout=0.5)
define("port", default=8888, help="run on the given port", type=int)
clients = set()

class Application(tornado.web.Application):
   def __init__(self):
       handlers = [
           (r"/", WebSocketHandler),
       ]
       tornado.web.Application.__init__(self, handlers,)

class WebSocketHandler(tornado.websocket.WebSocketHandler):
   def open(self, *args):
       clients.add(self)
       print "Connected"

   def on_message(self, message):
       dato_1 = str(message)
       print "%s received" % (dato_1)

       if dato_1.startswith("write="):
           ser.write("AB\n")
           ser.write('B')
           ser.write('\n')

           data = ser.read(1)  
           n = ser.inWaiting()  
           if n:
               data = data + ser.read(n)
           if data:
               print (str(data))

   def on_close(self):
       print "Disconnected"
       clients.remove(self)

def main():
   tornado.options.parse_command_line()
   app = Application()
   app.listen(options.port)
   tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
   main()


But I always get his output and error:
Code: [Select]

[E 131116 15:34:21 websocket:306] Uncaught exception in /
    Traceback (most recent call last):
      File "/usr/lib/python2.7/site-packages/tornado/websocket.py", line 303, in wrapper
        return callback(*args, **kwargs)
      File "/www/sd/yun_tests/server.py", line 85, in on_message
        data = ser.read(1)              # read one, blocking
      File "/usr/lib/python2.7/site-packages/serial/serialposix.py", line 353, in read
        buf = os.read(self.fd, size-len(read))
    OSError: [Errno 11] Resource temporarily unavailable

And on a second try this one:
Code: [Select]

write=[[23,56],[77,44]] received
[E 131116 15:35:04 websocket:306] Uncaught exception in /
   Traceback (most recent call last):
     File "/usr/lib/python2.7/site-packages/tornado/websocket.py", line 303, in wrapper
       return callback(*args, **kwargs)
     File "/www/sd/yun_tests/server.py", line 79, in on_message
       ser.write("AB\n")
     File "/usr/lib/python2.7/site-packages/serial/serialposix.py", line 372, in write
       n = os.write(self.fd, d)
   OSError: [Errno 5] Input/output error

violinuxer


Hi there!
I'm playing around with Serial1 to get a two way communication with my python script.
But I'm totally out of luck. Tried so many things but I'm only able to get it one way talking from python to Arduino.
Maybe someone can give me some help?


First of all, do you get any output from the following command? Run the command and then reset the ATMega. You should see your test message on screen.
Code: [Select]
# cat /dev/ttyATH0

It looks to me like /dev/ttyATH0 is in use. From my experience, Linino (by default) spawns a shell on /dev/ttyATH0, which may have ownership of the device. Take a look at the source in Bridge.h, it gives some good insights on how the serial connection works. Also take a look at the files on the yun in the /usr/lib/python2.7/bridge/ folder. I should also note that the serial bridge runs at 250000 baud, not 115200.

You might try commenting out the line in /etc/inittab that contains spawns a shell on /dev/ATH0. Before you do this, make sure you can access the device over SSH, as it WILL disable the SerialTerm sketch that Arduino.cc provides.

Hope this is of some help!

violinuxer

mamu


You might try commenting out the line in /etc/inittab that contains spawns a shell on /dev/ATH0. Before you do this, make sure you can access the device over SSH, as it WILL disable the SerialTerm sketch that Arduino.cc provides.

Hm, must have done something wrong. Renamed inittab to mminittab and now I'm no longer able to connect using ssh.
I get:
-ssh: Could not resolve hostname arduino.local.: nodename nor servname provided, or not known

violinuxer

First of all, did you leave a copy of /etc/inittab? Also, you can try ssh to 192.168.240.1 (the arduino's IP address). Otherwise, I can help you reset to factory settings if you need it.

Good luck!

violinuxer

mamu


First of all, did you leave a copy of /etc/inittab? Also, you can try ssh to 192.168.240.1 (the arduino's IP address). Otherwise, I can help you reset to factory settings if you need it.

Good luck!

violinuxer

Nope, no copy of inittab. I renamed it.
Now trying using ethernet port and not over Wifi.

violinuxer



First of all, did you leave a copy of /etc/inittab? Also, you can try ssh to 192.168.240.1 (the arduino's IP address). Otherwise, I can help you reset to factory settings if you need it.

Good luck!

violinuxer

Nope, no copy of inittab. I renamed it.
Now trying using ethernet port and not over Wifi.


Oh boy! Linux definitely won't boot without inittab....  :smiley-eek-blue:

I should have been more explicit... The arduino linux part won't boot fully without inittab. We should be able to rescue the system through failsafe mode.

First, copy and paste the sketch from the bottom of this page into the Arduino IDE and then download it to your Yun over the USB cable (COM<some number>). You can select the port in the tools-> ports menu of the IDE.

http://arduino.cc/en/Tutorial/YunSerialTerminal

With the arduino still plugged into your computer, open up the serial monitor, then hit the YUN RST button on your arduino (just inside the pin headers).

You should get output from the Arduino boot process. Copy and paste the output into a forum post so we can see whats going on...

violinuxer

mamu

Oh, man. Hope you can help me fixing it. I have to make 2 posts because of the maximum number of chars per post (which is 9500)

Code: [Select]

U-Boot 1.1.4-gd995931a-dirty (Aug  7 2013 - 16:16:04)

Arduino Yun (ar9331) U-boot

DRAM:  64 MB
Top of RAM usable for U-Boot at: 84000000
Reserving 142k for U-Boot at: 83fdc000
Reserving 192k for malloc() at: 83fac000
Reserving 44 Bytes for Board Info at: 83fabfd4
Reserving 36 Bytes for Global Data at: 83fabfb0
Reserving 128k for boot params() at: 83f8bfb0
Stack Pointer at: 83f8bf98
Now running in RAM - U-Boot at: 83fdc000
Flash Manuf Id 0xef, DeviceId0 0x40, DeviceId1 0x18
flash size 16777216, sector count = 256
Flash: 16 MB
Using default environment

In:    serial
Out:   serial
Err:   serial
Net:   ag7240_enet_initialize...
No valid address in Flash. Using fixed address
No valid address in Flash. Using fixed address
: cfg1 0x5 cfg2 0x7114
eth0: 00:03:7f:09:0b:ad
eth0 up
: cfg1 0xf cfg2 0x7214
eth1: 00:03:7f:09:0b:ad
athrs26_reg_init_lan
ATHRS26: resetting s26
ATHRS26: s26 reset done
eth1 up
eth0, eth1
Hit any key to stop autoboot:  4  3  2  1  0
## Booting image at 9fea0000 ...
  Image Name:   MIPS OpenWrt Linux-3.8.3
  Created:      2013-08-19  14:23:28 UTC
  Image Type:   MIPS Linux Kernel Image (lzma compressed)
  Data Size:    1170186 Bytes =  1.1 MB
  Load Address: 80060000
  Entry Point:  80060000
  Verifying Checksum at 0x9fea0040 ...OK
  Uncompressing Kernel Image ... OK
No initrd
## Transferring control to Linux (at address 80060000) ...
## Giving linux memsize in bytes, 67108864

Starting kernel ...

[    0.000000] Linux version 3.8.3 (federico@smilzo) (gcc version 4.6.4 20121210 (prerelease) (Linaro GCC 4.6-2012.12) ) #8 Mon Aug 19 16:22:39 CEST 2013
[    0.000000] bootconsole [early0] enabled
[    0.000000] CPU revision is: 00019374 (MIPS 24Kc)
[    0.000000] SoC: Atheros AR9330 rev 1
[    0.000000] Clocks: CPU:400.000MHz, DDR:400.000MHz, AHB:200.000MHz, Ref:25.000MHz
[    0.000000] Determined physical RAM map:
[    0.000000]  memory: 04000000 @ 00000000 (usable)
[    0.000000] Initrd not found or empty - disabling initrd
[    0.000000] Zone ranges:
[    0.000000]   Normal   [mem 0x00000000-0x03ffffff]
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x00000000-0x03ffffff]
[    0.000000] Primary instruction cache 64kB, VIPT, 4-way, linesize 32 bytes.
[    0.000000] Primary data cache 32kB, 4-way, VIPT, cache aliases, linesize 32 bytes
[    0.000000] Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 16256
[    0.000000] Kernel command line:  board=Linino console=ttyATH0,250000 mtdparts=spi0.0:256k(u-boot)ro,64k(u-boot-env)ro,14656k(rootfs),1280k(kernel),64k(nvram),64k(art),15936k@0x50000(firmware) rootfstype=squashfs,jffs2 noinitrd
[    0.000000] PID hash table entries: 256 (order: -2, 1024 bytes)
[    0.000000] Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)
[    0.000000] Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)
[    0.000000] __ex_table already sorted, skipping sort
[    0.000000] Writing ErrCtl register=00000000
[    0.000000] Readback ErrCtl register=00000000
[    0.000000] Memory: 60900k/65536k available (2565k kernel code, 4636k reserved, 655k data, 232k init, 0k highmem)
[    0.000000] SLUB: Genslabs=9, HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[    0.000000] NR_IRQS:51
[    0.000000] Calibrating delay loop... 265.42 BogoMIPS (lpj=1327104)
[    0.080000] pid_max: default: 32768 minimum: 301
[    0.080000] Mount-cache hash table entries: 512
[    0.080000] NET: Registered protocol family 16
[    0.090000] MIPS: machine is Arduino Yun
[    0.530000] Setting DogStick2 GPIO
[    0.550000] bio: create slab <bio-0> at 0
[    0.550000] SCSI subsystem initialized
[    0.550000] usbcore: registered new interface driver usbfs
[    0.560000] usbcore: registered new interface driver hub
[    0.560000] usbcore: registered new device driver usb
[    0.560000] Switching to clocksource MIPS
[    0.570000] NET: Registered protocol family 2
[    0.570000] TCP established hash table entries: 512 (order: 0, 4096 bytes)
[    0.570000] TCP bind hash table entries: 512 (order: -1, 2048 bytes)
[    0.570000] TCP: Hash tables configured (established 512 bind 512)
[    0.570000] TCP: reno registered
[    0.570000] UDP hash table entries: 256 (order: 0, 4096 bytes)
[    0.570000] UDP-Lite hash table entries: 256 (order: 0, 4096 bytes)
[    0.580000] NET: Registered protocol family 1
[    0.600000] squashfs: version 4.0 (2009/01/31) Phillip Lougher
[    0.600000] jffs2: version 2.2 (NAND) (SUMMARY) (LZMA) (RTIME) (CMODE_PRIORITY) (c) 2001-2006 Red Hat, Inc.
[    0.610000] msgmni has been set to 118
[    0.610000] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 253)
[    0.610000] io scheduler noop registered
[    0.610000] io scheduler deadline registered (default)
[    0.610000] Serial: 8250/16550 driver, 16 ports, IRQ sharing enabled
[    0.620000] ar933x-uart: ttyATH0 at MMIO 0x18020000 (irq = 11) is a AR933X UART
[    0.630000] console [ttyATH0] enabled, bootconsole disabled
[    0.630000] console [ttyATH0] enabled, bootconsole disabled
[    0.630000] ath79-spi ath79-spi: master is unqueued, this is deprecated
[    0.640000] m25p80 spi0.0: found w25q128, expected m25p80
[    0.640000] m25p80 spi0.0: w25q128 (16384 Kbytes)
[    0.640000] 7 cmdlinepart partitions found on MTD device spi0.0
[    0.650000] Creating 7 MTD partitions on "spi0.0":
[    0.650000] 0x000000000000-0x000000040000 : "u-boot"
[    0.650000] 0x000000040000-0x000000050000 : "u-boot-env"
[    0.660000] 0x000000050000-0x000000ea0000 : "rootfs"
[    0.660000] mtd: partition "rootfs" set to be root filesystem
[    0.660000] mtd: partition "rootfs_data" created automatically, ofs=720000, len=780000
[    0.660000] 0x000000720000-0x000000ea0000 : "rootfs_data"
[    0.670000] 0x000000ea0000-0x000000fe0000 : "kernel"
[    0.670000] 0x000000fe0000-0x000000ff0000 : "nvram"
[    0.680000] 0x000000ff0000-0x000001000000 : "art"
[    0.680000] 0x000000050000-0x000000fe0000 : "firmware"
[    0.700000] libphy: ag71xx_mdio: probed
[    0.700000] eth0: Atheros AG71xx at 0xba000000, irq 5, mode:GMII
[    1.250000] eth0: Found an AR7240/AR9330 built-in switch
[    2.280000] eth1: Atheros AG71xx at 0xb9000000, irq 4, mode:MII
[    2.830000] ag71xx ag71xx.0 eth1: connected to PHY at ag71xx-mdio.1:04 [uid=004dd041, driver=Generic PHY]
[    2.830000] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[    2.840000] ehci-platform: EHCI generic platform driver
[    2.840000] ehci-platform ehci-platform: EHCI Host Controller
[    2.840000] ehci-platform ehci-platform: new USB bus registered, assigned bus number 1
[    2.840000] ehci-platform ehci-platform: irq 3, io mem 0x1b000000
[    2.870000] ehci-platform ehci-platform: USB 2.0 started, EHCI 1.00
[    2.870000] usb usb1: New USB device found, idVendor=1d6b, idProduct=0002
[    2.870000] usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1
[    2.870000] usb usb1: Product: EHCI Host Controller
[    2.880000] usb usb1: Manufacturer: Linux 3.8.3 ehci_hcd
[    2.880000] usb usb1: SerialNumber: ehci-platform
[    2.880000] hub 1-0:1.0: USB hub found
[    2.880000] hub 1-0:1.0: 1 port detected
[    2.890000] Initializing USB Mass Storage driver...
[    2.890000] usbcore: registered new interface driver usb-storage
[    2.890000] USB Mass Storage support registered.
[    2.890000] input: gpio-keys-polled as /devices/platform/gpio-keys-polled/input/input0
[    2.900000] sdhci: Secure Digital Host Controller Interface driver
[    2.900000] sdhci: Copyright(c) Pierre Ossman
[    2.900000] usbcore: registered new interface driver ushc
[    2.900000] TCP: cubic registered
[    2.900000] NET: Registered protocol family 17
[    2.910000] Bridge firewalling registered
[    2.910000] 8021q: 802.1Q VLAN Support v1.8
[    2.920000] VFS: Mounted root (squashfs filesystem) readonly on device 31:2.
[    2.920000] Freeing unused kernel memory: 232k freed
[    3.250000] usb 1-1: new high-speed USB device number 2 using ehci-platform
[    3.440000] usb 1-1: New USB device found, idVendor=058f, idProduct=6254
[    3.440000] usb 1-1: New USB device strings: Mfr=0, Product=1, SerialNumber=0
[    3.440000] usb 1-1: Product: USB2.0Hub
[    3.550000] hub 1-1:1.0: USB hub found
[    3.550000] hub 1-1:1.0: 4 ports detected
[    3.940000] usb 1-1.4: new high-speed USB device number 3 using ehci-platform

mamu

and here the rest:
Code: [Select]
[    4.070000] usb 1-1.4: New USB device found, idVendor=058f, idProduct=6366
[    4.070000] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[    4.070000] usb 1-1.4: Product: Mass Storage Device
[    4.080000] usb 1-1.4: Manufacturer: Generic
[    4.080000] usb 1-1.4: SerialNumber: 058F0O1111B1
[    4.100000] scsi0 : usb-storage 1-1.4:1.0
[    5.130000] scsi 0:0:0:0: Direct-Access     Multi    Flash Reader     1.00 PQ: 0 ANSI: 0
[    5.300000] Error: Driver 'gpio-keys-polled' is already registered, aborting...
- preinit -
Press the [f] key and hit [enter] to enter failsafe mode
- regular preinit -
[   10.040000] jffs2: notice: (528) jffs2_build_xattr_subsystem: complete building xattr subsystem, 1 of xdatum (1 unchecked, 0 orphan) and 13 of xref (0 dead, 0 orphan) found.
[   12.020000] sd 0:0:0:0: [sda] 1987584 512-byte logical blocks: (1.01 GB/970 MiB)
[   12.030000] sd 0:0:0:0: [sda] Write Protect is off
[   12.030000] sd 0:0:0:0: [sda] No Caching mode page present
[   12.030000] sd 0:0:0:0: [sda] Assuming drive cache: write through
[   12.040000] sd 0:0:0:0: [sda] No Caching mode page present
[   12.040000] sd 0:0:0:0: [sda] Assuming drive cache: write through
[   12.050000]  sda: sda1
[   12.050000] sd 0:0:0:0: [sda] No Caching mode page present
[   12.050000] sd 0:0:0:0: [sda] Assuming drive cache: write through
[   12.050000] sd 0:0:0:0: [sda] Attached SCSI removable disk
switching to jffs2
- init -

Please press Enter to activate this console.

Go Up