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 .

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 wifiMonitor.py

There are 4 things that are supposed to be configured (see wifiMonitor.py):

LOGFILE="/tmp/wifiMonitor"
LOGFILESD="/mnt/sda1/arduino/wifiMonitor_"
RESTART_WIFI_WHEN_LOST = True
TO_PING = "192.168.1.1"

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

general:
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 https://github.com/NicoLugil/wifiMonitor
git remote -v
git fetch origin
git merge origin/master
After creating gh-pages (project page) using website, get it on local copy:
https://help.github.com/articles/creating-pages-with-the-automatic-generator
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 https://github.com/user/repo.git
#  * [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. http://myyafa.blogspot.be/2014/05/create-and-format-partitions-on-yun-sd.html)

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 http://forum.arduino.cc/index.php?topic=289215.0) (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
save:
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.
reboot:
reboot
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 http://forum.arduino.cc/index.php?topic=235360.msg1693350#msg1693350 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:
df
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.


#!/usr/bin/python
import sys
import imaplib
import private.pw
import email

USERNAME = private.pw.myMailUser
PASSWORD = private.pw.myMailPass
MAILTO  = private.pw.myMailTo

# first check if there is new mail
PROTO="https://"
SERVER="mail.google.com"
PATH="/gmail/feed/atom"

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

imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
imap_server.login(USERNAME, PASSWORD)
imap_server.select('INBOX')

result, data = imap_server.uid('search', None, "UNSEEN") # get unseen message UIDs
for my_uid in data[0].split():
   my_msg=''
   #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[])')
   raw_email=data[0][1]
   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
imap_server.close()
imap_server.logout()



Note: feedparser should be installed on the Arduino Yun first. I did this as follows:
  • download it from https://code.google.com/p/feedparser/downloads/list (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 setup.py 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 http://forum.arduino.cc/index.php?topic=187319.msg1414629#msg1414629

In summary:

install the ssl stuff for python:

opkg update
opkg install python-openssl

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


#!/usr/bin/python
import smtplib
from email.mime.text import MIMEText
import private.pw

USERNAME = private.pw.myMailUser
PASSWORD = private.pw.myMailPass
MAILTO  = "something@somewhat.com"

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

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, MAILTO, msg.as_string())
server.quit()

The username and passwords are stored in the file pw.py 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