You are on page 1of 4

C# Weather Server for the PC

Copyright 2007 by Barry B. Brey

This program servers the weather to the PIC from www.weather.com using any zip code programmed into
the PIC when it is initialized. Set the form visible property to false, the shown in task bar property to false,
and set the startup state to minimized. This hides the application. You might also want to place this into
the startup folder so it runs whenever the PC is started.
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.Runtime.InteropServices;

// for C# 2007

// for DLL import

namespace Weather
{
// There is an apparent bug in the webbrowser that requires that the
// following change to the browser needs to be made to prevent a popup
// scripting error message from appearing because of a script error at
// weather.com. To turn off the Scripting Debugger in Internet Explorer,
// on the Tools menu, click Internet Options. On the Advanced tab, select
// the Disable all script debugging check boxes, and then click OK.
//
public partial class Form1 : Form
{
// must place mpusbapi.dll in the bin/debug and bin/debug folders
[DllImport("mpusbapi.dll")]
private static extern int _MPUSBGetDLLVersion();
[DllImport("mpusbapi.dll")]
private static extern int _MPUSBGetDeviceCount(byte[] array);
[DllImport("mpusbapi.dll")]
private static extern IntPtr _MPUSBOpen(int a, byte[] b, byte[] c,
int d, int e);
[DllImport("mpusbapi.dll")]
private static extern IntPtr _MPUSBWrite(IntPtr a, byte[] b, int c,
ref int d, int e);
[DllImport("mpusbapi.dll")]
private static extern IntPtr _MPUSBRead(IntPtr a, byte[] b, int c,
ref int d, int e);
[DllImport("mpusbapi.dll")]
private static extern IntPtr _MPUSBReadInt(IntPtr a, byte[] b, int c,
int[] d, int e);
[DllImport("mpusbapi.dll")]
private static extern bool _MPUSBClose(IntPtr a);
private static IntPtr outPipe;
private static IntPtr inPipe;
private enum MP_MODE { MP_WRITE, MP_READ };
private string zipCode;
private int state = 0;
private int temp;
public Form1()
{
InitializeComponent();
}

private byte GetStatus()


{
int bytesWritten = 0;
int bytesRead = 0;
byte[] pipeOutData = { 0x22, 1, 0 };
byte[] pipeInData = new byte[21];
_MPUSBWrite(outPipe, pipeOutData, 3, ref bytesWritten, 10);
_MPUSBRead(inPipe, pipeInData, 21, ref bytesRead, 10);
return pipeInData[2];
}
private void SendData(char chr)
{
int bytesWritten = 0;
int bytesRead = 0;
byte[] pipeOutData = { 0x21, 1, Convert.ToByte(chr) };
byte[] pipeInData = new byte[21];
_MPUSBWrite(outPipe, pipeOutData, 3, ref bytesWritten, 10);
_MPUSBRead(inPipe, pipeInData, 21, ref bytesRead, 10);
}
private byte GetData()
{
int bytesWritten = 0;
int bytesRead = 0;
byte[] pipeOutData = { 0x20, 1, 0 };
byte[] pipeInData = new byte[21];
_MPUSBWrite(outPipe, pipeOutData, 3, ref bytesWritten, 10);
_MPUSBRead(inPipe, pipeInData, 21, ref bytesRead, 10);
return pipeInData[2];
}
private void timer1_Tick(object sender, EventArgs e)
{
// timer 1 is enabled with 100 ms ticks
if ((GetStatus() & 2) == 0)
{
if (state == 0)
{
temp = Convert.ToInt32(GetData());
state++;
}
else if (state == 1)
{
// once every 10 minutes
temp |= Convert.ToInt32(GetData() << 8);
state = 0;
zipCode = temp.ToString();
zipCode.PadLeft(5, '0');
string mes =
@"http://www.weather.com/weather/local/" + zipCode;
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate(new Uri(mes));
webBrowser1.Refresh();
}
}
}
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// process webpages from www.weather.com
string[] split;
string[] split1;
string[] split2;

string text = webBrowser1.DocumentText;


string[] stringSeparators = new string[] { "<TITLE>" };
split = text.Split(stringSeparators, StringSplitOptions.None);
split = split[1].Split(' ');
if (split.Contains("Weather"))
{
string where = split[0];
if (split[1] != "Weather")
{
where += " " + split[1];
}
where += " Weather";
stringSeparators = new string[] { "context=" };
// isolate the local weather from the page
split = text.Split(stringSeparators, StringSplitOptions.None);
stringSeparators = new string[] { "temp=" };
split1 = split[1].Split(stringSeparators,
StringSplitOptions.None);
split2 = split1[1].Split('&');
// get the inportant information
webBrowser1.Stop();
// don't want the popups so STOP
sbyte temperature = Convert.ToSByte(split2[0]);
byte humidity = Convert.ToByte(split2[2].Remove(0, 6));
byte windSpeed = Convert.ToByte(split2[4].Remove(0, 5));
string condition = split2[5].Remove(0, 5);
while ((GetStatus() & 1) == 1) ;
SendData(Convert.ToChar(where.Length));
foreach (char c in where)
{
while ((GetStatus() & 1) == 1) ;
SendData(c);
}
while ((GetStatus() & 1) == 1) ;
SendData(Convert.ToChar(temperature));
while ((GetStatus() & 1) == 1) ;
SendData(Convert.ToChar(humidity));
while ((GetStatus() & 1) == 1) ;
SendData(Convert.ToChar(windSpeed));
while ((GetStatus() & 1) == 1) ;
SendData(Convert.ToChar(condition.Length));
foreach (char c in condition)
{
while ((GetStatus() & 1) == 1) ;
if (c != '_')
SendData(c);
else
SendData(' ');
}
}
else
{
webBrowser1.Stop();
// don't want the popups so STOP
}
}
private void Form1_Load(object sender, EventArgs e)
{
byte[] vid_pid = Encoding.ASCII.GetBytes("vid_04d8&pid_000b");
// Default PIC Boot firmware
byte[] out_pipe = Encoding.ASCII.GetBytes("\\MCHP_EP1");
// output stream
byte[] in_pipe = Encoding.ASCII.GetBytes("\\MCHP_EP1");
// input stream

int count = _MPUSBGetDeviceCount(vid_pid);// check for a connection


if (count > 0)
{
outPipe = _MPUSBOpen(0, vid_pid, out_pipe,
(int)MP_MODE.MP_WRITE, 0);
inPipe = _MPUSBOpen(0, vid_pid, in_pipe, (int)MP_MODE.MP_READ,
0);
if ((GetStatus() & 2) == 0)
// initial USB synchronization
{
temp = Convert.ToInt32(GetData());
}
}
else
{
MessageBox.Show("Failed to open data pipes...try again!");
Application.Exit();
}
}
}
}

You might also like