Testing WiFi using api.openweathermap.org
Works a treat :
using System;
using System.Collections;
using System.Threading;
using IotLabs.Hardware.Bluegiga;
using IotLabs.Net;
using IoT_Base;
using Microsoft.SPOT;
using STM32;
namespace IoT_Base_Test
{
public class Program
{
private static SSD1306 _display;
private static SerialWifi _wifi;
private static int _count;
private static readonly ArrayList CityList = new ArrayList();
public static void Main()
{
_display = new SSD1306(0x3C, 400, Pin.PB1);
_display.Initialize();
DisplayData("Weather.Net", 3);
Thread.Sleep(3000);
CityList.Add(new WeatherData { City = "Auckland", Country = "NZ" });
CityList.Add(new WeatherData { City = "Wellington", Country = "NZ" });
CityList.Add(new WeatherData { City = "London", Country = "UK" });
CityList.Add(new WeatherData { City = "Paris", Country = "FR" });
CityList.Add(new WeatherData { City = "Berlin", Country = "DE" });
CityList.Add(new WeatherData { City = "New York", Country = "US" });
CityList.Add(new WeatherData { City = "Calgary", Country = "CA" });
CityList.Add(new WeatherData { City = "Tokyo", Country = "JP" });
CityList.Add(new WeatherData { City = "Timbuktu", Country = "ML" });
DisplayData("Power on WiFi", 3,true);
Hardware.WiFi.EnableWiFi();
_wifi = new SerialWifi("COM2", Hardware.WiFi.WifiResetPin);
_wifi.Connected += new EventHandler(OnConnected);
_wifi.CommandFailed += new WifiBase.CommandFailedDelegate(OnCommandFailed);
DisplayData("Connecting...", 3, true);
_wifi.Connect("xxxxxx", "xxxxxx");
Thread.Sleep(Timeout.Infinite);
}
static void OnConnected(object sender, EventArgs e)
{
DisplayData("Connected...", 3, true);
GetWeatherForCity((WeatherData)CityList[0]);
}
static void GetWeatherForCity(WeatherData data)
{
var httpClient = new HttpClient(_wifi, "api.openweathermap.org");
httpClient.HttpResponseReady += OnHttpResponseReady;
httpClient.Get("/data/2.5/weather?q=" + data.City + "," + data.Country + "&units=metric&APPID=c47c7f28f4d8004b3f2fe258f269fd11");
}
static void OnHttpResponseReady(object sender, HttpResponseReadyEventArgs e)
{
if (e.Response.ResponseCode == 200 || e.Response.ResponseCode == 204)
{
try
{
string data = e.Response.ResponseBody;
int start = data.IndexOf("{\"temp") + 1;
int end = data.IndexOf("}", start);
string temp = data.Substring(start, end - start);
string[] vals = temp.Split(',');
string[] tempData = vals[0].Split(':');
WeatherData cityData = (WeatherData) CityList[_count];
DisplayData(cityData.City + " " + cityData.Country,2,true,false);
DisplayData("Temp: " + tempData[1] + " deg C", 4);
_count++;
if (_count > CityList.Count - 1) _count = 0;
Thread.Sleep(2000);
}
catch (Exception)
{
_count = 0;
}
GetWeatherForCity((WeatherData)CityList[_count]);
}
else
{
Debug.Print("HTTP Request failed (" + e.Response.ResponseCode + ")");
}
}
static void OnCommandFailed(object sender, CommandFailedEventArgs e)
{
Debug.Print("ERROR = " + e.ErrorDescription);
}
static void DisplayData(string data, int line, bool clearScreen = false, bool refresh = true)
{
if(clearScreen)_display.ClearScreen();
_display.DrawString(SSD1306.Align.Center, line,data);
if(refresh) _display.Refresh();
}
}
public class WeatherData
{
public string City;
public string Country;
}
}