IoT Button for Door Opening – Part 2

In the previous blog post, I wrote how we built an IoT device to open the office door via Slack. We controlled a relay via a Raspberry Pi to create a short circuit on the intercom’s door opening button. A server on the Raspberry Pi is called via a POST request when someone types “/door open” in our Slack channel.

This solution works perfectly so that everyone can open the door on the mobile phone from the outside of the building. But from the inside, it would be easier to open the door for visitors with a button press. For this reason, I built an IoT button and connected it to our solution.


Choosing the IoT hardware and framework

I started researching to find the easiest solution for this implementation. The ESP8266 is the most widespread device that is used for this kind of applications. There are many ways to program it. I already had some prior experience with the NodeMCU board, which is basically a small development kit for the ESP8266. I used to program it in Lua via the ESPlorer. But this software already got a bit long in the tooth and does not look like state of the art anymore. I have been programming web applications in JavaScript and have been looking for a way to program also the ESP8266 in JavaScript. One solution that came across was the Espruino, but the community around it looked quite small and larger updates in the past are not visible. Consequently, I found Mongoose OS which looks promising and easy to set up. It can be used with the NodeMCU which I already had at home, but also provides starter kits for the big cloud platforms.

Implementation

1) Download and install the driver for NodeMCU from Mongoose OS

Download either silabs or CH34x, dependent on the NodeMCU type.

2) Download and install the Mongoose OS Software for Windows/MAC:

3) Start the software and configure the hardware

The software opens in the browser and you can select device, download firmware and set up the WiFi connection.

4) Add the code for the HTTP (POST) request to the init.js file

GPIO.set_button_handler(button, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {

HTTP.query({
url: '[URL]/open',
headers: {
'Authorization': 'Basic [AUTHCODE]',
},
data: {},
success: function(body, full_http_msg) {print(body);},
error: function(err) {print(err);}
});
}, null);

5) Test it

If you press the “Flash” button on the NodeMCU, the function for the HTTP request is triggered.

6) The device is now successfully programmed and can be used.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.