Introduction: Doorbell Logger With NodeMCU and Micropython
This is a small project that I started out of curiosity. Sometimes our post packages are delivered to the neighbors even when we are at home. So I wanted to track if the postman actually uses our bell. I found this nice little article in the Make magazine (IoT special) about building a door bell alarm using a Raspberry Pi and a sound sensor. This gave me the necessary drive to implement my own project. I found a Raspberry Pi a bit of an overkill for such a simple task. Even the Raspi Zero costs about 12€ and still needs a WiFi adapter. This is why I decided to go for a NodeMCU. You can get them for 6€ on Ebay and WiFi is already integrated. Also you can run Micropython and I wanted to try this Python implementation for micro controllers for a long time already.
- Duration: 1 hour
- Needed skills: basic knowledge in Python, Flask and command line
- Costs: ~15€
Step 1: Hardware Components
I was surprised about how little components are needed for this task.
- 1 x NodeMCU IoT platform 6,00€ (Ebay)
- 1 x Sound Sensor with digital output 5,00€ (Ebay)
- 1 x USB cable
- 1 x USB Power supply
- 3 x connection wires
That's it. You need to connect the output of the sound sensor to PIN16 which is D4. Also connect the power supply to the fitting PINs on the NodeMCU. Change the sensitivity of the sound sensor with the potentiometer.
Step 2: Starting Up the NodeMCU
There is a really comprehensive tutorial for installing MicroPython on the NodeMCU (the chip is ESP8266). You can simply follow it. For this to work you need to install the driver for the NodeMCU serial chip first.
Install the driver for the serial chip on the NodeMCU
The Serial/USB chip used on the NodeMCU is a CH340G, a Chinese low-cost chip which replaces the FTDI chips used on Arduino boards. You will need to install the fitting driver for your operating system. I used the drivers listed on this site. Just follow the instructions.
Now just follow the installation instructions in the guide "Getting started with MicroPython on the ESP8266".
Connect to the NodeMCU via serial REPL
Use your serial console to connect to the NodeMCU. You will find detailed instructions for this on the MicroPython tutorial "Getting a MicroPython REPL prompt". In the end you should see a command prompt like the one in the picture. Now you can interact with your NodeMCU. First we want to connect to our WiFi network. We can do this by typing the following commands.
>>> import network<br>>>> sta_if = network.WLAN(network.STA_IF)<br>>>> sta_if.active(True)<br>>>> sta_if.connect('SSHID'<your essid="" style="background-color: initial;">, 'PASSWORD<your password="">')<br></your></your>>>> sta_if.ifconfig() ('192.168.0.2', '255.255.255.0', '192.168.0.1', '184.108.40.206')<br>
This will activate the WiFi connection to your network. Replace 'SSHID' and 'PASSWORD' with your network id and -password. Detailed instructions on network connections can be found on MicroPython "Network Basics".
Establish connection via WiFi REPL
Now that we have activated WiFi support we can close the serial console and use the web console to talk to our NodeMCU and send program files to it. Open the WebREPL on http://micropython.org/webrepl/, insert the IP of your device and press connect. Now you should see the same prompt as in the serial console. Now we are ready to program the Doorbell Logger.
Step 3: Programming the Logging Client
The program for our doorbell logger client is fairly simple. Save the following code in a file called main.py.
import machine import time from urequests import post HOST_URI = 'http://yourserver/bell/api/add' bell = machine.Pin(16, machine.Pin.IN) def send(): resp = post(HOST_URI) if resp.text == 'ok': led.high() time.sleep(10) led.low() time.sleep(5) while True: if bell.value() == 0: send()
What this actually does is wait for a signal on PIN16 which is connected to the sound sensor. If this happens the NodeMCU sends a POST request to a HOST defined by the HOST_URI. On the host there is running a small Flask server which will log the events in a small database.
Now we will transfer the main.py file to our NodeMCU by using the WebREPL.
Step 4: Programming the Logging Server
For programming the logging server we will use Flask. Flask is a micro-framework written in Python. The code for the server can be found on Github: https://github.com/MrLeeh/bellwatcher. Clone the repository on your local computer.
Now we will create and activate a new Python virtual environment with the following commands:
$ virtualenv venv ... $ source venv/bin/activate
To install all necessary packages we will use the pip package manager.
$ pip install -r requirements.txt
Now we only need to start the server by calling
$ python app.py Running on http://localhost:8081/ (Press CTRL+C to quit)
Open your browser and visit http://localhost:8081/bell/ and you should see the above screen. Now if an event has been triggered you will find a new entry in the list. You refresh and delete the list as needed. This is actually a very simple interface and it can be accustomed with whatever you want.
Step 5: Conclusion
So this was it. The project is very small and the features aren't very rich yet. But now I can track the postman and investigate if he really used the doorbell. An enhancement I'm thinking about is adding an algorithm to separate door ringing from normal noise (loud closing the door).