windows 10 + raspberry pi 2It is time to create our first C# Windows 10 Universal Application for Raspberry Pi 2. You can find LED blinking example in an official documentation, so today we are gonna create weather application. This application will connect to the remote server and get actual weather information based on city and country name, and display this information on a screen.

Prepare your computer

  1. Download Visual Studio 2015 Community Edition RC
  2. Select Custom installation and enable Universal Windows Apps Development Tools and Emulators for Windows Mobile options
  3. Install WindowsDeveloperProgramForIoT.msi you can find it inside Windows 10 IoT Core Insider Preview image files. Download here
  4. Create Blank App (Windows Universal) project
  5. Select ARM platform and  Remote Debugging:
    1. Enter your Raspberry Pi 2 IP address and don't use windows authentication. 
    2. Or go to Project settings -> Debug and set Target device to "Remote Machine", specify IP address and deselect 'Use authentication' box
  6. Open MainPage.xaml and add simple text:
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock Text="Hello World" 
                   Margin="10"
                   FontSize="100"
                   Foreground="Black"/>
    </Grid>
  7. Press F5 to deploy application to the Raspberry Pi

Real Application

Let's create UI for our output. We will have two simple TextBlocks. One for city and country name and other for the current temperature.

Open MainPage.xml and edit:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
	<StackPanel>
		<TextBlock x:Name="location"
		           FontSize="60"
			   Margin="5"/>
		<TextBlock x:Name="temperature"
			   FontSize="50"
			   Margin="5"/>
	</StackPanel>
</Grid>

We will get weather information from Open Weather Map. They have simple json API.

Create simple classes to deserialize json:

public class Temperature
{
    public double temp { get; set; }
}

public class Weather
{
    public Temperature main { get; set; }
}

Now it is time to load data from remote server and display it on the screen. I will use HttpClient together with DataContractJsonSerializer to do that.

Edit MainPage.cs:

public MainPage()
{
	this.InitializeComponent();

	string city = "Amsterdam";
	string country = "NL";

	location.Text = $"{city},{country}";
	HttpRequestMessage request = new HttpRequestMessage(
		HttpMethod.Get,
		$"http://api.openweathermap.org/data/2.5/weather?q={city},{country}");
	HttpClient client = new HttpClient();
	var response = client.SendAsync(request).Result;
	if (response.StatusCode == HttpStatusCode.OK)
	{
		var result = response.Content.ReadAsStringAsync().Result;
		var bytes = Encoding.Unicode.GetBytes(result);
		using (MemoryStream stream = new MemoryStream(bytes))
		{
			var serializer = new DataContractJsonSerializer(typeof(Weather));
			var weather = (Weather)serializer.ReadObject(stream);
			temperature.Text = $"Temperature: {(weather.main.temp - 273.15f):F2} °C";
		}
	}
	else
	{
		temperature.Text = "Error";
	}	
}

You can change city and country variables to match your location.

Run an application and you will see your location and current temperature on Raspberry Pi's screen. You can add city and country selection, as well as nice loading effects and async calls, but for simple example it is just enough.

As you can see WinRT stack available on Raspberry Pi 2. You can easily create Windows Universal Applications and debug them from Visual Studio. Enjoy!