Low-Cost Python Internet-Of-Things
The Internet of Things – or IoT for short – is a powerful buzzword in current day-to-day life. It promises a world where everyday object have network connectivity, allowing them to form a massive and secure machine-to-machine network. The potential applications range from smart cities, to smart grids, to smart homes, all powered by smart devices (I assume that smart is another buzzword?). But what does it all mean? Smart cities are probably the closest to reality and they allow a city to manage all its resources centrally. At any time the city knows where its police officers are, which lights are turned on, what traffic conditions are, how many pedestrians are in a given area, etc … . If an accident happens, such a smart city can immediately – even without human intervention – direct the closest police officer to the accident site, advice on the pedestrians that were in the area, redirect traffic to avoid the accident site, and brighten the lights in the area to facilitate rescue and investigation. The beauty of such a system is that it can be manned by just a few people and still be able to deal with a city with millions of inhabitants.
The truth, however, is that many of these smart systems are still in their infancy. We are nowhere near to reaching the promised capabilities, and security is often laughable. This applies especially to smart homes, where low costs often translate in products that are abandoned as soon as they are produced. Without any software updates, and without a sensible security policy implemented from the start, these devices offer enticing targets for criminals to take control over. Ironically, while the Internet of Things (IoT) still has to live up to its promise of making our lives easier, it has already lived up to its promise of being a prime target for these criminals. Only recently a massive DDoS attack crippled parts of the internet, and to blame was a distributed network of unsecure routers, internet-connected cameras, and DVRs.
For all its doom and gloom, the IoT is here to stay. The potential security issues are simply a fact of life and something you need to be aware of. The benefits of IoT are still mostly untapped though, and it is a surprisingly interesting and cheap topic to get involved in. In particular, the smart home offers an excellent opportunity to play with IoT devices while avoiding most security issues and with the potential for some real-life benefits.It’s only recently that IoT has become available to the masses to tinker with. A major revolution occurred with the introduction of the ESP8266 module, as shown above. This tiny system-on-chip provides a full computer with Wi-Fi connectivity in a package of only 2.4cm x 1.6cm. It has its own CPU, memory, storage, and a full TCP/IP stack to allow it to connect to your home Wi-Fi. All of that costs only a few dollars. A problem with the ESP8266 in that form is that it is somewhat difficult to use. If you are not familiar with Lua or C++ it will be difficult to make an ESP8266 do your bidding. Luckily, as we will see in the next paragraph, a special version of Python exists that can be installed on the ESP8266. This makes it significantly easier to program an ESP8266 in little time. The other problem is that the ESP8266 module in and of itself is rather bare. We don’t want to start soldering straight away; we first want to become a bit familiar with the system before we solder away.
To control any machine, we need programming languages to make it clear to the machine what we want it to do. Many, many different programming languages exist and they vary wildly in their difficulty, their capabilities, how well they are supported, and various other metrics. Python is a language that continues to garner interest and is in the top-5 of most popular languages. This is not surprising, as Python programmers typically have cleaner code with less errors, and they can tap into a wealth of libraries (affectionately referred to as “batteries”) that ensure one can develop extensive prototypes and even final systems in little time. Python is so succesful that even schools are deciding to use it over other languages such as Java or C++ because it leaves more time to learn and master programming. MicroPython is a very small and efficient version of Python (and using the newest Python 3 syntax) that can run on microcontrollers. The features it has are of course restricted due to the limited power of microcontrollers, and it comes with fewer “batteries” included, but it is overall a well-balanced implementation that gives you most of the Python benefits. To put it simply: thanks to MicroPython it will be possible to build our own IoT device in less time, and with less need to worry about various bugs that plague lower-level languages.
As mentioned earlier, we don’t only want an easy yet powerful programming language, but we also want an easy way to access the ESP8266 module. Many ESP8266 boards exist that combine an ESP8266 module with a micro-USB connector, some voltage regulators, pins, etc. Everything you need. They come in all shapes, sizes, and prices, but a particularly interesting one is the D1 mini. The D1 mini boards can be bought for only $3 on websites like AliExpress. They are so cheap that you can easily buy half a dozen of them and use them in a variety of projects such as automated Wi-Fi controllable lights to a weather station. Shipping can take a while, but given how much more expensive these are if bought directly from Europe it is well worth the wait.
Once you get hold of a D1 mini board, it’s fairly straightforward to install MicroPython on it. First though, you need to install a serial driver. This driver will allow your computer to communicate with the D1 mini board. The best place to look for the drivers is the WeMos website. However, if you are on the latest version of MacOS you instead want to install this driver as the WeMos driver does not work well (yet) with MacOS Sierra and will cause your system to freeze.
removing the CH340 drivers
If you do end up accidentally installing the wrong drivers on MacOS Sierra (or if you upgraded to MacOS Sierra with the old drivers installed), then follow the instructions on the GitHub source with the fixed driver to remove it.
With the driver installed, all you need is a micro-USB cable to hook up the D1 mini to a computer of a choice, and the latest MicroPython binary which you can find here – look for the header Firmware for ESP8266 to find it. With these things in place, head to the installation instructions and follow the steps in 1.3 to deploy the MicroPython firmware to the D1 mini. There are, however, a few things different for the D1 mini. The port you want to use is
/dev/tty.wchusbserial1420 and the baud rate is
115200. The flash size is also different as the D1 mini comes with a whopping 4MB, so you want to specify this as
32m (32 megabit). So, to erase the D1 mini you use:
esptool.py --port /dev/tty.wchusbserial1420
With the board erased, you can deploy the MicroPython firmware using:
esptool.py --port /dev/tty.wchusbserial1420 --baud 115200 write_flash --verify --flash_mode=dio --flash_size=32m -ff 40m 0x00000 firmware.bin
where you replace
firmware.bin with the name of the MicroPython binary you downloaded. If all goes well, you now have MicroPython installed on your D1 mini! After restarting the D1 mini – simply unplug its micro-USB cable and plug it back in – you should see a new MicroPython-xxxxxx Wi-Fi network. You can connect to this network and use the password micropythoN to log in (notice the capital N at the end).
The final piece of the puzzle is how you can now access your freshly flashed D1 mini and start writing Python code on it. To do this, you need to use the REPL. More information on it can be found here. To access my own D1 mini I prefer to use MacOS and
screen as follows:
screen /dev/tty.wchusbserial1420 115200
When you access the system some gibberish may appear, but after pressing return you should see the Python REPL prompt
>>>. If you do, it’s a clear indication that all of the previous steps went fine. If you don’t, then try to unplug the D1 mini and plugging it back in. If that fails, try to erase the D1 mini board again and redeploy the MicroPython firmware. Once you are connected to the REPL using a wired connection, you can also enable the WebREPL if you so want. This will allow you to program the D1 mini over Wi-Fi. This does pose some security risk, so be sure to only enable the WebREPL if needed and do chose a strong password. The instructions are on the same page as those for the REPL access under the header 2.2. WebREPL - a prompt over WiFi.
If all goes well, you now have a versatile – and cheap!! – IoT device that is easy to program. Of course, on its own it’s little more than a really slow computer. However, as soon as you start attaching lights/sensors/batteries/etc. to its ports you you obtain a slow computer that is able to control lights, sense its environment, and do all of this while completely hidden from sight. The sky is the limit in terms of abilities, but exploring the myriad of things you can do with it is the topic of a future post.