Alarm

The alarm device has several functions in the home security system.

There can be more than 1 alarm device placed in the home. The idea is to place the device in a location where the home owner can see the state of the window/door circuits as well as hear the alarm.

Messages

The following MQTT messages will be published from this device.

AddressDataDescription
/house1/alarm/firstfloor/ping any message This is a simple message that can be used to test that your device is sending messages to the broker and if the broker receives it the message will be published back to the device it came from

The device should subscribe to the following MQTT messages.

AddressDataDescription
/house1/alarm/firstfloor/pingresp the message that was received by the broker This is the response message from the broker that this device sent via the ping topic.
/house1/alarmpanel/masterbedroom/burglar on/off The master bedroom burglar alarm is turned on or off, as long as the alarm is on flash LED 1 on for 2 seconds then off for 2 seconds
/house1/alarmpanel/bedroom1/burglar on/off The bedroom 1 burglar alarm is turned on or off, as long as the alarm is on flash LED 1 on for 2 seconds then off for 2 seconds
/house1/alarmpanel/bedroom2/burglar on/off The bedroom 2 burglar alarm is turned on or off, as long as the alarm is on flash LED 1 on for 2 seconds then off for 2 seconds
/house1/alarmpanel/firstfloor/burglar on/off The first floor burglar alarm is turned on or off, as long as the alarm is on flash LED 1 on for 2 seconds then off for 2 seconds
/house1/alarmpanel/masterbedroom/window closed/opened The master bedroom windows are opened or closed, opened = LED 2 on, closed = LED 2 off
/house1/alarmpanel/bedroom1/window closed/opened The bedroom 1 windows are opened or closed, opened = LED 3 on, closed = LED 3 off
/house1/alarmpanel/bedroom2/window closed/opened The bedroom 2 windows are opened or closed, opened = LED 4 on, closed = LED 4 off
/house1/alarmpanel/firstfloor/window closed/opened The first floor windows are opened or closed, opened = LED 5 on, closed = LED 5 off
/house1/alarmpanel/firstfloor/motion closed/opened The first floor motion detector triggered, opened = LED 5 on, closed = LED 5 off
/house1/externaldoor/front/door closed/opened The external front door is opened or closed, opened = LED 6 on, closed = LED 6 off
/house1/externaldoor/back/door closed/opened The external back door is opened or closed, opened = LED 7 on, closed = LED 7 off
/house1/externaldoor/side/door closed/opened The external side door is opened or closed, opened = LED 8 on, closed = LED 8 off

Hardware

You will need the following hardware to simulate the input and output the door bell has.

Qty Name Description
1 Netduino Plus The micro board used to execute the logic for the device
1 LEDs LED 1 Simulates the alarm is sounding off
8 LEDs LED 2 thru 9 shows what window or door is opened
1 Ground Strip Exposes more ground connections
1 6 pin header Connect wires from switches and LEDs to the Netduino
2 8 pin header Connect wires from switches and LEDs to the Netduino

Completing the Device

This lab can be done using your own workstation. If you are using your own workstation then you need to go through the steps outlined in the Getting Ready for the Meeting. If a workstation was provided to you at the event then the proper setup would have already been done for you.

The source code for this lab is broken up into two subfolders for each device. One subfolder has all the code completed and is available for your reference (look in the End.* folders). Use this solution for when you get stuck in the lab and you need a hint on how to complete a section. The other solution (look in the Begin.* folders) will be the one that you start out with that has some basic code already done for you but you will be adding to it as you move along in the lab.

Initial Setup of the Device

  1. Get the hardware you need for the device (Netduino, LED's and Switches)
  2. You don't need to wire up the LED's and Switches to the Netduino yet, just have them handy
  3. Get the sign in sheet from the instructor
    1. Fill out the Unique Client ID (use your initials as long as no one else has)
    2. Make a note of the House Code, Device Code and Location Code from the sign in sheet as you will need to use them in the code for the lab.
  4. If the instructor hasn't already provided you with the code then you can get the Netduino Home Security source code zip from the github repository
  5. Unzip the entire contents of the zip into a directory of your choice
  6. Using Visual Studio open up the src\Begin.HomeSecurity.Device.Alarm\HomeSecurity.Device.Alarm.sln
  7. Compile the solution (you should get 0 errors)
  8. Open up Program.cs
  9. Set the _mqttDeviceId variable to the Unique Client Id that you put on the signup sheet
  10. Change the _locationCode = 'firstfloor' to the Location Code that was on the signup sheet
  11. The instructor should have informed you if you needed to connect up to a Local MQTT Broker or the one deployed to Azure.
    1. Uncomment the correct line of code that was assigning the _mqttConnection to the Local or Azure IP.
  12. Hookup the Netduino to your workstation using the Micro USB Cable. (note if the netduino was never hooked up to this workstation then wait for the device drivers on the Netduino to load)
  13. In Visual Studio open up the properties of the HomeSecurity.Device.Alarm Project and select the .Net Micro Framework tab
    1. Verify the Deployment Transport: is set to USB
    2. Verify the Deployment Device: is set to NetduinoPlus2_NetduinoPlus or NetduinoPlus_NetduinoPlus
  14. Compile and Run the Application
    • The application should run but it wont be able to connect to the MQTT broker because the Ethernet cable is not connected yet
    • If Visual Studio appears to hang and it is attempting to deploy the assemblies to the board then disconnect the board from USB and try again
  15. Connect the Ethernet cable to the Netduino and the Hub/Switch

Coding the ping and ping response

The next steps will take you through publishing your first MQTT message and then subscribing to your first topic. The code that actually implements the MQTT protocol is already done for you and is included in the project as a library (Device.MQTT_4.X) for you to call.

Ping

  1. Open up the AlarmController.cs class
  2. Open up the Start() method and look for the TODO Send out ... comment in the code
  3. Add the following line of code replacing the TODO comment:
  4. // Send out a ping topic with Hello World as the message and it should come back to this device as a pingresp
    _mqttService.Publish(new MqttParcel(Topic + "ping","Hello world",QoS.BestEfforts,false));
  5. If you want you can replace the Hello world text with anything you want so that you can tell the message was yours when you monitor the message bus.
  6. Make sure the instructor is running the MQTT Audit program on a laptop that is visible to everyone. If he isn't then tell him to launch it!
  7. Compile and Run your application and watch for your message to get picked up by the MQTT Audit program

Ping Response

  1. Open up the Subscribe() method on the AlarmController and look for the TODO setup your subscriptions here comment in the code
  2. Add the following line of code replacing the TODO comment:
    Subscription subscription = null; ;
    subscription = new Subscription(Topic + "pingresp", QoS.BestEfforts);
    messageId = _mqttService.Subscribe(subscription);
    
    • This tells the MQTT broker you want to subscribe to a specific topic (like /house1/alarm/firstfloor/pingresp)
    • When the topic arrives the PublisheArrived method will be called on the AlarmController
  3. Compile and Run your application and in Visual Studio you should see your message being written out to the Output window (See the _logger.Info call in the PublishedArrived method)
    • Basically your Netduino Application sent a Ping request to the MQTT Broker and the Master Control Panel in Azure was subscribed to your ping topic and it sent back a pingresp to your Netduino
  4. Next we will add logic to light the Onboard LED for 3 seconds when the pingresp arrives
  5. Create a private variable in the AlarmController to represent the Output Port for the onboard LED
    private OutputPort _pingResponseOutput = new OutputPort(Pins.ONBOARD_LED, false);
    
  6. Create a private variable in the AlarmController to represent a timer that will be used to turn off the LED after 3 seconds
    private static Timer _pingResponseTimer = null;
    
  7. In the constructor of the AlarmController class initialize the timer to never fire and have a callback of OnPingResponseTimer
    _pingResponseTimer = new Timer(new TimerCallback(OnPingResponseTimer), this._pingResponseOutput, Timeout.Infinite, Timeout.Infinite);
    
  8. Create the OnPingResponseTimer callback method
    private static void OnPingResponseTimer(object state)
    {
       _pingResponseTimer.Change(Timeout.Infinite, Timeout.Infinite);
       OutputPort output = (OutputPort)state;
       bool isOn = output.Read();
       output.Write(!isOn);
    }
    
  9. Now all you need to do is when the pingresp topic arrives is to turn on the LED and enable the timer to fire in 3 seconds
    private bool PublishArrived(object sender, PublishArrivedArgs e)
    {
        _logger.Info("Msg Recvd: " + e.Topic + " " + e.Payload.ToString());
    
        if (e.Topic.Equals(Topic + "pingresp"))
        {
            _pingResponseOutput.Write(true);
            _pingResponseTimer.Change(3000, 3000);
            return true;
        }
    
  10. Compile and Run the application and the blue onboard LED should come on for 3 seconds

So far you have learned how to do the following:

Coding the LED indicators for door, window and motion sensor changes

Since you know how to subscribe to messages and turn an LED on and off the next steps won't be so detailed.

  1. Create private variables for each LED OutputPort as outlined on the Schematic (D1 - D8)
  2. Add a subscription for each door, window and motion topic in the Subscribe method
    • Hint: you can use the + symbol in the topic for the Device Code so that you can subscribe to multiple devices at once
  3. Add code in the PublishArrived method to test for the specific topic and turn on/off the LED based on the message body being opened or closed
  4. Wire up the LED's to the Netduino
  5. Compile and Run the application
  6. Go to the Home Security Dashboard and click on various doors, windows and motion detectors and verify your LED's turn on/off

Coding the LED indicator for the burglar alarm

The logic behind this LED indicator is a little more complex, but you already know how to use timers, subscribe to topics and toggle output pins, so it shouldn't be too tough. Since the single burglar LED can turn on for any burglar topic coming from multiple alarm panels, you will need to keep some state about each panel and only turn off the alarm when all panels have sent the off message.

  1. Create a private OutputPort variable for the LED to represent the burglar alarm indicator on D0
  2. Create a private Timer variable to do the 2 second on/off logic of the burglar alarm LED
  3. Create the callback method the timer uses to toggle the burglar LED on/off
  4. Add a subscription for each burglar topic in the Subscribe method
    • Hint: you can use the + symbol in the topic for the Device Code so that you can subscribe to multiple devices at once
  5. Add code in the PublishArrived method to test for the specific burglar topic and enable or disable the timer based on the message
  6. Wire up the burglar LED to the Netduino
  7. Compile and Run the application
  8. Go to the Home Security Dashboard and make sure all windows, doors and motion detectors are off and click on the Sleep indicator to arm the security system. Then click on any of the window, door or motion sensors and the burglar alarm LED should go on

Schematic

Breadboard