The following model was created as a teaching aid for a demonstration of IoT concepts.
The associated PowerPoint is attached to the end of this Instructable.
This Instructable will cover:
- Creating the Telegram chat group and associated bot.
- Programming the ESP32.
- Building the model and electronic circuit.
Disclaimer: I am not a coder and so I am sure that much of the code could be cleaned up. This said, this project should give you a good place to start playing.
- House plans for laser cutter
- Buck converter (unless you use a LM3940)
- Resistors: 2x 5Ω, 1x 470Ω (these are not the values generally recommend on-line but the higher values would not work with my LEDs and associated circuit)
- LEDs: LED Ring, 2x white 5mm LEDs
- DHT11 (temperature and humidity sensor)
- RCWL-0516 (microwave sensor to detect movement)
- Momentary button
- Heat shrink tube
- Wire for connecting up the sensors
- Soldering iron
- Heat gun
- Wire strippers
Step 1: Creating the Telegram Chat Group and Associated Bot
There are many frameworks that you can use to manage IoT devices but for this demonstration I am using Telegram. It provides a good balance between security, ease of implementation and ease of use.
Go to your app store and install Telegram on your phone. I find it easier working on my computer so once your telegram account has been created, I would then install the desktop app (https://telegram.org/).
Creating the Bot
- Open Telegram and search for “botfather” and click on it; or open t.me/botfather
- If prompted, click the “start” button.
- Now type “/newbot“, following the instructions to create a new bot as well as to create the bot’s username.
- Copy the token down for later use. If you do use it you can always find it again by looking in the bot settings.
- Now enter “/mybots” and click on the bot that you have just created.
- From there open “Bot settings” and select “Allow Groups?“, making sure that this is enabled. Doing so allows you at add your bot to a chat group.
Depending what you are wanting to achieve, you might choose not use a chat group to control your IoT devices. The reason that I use a group is that it is then easy to add or remove users from the group i.e. it is easy to grant and remove access to use the devices associated with the chat group.
Creating the Chat Group
- Create a new group e.g. “My IoT Devices”, adding the new bot that you have just created. If you don’t seem able to find it, go back and check that it is enabled for groups.
- Also add “Telegram Bot Raw” (@RawDataBot) by @SeanChennel. This bot displays all the raw data received by the group, enabling you to see the required channel and chat IDs as well as do troubleshooting. Once you are finished using it, you can remove it from the group.
- When the raw data appears, look for the section which lists the “Title” together with the chat group name. Just above that you will see an “ID“. Note this ID number down. This will be used in the code to ensure that your IoT on reacts to message posted in this particular chat.
Armed with the bot’s API token and the above ID, you are now ready to programme the ESP32.
Step 2: Building the Model and Electronic Circuit
I ordered my plans from Esty (just search for “laser cut files house”) and cut the house out using a laser cutter.
The TV I modeled myself using FreeCAD (rename TV.FCStd.txt to TV.FCStd to use it in FreeCAD). It was used to hide alarm sensor (RCWL-0516). As the RCWL-0516 uses microwaves, it can be covered.
The Party light was a 12 LED ring which also served as the Livingroom light.
The buzzer was used for the door bell as well as for the alarm
The rest of the furniture was sourced from BlenderSwap and then scaled accordingly when 3D printing.
I used heatshrink tubing to help tidy up the wiring and hot-glue to stick everything in place. If I did this again I would consider doing the following:
- Instead of stripboard, I would design a PCB to hold the components.
- Hide the controller in some furniture or an outbuilding.
- Put in some false floors, ceilings or walls to hide the wiring
In my project I used a Buck Converter but in my schematic I have used an LM3940, as this is how I would do it if I did it again.
Step 3: Programming the ESP32
For a single, simple IoT device one could use an ESP-01 together with a relay. I am however wanting to control a number of inputs and outputs and so am use the ESP32.
Installing the ESP32 libraries
- In your Arduino IDE, go to File> Preferences
- Click on the window icon at the end of the “Additional Board Manager URLs” field and add “https://dl.espressif.com/dl/package_esp32_index.js…” to the bottom of the list. Don’t worry if there are no other entries.
- Open the Boards Manager. Go to Tools > Board > Boards Manager
- Search for ESP32, pressing install for “ESP32 by Espressif Systems“
- Go to Boards in Tools > Board and select your board. In my case it was “DOIT ESP32 DEVKIT V1“
- Plug your board in and then select the corresponding COM port. if you don’t see it, you will need to install the CP210x USB to UART Bridge VCP Drivers and then reboot your computer.
The final goal
- To give a specified group of people access to control certain aspects of the house.
- Arm, disarm and silence the alarm.
- Turn lighting on and off.
- Turn on party lights
- Send message if doorbell pressed.
- Check house status, including temperature.
This was one the more challenging pieces of code that I have written as I decided to play with both cores on the ESP32. What this allowed me to do was to run the alarm and party lights on one core and the rest of the code on a separate core. This meant that the lights or alarm could loop independently of the rest of the programme and therefore not be interrupted when checking or sending Telegram messages. Initially this resulted in a number of nasty crashes which I managed to resolve by using Semaphores. I have not fully mastered all of this but in short this allowed me to prevent code on different cores from competing with each other for the same resources.
I still have one strange error i.e. if the first action after a restart is to press the doorbell then the watchdog triggers and the board reboots. If any other action is done first, all goes well and from then onwards the doorbell can be used without issue. Since this is just a proof of concept, being used to demo some IoT concepts, I can live with this for now (also I have spent far too much time trying to work out what causes this).
Step 4: Conclusion
While a tad frustrated that I could not fix the doorbell issue, I am very happy with how it all turned out. This is very much of a prototype and a lot more could be done to tidy up the code, circuit and wiring.
Initially I wanted to have a fan that would come on when the temperature sensor indicated that it was getting hot, but I ran out of time.
Update: I just found one more bug i.e. if the living room lights are turned on while the part lights sequence is active, they sometimes don’t come on and then need to be switched off and on again. This has something to do with the order in which the processes are running on each core.
Source: IoT Demo