External Door Entry

The external door device has several functions in the home security system.

There can be more than one external door in a home so part of the message topic includes the name of the door (i.e front, back, side).

Messages

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

AddressDataDescription
/house1/externaldoor/front/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/externaldoor/front/doorbell pushed Someone rang the front doorbell
/house1/externaldoor/front/lock unlocked/locked The front door is unlocked or locked
/house1/externaldoor/front/code 01 A code of 01 was entered on the front door keyless entry pad.
/house1/externaldoor/front/door opened/closed The door is opened or closed.

The device should subscribe to the following MQTT messages.

AddressDataDescription
/house1/externaldoor/front/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/externaldoor/front/setlock lock/unlock The device should lock or unlock the door. Once it does so it should then send the house1/door/front/lock topic with either locked or unlocked as the message body depending on what the device decided to do with the lock.
/house1/externaldoor/front/codevalid true/false The keyless entry code that was entered on the device 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 external door has.

Qty Name Description
1 Netduino Plus The micro board used to execute the logic for the device
1 Pushbutton Switch Simulates the doorbell button
3 Pushbutton Switch Simulates keypad keys for the following: 0, 1 and enter
1 LED Simulates if the door is locked or unlocked. On = Locked, Off = Unlocked
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 door is open or closed. Pushed = door opened, Not Pushed = door closed
1 Ground Strip Exposes more ground connections
1 6 pin header Connect wires from switches and LEDs to the Netduino
1 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.ExternalDoor\HomeSecurity.Device.ExternalDoor.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 = 'front' 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.ExternalDoor 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 ExternalDoorController.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 ExternalDoorController 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 ExternalDoorController
  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 ExternalDoorController 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 ExternalDoorController 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 ExternalDoorController 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 locked indicators

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 variable for the LED OutputPort as outlined on the Schematic (D0)
  2. Add a subscription for the setlock 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 lock or unlock. After turning the LED on/off Publish a message to the lock topic with a message body value of locked or unlocked. This published message lets the dashboard know that the lock was actually locked or unlocked.
  4. Wire up the LED to the Netduino
  5. Compile and Run the application
  6. Go to the Home Security Dashboard and click on the specific door lock indicator and verify your lock LED turns on and off correctly

Coding the LED indicators for code invalid indicator

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 topic is 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 code in the PublishArrived method to test for the specific codevalid topic and enable or disable the timer based on the message (true = disable, false = enable)
  6. Wire up the Invalid Code LED to the Netduino
  7. Compile and Run the application
  8. 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 doorbell switch

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

I have to say that the AutoRepeatInputPort is becoming 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 doorbell input pin (D6).
  2. private AutoRepeatInputPort _doorBell = new AutoRepeatInputPort(Pins.GPIO_PIN_D6, Port.ResistorMode.PullUp, false);
    
  3. In the constructor of the ExternalDoorController class register the callback that will be called when the state of the pin changes
  4. // Setup the interrupt handlers that detect when a doorbell is pressed
    _doorBell.StateChanged += new AutoRepeatEventHandler(_doorBell_StateChanged);
    
  5. Code the callback methods that you just registered above. When the button is detected then you will publish the doorbell topic with a message body set to pushed
  6. void _doorBell_StateChanged(object sender, AutoRepeatEventArgs e)
    {
        switch (e.State)
        {
            case AutoRepeatInputPort.AutoRepeatState.Press:
                _logger.Debug("Doorbell Pushed");
                _mqttService.Publish(new MqttParcel(Topic + "doorbell", "pushed", QoS.BestEfforts, false));
                break;
        }
    }
    
  7. Wire up the doorbell switch as outlined in the schematic/breadboard
  8. Compile and Run the application
  9. Depress the doorbell switch and watch the dashboard to be sure the indicator lights up for a couple seconds

Coding the door switch

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 the Door circuit (D2).
  2. Change the constructor of the ExternalDoorController 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 Door sensor changes state
  4. Wire up the Door switch 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 keypad 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, that's the job of the master control panel running somewhere else.

  1. Create a private AutoRepeatInputPort variable for each key(D3, D4 and D5).
  2. Change the constructor of the ExternalDoorController 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 lock the door by clicking the Locked indicator (your locked LED should come on). Use the keyboard to enter in a secrete code of 01 and press enter. The door should unlock
  7. Also try an invalid code and the Invalid Code LED should blink twice after you press enter

Schematic

Breadboard