Introduction

Figure 1: Weather Demo Device
Introduction
In this article, we will look into building a sample Weather App using Raspberry PI, Mono C# and RaspberryPi.Net (Mono.NET interface to the GPIO pins).
- Raspberry PI (Model A, B or B+).
- Ethernet or USB WiFi Adapter.
- A Desktop/Laptop installed with MonoDevelop IDE.
- One LED and a 330 ohm 1/4 watt resistor.
- HDMI to DVI cable or HDMI cable depending on whether you are connecting PI to your computer monitor or HDMI TV.
Please Note: The following code is being reused and modified to work:
https://github.com/raharrison/GoogleAPI/tree/master/GoogleWeather
Background
If you are a beginner to Raspberry PI, please have a look into an excellent article by Guruprasad.K.Basavaraju which helped me understand the basics.
The first thing one has to do is register yourself and get the API Key from the following link:
We will be making an HTTP Web Request to the following formatted URL and get the weather information. Please note that this API is free for personal and non-commercial use with a "fairuse" of 250 daily accesses.
HTTP Web Request
Link
The key thing here is the API Key and the city for which you are interested in getting the weather information.
HTTP Web Response
The following is the HTTP Response consisting of forecast and weather conditions. We will be only concentrating on reading the current weather conditions.
Hide Copy Code
- <xml_api_reply version="1">
- <weather module_id="0" tab_id="0">
- <current_conditions>
- <!-- Some inner tags containing data of current weather -->
- <condition data="Sunny" />
- <temp_f data="32" />
- <temp_c data="0" />
- <humidity data="Humidity: 0%" />
- <icon data="/images/weather/sunny.gif" />
- <wind_condition data="Wind: SE at 9 mph" /> </current_conditions>
- </weather>
- </xml_api_reply>
- public class WeatherEntity
- {
- public string City
- {
- get;
- set;
- }
- public string Condition
- {
- get;
- set;
- }
- public string TempInFahrenheit
- {
- get;
- set;
- }
- public string TempInCentigrade
- {
- get;
- set;
- }
- public string Humidity
- {
- get;
- set;
- }
- public string Wind
- {
- get;
- set;
- }
- public string Day
- {
- get;
- set;
- }
- public string High
- {
- get;
- set;
- }
- public string Low
- {
- get;
- set;
- }
- }
- public static WeatherEntity GetWeatherCondition(string apiKey, string city)
- {
- var weatherEntity = new WeatherEntity();
- using(var weatherXmlReader = new XmlTextReader(string.Format("http://api.previmeteo.com/{0}/ig/api?weather={1}", apiKey, city)))
- {
- var doc = new XmlDocument();
- doc.Load(weatherXmlReader);
- if(doc.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
- {
- weatherEntity = null;
- }
- else
- {
- weatherEntity.City = doc.SelectSingleNode("/xml_api_reply/weather/forecast_information/city")
- .Attributes["data"].InnerText;
- weatherEntity.Condition = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/condition")
- .Attributes["data"].InnerText;
- weatherEntity.TempInCentigrade = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_c")
- .Attributes["data"].InnerText;
- weatherEntity.TempInFahrenheit = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_f")
- .Attributes["data"].InnerText;
- weatherEntity.Humidity = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/humidity")
- .Attributes["data"].InnerText;
- weatherEntity.Wind = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/wind_condition")
- .Attributes["data"].InnerText;
- }
- }
- return weatherEntity;
- }


- public static void Main(string[] args)
- {
- String apiKey = "<API_KEY>";
- GPIOFile led = null;
- Console.WriteLine("Enter city name: ");
- var city = Console.ReadLine();
- try
- {
- var weather = GetWeatherCondition(apiKey, city);
- if(weather != null)
- {
- Console.WriteLine("************************************************** ");
- Console.WriteLine("TempC: " + weather.TempInCentigrade);
- Console.WriteLine("TempF: " + weather.TempInFahrenheit);
- Console.WriteLine("Condition: " + weather.Condition);
- Console.WriteLine(weather.Humidity);
- Console.WriteLine(weather.Wind);
- Console.WriteLine("************************************************** ");
- // if the temperature is less than 5 deg, then glow an LED
- if(int.Parse(weather.TempInCentigrade) < 5)
- {
- led = new GPIOFile(GPIOPins.V2_GPIO_17);
- led.Write(PinState.High);
- }
- }
- else
- {
- Console.WriteLine("Problem in fetching in the weather information.");
- }
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- Console.WriteLine("Press any key to exit!");
- Console.ReadLine();
- if(led != null) led.Write(PinState.Low);
- }
RaspberryPi.Net GitHub


Sr KarthigaPosted Feb 14, 2016, 7:52 AM
Good one sir
Mukesh KumarPosted Oct 31, 2015, 11:19 AM
Great Work!
Gowtham KPosted Oct 31, 2015, 10:37 AM
Good One
Harshad PansuriyaPosted Oct 31, 2015, 7:45 AM
Nice one