The autohomy security milestone

All in one security solution for your home

Autonomous security for your homy house.

Technical Details:

  • smoke detector for fire alarm
  • 12V/5A LED Strip driver for light control
  • motion detection PIR sensor for light control and intruder detection
  • 85dB acustic alarm
  • temperatur sensor for heating control and fire alarm
  • humidity sensor

Schematics for interfacing the smoke detector with the NodeMcu

K1-3 … NodeMcu GPIO output alarm
K1-2 … NodeMcu GND
K1-1 … NodeMcu GPIO input smoke detected

K2-3 … Smoke detector test switch 12V
K2-2 … Smoke detector test switch GND
K2-1 … Smoke detector red buzzer cable

Picture of the enhanced alarm board

Schematics for interfacing the LED strip with the NodeMcu

Picture of the Mosfet

Picture of the DHT11

Picture of the DC/DC converter 12V to 5V

Picture of all

Attention! When programming the Wemos D1 mini via USB port, it is necessary to have the 12V power supply attached to the system. Otherwise the 5V from the USB port will burn the fire alarm chip! Really crazy but found not solution, yet.

 

Pin Mapping

Updating code via webserver

To update the code via the webserver, you need to install a virtual COM port to TCP driver.

On a windows system you can download com0com from https://sourceforge.net/projects/com0com/ , a virtual Nullmodem cable.
Start setup.exe and 4 COM ports will be installed we need the one beginning with COM (e.g. COM46 and COM47).
Then we need com2tcp from https://sourceforge.net/projects/com0com/files/com2tcp/1.3.0.0/ and in a CMD shell we have to run:

To set up the tunnel in Linux just install socat:

Downloading the code:

First we have to display the webpage of the node via a webbrowser. Then we turn telnet mode on by pressing the button “Turn telnet mode on”. After that we can start com2tcp like above.
Via Esplorer we open the virtual com port 47 and set in the Esplorer settings the “dumb mode”.
Then open the file you want to update and send it by pressing the button “Save to ESP” to the node. You can work with Esplorer like you are connected via the serial port, with the only exception that button “Upload” is not working, use the button “Save to ESP” instead.

Sometimes “Save to ESP” has a problem with long lines, you can see it in Esplorers output window:

In such a case break the lines into smaller ones and it will work.

Basic definitions in a autohomy system

In order to achive our goal of autohomy we have to set up some basic definitions

Broker: A broker is a mqtt broker. In autohomy this is a NanoPi with debian linux running on it. See NanoPi installation.

MqttRoot: Is the base topic of a node.

Node: A node is a autonomous thing, that can have several sensors and/or actuators. It communicates via mqtt with the world. Mostly in autohomy this is a Wemos D1 mini NodeMcu.

Updating code via mqtt

To update any code file you only have to send the file to “MqttRoot“/update/ with the file name as topic, e.g. if you want to update the message.lua script on node “home/buero/light” you send the file to topic “home/buero/light/update/message.lua”. Please note: That will only work for files smaller than 985 bytes.

On a nanoPi with debian installed, which is our default configuration (see NanoPi installation), you open a shell and give the following command:
mosquitto_pub -h localhost -t “home/buero/light/update/message.lua” -f message.lua

If you want to update all your nodes in your home with a new message.lua file,  you give the following command:
mosquitto_pub -h localhost -t “home/+/+/update/message.lua” -f message.lua

If you want to update all your light nodes in your home with a new light.lua file,  you give the following command:
mosquitto_pub -h localhost -t “home/+/light/update/light.lua” -f light.lua

If you want to update files larger than 984 bytes, then you have to send it in chunks. You can use this little script for it on the NanoPi. Copy the following code to a file called “update”.

For updating the file message.lua on node home/buero/light type in a command shell the command:

Wait for 10 seconds after updating, because the code appends all chunks sent within 10 seconds. Files bigger then 8kb cannot be updated. If you want to, you have to change the timer in messages.lua to a higher value than 10 seconds, but it is not a good programming style to have so big files.

Hints for programming in Lua on NodeMcu

Be cautious in your programming style, because the NodeMcu has a very limited memory. A drawdown of using an intepreter language like lua is that all your code has to be loaded on the heap.

But if you consider some points it is really great to work with Lua.

Decide carefully when using functions. On the NodeMcu you have to weight memory usage over readability of your code. Functions need about 200 bytes of memory overhead, so avoid setter and getter functions. A good rule is to only make a function for code that is more than 20 lines and if you really see an advantage in readability.

If you want to use a function for code that is only needed once for readability,  set the function to nil after its call.
Because in Lua a function is nothing other than a an entry in a table this approch can free up memory.

Give functions that are not frequently needed or not time critical in a separate file and load them only when you need them. After the function call set them to nil, so that the garbage collector can free up the memory.

Do not create files bigger than 984 bytes. This has two advantages. First the file can be loaded quicker from flash, especially useful if you want to load it at runtime. Second advantage is that the file can be directly updated via Mqtt. See Updating code via Mqtt.

There is also a interesting blog for holding functions in flash memory.

Programming the Wemos D1 Mini module

Programming the Wemos D1 Mini module

An good tool to program and test the module is the Esplorer software. You can download it from https://github.com/4refr0nt/ESPlorer.

Press the button Upload, select the files you want to upload to the Wemos D1 Mini Module and press the Open button. After all files are uploaded you can set RTS button of serial port to enabled and the to disabled to reset the module via the reset pin, or press Reset to make a software reset.

 

Please read the hints for working in Lua on NodeMcu.

Control of LED strip via PIR sensor

How to control a led stripe via a PIR sensor

Seems an easy task, but there where some pitfalls.
For this project we needed the following parts:

Wemos D1 Mini NodeMcu
MOSFET IRF520 driver module
Aluminum Heat Sink for TO-220 MOSFET
DC-DC Buck Converter Step Down Module
HCSR501 human infrared sensor module
DHT11 Digital Temperature and Humidity Sensor
10cm 2.54mm 1pin Female to Female jumper wire Dupont cable

Put the things together

First we flash the Wemos D1 Mini NodeMcu. Then we connect the mosfet driver module via 3 dupont cables to the NodeMcu (SIG-D5, GND-G and VCC-5V).

The lua code files

init.lua

program.lua

config.lua

log.lua

light.lua

th.lua

motion.lua

web.lua

webserver.lua

webpage.lua

webpage.html

config.html

Flashing the NodeMcu firmware

Flashing the NodeMcu firmware

The default firmware on the Wemos D1 Mini NodeMcu is AT command based, so we have to flash the NodeMcu firmware into it.

Download and install NodeMcu flashtool.

Download and install ch340 USB seriell driver.

Connect Wemos D1 mini NodeMcu with a micro USB cable to your PC.
Download the firmware from here and the ESP init file from here or get it via NodeMcu homepage.
Switch to tab Config and select Firmware and ESP init file. Make sure to select the checkboxes and enter 0x00000 as base adress for the firmware and 0x3FC000 for the ESP init file.

Switch back to tab Operation and press Flash(F) to start flashing.

Now you have the Wemos D1 Mini NodeMcu running with the correct NodeMcu firmware.
Press the reset button on the Wemos D1 mini module and wait 5 minutes before you begin programming the module, because the firmware is formatting the filesystem on the first reset after flashing.