News:

We're back online! If you encounter any issues using the forum, please file a report in the Engine Room.

Main Menu

Gentlemen*! Behold — This Thing I Made!

Started by von Corax, April 10, 2023, 08:13:48 PM

Previous topic - Next topic

von Corax

This isn't very Steampunk yet, although I do have tentative plans to remedy that oversight.

In November I purchased a FreeNove Ultimate Starter Kit for ESP32. For those not familiar with this device, the Espressif ESP32 is a microcontroller, which is to say a small, low-power-draw microprocessor with built-in RAM to run programs, built-in flash memory to store programs, and a plethora of individually-controlled "General-Purpose Input/Output" pins, some of which have analogue-to-digital converters to measure variable input voltages and turn that measurement into a binary number. In the case of the ESP32, it also has built-in Wi-Fi and Bluetooth radios.

Having completed the tutorials included in the Starter Kit, I have decided to begin building my personal Internet of Things. This first Thing, which is not yet Internet-enabled, is a thermohygrometer to measure and report the temperature and humidity here at the Institute.

My First Thing: ShowHide

The Circuitry: ShowHide

The circuit currently consists of an ESP32-WROVER module (at left), connected to a DHT22 temperature/humidity sensor module (the red and white module at the centre of the breadboard) and a GJD1602A LCD module communicating via I2C (far right and below).
The Current Weather at the Institute: ShowHide


This version of the Thing is basically identical to the circuit in the FreeNove tutorial, with the exception of a DHT22 module replacing the tutorial's DHT11.

The ESP32 can be programmed in C++, but it also can run MicroPython firmware; I have taken the Python option and will post my code in a few hours.

My current plans for this project are to first network-enable it so that it reports the readings to my computer where I can insert them into a time-series database for future analysis and charting. I then plan to replace the FreeNove ESP32-WROVER module with one of the ESP32-WROOM modules I purchased this week, remove the display, solder it up, and then build several more of them. Eventually I would like to put it in a nice steamy box, perhaps with an e-paper display. I would also like to acquire a Bluetooth-enabled outdoor weather station in order to track how outdoor conditions affect those indoors, although this last is some distance down the road.

*This, of course, includes Ladies, Steam-Powered Automata and Divers Non-Conformants; it's just that I'm doing a bit here, eh!
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

von Corax

Here is the code which runs my Thing. The ESP32 can run the MicroPython firmware, so my code is in Python. I use an IDE called Thonny, which is capable of uploading firmware and code to the ESP32.

This version 0.9 is taken directly from the FreeNove tutorial.

MicroPython executes the file boot.py first. This version of boot.py simply iterates through the other files that have been flashed and executes them each in turn.

Code (boot.py) Select

#!/opt/bin/lv_micropython
import uos as os
import uerrno as errno
iter = os.ilistdir()
IS_DIR = 0x4000
IS_REGULAR = 0x8000

while True:
    try:
        entry = next(iter)
        filename = entry[0]
        file_type = entry[1]
        if filename == 'boot.py':
            continue
        else:
            print("===============================")
            print(filename,end="")
            if file_type == IS_DIR:
                print(", File is a directory")
                print("===============================")
            else:
                print("\n===============================")
                #print("Contents:")
                #with open(filename) as f:
                #   for line in enumerate(f):
                #       print("{}".format(line[1]),end="")
                #print("")
                exec(open(filename).read(),globals())
    except StopIteration:
        break


Hygrothermograph.py is executed by boot.py and is the code which reads the sensor every 2 seconds and writes the readings to the display module. Again, this is the FreeNove code, with the exception that I changed the display module's I2C address and the use of the DHT22 sensor in place of the DHT11.
Code (Hydrothermograph.py) Select

from time import sleep_ms
from machine import I2C, Pin
from I2C_LCD import I2cLcd
import dht

#DHT = dht.DHT11(Pin(18))
DHT = dht.DHT22(Pin(18))

#DEFAULT_I2C_ADDR = 0x27
DEFAULT_I2C_ADDR = 0x3f
i2c = I2C(scl=Pin(14), sda=Pin(13), freq=400000)
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)

try:
    while True:
        DHT.measure()
        lcd.move_to(0, 0)
        lcd.putstr("Temperature:")
        lcd.putstr(str(DHT.temperature()))
        lcd.move_to(0, 1)
        lcd.putstr("Humidity:   ")
        lcd.putstr(str(DHT.humidity()))
        sleep_ms(2000)
except:
    pass


I2C_LCD.py and LCD_API.py are library files which support control over the 1602 I2C module. When I remove the LCD display these files will go with it, and eventually the e-paper display will require different library files.
Code (I2C_LCD.py) Select

from LCD_API import LcdApi
from machine import I2C
from time import sleep_ms

# The PCF8574 has a jumper selectable address: 0x20 - 0x27
DEFAULT_I2C_ADDR = 0x27

# Defines shifts or masks for the various LCD line attached to the PCF8574

MASK_RS = 0x01
MASK_RW = 0x02
MASK_E = 0x04
SHIFT_BACKLIGHT = 3
SHIFT_DATA = 4


class I2cLcd(LcdApi):
    def __init__(self, i2c, i2c_addr, num_lines, num_columns):
        self.i2c = i2c
        self.i2c_addr = i2c_addr
        self.i2c.writeto(self.i2c_addr, bytearray([0]))
        sleep_ms(20)   # Allow LCD time to powerup
        # Send reset 3 times
        self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
        sleep_ms(5)    # need to delay at least 4.1 msec
        self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
        sleep_ms(1)
        self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
        sleep_ms(1)
        # Put LCD into 4 bit mode
        self.hal_write_init_nibble(self.LCD_FUNCTION)
        sleep_ms(1)
        LcdApi.__init__(self, num_lines, num_columns)
        cmd = self.LCD_FUNCTION
        if num_lines > 1:
            cmd |= self.LCD_FUNCTION_2LINES
        self.hal_write_command(cmd)

    def hal_write_init_nibble(self, nibble):
        """Writes an initialization nibble to the LCD.

        This particular function is only used during initialization.
        """
        byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA
        self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E]))
        self.i2c.writeto(self.i2c_addr, bytearray([byte]))

    def hal_backlight_on(self):
        """Allows the hal layer to turn the backlight on."""
        self.i2c.writeto(self.i2c_addr, bytearray([1 << SHIFT_BACKLIGHT]))

    def hal_backlight_off(self):
        """Allows the hal layer to turn the backlight off."""
        self.i2c.writeto(self.i2c_addr, bytearray([0]))

    def hal_write_command(self, cmd):
        """Writes a command to the LCD.

        Data is latched on the falling edge of E.
        """
        byte = ((self.backlight << SHIFT_BACKLIGHT) | (((cmd >> 4) & 0x0f) << SHIFT_DATA))
        self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E]))
        self.i2c.writeto(self.i2c_addr, bytearray([byte]))
        byte = ((self.backlight << SHIFT_BACKLIGHT) | ((cmd & 0x0f) << SHIFT_DATA))
        self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E]))
        self.i2c.writeto(self.i2c_addr, bytearray([byte]))
        if cmd <= 3:
            # The home and clear commands require a worst case delay of 4.1 msec
            sleep_ms(5)

    def hal_write_data(self, data):
        """Write data to the LCD."""
        byte = (MASK_RS | (self.backlight << SHIFT_BACKLIGHT) | (((data >> 4) & 0x0f) << SHIFT_DATA))
        self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E]))
        self.i2c.writeto(self.i2c_addr, bytearray([byte]))
        byte = (MASK_RS | (self.backlight << SHIFT_BACKLIGHT) | ((data & 0x0f) << SHIFT_DATA))
        self.i2c.writeto(self.i2c_addr, bytearray([byte | MASK_E]))
        self.i2c.writeto(self.i2c_addr, bytearray([byte]))

Code (LCD_API.py) Select

import time

class LcdApi:
    # The following constant names were lifted from the avrlib lcd.h
    # header file, however, I changed the definitions from bit numbers
    # to bit masks.
    #
    # LCD controller command set

    LCD_CLR = 0x01              # DB0: clear display
    LCD_HOME = 0x02             # DB1: return to home position

    LCD_ENTRY_MODE = 0x04       # DB2: set entry mode
    LCD_ENTRY_INC = 0x02        # --DB1: increment
    LCD_ENTRY_SHIFT = 0x01      # --DB0: shift

    LCD_ON_CTRL = 0x08          # DB3: turn lcd/cursor on
    LCD_ON_DISPLAY = 0x04       # --DB2: turn display on
    LCD_ON_CURSOR = 0x02        # --DB1: turn cursor on
    LCD_ON_BLINK = 0x01         # --DB0: blinking cursor

    LCD_MOVE = 0x10             # DB4: move cursor/display
    LCD_MOVE_DISP = 0x08        # --DB3: move display (0-> move cursor)
    LCD_MOVE_RIGHT = 0x04       # --DB2: move right (0-> left)

    LCD_FUNCTION = 0x20         # DB5: function set
    LCD_FUNCTION_8BIT = 0x10    # --DB4: set 8BIT mode (0->4BIT mode)
    LCD_FUNCTION_2LINES = 0x08  # --DB3: two lines (0->one line)
    LCD_FUNCTION_10DOTS = 0x04  # --DB2: 5x10 font (0->5x7 font)
    LCD_FUNCTION_RESET = 0x30   # See "Initializing by Instruction" section

    LCD_CGRAM = 0x40            # DB6: set CG RAM address
    LCD_DDRAM = 0x80            # DB7: set DD RAM address

    LCD_RS_CMD = 0
    LCD_RS_DATA = 1

    LCD_RW_WRITE = 0
    LCD_RW_READ = 1

    def __init__(self, num_lines, num_columns):
        self.num_lines = num_lines
        if self.num_lines > 4:
            self.num_lines = 4
        self.num_columns = num_columns
        if self.num_columns > 40:
            self.num_columns = 40
        self.cursor_x = 0
        self.cursor_y = 0
        self.backlight = True
        self.display_off()
        self.backlight_on()
        self.clear()
        self.hal_write_command(self.LCD_ENTRY_MODE | self.LCD_ENTRY_INC)
        self.hide_cursor()
        self.display_on()

    def clear(self):
        """Clears the LCD display and moves the cursor to the top left
        corner.
        """
        self.hal_write_command(self.LCD_CLR)
        self.hal_write_command(self.LCD_HOME)
        self.cursor_x = 0
        self.cursor_y = 0

    def show_cursor(self):
        """Causes the cursor to be made visible."""
        self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
                               self.LCD_ON_CURSOR)

    def hide_cursor(self):
        """Causes the cursor to be hidden."""
        self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY)

    def blink_cursor_on(self):
        """Turns on the cursor, and makes it blink."""
        self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
                               self.LCD_ON_CURSOR | self.LCD_ON_BLINK)

    def blink_cursor_off(self):
        """Turns on the cursor, and makes it no blink (i.e. be solid)."""
        self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY |
                               self.LCD_ON_CURSOR)

    def display_on(self):
        """Turns on (i.e. unblanks) the LCD."""
        self.hal_write_command(self.LCD_ON_CTRL | self.LCD_ON_DISPLAY)

    def display_off(self):
        """Turns off (i.e. blanks) the LCD."""
        self.hal_write_command(self.LCD_ON_CTRL)

    def backlight_on(self):
        """Turns the backlight on.

        This isn't really an LCD command, but some modules have backlight
        controls, so this allows the hal to pass through the command.
        """
        self.backlight = True
        self.hal_backlight_on()

    def backlight_off(self):
        """Turns the backlight off.

        This isn't really an LCD command, but some modules have backlight
        controls, so this allows the hal to pass through the command.
        """
        self.backlight = False
        self.hal_backlight_off()

    def move_to(self, cursor_x, cursor_y):
        """Moves the cursor position to the indicated position. The cursor
        position is zero based (i.e. cursor_x == 0 indicates first column).
        """
        self.cursor_x = cursor_x
        self.cursor_y = cursor_y
        addr = cursor_x & 0x3f
        if cursor_y & 1:
            addr += 0x40    # Lines 1 & 3 add 0x40
        if cursor_y & 2:
            addr += 0x14    # Lines 2 & 3 add 0x14
        self.hal_write_command(self.LCD_DDRAM | addr)

    def putchar(self, char):
        """Writes the indicated character to the LCD at the current cursor
        position, and advances the cursor by one position.
        """
        if char != '\n':
            self.hal_write_data(ord(char))
            self.cursor_x += 1
        if self.cursor_x >= self.num_columns or char == '\n':
            self.cursor_x = 0
            self.cursor_y += 1
            if self.cursor_y >= self.num_lines:
                self.cursor_y = 0
            self.move_to(self.cursor_x, self.cursor_y)

    def putstr(self, string):
        """Write the indicated string to the LCD at the current cursor
        position and advances the cursor position appropriately.
        """
        for char in string:
            self.putchar(char)

    def custom_char(self, location, charmap):
        """Write a character to one of the 8 CGRAM locations, available
        as chr(0) through chr(7).
        """
        location &= 0x7
        self.hal_write_command(self.LCD_CGRAM | (location << 3))
        self.hal_sleep_us(40)
        for i in range(8):
            self.hal_write_data(charmap[i])
            self.hal_sleep_us(40)
        self.move_to(self.cursor_x, self.cursor_y)

    def hal_backlight_on(self):
        """Allows the hal layer to turn the backlight on.

        If desired, a derived HAL class will implement this function.
        """
        pass

    def hal_backlight_off(self):
        """Allows the hal layer to turn the backlight off.

        If desired, a derived HAL class will implement this function.
        """
        pass

    def hal_write_command(self, cmd):
        """Write a command to the LCD.

        It is expected that a derived HAL class will implement this
        function.
        """
        raise NotImplementedError

    def hal_write_data(self, data):
        """Write data to the LCD.

        It is expected that a derived HAL class will implement this
        function.
        """
        raise NotImplementedError

    def hal_sleep_us(self, usecs):
        """Sleep for some time (given in microseconds)."""
        time.sleep_us(usecs)


I will have a circuit schematic up as soon as I find a CAD program I like.
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

J. Wilhelm

Quote from: von Corax on April 10, 2023, 08:13:48 PM
This isn't very Steampunk yet, although I do have tentative plans to remedy that oversight.

In November I purchased a FreeNove Ultimate Starter Kit for ESP32. For those not familiar with this device, the Espressif ESP32 is a microcontroller, which is to say a small, low-power-draw microprocessor with built-in RAM to run programs, built-in flash memory to store programs, and a plethora of individually-controlled "General-Purpose Input/Output" pins, some of which have analogue-to-digital converters to measure variable input voltages and turn that measurement into a binary number. In the case of the ESP32, it also has built-in Wi-Fi and Bluetooth radios.

Having completed the tutorials included in the Starter Kit, I have decided to begin building my personal Internet of Things. This first Thing, which is not yet Internet-enabled, is a thermohygrometer to measure and report the temperature and humidity here at the Institute.

My First Thing: ShowHide

The Circuitry: ShowHide

The circuit currently consists of an ESP32-WROVER module (at left), connected to a DHT22 temperature/humidity sensor module (the red and white module at the centre of the breadboard) and a GJD1602A LCD module communicating via I2C (far right and below).
The Current Weather at the Institute: ShowHide


This version of the Thing is basically identical to the circuit in the FreeNove tutorial, with the exception of a DHT22 module replacing the tutorial's DHT11.

The ESP32 can be programmed in C++, but it also can run MicroPython firmware; I have taken the Python option and will post my code in a few hours.

My current plans for this project are to first network-enable it so that it reports the readings to my computer where I can insert them into a time-series database for future analysis and charting. I then plan to replace the FreeNove ESP32-WROVER module with one of the ESP32-WROOM modules I purchased this week, remove the display, solder it up, and then build several more of them. Eventually I would like to put it in a nice steamy box, perhaps with an e-paper display. I would also like to acquire a Bluetooth-enabled outdoor weather station in order to track how outdoor conditions affect those indoors, although this last is some distance down the road.

*This, of course, includes Ladies, Steam-Powered Automata and Divers Non-Conformants; it's just that I'm doing a bit here, eh!

Well, that's definitely a Thing.  While I'll pretend to understand the apparatus and programming code at first sight, let me ask you, what kind of enclosure would be appropriate and not interfere with temperature/humidity measurements? These are indoor measurements right?



Madasasteamfish

As someone who has to deal with such sensors in a professional capacity, this is definitely presents a dangerous possibility for Steampunk shenanigans....
I made a note in my diary on the way over here. Simply says; "Bugger!"

"DON'T THINK OF IT AS DYING, JUST THINK OF IT AS LEAVING EARLY TO AVOID THE RUSH."

Sorontar

Nice start!

I have been thinking of settling up a lcd display for the side of a tricorn with a waving pirate flag and related imagery programmed. No sensors, but as usual, I have too many ideas but not enough wings.

Sorontar
Sorontar, Captain of 'The Aethereal Dancer'
Advisor to HM Engineers on matters aethereal, aeronautic and cosmographic
http://eyrie.sorontar.com

von Corax

Some refactoring to the Python code:
Code (boot.py) Select

# This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
#import webrepl
#webrepl.start()

boot.py now does nothing. In future, it will connect to WiFi.

Code (main.py) Select

import time

from machine import I2C, Pin

import dht

from I2C_LCD import I2cLcd

#DEFAULT_I2C_ADDR = 0x27
DEFAULT_I2C_ADDR = 0x3f

MEASUREMENT_INTERVAL_MS = 2000

#DHT = dht.DHT11(Pin(18))
DHT = dht.DHT22(Pin(18))

i2c = I2C(scl=Pin(14), sda=Pin(13), freq=400000)
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)

def display(temp, hum):
    try:
        lcd.move_to(0, 0)
        lcd.putstr("Temperature:")
        lcd.putstr(str(temp))
        lcd.move_to(0, 1)
        lcd.putstr("Humidity:   ")
        lcd.putstr(str(hum))
    except:
        pass

def read_and_display():
    try:
        DHT.measure()
        temperature = DHT.temperature()
        humidity = DHT.humidity()
        display(temperature, humidity)
    except:
        pass
    return temperature, humidity

if __name__ == '__main__':
    while True:
        temperature, humidity = read_and_display()
        time.sleep_ms(MEASUREMENT_INTERVAL_MS)

Previously, boot.py would run every file in the root directory; now main.py is run automagically as soon as boot.py exits. Aside from that, the code changes consist of breaking down the monolithic code into single-purpose functions.

The files I2C_LCD.py and LCD_API.py are unchanged from the previous version.
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

von Corax

I fear I've gotten myself into a yak-shaving exercise.

The whole point of an IoT device is (typically) to generate data which is then stored for further analysis. To implement this, I had decided on the following open-source software stack:

  • PostgreSQL (a relational database)
  • TimescaleDB (a PostgreSQL extension which turns PostgreSQL into a time-series database
  • Prometheus (a network-monitoring tool which collects data from various endpoints and stores them in a time-series database)
  • Promscale Connector (a remote storage backend for Prometheus which allows Prometheus metrics to be stored in a TimescaleDB database)
  • Promscale Extension (a PostgreSQL extension which provides datatypes and functions to support Promscale Connector)

Unfortunately, as of March Promscale (connector and extension) are abandoned and unsupported, and there are no other options for using Prometheus with PostgreSQL. In a fit of madness, I have ordered from Amazon a book on Go (the language in which Promscale Connector is written) and Rust (the language in which Promscale Extension is written.)

Hmm... That yak would look much better with short hair.

My fear is that, once I've taught myself sufficient Go and Rust to understand the inner workings of the software, I will feel compelled to adopt the projects and become their official Maintainer. I know there are others as dismayed as I at the projects' abandonment, and an open-source project (let alone two) would look really good on a resumé.

Yak, meet razor.
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

J. Wilhelm

#7
Quote from: von Corax on May 06, 2023, 03:42:53 AM
I fear I've gotten myself into a yak-shaving exercise.

The whole point of an IoT device is (typically) to generate data which is then stored for further analysis. To implement this, I had decided on the following open-source software stack:

  • PostgreSQL (a relational database)
  • TimescaleDB (a PostgreSQL extension which turns PostgreSQL into a time-series database
  • Prometheus (a network-monitoring tool which collects data from various endpoints and stores them in a time-series database)
  • Promscale Connector (a remote storage backend for Prometheus which allows Prometheus metrics to be stored in a TimescaleDB database)
  • Promscale Extension (a PostgreSQL extension which provides datatypes and functions to support Promscale Connector)

Unfortunately, as of March Promscale (connector and extension) are abandoned and unsupported, and there are no other options for using Prometheus with PostgreSQL. In a fit of madness, I have ordered from Amazon a book on Go (the language in which Promscale Connector is written) and Rust (the language in which Promscale Extension is written.)

Hmm... That yak would look much better with short hair.

My fear is that, once I've taught myself sufficient Go and Rust to understand the inner workings of the software, I will feel compelled to adopt the projects and become their official Maintainer. I know there are others as dismayed as I at the projects' abandonment, and an open-source project (let alone two) would look really good on a resumé.

Yak, meet razor.

Unfortunately I don't understand any of that database jargon,  and what I see is multiple dependencies. What I can tell you from developing in-house code in college is that trying to rewrite someone else's code is a nightmare to say the least. Not that it wouldn't be professionally written  ::) but my experience is that multiple code contributions usually result in a Frankenstein of a code that hides very nasty secrets inside -if you can find them. Then again, in the universe of code development, that's exactly what you're getting paid to do. And I agree, it would look good on your resume - that's your pay!

I fear you're going to find a lot of things inside that yak hair. Why were the projects abandoned? In my case, I always preferred to do my own coding and keep it as simple as possible; ie shaving my own hair as opposed to shaving a yak, or if at all possible have the yak hair shipped to my address.


https://m.youtube.com/watch?v=Ie_qnUHO1yA


Sorontar

Quote from: von Corax on May 06, 2023, 03:42:53 AM
I fear I've gotten myself into a yak-shaving exercise.

[snip]
Hmm... That yak would look much better with short hair.

My fear is that, once I've taught myself sufficient Go and Rust to understand the inner workings of the software, I will feel compelled to adopt the projects and become their official Maintainer. I know there are others as dismayed as I at the projects' abandonment, and an open-source project (let alone two) would look really good on a resumé.

Yak, meet razor.

Is the yak's name Occam?

Sorontar
Sorontar, Captain of 'The Aethereal Dancer'
Advisor to HM Engineers on matters aethereal, aeronautic and cosmographic
http://eyrie.sorontar.com

von Corax

So, yaks aside...

As I said, the main point of IoT is to collect data for further analysis. To that end, I have pulled from inventory a Dell Optiplex 7010, 8GB RAM and two 320GB hard drives from inventory and installed FreeBSD 13.2* along with the following ports:

  • bash (a command shell)
  • sudo (manages user privelege)
  • OpenNTPD (sets the system clock to the National Research Council of Canada standard)
  • avahi (lets computers find each other on the network)
along with

  • PostgreSQL 15.2 (a relational database manager)
  • TimescaleDB 2.10.2 (a PostgreSQL extension which implements a time-series database)
  • Prometheus 2.43.0 (collects system metrics and stores them in a time-series database)
  • Promscale Connector 0.17.0 (stores Prometheus metrics in a Timescale database)
  • Promscale Extension 0.8.0 (a PostgreSQL extension which supports Promscale Connector)
  • Grafana 9.5.1 (visualizes Prometheus data in charts on a web page)

Getting everything working took some time, effort and learning; I'll not recount that part here, but anyone wanting to duplicate this project can PM me.

Once everything is connected and working, the Thing will transmit its temperature and humidity readings via MQTT (Message Queueing Telemetry Transport, for those keeping score) to Prometheus, which will store the readings in the database along with their timestamps. Grafana will then retrieve those readings and display them as a chart, along with whatever additional information I eventually decide to add to the system, such as perhaps current weather conditions.

*All software choices are religious/political decisions, and so shall not be debated here. :D
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

von Corax

Progress report: my Thing now connects to WiFi and transmits its readings via MQTT. Once I've tidied up the code I'll post a more detailed update.
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

von Corax

My Thing is now a small-i internet-enabled Thing. It connects to WiFi and uploads a measurement every 2 seconds to a central server, which will eventually pass that measurement on to a database and graphing application. (I currently have no plans to make this a Capital-I Internet Thing.)

The revised code (v1.0.0) is as follows:
Code (boot.py) Select

#boot.py
import network

WIFI_SSID = '********'       # obviously not my real SSID
WIFI_PASSWORD = '********'   # equally obviously not my real WiFi password

def connect():
    net = network.WLAN(network.STA_IF)    # Create network connection in Station mode
    if not net.isconnected():             # While a connection does not exist:
        net.active(True)                  # activate the connection...
        net.connect(WIFI_SSID, WIFI_PASSWORD)    # ...and try to log in
        while not net.isconnected():      # Loop until connection is established
            pass

connect()



boot.py, which is run on startup and on wakeup from deep sleep, establishes a WiFi connection and waits until the connection is successful.

Code (main.py) Select

#main.py
import dht
import json
import time

from machine import I2C, Pin
from umqtt.simple import MQTTClient

from I2C_LCD import I2cLcd

#DEFAULT_I2C_ADDR = 0x27
DEFAULT_I2C_ADDR = 0x3f

MEASUREMENT_INTERVAL_MS = 2000

CLIENT_ID = 'Thing'
SERVER = 'raspberrypi.local'
USERID = b'********'
PASSWD = b'********'
TOPIC = b'/sensors/bedroom/'

#DHT = dht.DHT11(Pin(18))
DHT = dht.DHT22(Pin(18))

i2c = I2C(scl=Pin(14), sda=Pin(13), freq=400000)
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)

def connect():
    client = MQTTClient(CLIENT_ID, SERVER, user=USERID, password=PASSWD, keepalive=60, port=1883)
    client.connect()
    return client

def publish(client, readings):
    payload = json.dumps(readings)
    client.publish(TOPIC, payload)

def display(readings):
    try:
        lcd.move_to(0, 0)
        lcd.putstr("Temperature:")
        lcd.putstr(str(readings["temperature"]))
        lcd.move_to(0, 1)
        lcd.putstr("Humidity:   ")
        lcd.putstr(str(readings["humidity"]))
    except:
        pass

def take_readings():
    readings = dict()
    try:
        DHT.measure()
        readings["temperature"] = DHT.temperature()
        readings["humidity"] = DHT.humidity()
    except:
        pass
    return readings

if __name__ == '__main__':
    client = connect()
    while True:
        readings = take_readings()
        display(readings)
        publish(client, readings)
        time.sleep_ms(MEASUREMENT_INTERVAL_MS)
       


This module first connects to the MQTT broker, then every 2 seconds it takes a reading, displays that reading, and publishes the reading in a JSON object to the MQTT message queue.

The I2C_LCD.py and LCD_API.py modules are unchanged from the original version.

One more component at this stage is the Raspberry Pi running the mosquitto MQTT broker (which could also have been installed on the Prometheus server.) The next step will be to install prometheus-mqtt-exporter on the Pi to load the Thing's data into the Prometheus monitoring system, and then configure Grafana to chart the readings over time.
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

von Corax

Another Yak, Not Like the Other Yak

I've run into a snag in the Thing project. I had intended to use a software package called Prometheus as the data-collection tool; Prometheus uses modules called "exporters" to ingest data from different sources, for example "node exporter" collects operational metrics from the machine on which it's installed, while "mqtt exporter" harvests data from an MQTT broker. Hypothetically, an exporter can be installed on any node on the network and will then be queried remotely by the Prometheus server. However, my network uses mDNS (Multicast DNS) for nodes to find each other (this is the "avahi" mentioned earlier) and, unlike every other piece of network-enabled software written in the past ten years, Prometheus is not compatible with mDNS. This is a known bug, and has been for the past six years.

The obvious solution would be to dump Prometheus in favour of an alternate package, so of course I'm not going to do that. In stead, I have an introductory text on the Go programming language, I plan to order another on Go networking, and I will then clone down the Prometheus repository and tear it apart to see if I can solve the bug myself. (Yes, I need to shave a yak before I can begin shaving the yak I intended to shave.)

(For those not familiar with hackish colloquialisms, "shaving a yak" is the terribly complex task you absolutely must undertake before you can complete the original, ostensibly-simple task you wanted to perform although you can't quite recall why.)
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

von Corax

The State of the Thing

The hardware: I have an idea which will eliminate the DHT22 sensor and the external display. I can implement this as a prototype easily enough; however, for the finished Thing I need to do some soldering which must wait until I find my soldering iron and a place to set it up, which requires that I set up a shop space, which requires that I find a new place to live... (It is in my 2-year plan as a high-priority item.) I also need to make a small purchase of additional hardware to replace the sensor and display.

The software: Once I have the aforementioned small hardware purchase made, I will need to make minor alterations to my MicroPython code. This depends only on my finding the motivation to make said purchase.

The infrastructure: I've decided to abandon my plan to use Prometheus as the data collection tool. Instead I plan to write my own middleware to connect my Thing to my Timescale database. This, again, awaits my motivation to make a small purchase, and beyond that is a simple matter of coding.
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

J. Wilhelm

Quote from: von Corax on August 23, 2023, 03:14:04 AM
The State of the Thing

The hardware: I have an idea which will eliminate the DHT22 sensor and the external display. I can implement this as a prototype easily enough; however, for the finished Thing I need to do some soldering which must wait until I find my soldering iron and a place to set it up, which requires that I set up a shop space, which requires that I find a new place to live... (It is in my 2-year plan as a high-priority item.) I also need to make a small purchase of additional hardware to replace the sensor and display.

The software: Once I have the aforementioned small hardware purchase made, I will need to make minor alterations to my MicroPython code. This depends only on my finding the motivation to make said purchase.

The infrastructure: I've decided to abandon my plan to use Prometheus as the data collection tool. Instead I plan to write my own middleware to connect my Thing to my Timescale database. This, again, awaits my motivation to make a small purchase, and beyond that is a simple matter of coding.

So it looks like a third species of yak is needed (finding a new place - that counts as a yak). Shaving may take the form of grass shearing (or finding a pre-sheared location).  Best of luck with the new place. I'll be shaving that species of yak soon enough myself (because I don't trust my landlord/roommate to not try something funny that'd absolutely require me to sue the yak manure out of him).

von Corax

Shacking a Yak

The first of my yaks is being lathered and the razor stropped as I type this. I have made a down-payment on a house with a finished basement and high-speed Internet access. The closing date is just before Christmas, so by the end of the year I will have one yak shaved.

This is also convenient, since the Thing is intended to be a component in a home-automation project, and come the New Year I will have a whole home to automate. 8)
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

Sir Henry

I wish you the very best of luck. Since recently purchasing a house with three electrical systems, each working at a different voltage, I know how much work it can be.

And I do hope, for your sake, that each of the elements of automation goes more straight-forwardly than any other piece of 21st century technology. Virtually every piece of time-saving tech I have had the pleasure of working with in the last decade has taken days, weeks or occasionally months to get running properly when thirty years ago it would have been sorted with the flick of a switch.
I speak in syllabubbles. They rise to the surface by the force of levity and pop out of my mouth unneeded and unheeded.
Cry "Have at!" and let's lick the togs of Waugh!
Arsed not for whom the bell tolls, it tolls for tea.

von Corax

I'm not too worried. It's a fairly new house, and I'm having it inspected this afternoon, so there shouldn't be any surprises.

(How do you end up with 3 different voltages?)
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading

Sir Henry

The house used to be a Home for disable adults, so it had Safety Features.

1)There's the main wiring for the house at 240v. Though because every room has a shutoff switch (for all the electrics in the room) outside the room and a test circuit, the switches have as many as 9 wires going into each one.

2) The fire alarm and sensors are all wired up in a 12v circuit throughout the house. When we first lit the wood stove, alarms were going off in places no-one had been for over 20 years!

3) The best of all. All the internal doors are fire doors but as it was residential they were open all the time. This was done with an electromagnet attached to the wall behind every door and a metal disk attached to the back of every door. When the fire alarm went off it cut power to the magnets and all the doors would close thanks to the strong door-closing mechanism on each one.
This was a 96v circuit.
What happens when you leave all the doors magnetically left open for 2 years while the place is empty, I hear you ask. Glad you asked. Several of the magnets were hot to the touch when we moved in and every single door frame had needed to be reset as they had all been pulled out of square. And it turns out that you can fill the large gaps this creates with baby wipes. Loads and loads of baby wipes.  :o

Best of luck with yours.
I speak in syllabubbles. They rise to the surface by the force of levity and pop out of my mouth unneeded and unheeded.
Cry "Have at!" and let's lick the togs of Waugh!
Arsed not for whom the bell tolls, it tolls for tea.

von Corax

Quote from: Sir Henry on October 26, 2023, 04:46:06 PMThe house used to be a Home for disable adults, so it had Safety Features.

1)There's the main wiring for the house at 240v. Though because every room has a shutoff switch (for all the electrics in the room) outside the room and a test circuit, the switches have as many as 9 wires going into each one.

2) The fire alarm and sensors are all wired up in a 12v circuit throughout the house. When we first lit the wood stove, alarms were going off in places no-one had been for over 20 years!

3) The best of all. All the internal doors are fire doors but as it was residential they were open all the time. This was done with an electromagnet attached to the wall behind every door and a metal disk attached to the back of every door. When the fire alarm went off it cut power to the magnets and all the doors would close thanks to the strong door-closing mechanism on each one.
This was a 96v circuit.
What happens when you leave all the doors magnetically left open for 2 years while the place is empty, I hear you ask. Glad you asked. Several of the magnets were hot to the touch when we moved in and every single door frame had needed to be reset as they had all been pulled out of square. And it turns out that you can fill the large gaps this creates with baby wipes. Loads and loads of baby wipes.  :o

Best of luck with yours.

I guess I shouldn't say anything about yours, since all North American homes are wired 240v/120v split phase, and my doorbell(s) run on 24v AC.

In any case, I now have a home, and a Place for my Stuff, so as soon as I can find said Stuff I can get back to work on my Thing.
By the power of caffeine do I set my mind in motion
By the Beans of Life do my thoughts acquire speed
My hands acquire a shaking
The shaking becomes a warning
By the power of caffeine do I set my mind in motion
The Leverkusen Institute of Paleocybernetics is 5845 km from Reading