Door Sensor and MQTT Server

Magnet Switch

Writup found on

sourecode found at


The Purpose of this lab is to take the principles learned and the devices setup in lab2 and lab3 and use them with a simple closed door sensor and a mqtt protocol hub. This will allow us to build a car sensor that detects how far away you are from the garage wall and it only works when the garage door is open.


  • 3 x WEMOS d1 mini

  • jumper wires

  • 3 x breadboards

  • 1 red led

  • 1 green led

  • 1 yellow led

  • 1 HC-SR04 ultrasonic sensor

  • 3 x 220ohm resistors (RRBlBlBr)

  • 1 x 1kohm(or similar) resistor

  • Raspberry Pi

  • 4 x micro USB cables


I used the following references in this lab:

Monitor Your Door Using Magnetic Reed Switch and Arduino

MQTT PubSubClient Tutorial for Arduino, ESP8266, and ESP32


  1. First of all here is the overall architecture of the project.

  2. Notice that the Raspberry Pi will be used to run the MQTT hub, and the proximity and door sensor will be sending their status to the hub on different channels while the stoplight will listen to the two channels and perform the needed logic.

  3. Setup the Proximity Sensor & stoplight as described in lab2 and lab3

  4. The next step is to create the door sensor. Notice that the resistor grounds the circuit when the door switch is open. This is important because it causes the digital read pin to read 0 instead of some unknown value. This guide gave a good walk through of how to exactly wire it.

  5. To setup the MQTT Server on the raspberry pi simply install the mosquitto package.

apt install mosquitto

You may have to enable the service to make sure that it runs on boot

systemctl enable mosquitto

Once that is running a simple command will let you watch incoming messages or send messages on your own

mosquitto_sub -t door
mosquitto_pub -t door -m “Hello World”

  1. Finally we need setup all the sensors and stoplight to use mqtt instead of http. To do this I followed this guide. Essentially you setup a connection to a mqtt server (raspberry pi) and then you can publish messages to a specific channel. I set each sensor to publish to its on channel and then I replaced the http get requests with a mqtt publish. Then the stoplight can subscribe to multiple channels and then you have an event handler that fires every time a new message comes in. I parsed the data from the message and depending on which channel or content of the message I then lit up the correct color on the stoplight or turned off/on the stoplight all together.

  2. Once those steps are done you should have a working stoplight!

Thought Questions

  1. How does the communication between devices change with the shift to using an event hub? How does this facilitate greater scalability?

  • The communication is not from Arduino to Arduino anymore. Now all the devices communicate to the mqtt server and they can choose which messages they receive and how they respond. This allows you to have many more devices and all they need to communicate with is the central server.

  1. What are strengths and weaknesses of the direct communication between the sensor and actuator? What are strengths and weaknesses of the event hub? Which do you feel is better for this application?

  • The strength of direct communication is that when you have 2 devices they you only need 2 devices to facilitate the communication you also can use different types of communication like radio or inferred . The drawback is that when you add more devices it becomes a more and more complex interaction and troubleshooting it can be a challenge. The strength of an event hub is that you can add many more devices without increasing the complexity very much. The downside is in simpler application you end up with a more complicated setup then you might need. For this application I believe a hub is better. While you could do it with direct, the debugging and testing that comes through mqtt is beneficial. Hub

  1. What was the biggest challenge you overcame in this lab?

  • For me the entire lab was a bit of a challenge. Connecting to the mqtt server from the Arduino’s and then adapting the prior labs to incorporate that took me the longest.

  1. Please estimate the total time you spent on this lab and report

  • lab: 5 hours, write up: 2 hours

Posted in IOT

Leave a Reply

Your email address will not be published. Required fields are marked *