Tags

, , , , , , ,

Arkwood, my sordid Belgian friend, is in a love triangle. A triple pickle. ‘Forget it,’ I told him, ‘Let’s code.’

The plan: ask our Raspberry Pi if it’s hot. The Pi will then fetch the current weather conditions in Hong Kong and contrast against our local climate. If Arkwood’s house is colder than Asia, the blue bulb is switched on. Otherwise we get the sizzling red bulb.

‘But–’

I held up a hand to stop him. ‘Don’t question The Plan.’

Okay, so first we need to talk to our little computer. Easy. We will use the handy PiAUISuite software I have previously posted about, that allows voice commands to be mapped to whatever we damn well please. In this case I will say, ‘Temperature!’ and it will execute our Python weather program.

Here’s the line in the commands.conf file to take care of our wicked desires:

~Temperature==python /home/pi/Desktop/MyPython/Weather.py

Lovely. Now let us peer into the script, to spy upon its deeds.

# imports
from urllib2 import urlopen
import json
import serial
from time import sleep

# let's start by getting the current temperature in Hong Kong
# using the worldweatheronline.com api
url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Hong+Kong&format=json&num_of_days=5&key=[getyourownkey]"

try:
    response = urlopen(url)
except:
    print ("Error getting foreign weather")    

object = json.load(response)

foreignTemp = int(object["data"]["current_condition"][0]["temp_C"])

After setting our imports, we fetch the latest conditions from Hong Kong. Now, I’ve just registered an account with worldweatheronline.com so I am not in a position to say whether they are best of breed. What I can say is it’s free. You get 3 requests a second, 500 an hour. And it will spit out the results in json, xml or csv. Here’s the json:

{
    "data": {
        "current_condition": [{
            "cloudcover": "50",
            "humidity": "83",
            "observation_time": "11:23 PM",
            "precipMM": "0.0",
            "pressure": "1018",
            "temp_C": "20",
            "temp_F": "68",
            "visibility": "6",
            "weatherCode": "116",
            "weatherDesc": [{
                "value": "Partly Cloudy"
            }],
            "weatherIconUrl": [{
                "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png"
            }],
            "winddir16Point": "ENE",
            "winddirDegree": "69",
            "windspeedKmph": "0",
            "windspeedMiles": "0"
        }],
        "request": [{
            "query": "Hong Kong, Hong Kong",
            "type": "City"
        }],
        "weather": [{
            "date": "2014-02-26",
            "precipMM": "1.1",
            "tempMaxC": "26",
            "tempMaxF": "78",
            "tempMinC": "21",
            "tempMinF": "69",
            "weatherCode": "113",
            "weatherDesc": [{
                "value": "Sunny"
            }],
            "weatherIconUrl": [{
                "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
            }],
            "winddir16Point": "ENE",
            "winddirDegree": "76",
            "winddirection": "ENE",
            "windspeedKmph": "12",
            "windspeedMiles": "7"
        }, {
            "date": "2014-02-27",
            "precipMM": "0.1",
            "tempMaxC": "25",
            "tempMaxF": "76",
            "tempMinC": "21",
            "tempMinF": "69",
            "weatherCode": "113",
            "weatherDesc": [{
                "value": "Sunny"
            }],
            "weatherIconUrl": [{
                "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
            }],
            "winddir16Point": "ESE",
            "winddirDegree": "105",
            "winddirection": "ESE",
            "windspeedKmph": "24",
            "windspeedMiles": "15"
        }, {
            "date": "2014-02-28",
            "precipMM": "0.6",
            "tempMaxC": "24",
            "tempMaxF": "75",
            "tempMinC": "21",
            "tempMinF": "69",
            "weatherCode": "119",
            "weatherDesc": [{
                "value": "Cloudy"
            }],
            "weatherIconUrl": [{
                "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0003_white_cloud.png"
            }],
            "winddir16Point": "ESE",
            "winddirDegree": "113",
            "winddirection": "ESE",
            "windspeedKmph": "24",
            "windspeedMiles": "15"
        }, {
            "date": "2014-03-01",
            "precipMM": "0.3",
            "tempMaxC": "26",
            "tempMaxF": "79",
            "tempMinC": "20",
            "tempMinF": "68",
            "weatherCode": "116",
            "weatherDesc": [{
                "value": "Partly Cloudy"
            }],
            "weatherIconUrl": [{
                "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png"
            }],
            "winddir16Point": "SSE",
            "winddirDegree": "150",
            "winddirection": "SSE",
            "windspeedKmph": "13",
            "windspeedMiles": "8"
        }, {
            "date": "2014-03-02",
            "precipMM": "0.0",
            "tempMaxC": "24",
            "tempMaxF": "76",
            "tempMinC": "20",
            "tempMinF": "69",
            "weatherCode": "113",
            "weatherDesc": [{
                "value": "Sunny"
            }],
            "weatherIconUrl": [{
                "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
            }],
            "winddir16Point": "SE",
            "winddirDegree": "142",
            "winddirection": "SE",
            "windspeedKmph": "21",
            "windspeedMiles": "13"
        }]
    }
}

Lots to play with. Arkwood was frothing at the mouth. Now that we have our foreign temperature, it’s time to probe our own thermometer. We do this by way of our RasWIK wireless inventors kit (see previous posts). Here’s the messages we send from our Pi to the Arduino-based development board:

# now we'll get the local weather i.e. Arkwood's bedroom
# by using the thermometer on our RasWIK wireless kit
port = '/dev/ttyAMA0'
baud = 9600

ser = serial.Serial(port=port, baudrate=baud)

sleep(0.2)
ser.write('a--A00READ--')

sleep(0.2)
reply = ser.read(12)

localTemp = int(calculateTemp(reply))

We send a radio signal and read the temperature of the room from the tiny thermometer pin. After that we do some calculations, which I’ve put into a function (if you want to see the clever maths, which I played no part in, take a look at the sample code available on Ciseco’s site). Okay, almost there. So now we have two temperatures – foreign and local – on which to compare, let’s write the code to light up the bulbs:

# now it's time to see how hot Arkwood's bedroom is
# if it's hotter than Hong Kong, put on the red bulb (my God!)
# otherwise his lair is a fridge, so flick on the blue bulb
if localTemp > foreignTemp:
    ser.write('a--D11HIGH--')
    ser.write('a--D06LOW---')
    print ("Arkwood's love pit is steamin' at " + str(localTemp))
    print ("Hong Kong is a shivering " + str(foreignTemp))
else:
    ser.write('a--D06HIGH--')
    ser.write('a--D11LOW---')
    print ("Hong Kong is frying at " + str(foreignTemp))
    print ("Arkwood's ice bucket is " + str(localTemp))

Dead simple. We fire a message to our wireless board to flick on either the blue or red light, and we’re done. A pointless exercise in quicksmart time.

Here’s the hardware:

raswik_weather

The thermometer is at the back of the breadboard, pretty hard to see, with the two bulbs up front.

Well that just about does her. Wraps her all up. But before I go, just a word on Arkwood’s sex troubles. After much persuasion, he finally put the horrible business to bed. Sheila, that is. Hilda, bless her filthy socks, was left out in the cold. But I’m ramblin’ again.

Advertisements