Door Bell

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

There can be more than 1 door bell device in the home. The idea is to place the device in a location where the home owner would be likely to see or hear the bell.

Messages

The following MQTT messages will be published from this device.

AddressDataDescription
/house1/doorbell/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/doorbell/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/externaldoor/front/doorbell pushed Someone rang the front doorbell, so turn on LED 1 for 3 seconds to simulate the doorbell ringing
/house1/externaldoor/back/doorbell pushed Someone rang the back doorbell, so turn on LED 2 for 3 seconds to simulate the doorbell ringing
/house1/externaldoor/side/doorbell pushed Someone rang the side doorbell, so turn on LED 3 for 3 seconds to simulate the doorbell ringing

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
3 LEDs Simulates which door bell is pushed
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.Doorbell\HomeSecurity.Device.Doorbell.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.Doorbell 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 DoorbellController.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 DoorbellController 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 DoorbellController
  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 DoorbellController 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 DoorbellController 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 DoorbellController 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 each doorbell

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 - D2)
  2. Create private variables for each timer that will be used to turn off the LED after 3 seconds
  3. Initialize the timers in the constructor to be disabled
  4. Add a subscription for each doorbell 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 doorbell topic and turn on the corresponding LED and start a 3 second timer
  6. Wire up the LED's to the Netduino
  7. Compile and Run the application
  8. Go to the Home Security Dashboard and click on various doorbell indicators and verify your LED's turn on for 3 seconds and then back off

Schematic

Breadboard