Building a DIY Home Alarm Part 2 (Netduino Plus WebServer)

For this second part in the series I wanted to prototype the network connectivity of the Netduino. Why? Because I want the alarm to message me some how if some one breaks in AND I want to be able to interact with it from my phone.

Prototype Features

  • Run a threaded web server, capable of returning 200 OK with the HTTP body of “Hello World”
  • Flash the onboard LED whenever the webserver services an inbound request
  • Trigger a sound (I’ll cover this sound in more detail later in my posts). I was using a transistor switch to trigger a 555 timer which makes a 3khz sound to the speaker

Approach

I started off using any old network cable to connect to the Netduino. After some thought and discussion on the NetDuino chat I realized I should be using a Crossover cable. Luckily I had one to hand. (Good thing I hoard my electronics stuff :P). I made sure the network cable was connected before I turned the NetDuino on. I think I read somewhere that with certain versions of the flashed software, it can be troublesome connecting the network cable afterwards.

I discovered that the Netduino’s network configuration can be configured programatically or through the tool that comes with the microframework MFDeploy.exe (I found it in C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Tools). Through a lot of experimentation I figured the easiest way of me connecting the pc and the netduino would be to manually assign ip addresses on the same subnet mask. My laptop was initially setup for DHCP assigned ip on the physical network connection and the netduino was pre-wired with an ip. I set the pc to be a manually assigned IP of 10.5.0.1 and the netduino to 10.5.0.2 and both on a subnet mask of 255.0.0.0.

Using Fiddler I sent HTTP Get Requests to the web server and received 200 OK messages back with the content “Hello World”.

Demo

Source Code

Note: I used the .Net Micro Framework Networking Toolkit to run the web server.
PS: Make sure you use the assembles found in the folders prefixed with “Micro” as they are compiled for the micro framework.
PPS: I used port 8080 as my machine is running IIS on port 80.

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.NetduinoPlus;
using MFToolkit.Net.Web;
using Microsoft.SPOT.Net.NetworkInformation;

namespace WebServer
{
    public class Program
    {
        public static void Main()
        {
            HttpServer httpServer = new HttpServer(8080, new MyHttpHandler());
            httpServer.Start();
        }
    }

    public class MyHttpHandler : IHttpHandler
    {
        private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        private OutputPort sound = new OutputPort(Pins.GPIO_PIN_D5, false);
        private bool playSound = false;

        public void ProcessRequest(HttpContext context)
        {
            led.Write(true);
            BuildHttpResponse(context.Response);
            led.Write(false);

            if (playSound == false)
            {
                playSound = true;
            }
            else
            {
                playSound = false;
            }

            sound.Write(playSound);
        }

        public void BuildHttpResponse(HttpResponse httpResponse)
        {
            httpResponse.HttpStatus = HttpStatusCode.OK;
            httpResponse.WriteLine("Hello World");
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s