/templogger

Low-power Micropython MQTT temperature logger for DS18B20 and ESP8266 (Banggood ESP12E) running on 18650 Lipo.

Primary LanguagePython

Low-power Micropython MQTT temperature logger for DS18B20 and ESP8266 (Banggood ESP12E) running on 18650 Lipo.

Features:

  • Temperature sensor
  • 1/60 Hz update interval
  • Low-power (deepsleep)
  • Battery level indicator
  • Battery voltage level
  • Logging to MQTT
  • Multiple sensor support
  • Webrepl alternative boot

Overview

Functionality

This firmware is configured to run the following steps at boot:

  • Read battery voltage
  • Read all connected one-wire temperature sensors
  • Wait for Wifi connection to be established (timeout 10s)
  • Publish values to MQTT host named mqtt on default MQTT port.
  • Shutdown and enter deep-sleep to conserve power
  • Wakeup (boot) after 60 seconds (minus runtime)

This cycle repeats until the battery is dead or alternative boot is selected (see below).

Example output

Logging to MQTT will look like this:

templog/9b80e600/battery ok
templog/9b80e600/280d7427050000e5 22.37
templog/9b80e600/voltage 4.11

The first topic level is always templog, the next level is the unique device ID.

The battery topic show status of the battery ok for anything above 3.5v, low for everything below.

The voltage topic shows the actual battery voltage in volts.

For every onewire temperature sensor a topic is added with the unique ID of that sensor. Multiple sensors are supported and adding/removing sensors is possible without powercycle.

When using webrepl in testing mode (see below) sensor information is also printed as python dictionary like this:

>>> import templog; templog.templog(sleep=False)
{'battery': 'ok', 'voltage': 4.111, '280d7427050000e5': 22.37}

Hardware

Requirements:

  • ESP12E (or other ESP8266 device, but tested on ESP12E)
  • DS18B20 one-wire temperature sensor(s)
  • 18650 battery (or alternative power source)
  • onewire pullup resistor (4k7 or similar)

The following connections need to be made:

  • Battery + to VDD, CH_PD and GPIO02
  • Battery - to GND and GPIO15
  • VDD to onewire VDD
  • GND to onewire GND
  • VDD to pull-up resistor
  • Pull-up resistor to GPIO12
  • GPIO12 to onewire data
  • GPIO16 (WAKE) to RST

ESP12E pinout

Circuit overview

Not shown on picture a wire between VDD and CH_PD!

Setup

Clone this repository using --recursive or update submodules git submodule update --init.

Ensure ESP12E device has:

Next copy all these files using webrepl_cli:

webrepl/webrepl_cli.py templog.py <ip of device>:
webrepl/webrepl_cli.py micropython-lib/umqtt.simple/umqtt/simple <ip of device>:umqtt/

Log into webrepl, connect to the device and run:

import templog; templog.templog(sleep=False)

This will read battery and temperature values, print them and publish to MQTT but not go into deep sleep.

Enable templogger on boot:

webrepl/webrepl_cli.py boot.py <ip of device>:

Alternative boot to webrepl

To run Webrepl (upload new firmware/debug) instead of the temperature logger at boot pull down the 1w datapin (GPIO12) to ground. During bootup this will select webrepl. Since the software boots every 60 seconds after deepsleep just connecting the 1w pin to ground should have it end up in webrepl within a minute without cycling power.

To return to normal templogging mode powercycle the device or run: import machine; machine.reset().

Battery voltage level

To allow measurement of battery voltage instead of ADC input pin run adc_vdd.py once. This will configure the ADC to allow reading the voltage accross gnd and vdd.

Home assistant

This device is used in combination with Home-assistant, configuration example:

mqtt:
  broker: localhost

sensor:
  - name: temp_sensor
    platform: mqtt
    state_topic: "templog/9b80e600/280d7427050000e5"
    unit_of_measurement: °C
  - name: temp_sensor_battery
    platform: mqtt
    state_topic: "templog/9b80e600/battery"
  - name: temp_sensor_voltage
    platform: mqtt
    state_topic: "templog/9b80e600/voltage"
    unit_of_measurement: V

group:
  temp_sensor:
    name: Mobile temperature sensor
    entities:
      - sensor.temp_sensor
      - sensor.temp_sensor_battery
      - sensor.temp_sensor_voltage