After their successes with the mechanics and Arduino Yun, Hans and Matilda decided to celebrate with a trip to the local inn, the "Adam and Eve". They walked up to the bar and was greeted by the bartender. Whilst they were being served they noticed a large snake behind the bar.

adameve1.jpgadameve2.jpg

Photos thanks to Will Russell

Suddenly the door burst open and a large troll charged in and marched up to the bar. He started shouting in a language which Hans and Matilda did not recognise. The snake lifted it's head and proceeded to slide along the back of the bar until it reached a dusty looking bottle. The snake flicked its tongue. The bartender picked up the bottle and poured a generous measure into a glass and passed it to the troll. The troll slapped some gold coins on the bar and marched over to a full table of people who quickly vacated to give the troll a seat.

 

From over the other side of the bar Hans and Matilda heard some raised voices. A group of dwarves were having some drinks with a tall dark haired lady. A dwarf with glasses seemed to be angry with one of the others who he claimed was not doing his fair share of the mining. The argument quickly deteriorated into a fight and the accused dwarf picked up a bottle and raised it above his head. Before he could bring down his arm it was wrapped in the coils of the snake. Hans and Matilda watched in amazement as the two fighting dwarves disappeared into endless coils of snake. It was at this point they realised exactly how big the snake was. The snake and dwarf coils headed outside, shortly after only the snake returned.

 

After finishing their drinks, Hans and Matilda thanked the bartender and asked about the snake. He told them he had bought it from a Burmese traveller when it was only a foot long. He had initially fed it on rats caught in the cellar, now he just leaves it to roam the forest at night. Hans and Matilda walked home much more quickly than usual that evening.

 

The next morning Hans was working on the Arduino Yun, he realised that both the parsing of the weather data and also security checks could be handled in Python on the OpenWRT side of the Yun.

 

Hans thought that the Python script could return a simple string representing the weather, either success or an error with precipitation and temperature as additional parameters.

 

Message formats:
OK!,Cloudy,4,12.5
ERR,Timeout,0,0.0

 

He also confirmed that python already installed on the Yun.

PythonHello.png

Hans looked at the different options for reading an API and the requests library looks to be a good solution. It was straight forward to install this on the development machine.

 

pip install requests



 

To install it onto the Yun required a little more effort as PIP is not installed.

 

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



When Hans tried to install this, the Yun ran out of space. "No space left on device". So he followed the instructions for mounting the file system onto an SD card. http://www.arduino.cc/en/Tutorial/ExpandingYunDiskSpace. After making space for the tools used to expand the disks he succeeded expanding the disks and returned to installing pip.

 

To prove his concept would work he created a simple demo.

import requests, json
print 'Getting Weather'
weather_url = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition.text%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22Chicago%20IL%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
r = requests.get(weather_url)
print r.status_code
j = json.loads(r.content)
print j['query']['results']['channel']['item']['condition']['text']


 

Not a perfect result but definitely a few steps in the right direction.

root@EnchantedCottage:/mnt/sda1/Python# python GetWeather.py
Getting Weather
/usr/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
200
Rain
root@EnchantedCottage:/mnt/sda1/Python#

 

Now it was just a case of dealing with the InsecurePlatformWarning, handling timeouts and network errors, parsing the JSON and passing in the correct location code.

 

Python Reference

Exception handling
http://doughellmann.com/2009/06/19/python-exception-handling-techniques.html

Make call using requests.get
http://isbullsh.it/2012/06/Rest-api-in-python/

Certificates
https://www.python.org/dev/peps/pep-0476/#trust-database

https://pypi.python.org/pypi/backports.ssl_match_hostname/

http://stackoverflow.com/questions/1087227/validate-ssl-certificates-with-python

Also timeouts etc here
http://docs.python-requests.org/en/latest/user/advanced/

 

Next: Enchanted Objects Design Challenge - The flaming postman, the blind man and a trip to the market