Nacktnasenwombat
Published © CC BY-NC

Temperature/Humidity Measurement with NodeMCU

Temperature and humidity measurement with BME280, a Raspberry Pi and NodeMCU. I use MQTT, InfluxDB, Telegraf, Grafana and MicroPython.

IntermediateFull instructions provided2 hours7,323
Temperature/Humidity Measurement with NodeMCU

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Bosch BME280
×1
NodeMCU LUA Amica V2 Modul mit ESP8266 12E
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

MicroPython
MicroPython
Python for microcontrollers (like esp8266)
InfluxDB
easy to use database made for measurements
Telegraf
plugin-driven server agent for collecting & reporting metrics
Grafana
open platform for beautiful analytics and monitoring
MQTT
MQTT

Story

Read more

Schematics

circuit design

NodeMCU with BME280

Code

boot.py

MicroPython
boot.py for the ESP8266-Controller
from umqtt.robust import MQTTClient
from bme280 import BME280
import network
import machine
from time import sleep


# define environment variables
LOCATION         = "location" 
MQTT_BROKER      = "broker"
MQTT_TOPIC       = "sensors/"+LOCATION
WLAN_SSID        = "SSID"
WLAN_PASS        = "password"

c = MQTTClient("umqtt_client_"+LOCATION,MQTT_BROKER)

# WLAN connection 
def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        wlan.connect(WLAN_SSID,WLAN_PASS)
        while not wlan.isconnected():
            pass

def do_disconnect():
    wlan = network.WLAN(network.STA_IF)
    wlan.disconnect()
    wlan.active(False)

# publish topic to mqtt-broker
def write_mqtt(topic,temperature,humidity):
    payload = b'{"location":"' + LOCATION + '","temperature":' + temperature + ',"humidity":' + humidity + '}'
    try:
        c.connect()
        c.publish(b""+topic,payload)
        c.disconnect()
    except Exception as e:
        print(str(e))

# initialize BME280
i2c = machine.I2C(scl=machine.Pin(5),sda=machine.Pin(4))
bme =  BME280(i2c=i2c)

# establish connection
do_connect()

# initialize RTC
p = machine.Pin(12,machine.Pin.OUT)
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)

# read temperature, pressure, humidity
t,p,h = bme.values
write_mqtt(MQTT_TOPIC,t[:-1],h[:-1]) # cut the last character, else it's no number!

# set RTC.ALARM0 to fire after 5 minutes (waking the device)
rtc.alarm(rtc.ALARM0, 300000)
do_disconnect()
machine.deepsleep()

BME280

The BME280 micropython library

Credits

Nacktnasenwombat

Nacktnasenwombat

3 projects • 4 followers

Comments