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

  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

Make sure you have enough space on the Yun.

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 to the PC
-- mounting the Yun SD card on the PC: 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 install

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())

Done, below an alternative for step2

Note: you probably also need to install f(use opkg) ollowing packages before running a server:

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__)"

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:


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)

 - 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  (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/ .

Friday, July 25, 2014

wifi monitor to troubleshoot Yun wifi - Results

RESTART_... = true,
sketch: simple LED blink
2014-07-25 08:11 : wifi OK, uptime=6:59:04.868013, ok/nok: 2310/3
no particular pattern

Thursday, July 24, 2014

wifi monitor to troubleshoot Yun wifi

My Yun's wifi seems to be very unstable.

I created a python script to monitor the Yun's wifi connection.

This script can be downloaded here.

The main script is

There are 4 things that are supposed to be configured (see

TO_PING = ""

The first 2 give the location of where the logs are stored: LOGFILE is the main destination, with now and then a backup to LOGFILESD.
When it sees the wifi coonection is lost, it will either:
- do nothing (just wait and hope it gets restored)
- restart the Yun's wifi
This is configured by the RESTART_WIFI_WHEN_LOST option.
To determine if the Wifi is ok, it will ping a particular site or IP address. This is set by the TO_PING option. In my case I just ping my router.

In short the script will now and then ping to TO_PING site to determine if Wifi is ok and do the appropriate action if not. It logs the ratio of ok/not_ok and the current uptime.

Wednesday, July 23, 2014

Arduino Yun Wifi stuff (reset, access point mode, ...)

The Yun has 3 reset buttons: one of those is a Wifi reset button (WLAN Rst)

Depending on how long you press the button the following will happen (I think):

  • Pressing the WLAN Rst button for <5 sec does not do any reset at all, correct (it just lets a LED blink)
  • Pressing the Wifi Rst button for >=5 but <30 sec will run the wifi-reset-and-reboot script. This script will return the Yun in access point (AP) mode and will reboot the Yun. The Yun should then present its network SSID.
  • Pressing for >= 30 sec will restore some factory settings

If you just want to reset the Wifi, you can do this with the wifi script (should be in /sbin):

    root@Arduino:/tmp# wifi down && sleep 5 && wifi
    Successfully initialized wpa_supplicant

You probably can also do:

    ifconfig wlan0 down; ifconfig wlan0 up

Note that rc.local contains by default the entry wifi-live-or-reset. After the Yun boots, it will check for the configured Wifi network. If it is not found within 60sec, the Yun will go into access point mode.

Saturday, July 5, 2014

git stuff

git add .
git commit .
git push origin master
git reset .
git rm
vi .gitignore
to remove local changes from (uncommitted) file:
git checkout filename
when creating a new project on gitHub, get it on PC:
mkdir wifiMonitor
cd wifiMonitor/
git init
git remote add origin
git remote -v
git fetch origin
git merge origin/master
After creating gh-pages (project page) using website, get it on local copy:
in essence:
cd repository
git fetch origin
# remote: Counting objects: 92, done.
# remote: Compressing objects: 100% (63/63), done.
# remote: Total 68 (delta 41), reused 0 (delta 0)
# Unpacking objects: 100% (68/68), done.
# From
#  * [new branch]      gh-pages     -> origin/gh-pages

git checkout gh-pages
# Branch gh-pages set up to track remote branch gh-pages from origin.
# Switched to a new branch 'gh-pages'

Sunday, May 25, 2014

Moving the linux filesystem OpenWrt to the SD card

Standard the OS is on the flash of the Yun. It is possible to move (most) to the SD card.

Advantages are:
- much more space, allowing to install many more packages
- the flash can be written only a limited amount of times. If you move the OS to the SD card, this is no longer an issue. The SD card also has a limited amount of writes, but typically more than the flash, and it is much easier to replace the SD card than the flash.

Before starting, create at least one ext4 partition on the SD card (see e.g.

On the Yun do (pivot root option):
opkg update
opkg install block-mount
cd /
mkdir -p /tmp/cproot
mount --bind / /tmp/cproot
tar -C /tmp/cproot -cvf - . | tar -C /mnt/sda1 -xf -
umount /tmp/cproot
edit the file /etc/config/fstab and add:

config mount
        option target        /
        option device        /dev/sda1
        option fstype        ext4
        option options       rw,sync
        option enabled       1
        option enabled_fsck  0
That's it. Reboot and the OS should now reside on the SD. If you still want to access the old filesystem on the flash (this is still used at boot for example) you can do
cd /
mkdir original-boot
and add
config mount
        option target   /original-boot
        option device   /dev/mtdblock3
        option fstype   jffs2
        option options  rw,sync
        option enabled  1
        option enabled_fsck     0
to the /etc/config/fstab file (see also (the one on the SD card)

create and format partitions on the Yun SD card

I have an 8GB SD card, I want to make a 4GB and a 3GB partition (it currently has only 1 8GB parition). With the SD card in the Yun:
fdisk /dev/sda
delete my existing partition:
Command (m for help): d
Selected partition 1
Partition 1 is deleted
create the 2 new ones:
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
Using default value 1
First sector (2048-15523839, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-15523839, default 15523839): +4G
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): n   
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 2): 
Using default value 2
First sector (8390656-15523839, default 8390656): 
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-15523839, default 15523839): +3G
Partition 2 of type Linux and of size 3 GiB is set
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
format the new paritions:
opkg update
opkg install mtd-utils
opkg install e2fsprogs

mkfs.ext4 -L YunSD1 /dev/sda1
mkfs.ext4 -L YunSD2 /dev/sda2

update: when I tried this again in 2015, I had to do:

umount /dev/sda1
umount /dev/sda2

prior to the mkfs commands

Saturday, May 24, 2014

Upgrade Yun with new image (OpenWrt-Yun 1.0)

There is a new image for the Yun with important fixes, and better link to OpenWrt.
More info and detailed installation guidelines can be found on Before doing the procedure I saved the installed packages to a file:
opkg list-installed
Next I formatted my SD card(not needed, but I want a clean start). First I did this on my Ubuntu PC but this gave issues with user groups and permissions. So I did the formatting on my Yun directly:
umount /mnt/sda1
mkfs.ext4 -L YunSD /dev/sda1
note the /dev/sda1 iso /mnt/sda1 for the mkfs command.
mkdir /mnt/sda1/arduino

Next, extract the zip file for the update into the root folder of the SD card.

ssh to the Yun and execute:
run-sysupgrade /mnt/sda1/openwrt-ar71xx-generic-yun-16M-squashfs-sysupgrade.bin
result is (nothing needs to be done when it looks like there is a prompt with the Writing from ...:
Sending TERM to remaining processes ... uhttpd dbus-daemon dnsmasq thd ntpd uSDaemon sftp-server sleep syslogd klogd hotplug2 procd ubusd netifd 
Sending KILL to remaining processes ... 
Switching to ramdisk...
Performing system upgrade...
Unlocking firmware ...

Writing from  to firmware ...     
Upgrade completed
Rebooting system...
Rebooting didnt seem to happen automatically. Power cycled and after a while I saw the USB led on, a sign of the new image. The Yun is completely fresh, so you need to start all over, connecting to the Yun wireless network, enter default password arduino, etc...

Saturday, January 4, 2014

Arduino Yun: reading mails with Python: the simple way

All the previous stuff works, but was unnecessarily complicated. It can be done much simpler with Python. The following code will check all unseen messages and print the main 'body' of the mail.

import sys
import imaplib
import email


# first check if there is new mail

n_email = int(feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)["feed"]["fullcount"])
if n_email > 0:
    print "New mail!"
    print "No new mail!"

imap_server = imaplib.IMAP4_SSL("",993)
imap_server.login(USERNAME, PASSWORD)'INBOX')

result, data = imap_server.uid('search', None, "UNSEEN") # get unseen message UIDs
for my_uid in data[0].split():
   #result, data = imap_server.uid('fetch',my_uid, '(BODY.PEEK[HEADER.FIELDS (FROM)])')   # spaces are important here !!
   #result, data = imap_server.uid('fetch',my_uid, '(BODY.PEEK[])')   # use this if dont want to flag mail as seen
   result, data = imap_server.uid('fetch',my_uid, '(BODY[])')
   email_msg = email.message_from_string(raw_email)
   maintype = email_msg.get_content_maintype()
   if maintype == 'multipart':
      for part in email_msg.get_payload():
         if part.get_content_maintype() == 'text':
            my_msg = part.get_payload()
   elif maintype == 'text':
     my_msg = email_msg.get_payload()
   print my_msg

Note: feedparser should be installed on the Arduino Yun first. I did this as follows:
  • download it from (tar.gz) 
  • copy it to somewhere on the Yun SD card 
  • unzip and untar: 
    • gunzip feedp...
    • tar -xvf feedpa...
  • opkg update 
  • opkg install setuptools 
  • cd to the uncompressed folder 
  • python install
Note2: you need to have openssl stuff installed for this:
  • opkg update
  • opkg install python-openssl

Thursday, January 2, 2014

Sending email with the Arduino Yun

See thread

In summary:

install the ssl stuff for python:

opkg update
opkg install python-openssl

next you can send mails as follows (using gmail smtp)

import smtplib
from email.mime.text import MIMEText

MAILTO  = ""

msg = MIMEText('Hello,\nMy name is Yun, \nhow are you')
msg['Subject'] = 'Mail from Yun'
msg['From'] = USERNAME
msg['To'] = MAILTO

server = smtplib.SMTP('')
server.sendmail(USERNAME, MAILTO, msg.as_string())

The username and passwords are stored in the file in subdir private

Wednesday, January 1, 2014

ramdisk /tmp

The /tmp directory is a RAM based directory. Means that it doesn't wear out like an SD card, or so...
Obviously when rebooting, you lose all the content. It still is the most obvious place to put temporary files, etc...

To make it look like a seperate drive you can do:

mkdir /tmp/ramdisk

in rc.local.
If you want it to look like a mounted drive you can also

ln -s /tmp/ramdisk /mnt/ramdisk