Running Django on Arduino Yún
Arduino Yún is awesome device for many reasons but one is my favorite: it's running small Linux with Python. And when I hear Python, I think Django. You can make simple webserver in C and ATmega32u4 but it won't be easy.
There are two problems with Yún and Django:
- Even clean Yún has only few megabytes free
- Atheros (on which Linux is running) isn't very fast, so running pip may take a very long time
We can fix those problems using microSD card or an USB drive and preparing Django before.
Firstly we have to prepare your microSD card/USB drive to be mounted on Yún. Easiest way is to format it with FAT32.
In this example I'll use Django webserver from our Open Home project (you can use whatever project you want).
To download Django project open terminal and type:
$ cd /Volumes/YUN $ git clone https://github.com/appsome/open-home.git $ cd open-home/Python/webserver/
As there's no space nor processing power to run pip on Yún, we'll just install all packages into current directory:
$ pip install --target=. -I -r requirements.txt
After it's done, eject the drive and plug it into Yún, connect to it via ssh and install missing Python libraries:
$ ssh firstname.lastname@example.org # opkg update # opkg install python-sqlite3 python-openssl python-expat
Kombu library requires
librt.so.1, but on Linino we only have
librt.so.0. You can symlink them and hope for the best:
# ln -s /lib/librt.so.0 /lib/librt.so.1
SD card/USB drive should be automatically mounted (under
/mnt/sdb1), so enter webserver's directory and sync DB:
# cd /mnt/sda1/open-home/Python/webserver/ # python manage.py syncdb
Wait a while, then enter your admin user's info, migrate database and run server:
# python manage.py migrate # python manage.py runserver 0.0.0.0:9876
Open your browser and go to http://arduino.local:9876/admin and you should see Django's admin :)
Open Home webserver requires Celery worker to be running, also it would be nice to start server on boot, but it's a story for another post :)