Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using Komodex.NETMF;
using System.IO;
namespace N3WWeather
{
public class Program
{
// Visit Weather Underground to get a free API key: http://www.wunderground.com/weather/api/
private const string WeatherUndergroundAPIKey = "YOUR_API_KEY_HERE";
// Set the delay between weather updates
private static int _weatherUpdateDelayMinutes = 5;
// Temperature scale to display
private static TemperatureScale _displayScale = TemperatureScale.Fahrenheit;
public static void Main()
{
// Initialize Seven Segment Display
var display = new SevenSegmentDisplay();
display.SetValue("....");
// Wait for network connection and DHCP
while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
Thread.Sleep(10);
// Set up our display unit
Digit unit = Digit.Blank;
switch (_displayScale)
{
case TemperatureScale.Fahrenheit:
unit = Digit.F;
break;
case TemperatureScale.Celsius:
unit = Digit.C;
break;
}
// Continuously update weather
while (true)
{
try
{
double temperature = GetWeather();
display.SetTemperatureDisplay(temperature, unit);
}
catch { }
Thread.Sleep(1000 * 60 * _weatherUpdateDelayMinutes);
}
}
/// <summary>
/// Retrieves the current temperature from Weather Underground.
/// </summary>
/// <param name="query">Weather query to perform, e.g., "CA/San_Francisco" or a US zipcode. Use null to specify automatic IP address-based location.</param>
private static double GetWeather(string query = null)
{
// If no query was specified, automatically determine the current location based on the connecting IP.
if (query == null)
query = "autoip";
// Set up the request URI
string uri = "http://api.wunderground.com/api/" + WeatherUndergroundAPIKey + "/conditions/q/" + query + ".xml";
// Set up search values
string searchStart, searchEnd;
switch (_displayScale)
{
case TemperatureScale.Fahrenheit:
searchStart = "<temp_f>";
searchEnd = "</temp_f>";
break;
case TemperatureScale.Celsius:
searchStart = "<temp_c>";
searchEnd = "</temp_c>";
break;
default:
throw new Exception();
}
// Make the request
using (var request = (HttpWebRequest)WebRequest.Create(uri))
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
string line;
int indexStart, indexEnd;
do
{
// Read each line, looking for our search values
line = reader.ReadLine();
indexStart = line.IndexOf(searchStart);
if (indexStart >= 0)
{
indexStart += searchStart.Length;
indexEnd = line.IndexOf(searchEnd, indexStart);
return double.Parse(line.Substring(indexStart, indexEnd - indexStart));
}
} while (!reader.EndOfStream);
}
// Couldn't find a result
throw new Exception();
}
}
enum TemperatureScale
{
Fahrenheit,
Celsius,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment