2. April 2017

Custom font for OLED display connected to ESP8266 via SPI

Small OLED displays can easily extend the functionality of ESP8266.

I made an experiment with 128×64 OLED display from Com-Four.

The first challenge was how to connect the display to ESP8266. The recommended way for high performance is to use Serial Peripheral Interface Bus (SPI). The advantage of this approach is the speed, the disadvantage is that it will take more pins.

The display could be connected in following way (also described in the example of ESP8266_SD1306 library):

ESP8266 - SD1306
GND     - GND
3V      - VDD
D5      - SCK (also known as CLK)
D7      - SDA (also known as MOSI/DOUT)
D0      - RES
D2      - DC
D8      - CS

If you’re using PlatformIO, just add ESP8266_SD1306 library to dependencies in platfromio.ini:

lib_deps =
 ESP8266_SSD1306

Now you can run any example from Squix78 library. The library contains 3 sizes of Arial font: 10, 16 and 24px.

My goal was to display temperature from Observatory in Brno. Retrieving temperature and sending it to MQTT for ESP8266 was quite easy.

#!/usr/bin/env python3

import paho.mqtt.publish as publish

import urllib.request
f = urllib.request.urlopen('http://www.hvezdarna.cz/meteo/lastmeteodata')
content = f.read().decode('utf-8')

items = content.split(' ')

publish.single('/home/monitor/display/0', items[4], hostname='localhost')

I used default Arial 24 font. The problem was that the number was too small and barely readable from a distance. Luckily Daniel Eichhorn published great online tool which is able to generate font of any size for OLED display: http://oleddisplay.squix.ch.

My first attempt was to generate Roboto Light 54px font. It was working, just number 4 was not visible. I discovered a bug in the generator, that too big font will overflow default size of char in the jump table.

After several attempts I’ve found the right font for me DejaVu Sans 52px. This font was far more readable.

The last touch to make the font more readable was to tune down contrast little bit by the command:

display.setContrast(10);

I can definitely recommend this type of OLED display. It has good readability even during a sunny day. The code is available at GitHub in LampESP project.

1. March 2017

Microsoft Azure IoT Hub MQTT device to device communication is not supported

Microsoft Azure provides IoT Hub. It might sound great because it has support for MQTT. It is very important to mention that Azure IoT Hub has a different way how it operates than Mosquitto which means that architecture of your IoT solution must be different.

Let’s check differences.

Default port:

  • Mosquitto is 1883 which is not secure, it is possible to enable TLS
  • IoT Hub 8883 secured by TLS/SSL (only)

Publishing and subscription of messages:

That’s a huge difference. In the case of Mosquitto you can easily build network of devices which can communicate together and management logic could be injected by Node-RED. In the case of IoT Hub whole interaction is managed by the cloud.

If you still want to play with IoT Hub and MQTT, I recommend article written by Satish Pagare who explains how to use mosquitto_sub and mosquitto_pub to interact with IoT Hub.

20. February 2017

How to send command from Python via MQTT to RGB LED connected to ESP8266

I’ve described how to send server load as a number to MQTT in the previous article. The number could be then translated via Node-RED to command for LampESP with RGB LED. The result is simple. LED indicates server load by displaying different colors.

The other option is to deliver color command directly from the server using Python.

Just install paho-mqtt:

pip install paho-mqtt

Here is small snippet of Python code (publish_server_load.py):

#!/usr/bin/env python3

import paho.mqtt.publish as publish
import os

color = 'red'
load = os.getloadavg()[0]
if load < 0.7:
    color = 'black'
elif load < 1.5:
    color = 'blue'
elif load < 3:
    color = 'green'
elif load < 7:
    color = 'orange'

publish.single('/server/monitoring/command', color, hostname='iot.sinusgear.com')

ESP should listen to /server/monitoring. Code of LampESP 0.3 is available at GitHub.

Put this code into crontab

* * * * * /usr/local/bin/publish_server_load.py

If you’re using virtualenv the command should be:

* * * * * /opt/my-python-env3/bin/python /usr/local/bin/publish_server_load.py

13. February 2017

Vertical text block selection in PlatformIO/Atom on Linux

Sometimes it is necessary to modify multiple lines at once. PlatformIO has support for multiple cursors which allows you to perform operation on multiple places.

You can place separate cursors by holding Ctrl and left click by mouse. That is handy, but when you need 4 or more lines it’s a little bit cumbersome.

When you need to select vertical text block of text in PlatformIO on Linux just hold Shift+Alt and use arrows Up/Down to select text block. Make sure that you press and hold keys in defined order. 1st is Shift and 2nd is Alt. If you change the order then the selection won’t work.

Here is how it looks like.

Shift+Alt+Up/Down:

Now you can use Shift+Left/Right to select block of text:

If you’d like to discuss this feature, there is a very very long discussion thread about it at Atom’s web page.

11. February 2017

Display server load on Linux Mint desktop delivered via MQTT

In previous article I wrote how to subscribe to MQTT topics and display also timestamp with color. There was one MQTT topic which represented server load and it demonstrates that MQTT could be used for server monitoring.

Here is an example how to measure server load and publish it as MQTT topic. Open crontab:

crontab -e

Add following line:

* * * * * /usr/bin/mosquitto_pub -t /server/reactor/load -m `cat /proc/loadavg | sed -e 's/ .*//g'`

Cron will invoke the command every minute. Command will publish data to MQTT topic /server/reactor/load (note: reactor is the name of my server).

We can subscribe at a workstation to the topic and execute some commands. mosquitto_sub will keep running in the background and we can use xargs to invoke a new command once the new message arrives.

Following example shows how to display notification on Linux Mint – Cinnamon using

mosquitto_sub -t '/server/reactor/load' -h iot.sinusgear.com | xargs -d$'\n' -L1 sh -c 'notify-send "Server load: $0"'

The result at workstation:

The only downside is that notifications are spamming desktop every minute. It does not make sense to notify admin when server load is too low. We can add a little bit of logic and display notifications about load which exceeded a certain threshold.

mosquitto_sub -t '/server/reactor/load' -h iot.sinusgear.com | xargs -d$'\n' -L1 /bin/zsh -c 'if (( $(echo "$0 > 1.2" | bc -l ) )); then notify-send "Server load: $0"; fi'

Note: just be aware that message from MQTT is not sanitized. Run such command only in trusted environment or add type check of value received from MQTT.

Enjoy :-)