Alarm Control Panel

The alarm control panel device has several functions in the security system.

There can be more than one alarm control panel in a home so part of the message topic includes the name of the panel (i.e firstfloor, masterbedroom, bedroom1, bedroom2).

Messages

The following MQTT messages will be published from this device (note only the firstfloor topics are shown here).

AddressDataDescription
/house1/alarmpanel/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
/house1/alarmpanel/firstfloor/alarmstate away/sleep This alarm panel has changed the state of the security system
/house1/alarmpanel/firstfloor/code 01 A code of 01 was entered on the first floor control panel pad.
/house1/alarmpanel/firstfloor/window opened/closed The window circuit is opened or closed.
/house1/alarmpanel/firstfloor/motion opened/closed The motion circuit is opened or closed.

The device should subscribe to the following MQTT messages.

AddressDataDescription
/house1/alarmpanel/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/firstfloor/setalarmstate away/sleep/off The device should change the LEDs showing the newly set state of the security system, away = LED 1 ON and LED 2 OFF, sleep = LED 1 OFF and LED 2 ON, off = LED 1 and 2 OFF
/house1/alarmpanel/firstfloor/codevalid true/false The code entered on the key panel was valid or not (true or false) If the value is false then flash the LED on/off twice.
/house1/alarmpanel/firstfloor/alarmstatevalid true/false The request to change the alarm state was valid or not(true or false) If the value is false then flash the LED on/off twice.

Hardware

You will need the following hardware to simulate the input and output the alarm control panel.

Qty Name Description
1 Netduino Plus The micro board used to execute the logic for the device
3 Pushbutton Switch Simulates keypad keys for the following: 0, 1 and enter
2 LED LED 1 and 2 indicates the state of the security system.
away = LED 1 ON and LED 2 OFF
sleep = LED 1 OFF and LED 2 ON
off = LED 1 and 2 OFF
1 LED Tells the user that the code entered on the simulated keypad was wrong. Flashes on/off 2 times.
1 Pushbutton Switch Simulates if the window circuit is open or closed. Pushed = window opened, Not Pushed = window closed
1 Pushbutton Switch Simulates if the motion circuit is open or closed. Pushed = motion detected, Not Pushed = no motion detected
1 Pushbutton Switch Set the alarm into sleep mode
1 Pushbutton Switch Set the alarm into away mode
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
  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.AlarmPanel\HomeSecurity.Device.AlarmPanel.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.AlarmPanel 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

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 AlarmPanelController.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 get picked up by the MQTT Audit program

Ping Response

  1. Open up the Subscribe() method on the AlarmPanelController 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 AlarmPanelController
  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 AlarmPanelController 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 AlarmPanelController 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 AlarmPanelController 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 Away and Sleep mode

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 (D0 & D1)
  2. Add a subscription for the setalarmstate topic in the Subscribe method
  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 away or sleep
  4. Wire up the LED's to the Netduino
  5. Compile and Run the application
  6. Go to the Home Security Dashboard and make sure all doors, windows and motion detectors are closed, then click on Away or Sleep indicator and verify your Away and Sleep indicators turn on correctly
  7. To disarm the alarm enter in a code of 01 and click on the enter button on the dashboard UI. Your Away and Sleep LEDs should turn off.

Coding the LED indicator for invalid code

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. This LED turns on/off for 1 second 2 times if the codevalid or alarmstatevalid topics are received and the message body is false.

  1. Create a private OutputPort variable for the LED to represent the invalid code indicator on D2
  2. Create a private Timer variable to do the 1 second on/off logic of the invalid code LED
  3. Create the callback method the timer uses to toggle the invalid code LED on/off
  4. Add a subscription for the codevalid topic in the Subscribe method
    • Note: you only want to subscribe to codevalid topics for this location code because it doesn't make sense to toggle the LED on a panel that is not being used
  5. Add a subscription for the alarmstatevalid topic in the Subscribe method
    • Note: you only want to subscribe to alarmstatevalid topics for this location code because it doesn't make sense to toggle the LED on a panel that is not being used
  6. Add code in the PublishArrived method to test for the specific codevalid or alarmstatevalid topic and enable or disable the timer based on the message (true = disable, false = enable)
  7. Wire up the Invalid Code LED to the Netduino
  8. Compile and Run the application
  9. You wont really be able to test this until you get the keyboard code put together, unless you send specific topics/messages to the broker to simulate a invalid code condition.

Coding the Away and Sleep mode switches

There are several ways to read a pin on a Netduino:

I have to say that the AutoRepeatInputPort is becomming my favorite and it is easy to use, so I suggest you use it. Since this is the first time you probably have used it I will give a little more detail on how it is coded. I have already downloaded the Toolbox libraries and they are referenced in the project so you wont have to do that.
  1. Create a private AutoRepeatInputPort variable for the Away and Sleep input pins (D8 and D9).
  2. private AutoRepeatInputPort _sleepMode = new AutoRepeatInputPort(Pins.GPIO_PIN_D8, Port.ResistorMode.PullUp, false);
    private AutoRepeatInputPort _awayMode = new AutoRepeatInputPort(Pins.GPIO_PIN_D9, Port.ResistorMode.PullUp, false);
    
  3. In the constructor of the AlarmPanelController class register the callback that will be called when the state of the pin changes
  4. // Setup the interrupt handlers that detect when the sleep button is pressed
    _sleepMode.StateChanged += new AutoRepeatEventHandler(_sleepMode_StateChanged);
    
    // Setup the interrupt handlers that detect when the alarm button is pressed
    _awayMode.StateChanged += new AutoRepeatEventHandler(_awayMode_StateChanged);
    
    
  5. Code the callback methods that you just registered above
  6. void _awayMode_StateChanged(object sender, AutoRepeatEventArgs e)
    {
        switch (e.State)
        {
            case AutoRepeatInputPort.AutoRepeatState.Press:
                // The away security mode button was pressed
                _logger.Debug("Away button pressed");
                _mqttService.Publish(new MqttParcel(Topic + "alarmstate", "away", QoS.BestEfforts, false));
                break;
        }
    }
    
    void _sleepMode_StateChanged(object sender, AutoRepeatEventArgs e)
    {
        switch (e.State)
        {
            case AutoRepeatInputPort.AutoRepeatState.Press:
                // The sleep security mode button was pressed
                _logger.Debug("Sleep button pressed");
                _mqttService.Publish(new MqttParcel(Topic + "alarmstate", "sleep", QoS.BestEfforts, false));
                break;
        }
    }
    
  7. Wire up the Away and Sleep switches as outlined in the schematic/breadboard
  8. Compile and Run the application
  9. If the security system is not already armed and all doors, windows and motion detectors are not triggered then you should be able to press the Sleep or Away button and the security system will become armed.
  10. To disarm the security system go to the Dashboard and enter in 01 for the Secret Code and press the Enter button

Coding the Window and Motion detector switches

Since you already know how to use the AutoRepeatInputPort there wont be a lot of steps to this.

  1. Create a private AutoRepeatInputPort variable for each Window and Motion circuit (D3 and D7).
  2. Change the constructor of the AlarmPanelController class to register for the events
  3. Code the callbacks for the events and add the code to send out the correct MQTT message when the Window or Motion sensors change state
  4. Wire up the Window and Motion switches as outlined in the schematic/breadboard
  5. Compile and Run the application
  6. Verify that when you depress the switches that the correct indicator changes state on the dashboard

Coding the keyboard switches

Since the keyboard is really just 3 keys (0, 1 and Enter) you will use the same AutoRepeatInputPort class to detect what the user typed in. The user can type any combination of 0 or 1 keys before they press the Enter key so you will have to keep track of the keys and the sequence in which they were depressed. The job of this device is to just collect the keystrokes and send it off to the broker when the user presses the enter key. This device does not validate the secrete code at all, thats the job of the master control panel running somewhere else.

  1. Create a private AutoRepeatInputPort variable for each key(D4, D5 and D6).
  2. Change the constructor of the AlarmPanelController class to register for the events from each key
  3. Code the callbacks for the events and add the code to send out the correct MQTT message when the user presses the Enter key
  4. Wire up the 0, 1 and Enter key switches as outlined in the schematic/breadboard
  5. Compile and Run the application
  6. Using the dashboard place the alarm in away or sleep mode and use the keyboard to enter in a secrete code of 01 and press enter. The alarm should disarm
  7. Also try an invalid code and the Invalid Code LED should blink twice after you press enter

Schematic

Breadboard