Thursday, January 29, 2015

Publishing sketch values on a web page hosted by the Yun

This is a simple way to get values from the sketch made visible to a browser that can connect to the Yun.
The Arduino Yun wil be used as leightweight webserver.
The sketch just puts the values in the Bridge, the webserver reads them from there.

I am using "bottle" as example webserver.

Sketch code:

/* vim: set filetype=cpp: */
#include 

void setup()
{
  Bridge.begin();
}

void loop()
{

    char value[40];  // 40 is probably too much
    uint32_t my_time;
    uint32_t my_random;

    while(true)
    {
       my_time=millis();
       my_random=random(99);

       ultoa(my_time,value,10);
       Bridge.put("TIME",value);
       ultoa(my_random,value,10);
       Bridge.put("RAND",value);

       delay(1000);
    }

}

Python code:

#!/usr/bin/python

import sys
from bottle import run, route, get, post, request

sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
bc = bridgeclient()

@route('/status')
def status():
    try:
        t =  bc.get("TIME")
        r =  bc.get("RAND")
        return "Time="+t+"
 Rand="+r
    except:
        return "Oops\n"

def main():

    run(host='0.0.0.0', port=48964, debug=True)

if __name__ == '__main__':
    main()


After starting the python code on the linux side you can browse to http://arduino.local:48964/status and get:


Wednesday, December 31, 2014

Create a SWAP partition on the Arduino Yun

This will create a 128MB (=2x RAM) swap partition on the Yun's SD card. I already have 2 partitions on the SD, this will be the 3th. First create an extra partition and format it as explained at

http://myyafa.blogspot.be/2014/05/create-and-format-partitions-on-yun-sd.html

  After another reboot (maybe using mount command this reboot isn't needed?), df displays:
Filesystem           1K-blocks      Used Available Use% Mounted on
rootfs                 4183000    410916   3562372  10% /
/dev/root                 7680      7680         0 100% /rom
tmpfs                    30560       112     30448   0% /tmp
tmpfs                      512         0       512   0% /dev
/dev/sda1              4183000    410916   3562372  10% /
/dev/sda2              3138272    112080   2868908   4% /mnt/sda2
/dev/sda3               129267      7982    114732   7% /mnt/sda3
Next do:
root@Arduino:~# umount /dev/sda3
root@Arduino:~# mkswap /dev/sda3
Setting up swapspace version 1, size = 131068 KiB
no label, UUID=54a098ed-4e70-4392-8232-bd1f394df68a
root@Arduino:~# swapon /dev/sda3
Probably if I hadn't rebooted or so, the unmounting wasnt necessary... In /etc/config/fstab add:
config swap
        option device   /dev/sda3
        option enabled  1

swapiness can be controlled via:
sysctl -w vm.swappiness=10
echo 10 > /proc/sys/vm/swappiness
This will not remember the value across booting. For that edit /etc/sysctl.conf so that it contains:
vm.swappiness = 10

The free command or 'swapon -s' command can be given to get info about your swap.

Tuesday, December 30, 2014

Flask and bottle on Arduino Yun

Installing flask or bottle:

pip install flask
pip install bottle

Installing the sqlite plugin (if you want to use this):

pip install bottle-sqlite

Installing pip:

opkg update
opkg install distribute
opkg install python-openssl
easy_install pip

Monday, December 29, 2014

Installing Django on arduino Yun

Edit: the stuff below is ok, but it is much simpler to install django on the arduino yun:

opkg update
opkg install python-django





step1:
Make sure you have enough space on the Yun. http://myyafa.blogspot.be/2014/05/moving-linux-filesystem-openwrt-to-sd.html

step2:
I tried some methods that should work, but that didn't (see alternative-step2). What did work for me was:

- download Django to the Yun SD card. E.g. by:
-- downloading the tar.gz from https://www.djangoproject.com/download/ to the PC
-- mounting the Yun SD card on the PC: http://myyafa.blogspot.be/2013/11/mounting-yun-sd-card-on-my-linux.html and create a tmp dir in there.
-- copy Django-1.7.1.tar.gz to that directory

- ssh to the Yun, cd to the tar.gz file location and issue the commands:

gunzip Django-1.7.1.tar.gz
tar -xvf Django-1.7.1.tar
cd Django-1.7.1
python setup.py install

step3:
test the installation

issue the python command, then try importing django at the python prompt:

root@Arduino:/mnt/sda1/arduino/tmp/Django-1.7.1# python
Python 2.7.3 (default, May 17 2014, 04:56:10)
[GCC 4.6.3 20120201 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.7.1

Done, below an alternative for step2

Note: you probably also need to install f(use opkg) ollowing packages before running a server:
python-sqlite3
python-expat
python-openssl

To remove this manually installed (only then!) version: remove the directory inside site-packages found by:

python -c "import sys; sys.path = sys.path[1:]; import django; print(django.__path__)"


alternative-step2:
These should work, but they didnt for me:

opkg update
opkg install distribute
opkg install python-openssl
easy_install pip
pip install django

but for some reason, the process gets killed.

Also when putting the tar.gz on the Yun, and then

pip install Django-1.7.1.tar.gz

suffers from being killed





Sunday, August 31, 2014

configuring the Yun wifi access

To change/select which WLAN access point the Yun should use you can just edit the following file:

     /etc/config/wireless

Just change the SSID and key.

Getting the status of the wifi:

      iwconfig wlan0

Sunday, August 24, 2014

Reset or reboot not working

I had an issue that when typing reboot, or pressing the reset button, the Yun (open wrt) would not correctly reboot (e.g. the white LED did no go on anymore). The only way the reboot worked correctly was by power cycling the device. Also a watchdog based reboot didnt work.

Cause: my running sketch was sending stuff to the Console. This will interrupt the boot. The "old" Uboot version will be interrupted by any char. A bridge operation will cause the same problem (I think even a get as this is implemented as transfer)

Solution:
 - newer uboot version (with openwrt-yun 1.3) is more strict and will only interrupt on the characters "ard". The upgrade is unfortunatley a manual step that will void warranty. See http://arduino.cc/en/Tutorial/YunUBootReflash  (you can do only the uboot stuff)

extra info: takes about 4sec to enter uboot (so probably a good idea to let a sketch do nothing the 4 first seconds)

Saturday, August 2, 2014

Python - fix indent on existing files

To fix indentation on existing python code on Ubuntu:
sudo apt-get install python-examples
Then to fix all files in the current directory:
/usr/share/doc/python2.7/examples/Tools/scripts/reindent.py .