Showing posts with label web application on Yun. Show all posts
Showing posts with label web application on Yun. Show all posts

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:


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