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.

NanoPi Installation

NanoPi M1 Installation

Download win32diskimager.rar and extract it to an ordner.
Go to the ordner and double click Win32DiskImager.
Download nanopi-m1-debian-sd4g-20160907.zip and extract it to an ordner.
Put the MicroSD card into the card reader MicroSD card & reader.
In Win32DiskImager select Image File and Device and press Write.

Detailed instructions can be found at wiki.friendlyarm.com/wiki/index.php/NanoPi_M1

First run up

After flashing the image, insert the MicroSD card into the NanoPi, connect the LAN cable, the hdmi cable, a keyboard and mouse and plug in the power supply. Even a 2A power supply is suggested, I’m working with a 1A without problems.

After the first run up do the following on the debian linux operating system.

After the reboot we suggest the following steps:

Next we install the mqtt broker

Then we install node-red

Then the webserver

The database

Other useful programs