Phidgets
Published © GPL3+

Weather Display

Make an easy weather display with Phidgets and OpenWeather

IntermediateFull instructions provided1 hour260
Weather Display

Things used in this project

Hardware components

Phidgets VINT Hub Phidget
×1
Phidgets Graphic LCD Phidget
×1

Story

Read more

Code

Code snippet #3

Plain text
static string readXML(string arg1, string arg2)
{
    String URLString = "http://api.openweathermap.org/data/2.5/weather?q=Calgary&APPID=fakeid111111111111111111111111111&units=metric&mode=xml";
    XmlTextReader reader = new XmlTextReader(URLString);
    reader.ReadToFollowing(arg1);
    reader.MoveToAttribute(arg2);
    return reader.Value;
}

Code snippet #4

Plain text
static void updateWeather(LCD lcd) {
	string city = readXML("city", "name");
	string temperature = readXML("temperature", "value");
	string humidity = readXML("humidity", "value");
	string windspeed = readXML("speed", "value");
	string descript = readXML("weather", "value");
	string iconID = readXML("weather", "icon");
}

Code snippet #5

Plain text
import xml.etree.ElementTree as ET
from urllib.request import urlopen

url = urlopen('http://api.openweathermap.org/data/2.5/weather?q=Calgary&APPID=fakeid111111111111111111111111111&units=metric&mode=xml')
tree = ET.parse(url)
root = tree.getroot()

def readXML(arg1, arg2):
    for item in root.iter(arg1):
        return item.get(arg2)

print(readXML('city','name'))
print(readXML('temperature','value'))
print(readXML('humidity','value'))
print(readXML('speed','value'))
print(readXML('weather','value'))
print(readXML('weather','icon'))

Code snippet #6

Plain text
static void updateWeather(LCD lcd)
{
    string city = readXML("city", "name");
    string temperature = readXML("temperature", "value");
    string humidity = readXML("humidity", "value");
    string windspeed = readXML("speed", "value");
    string descript = readXML("weather", "value");
    string iconID = readXML("weather", "icon");
    if (temperature.Length > 5)
    {
        temperature = temperature.Remove(5);
    }   
    //Temperature box
    int x = (44 - ((temperature.Length * 6) + 12)) / 2;
    lcd.WriteText(LCDFont.Dimensions_6x12, x, 15, temperature);
    lcd.WriteText(LCDFont.User1, x + temperature.Length * 6, 15, "0");
    lcd.WriteText(LCDFont.Dimensions_6x12, x + temperature.Length * 6 + 6, 15, "C");
    //Weather image + descript box
    byte[] temp;
    if (iconID == "01d")
        temp = _01d;
    else if (iconID == "02d")
        temp = _02d;
    else if (iconID == "03d")
        temp = _03d;
    else if (iconID == "04d")
        temp = _04d;
    else if (iconID == "09d")
        temp = _09d;
    else if (iconID == "10d")
        temp = _10d;
    else if (iconID == "11d")
        temp = _11d;
    else if (iconID == "13d")
        temp = _13d;
    else if (iconID == "50d")
        temp = _50d;
    else if (iconID == "01n")
        temp = _01n;
    else if (iconID == "02n")
        temp = _02n;
    else if (iconID == "10n")
        temp = _10n;
    else
        temp = unknown;

    lcd.WriteBitmap(2, 31, 32, 32, temp);
    lcd.WriteText(LCDFont.Dimensions_5x8, 40, 42, descript);

    //Extra info box
    lcd.WriteText(LCDFont.Dimensions_5x8, 50, 11, "Humidity: " + humidity + "%");
    lcd.WriteText(LCDFont.Dimensions_5x8, 50, 20, "Wind: " + windspeed + "km/h");
}
static void redraw(LCD lcd)
{
    lcd.Clear();
    //draw borders around outside
    lcd.DrawLine(0, 0, 127, 0);
    lcd.DrawLine(0, 0, 0, 63);
    lcd.DrawLine(127, 0, 127, 63);
    lcd.DrawLine(0, 63, 127, 63);

    //draw borders inside
    lcd.DrawLine(0, 10, 128, 10);
    lcd.DrawLine(43, 10, 43, 30);
    lcd.DrawLine(1, 30, 127, 30);
    lcd.WriteText(LCDFont.Dimensions_5x8, 1, 1, DateTime.Now.ToString(" ddd, MMM d hh:mm:ss tt"));
    updateWeather(lcd);
    lcd.Flush();
}<br>

Code snippet #7

Plain text
def updateWeather():
    city = readXML('city', 'name')
    temperature = readXML('temperature', 'value')
    humidity = readXML('humidity', 'value')
    windspeed = readXML('speed', 'value')
    descript = readXML('weather', 'value')
    iconID = readXML('weather', 'icon')
    if(len(temperature) > 5):
        temperature = temperature[:-1:] #remove last char so it fits
    #temperature box
    x = (44 - ((len(temperature) * 6) + 12)) / 2
    x = int(x) #force to int
    lcd.writeText(LCDFont.FONT_6x12, x, 15, temperature)
    lcd.writeText(LCDFont.FONT_User1, x + len(temperature) * 6, 15, "0")
    lcd.writeText(LCDFont.FONT_6x12, x + len(temperature) * 6 + 6, 15, "C")

    #Weather icon + descript box
    temp = []
    if(iconID == "01d"):
        temp = _01d
    elif(iconID == "02d"):
        temp = _02d
    elif (iconID == "03d"):
        temp = _03d
    elif (iconID == "04d"):
        temp = _04d
    elif (iconID == "09d"):
        temp = _09d
    elif (iconID == "10d"):
        temp = _10d
    elif (iconID == "11d"):
        temp = _11d
    elif (iconID == "13d"):
        temp = _13d
    elif (iconID == "50d"):
        temp = _50d
    elif (iconID == "01n"):
        temp = _01n
    elif (iconID == "02n"):
        temp = _02n
    elif (iconID == "10n"):
        temp = _10n
    else:
        temp = unknown

    lcd.writeBitmap(2, 31, 32, 32, temp)
    lcd.writeText(LCDFont.FONT_5x8, 40, 42, descript)

    #Extra info box
    lcd.writeText(LCDFont.FONT_5x8, 50, 11, "Humidity: " + humidity + "%")
    lcd.writeText(LCDFont.FONT_5x8, 50, 20, "Wind: " + windspeed + "km/h")

def redraw():
    lcd.clear()
    #Draw borders around outside
    lcd.drawLine(0, 0, 127, 0)
    lcd.drawLine(0, 0, 0, 63)
    lcd.drawLine(127, 0, 127, 63)
    lcd.drawLine(0, 63, 127, 63)

    #draw borders inside
    lcd.drawLine(0, 10, 128, 10)
    lcd.drawLine(43, 10, 43, 30)
    lcd.drawLine(1, 30, 127, 30)

    timeStr = datetime.now().strftime("%a, %b %d %I:%M:%S %p")
    lcd.writeText(LCDFont.FONT_5x8, 1, 1, timeStr)
    updateWeather()
    lcd.flush()<br>

Github

https://github.com/phidgeteer/LCDWeather.git

Credits

Phidgets
1 project • 0 followers
We specialize in making affordable, easy-to-use sensors and controllers that require minimal electronics knowledge.

Comments