<filename>mqtt-all.py
"""
Run mqtt broker on localhost: sudo apt-get install mosquitto mosquitto-clients
Example run: python3 mqtt-all.py --broker 192.168.1.164 --topic enviro
"""
#!/usr/bin/env python3

import argparse
import ST7735
import time
import math
from bme280 import BME280
from pms5003 import PMS5003, ReadTimeoutError, SerialTimeoutError
from enviroplus import gas

try:
    # Transitional fix for breaking change in LTR559
    from ltr559 import LTR559

    ltr559 = LTR559()
except ImportError:
    import ltr559

from subprocess import PIPE, Popen, check_output
from PIL import Image, ImageDraw, ImageFont
from fonts.ttf import RobotoMedium as UserFont
import json

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

try:
    from smbus2 import SMBus
except ImportError:
    from smbus import SMBus


DEFAULT_MQTT_BROKER_IP = "172.16.58.3"   # Original was "localhost"
DEFAULT_MQTT_BROKER_PORT = 1883
DEFAULT_MQTT_TOPIC = "envirocrawl"   # Original was "enviroplus"
DEFAULT_READ_INTERVAL = 5
DEFAULT_ENVIRO_PLUS = 1     # 1 = enviro+, 0 = basic enviro (no gas monitor)

# mqtt callbacks
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("connected OK")
    else:
        print("Bad connection Returned code=", rc)


def on_publish(client, userdata, mid):
    print("mid: " + str(mid))


# Read values from BME280 and return as dict
def read_bme280(bme280):
    # Compensation factor for temperature
    comp_factor = 2.25
    values = {}
    cpu_temp = get_cpu_temperature()
    raw_temp = bme280.get_temperature()  # float
    comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor)
    values["temperature"] = int(comp_temp)
    values["pressure"] = round(
        int(bme280.get_pressure() * 100), -1
    )  # round to nearest 10
    values["humidity"] = int(bme280.get_humidity())
    #
    # If we have a basic enviro then bale now since we won't have the gas detector
    #
    if args.enviro = 0:
        return values
    #
    # These baseline resistances will have to determined for each sensor in clean air (these are Ohms - not kOhms)
    # The conversion formulea came from https://community.openhab.org/t/diy-weather-station-w-enviro-mics6814-converting-ohms-to-ppm/85923/17
    #
    red_r0 = 200000
    ox_r0 = 20000
    nh3_r0 = 750000
    water_v = 3.0   # If the voltage is below this, then consider it a flood
    # Get the gas readings and generate raw (kOhm) and estimated ppm
    gas.enable_adc()
    data = gas.read_all()
    # AQI based on airnow.gov
    #   Very Unhealthy/Hazardous -> "POOR"(5), Unhealthy/Unhealthy for sensitive groups -> "INFERIOR"(4)
    #   Moderate -> "FAIR"(3), Good (lower half) -> "GOOD"(2), Good (top half) -> EXCELLENT(1)
    # We are assuming oxidized = NO2 for reporting purposes.
    values["oxidised"] = int(data.oxidising / 1000)
    values["oxidisedppm"] = oxppm = round(math.pow(10, math.log10(data.oxidising/ox_r0) - 0.8129),2)
    values["oxidisedden"] = int(1200*oxppm)     # Density in ug/m3 based on NO2 conversion
    if oxppm > 0.65:
        oxaqi = 5
    elif oxppm > 0.1:
        oxaqi = 4
    elif oxppm > 0.05:
        oxaqi = 3
    elif oxppm > 0.025:
        oxaqi = 2
    else:
        oxaqi = 1
    # We are assuming reduced = CO for reporting purposes
    values["reduced"] = int(data.reducing / 1000)
    values["reducedppm"] = rdppm = round(math.pow(10, -1.25 * math.log10(data.reducing/red_r0) + 0.64),2)
    if rdppm > 15.5:
        rdaqi = 5
    elif rdppm > 9.5:
        rdaqi = 4
    elif rdppm > 4.5:
        rdaqi = 3
    elif rdppm > 2.2:
        rdaqi = 2
    else:
        rdaqi = 1 
    # nh3 = voc for homebridge reporting (we re using Ammonia for desnity calculation)
    values["nh3"] = int(data.nh3 / 1000)
    values["nh3ppm"] = nhppm = round(math.pow(10, -1.8 * math.log10(data.nh3/nh3_r0) - 0.163),2)
    values["nh3den"] = int(700*nhppm)
    if rdppm > 100:     # Just a guess
        nhaqi = 5
    else:
        nhaqi = 1
    # Use the worst case air quality
    values["aqi"] = max(oxaqi, rdaqi,nhaqi)
    values["lux"] = int(ltr559.get_lux())
    # Now the ADC/water-detector
    values["adc"]  = data.adc
    values["leakDetected"] = False if data.adc > 3.0 else True
    return values


# Read values PMS5003 and return as dict
def read_pms5003(pms5003):
    values = {}
    try:
        pm_values = pms5003.read()  # int
        values["pm1"] = pm_values.pm_ug_per_m3(1)
        values["pm25"] = pm_values.pm_ug_per_m3(2.5)
        values["pm10"] = pm_values.pm_ug_per_m3(10)
    except ReadTimeoutError:
        pms5003.reset()
        pm_values = pms5003.read()
        values["pm1"] = pm_values.pm_ug_per_m3(1)
        values["pm25"] = pm_values.pm_ug_per_m3(2.5)
        values["pm10"] = pm_values.pm_ug_per_m3(10)
    return values


# Get CPU temperature to use for compensation
def get_cpu_temperature():
    process = Popen(
        ["vcgencmd", "measure_temp"], stdout=PIPE, universal_newlines=True
    )
    output, _error = process.communicate()
    return float(output[output.index("=") + 1 : output.rindex("'")])


# Get Raspberry Pi serial number to use as ID
def get_serial_number():
    with open("/proc/cpuinfo", "r") as f:
        for line in f:
            if line[0:6] == "Serial":
                return line.split(":")[1].strip()


# Check for Wi-Fi connection
def check_wifi():
    if check_output(["hostname", "-I"]):
        return True
    else:
        return False


# Display Raspberry Pi serial and Wi-Fi status on LCD
def display_status(disp, mqtt_broker):
    # Width and height to calculate text position
    WIDTH = disp.width
    HEIGHT = disp.height
    # Text settings
    font_size = 12
    font = ImageFont.truetype(UserFont, font_size)

    wifi_status = "connected" if check_wifi() else "disconnected"
    text_colour = (255, 255, 255)
    back_colour = (0, 170, 170) if check_wifi() else (85, 15, 15)
    device_serial_number = get_serial_number()
    message = "{}\nWi-Fi: {}\nmqtt-broker: {}".format(
        device_serial_number, wifi_status, mqtt_broker
    )
    img = Image.new("RGB", (WIDTH, HEIGHT), color=(0, 0, 0))
    draw = ImageDraw.Draw(img)
    size_x, size_y = draw.textsize(message, font)
    x = (WIDTH - size_x) / 2
    y = (HEIGHT / 2) - (size_y / 2)
    draw.rectangle((0, 0, 160, 80), back_colour)
    draw.text((x, y), message, font=font, fill=text_colour)
    disp.display(img)


def main():
    parser = argparse.ArgumentParser(
        description="Publish enviroplus values over mqtt"
    )
    parser.add_argument(
        "--broker",
        default=DEFAULT_MQTT_BROKER_IP,
        type=str,
        help="mqtt broker IP",
    )
    parser.add_argument(
        "--port",
        default=DEFAULT_MQTT_BROKER_PORT,
        type=int,
        help="mqtt broker port",
    )
    parser.add_argument(
        "--topic", default=DEFAULT_MQTT_TOPIC, type=str, help="mqtt topic"
    )
    parser.add_argument(
        "--interval",
        default=DEFAULT_READ_INTERVAL,
        type=int,
        help="the read interval in seconds",
    )
    parser.add_argument(
        "--plus",
        default=DEFAULT_ENVIRO_PLUS,
        type=int,
        help="Whether enviro+ 0 = no, 1 = yes",
    )
    args = parser.parse_args()

    # Raspberry Pi ID
    device_serial_number = get_serial_number()
    device_id = "raspi-" + device_serial_number

    print(
        f"""mqtt-all.py - Reads Enviro plus data and sends over mqtt.
    broker: {args.broker}
    client_id: {device_id}
    port: {args.port}
    topic: {args.topic}
    Press Ctrl+C to exit!
    """
    )

    mqtt_client = mqtt.Client(client_id=device_id)
    mqtt_client.on_connect = on_connect
    mqtt_client.on_publish = on_publish
    mqtt_client.connect(args.broker, port=args.port)

    bus = SMBus(1)

    # Create BME280 instance
    bme280 = BME280(i2c_dev=bus)

    # Create LCD instance
    disp = ST7735.ST7735(
        port=0, cs=1, dc=9, backlight=12, rotation=270, spi_speed_hz=10000000
    )

    # Initialize display
    disp.begin()

    # Try to create PMS5003 instance
    HAS_PMS = False
    try:
        pms5003 = PMS5003()
        pm_values = pms5003.read()
        HAS_PMS = True
        print("PMS5003 sensor is connected")
    except SerialTimeoutError:
        print("No PMS5003 sensor connected")

    # Display Raspberry Pi serial and Wi-Fi status
    print("RPi serial: {}".format(device_serial_number))
    print("Wi-Fi: {}\n".format("connected" if check_wifi() else "disconnected"))
    print("MQTT broker IP: {}".format(args.broker))

    # Main loop to read data, display, and send over mqtt
    mqtt_client.loop_start()
    while True:
        try:
            values = read_bme280(bme280)
            if HAS_PMS:
                pms_values = read_pms5003(pms5003)
                values.update(pms_values)
            values["serial"] = device_serial_number
            print(values)
            mqtt_client.publish(args.topic, json.dumps(values))
            display_status(disp, args.broker)
            time.sleep(args.interval)
        except Exception as e:
            print(e)


if __name__ == "__main__":
    main()

<filename>mqtt-all.py
"""
Run mqtt broker on localhost: sudo apt-get install mosquitto mosquitto-clients
Example run: python3 mqtt-all.py --broker 192.168.1.164 --topic enviro
"""
#!/usr/bin/env python3

import argparse
import ST7735
import time
import math
from bme280 import BME280
from pms5003 import PMS5003, ReadTimeoutError, SerialTimeoutError
from enviroplus import gas

try:
    # Transitional fix for breaking change in LTR559
    from ltr559 import LTR559

    ltr559 = LTR559()
except ImportError:
    import ltr559

from subprocess import PIPE, Popen, check_output
from PIL import Image, ImageDraw, ImageFont
from fonts.ttf import RobotoMedium as UserFont
import json

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

try:
    from smbus2 import SMBus
except ImportError:
    from smbus import SMBus


DEFAULT_MQTT_BROKER_IP = "172.16.58.3"   # Original was "localhost"
DEFAULT_MQTT_BROKER_PORT = 1883
DEFAULT_MQTT_TOPIC = "envirocrawl"   # Original was "enviroplus"
DEFAULT_READ_INTERVAL = 5
DEFAULT_ENVIRO_PLUS = 1     # 1 = enviro+, 0 = basic enviro (no gas monitor)

# mqtt callbacks
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("connected OK")
    else:
        print("Bad connection Returned code=", rc)


def on_publish(client, userdata, mid):
    print("mid: " + str(mid))


# Read values from BME280 and return as dict
def read_bme280(bme280):
    # Compensation factor for temperature
    comp_factor = 2.25
    values = {}
    cpu_temp = get_cpu_temperature()
    raw_temp = bme280.get_temperature()  # float
    comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor)
    values["temperature"] = int(comp_temp)
    values["pressure"] = round(
        int(bme280.get_pressure() * 100), -1
    )  # round to nearest 10
    values["humidity"] = int(bme280.get_humidity())
    #
    # If we have a basic enviro then bale now since we won't have the gas detector
    #
    if args.enviro = 0:
        return values
    #
    # These baseline resistances will have to determined for each sensor in clean air (these are Ohms - not kOhms)
    # The conversion formulea came from https://community.openhab.org/t/diy-weather-station-w-enviro-mics6814-converting-ohms-to-ppm/85923/17
    #
    red_r0 = 200000
    ox_r0 = 20000
    nh3_r0 = 750000
    water_v = 3.0   # If the voltage is below this, then consider it a flood
    # Get the gas readings and generate raw (kOhm) and estimated ppm
    gas.enable_adc()
    data = gas.read_all()
    # AQI based on airnow.gov
    #   Very Unhealthy/Hazardous -> "POOR"(5), Unhealthy/Unhealthy for sensitive groups -> "INFERIOR"(4)
    #   Moderate -> "FAIR"(3), Good (lower half) -> "GOOD"(2), Good (top half) -> EXCELLENT(1)
    # We are assuming oxidized = NO2 for reporting purposes.
    values["oxidised"] = int(data.oxidising / 1000)
    values["oxidisedppm"] = oxppm = round(math.pow(10, math.log10(data.oxidising/ox_r0) - 0.8129),2)
    values["oxidisedden"] = int(1200*oxppm)     # Density in ug/m3 based on NO2 conversion
    if oxppm > 0.65:
        oxaqi = 5
    elif oxppm > 0.1:
        oxaqi = 4
    elif oxppm > 0.05:
        oxaqi = 3
    elif oxppm > 0.025:
        oxaqi = 2
    else:
        oxaqi = 1
    # We are assuming reduced = CO for reporting purposes
    values["reduced"] = int(data.reducing / 1000)
    values["reducedppm"] = rdppm = round(math.pow(10, -1.25 * math.log10(data.reducing/red_r0) + 0.64),2)
    if rdppm > 15.5:
        rdaqi = 5
    elif rdppm > 9.5:
        rdaqi = 4
    elif rdppm > 4.5:
        rdaqi = 3
    elif rdppm > 2.2:
        rdaqi = 2
    else:
        rdaqi = 1 
    # nh3 = voc for homebridge reporting (we re using Ammonia for desnity calculation)
    values["nh3"] = int(data.nh3 / 1000)
    values["nh3ppm"] = nhppm = round(math.pow(10, -1.8 * math.log10(data.nh3/nh3_r0) - 0.163),2)
    values["nh3den"] = int(700*nhppm)
    if rdppm > 100:     # Just a guess
        nhaqi = 5
    else:
        nhaqi = 1
    # Use the worst case air quality
    values["aqi"] = max(oxaqi, rdaqi,nhaqi)
    values["lux"] = int(ltr559.get_lux())
    # Now the ADC/water-detector
    values["adc"]  = data.adc
    values["leakDetected"] = False if data.adc > 3.0 else True
    return values


# Read values PMS5003 and return as dict
def read_pms5003(pms5003):
    values = {}
    try:
        pm_values = pms5003.read()  # int
        values["pm1"] = pm_values.pm_ug_per_m3(1)
        values["pm25"] = pm_values.pm_ug_per_m3(2.5)
        values["pm10"] = pm_values.pm_ug_per_m3(10)
    except ReadTimeoutError:
        pms5003.reset()
        pm_values = pms5003.read()
        values["pm1"] = pm_values.pm_ug_per_m3(1)
        values["pm25"] = pm_values.pm_ug_per_m3(2.5)
        values["pm10"] = pm_values.pm_ug_per_m3(10)
    return values


# Get CPU temperature to use for compensation
def get_cpu_temperature():
    process = Popen(
        ["vcgencmd", "measure_temp"], stdout=PIPE, universal_newlines=True
    )
    output, _error = process.communicate()
    return float(output[output.index("=") + 1 : output.rindex("'")])


# Get Raspberry Pi serial number to use as ID
def get_serial_number():
    with open("/proc/cpuinfo", "r") as f:
        for line in f:
            if line[0:6] == "Serial":
                return line.split(":")[1].strip()


# Check for Wi-Fi connection
def check_wifi():
    if check_output(["hostname", "-I"]):
        return True
    else:
        return False


# Display Raspberry Pi serial and Wi-Fi status on LCD
def display_status(disp, mqtt_broker):
    # Width and height to calculate text position
    WIDTH = disp.width
    HEIGHT = disp.height
    # Text settings
    font_size = 12
    font = ImageFont.truetype(UserFont, font_size)

    wifi_status = "connected" if check_wifi() else "disconnected"
    text_colour = (255, 255, 255)
    back_colour = (0, 170, 170) if check_wifi() else (85, 15, 15)
    device_serial_number = get_serial_number()
    message = "{}\nWi-Fi: {}\nmqtt-broker: {}".format(
        device_serial_number, wifi_status, mqtt_broker
    )
    img = Image.new("RGB", (WIDTH, HEIGHT), color=(0, 0, 0))
    draw = ImageDraw.Draw(img)
    size_x, size_y = draw.textsize(message, font)
    x = (WIDTH - size_x) / 2
    y = (HEIGHT / 2) - (size_y / 2)
    draw.rectangle((0, 0, 160, 80), back_colour)
    draw.text((x, y), message, font=font, fill=text_colour)
    disp.display(img)


def main():
    parser = argparse.ArgumentParser(
        description="Publish enviroplus values over mqtt"
    )
    parser.add_argument(
        "--broker",
        default=DEFAULT_MQTT_BROKER_IP,
        type=str,
        help="mqtt broker IP",
    )
    parser.add_argument(
        "--port",
        default=DEFAULT_MQTT_BROKER_PORT,
        type=int,
        help="mqtt broker port",
    )
    parser.add_argument(
        "--topic", default=DEFAULT_MQTT_TOPIC, type=str, help="mqtt topic"
    )
    parser.add_argument(
        "--interval",
        default=DEFAULT_READ_INTERVAL,
        type=int,
        help="the read interval in seconds",
    )
    parser.add_argument(
        "--plus",
        default=DEFAULT_ENVIRO_PLUS,
        type=int,
        help="Whether enviro+ 0 = no, 1 = yes",
    )
    args = parser.parse_args()

    # Raspberry Pi ID
    device_serial_number = get_serial_number()
    device_id = "raspi-" + device_serial_number

    print(
        f"""mqtt-all.py - Reads Enviro plus data and sends over mqtt.
    broker: {args.broker}
    client_id: {device_id}
    port: {args.port}
    topic: {args.topic}
    Press Ctrl+C to exit!
    """
    )

    mqtt_client = mqtt.Client(client_id=device_id)
    mqtt_client.on_connect = on_connect
    mqtt_client.on_publish = on_publish
    mqtt_client.connect(args.broker, port=args.port)

    bus = SMBus(1)

    # Create BME280 instance
    bme280 = BME280(i2c_dev=bus)

    # Create LCD instance
    disp = ST7735.ST7735(
        port=0, cs=1, dc=9, backlight=12, rotation=270, spi_speed_hz=10000000
    )

    # Initialize display
    disp.begin()

    # Try to create PMS5003 instance
    HAS_PMS = False
    try:
        pms5003 = PMS5003()
        pm_values = pms5003.read()
        HAS_PMS = True
        print("PMS5003 sensor is connected")
    except SerialTimeoutError:
        print("No PMS5003 sensor connected")

    # Display Raspberry Pi serial and Wi-Fi status
    print("RPi serial: {}".format(device_serial_number))
    print("Wi-Fi: {}\n".format("connected" if check_wifi() else "disconnected"))
    print("MQTT broker IP: {}".format(args.broker))

    # Main loop to read data, display, and send over mqtt
    mqtt_client.loop_start()
    while True:
        try:
            values = read_bme280(bme280)
            if HAS_PMS:
                pms_values = read_pms5003(pms5003)
                values.update(pms_values)
            values["serial"] = device_serial_number
            print(values)
            mqtt_client.publish(args.topic, json.dumps(values))
            display_status(disp, args.broker)
            time.sleep(args.interval)
        except Exception as e:
            print(e)


if __name__ == "__main__":
    main()

Run mqtt broker on localhost: sudo apt-get install mosquitto mosquitto-clients Example run: python3 mqtt-all.py --broker 192.168.1.164 --topic enviro #!/usr/bin/env python3 # Transitional fix for breaking change in LTR559 # Original was "localhost" # Original was "enviroplus" # 1 = enviro+, 0 = basic enviro (no gas monitor) # mqtt callbacks # Read values from BME280 and return as dict # Compensation factor for temperature # float # round to nearest 10 # # If we have a basic enviro then bale now since we won't have the gas detector # # # These baseline resistances will have to determined for each sensor in clean air (these are Ohms - not kOhms) # The conversion formulea came from https://community.openhab.org/t/diy-weather-station-w-enviro-mics6814-converting-ohms-to-ppm/85923/17 # # If the voltage is below this, then consider it a flood # Get the gas readings and generate raw (kOhm) and estimated ppm # AQI based on airnow.gov #   Very Unhealthy/Hazardous -> "POOR"(5), Unhealthy/Unhealthy for sensitive groups -> "INFERIOR"(4) #   Moderate -> "FAIR"(3), Good (lower half) -> "GOOD"(2), Good (top half) -> EXCELLENT(1) # We are assuming oxidized = NO2 for reporting purposes. # Density in ug/m3 based on NO2 conversion # We are assuming reduced = CO for reporting purposes # nh3 = voc for homebridge reporting (we re using Ammonia for desnity calculation) # Just a guess # Use the worst case air quality # Now the ADC/water-detector # Read values PMS5003 and return as dict # int # Get CPU temperature to use for compensation # Get Raspberry Pi serial number to use as ID # Check for Wi-Fi connection # Display Raspberry Pi serial and Wi-Fi status on LCD # Width and height to calculate text position # Text settings # Raspberry Pi ID mqtt-all.py - Reads Enviro plus data and sends over mqtt.     broker: {args.broker}     client_id: {device_id}     port: {args.port}     topic: {args.topic}     Press Ctrl+C to exit! # Create BME280 instance # Create LCD instance # Initialize display # Try to create PMS5003 instance # Display Raspberry Pi serial and Wi-Fi status # Main loop to read data, display, and send over mqtt