Help! Enviro Indoor - failed to update to mqtt

Hi all,

Newbie here so apologies for my lack of knowledge. I’ve recently bought the enviro indoor which is the start (I’m hoping) of getting lots of data from around the house and outside.

I’m using a pi zero 2 (with bullseye 32 bit lite) with an IOT stack set up in docker, which I’ve set up using the guide here and their video. Easy Raspberry Pi IoT Server - Learn Embedded Systems

it sets up an MQTT broker, infuse DB and node red.

The indoor is set to sent to mqtt and I’ve added the ip of the pi zero where the IOT stack is set up. The mqtt broker mosquitto is up and running and healthy.

as far as I’m aware, I’ve set everything up correctly, but the indoor fails to send the data to MQTT. where might I be going wrong.

this is the error message.

! failed to upload ‘2022-11-12T15:32:38Z.json’ to mqtt

Thanks

James

Hi, have you tested your MQTT broker works externally from your raspberry pi zero docker instances?

You could try installing the mqtt client directly on the raspberry pi zero (through the command line) with something like:
sudo apt install -y mosquitto-clients
Then from the command line try the below using your raspberry PI zero external IP address, username and password.
sudo mosquitto_sub -h <ip address> -t testTopic -u <username> -P <password>

If that connects OK then likley indicates an issue with the enviro indoor, otherwise would be an issue with the mqtt broker config.

Hi Dave,

Thank you very much for your reply. I have today performed such a test after much online searching and YouTube watching.

I’ve managed to subscribe and publish to the topic on my laptop successfully and the data is flowing into the influxDB (albeit not in the format I was hoping, so that’s going to be more investigations)

hopefully you can see from the screen shot, on the top left I’ve published to the topic the same as the indoor ( enviro/indoor01) and on the top right, I’ve subscribed to that topic. where you can see the results. Across the bottom is the data in the influxDB from my tests.

I was thinking maybe it was because it was in the docker container, but these test prove that the mqtt is up and running as expected?

thanks

James

I should also add that to get the provisioning page to work in the first place, I had to update to the new firmware v.0.0.8.

I have not had an issue and am using mqtt with the same firmware version 0.0.8. There are a few issues raised in GitHub for mqtt. The only difference I can think is I have username/password setup for mqtt. Issues · pimoroni/enviro · GitHub

Will take a look at mine later to see if anything I can suggest, if not I am sure the Pinornoi team will have a suggestion.

Also, I had issues getting it into influxdb so wrote a seperate Python program to subscribe to the mqtt broker and then put it the format needed for influxDB. Just thought, you could do a quick check and reprovison the board and get it to publish direct to influxDB as there is an option for that.

Hi @Jimdowding

I had a second enviro indoor I was planning to setup so gave it a go tonight. Dont have any issues connecting to mqtt broker. Fresh install of 0.0.8, running on USB power to test.

config.py look slike this after provisoing over wifi (removed the sesntive data with Xs)

# enviro config file

# you may edit this file by hand but if you enter provisioning mode
# then the file will be automatically overwritten with new details

provisioned = True

# enter a nickname for this board
nickname = 'indoor-enviro'

# network access details
wifi_ssid = 'XXXXXXX'
wifi_password = 'XXXXXXXXX'

# how often to wake up and take a reading (in minutes)
reading_frequency = 5

# where to upload to ("web_hook", "mqtt", "adafruitio")
destination = 'mqtt'

# how often to upload data (number of cached readings)
upload_frequency = 1

# web hook settings
custom_http_url = ''
custom_http_username = ''
custom_http_password = ''

# mqtt broker settings
mqtt_broker_address = '192.168.0.XXX'
mqtt_broker_username = 'XXXXXXX'
mqtt_broker_password = 'XXXXXXXX'

# adafruit ui settings
adafruit_io_username = ''
adafruit_io_key = ''

# influxdb settings
influxdb_org = ''
influxdb_url = ''
influxdb_token = ''
influxdb_bucket = ''

# grow specific settings
auto_water = False
moisture_target_1 = 50
moisture_target_2 = 50
moisture_target_3 = 50

Then the log file (cleared it once provisoned gives this):

2000-01-01 00:00:02 [debug    / 115kB] > performing startup
2000-01-01 00:00:02 [info     / 127kB]   - wake reason: unknown
2000-01-01 00:00:02 [debug    / 126kB]   - turn on activity led
2000-01-01 00:00:02 [info     / 124kB] > clock not set, synchronise from ntp server
2000-01-01 00:00:02 [info     / 122kB] > connecting to wifi network 'XXXXXXXX'
2000-01-01 00:00:04 [debug    / 116kB]   - connecting
2000-01-01 00:00:07 [debug    / 102kB]   - connecting
2000-01-01 00:00:09 [info     /  95kB]   - ip address:  192.168.0.XXX
2022-11-14 19:59:09 [info     /  92kB]   - rtc synched
2022-11-14 19:59:09 [info     / 127kB] > 1 cache files need uploading
2022-11-14 19:59:09 [info     / 125kB] > already connected to wifi
2022-11-14 19:59:10 [info     / 110kB]   - uploaded 2022-11-14T19:59:09Z.json to mqtt
2022-11-14 19:59:10 [info     / 108kB] > going to sleep
2022-11-14 19:59:10 [debug    / 106kB]   - clearing and disabling timer and alarm
2022-11-14 19:59:10 [info     / 104kB]   - setting alarm to wake at 20:00pm
2022-11-14 19:59:10 [info     / 102kB]   - shutting down
2022-11-14 19:59:10 [debug    /  99kB]   - on usb power (so can't shutdown) halt and reset instead

Might be worth checking your config.py file against the one I got, If the logs are showing you connecting to your wifi network then perhaps it might be related to not having username/password for mqtt?

Hi Dave, thanks again for your help with this. it’s so frustrating because it should be simple.

so I re imaged the board with the latest 2uf file again and re set up the config, initially without an mqtt username and password and then when that unfortunately didn’t work, I included the username and password and set those up on the mosquitto broker and the node red instance. still the same results in the indoor log file.

this is my config at the moment.

# enviro config file

# you may edit this file by hand but if you enter provisioning mode
# then the file will be automatically overwritten with new details

provisioned = True

# enter a nickname for this board
nickname = 'indoor01'

# network access details
wifi_ssid = 'XXXXXXXX'
wifi_password = 'XXXXXXXX'

# how often to wake up and take a reading (in minutes)
reading_frequency = 15

# where to upload to ("web_hook", "mqtt", "adafruitio")
destination = 'mqtt'

# how often to upload data (number of cached readings)
upload_frequency = 5

# web hook settings
custom_http_url = ''
custom_http_username = ''
custom_http_password = ''

# mqtt broker settings
mqtt_broker_address = '192.168.1.XXX
mqtt_broker_username = 'XXXXXX'
mqtt_broker_password = 'XXXXXX'

# adafruit ui settings
adafruit_io_username = ''
adafruit_io_key = ''

# influxdb settings
influxdb_org = ''
influxdb_url = ''
influxdb_token = ''
influxdb_bucket = ''

# grow specific settings
auto_water = False
moisture_target_1 = 50
moisture_target_2 = 50
moisture_target_3 = 50

and these are the most recent logs.

2022-11-14 23:06:22 [info / 119kB] > 7 cache files need uploading
2022-11-14 23:06:22 [info / 116kB] > connecting to wifi network ‘XXXXXXXX’
2022-11-14 23:06:24 [debug / 110kB] - connecting
2022-11-14 23:06:27 [debug / 96kB] - connecting
2022-11-14 23:06:29 [info / 88kB] - ip address: 192.168.1.XXX
2022-11-14 23:06:29 [error / 115kB] ! failed to upload ‘2022-11-14T22:47:19Z.json’ to mqtt
2022-11-14 23:06:29 [error / 113kB] ! reading upload failed
2022-11-14 23:06:29 [info / 112kB] > going to sleep
2022-11-14 23:06:29 [debug / 109kB] - clearing and disabling timer and alarm
2022-11-14 23:06:29 [info / 107kB] - setting alarm to wake at 23:15pm
2022-11-14 23:06:29 [info / 105kB] - shutting down
2022-11-14 23:06:30 [debug / 103kB] - on usb power (so can’t shutdown) halt and reset instead

I think I might try and set up mosquitto directly on the pi instead of in docker and see if that makes any difference. then if that fails I try directly into influx.

In case it helps I am running Mosquitto on Raspberry PI also
mosquitto version 2.0.11

Was simple to install and setup to run a service after reboot,

sudo apt update && sudo apt upgrade
sudo apt install -y mosquitto mosquitto-clients
sudo systemctl enable mosquitto.service
1 Like

Hi Dave,

its finally getting through. I’m not sure if my issue was docker related. I removed the mosquitto docker container and installed mosquitto directly on the pi. The enviro indoor is now sending data across to the mqtt. Hurray!

now my next task is to make the data make sense via node red. at the moment in the influx DB its just timestamp and value.

Thanks again for your help.

James

Hi

@Jimdowding I Just thought I would share a bit of pyhton I cobbled together to act inbetween MQTT and put the readings into InfluxDB that might help. This gets everything into a good shape for that works. You have to make sure you install paho.mqtt.client and influxdb under python on the raspberry PI. Its a bit of mess but works, just need to change the properties for mqtt and influxDB. If you are not using passwords for mqtt might need to remove the line client.username_pw_set(user_name, mqtt_password)

Sure there are much better ways to do it but worked for me. I then just run the script locally on the raspberry pi in the background using somehting like and has been running fine for that last two weeks

nohup python indoor_enviro.py &

the script in this would be called indoor_enviro.py and uses “home” as the name for the DB on Influx. Hope it helps.

import sys
from datetime import datetime
import time
import json
import paho.mqtt.client as mqtt
from influxdb import InfluxDBClient

# mqtt client properties to use
mqtt_server = 'XXXXXXXXXl'
client_id = 'indoorenv3456a'
topic_sub = 'enviro/indoor-enviro'
# topic_msg = b'No Data To Send'
user_name = 'XXXXXXXX'
mqtt_password = 'XXXXXXXXX'

# influxDB properties to use

org = 'XXXXXX'
host = 'XXXXXXXX'
port = '8086'
username = 'XXXXXXX'
password = 'XXXXXXX'
bucket = 'home'
db = 'home'


def convert_time(timestamp):
    year = int(timestamp[0:4])
    month = int(timestamp[5:7])
    day = int(timestamp[8:10])
    hour = int(timestamp[11:13])
    minute = int(timestamp[14:16])
    second = int(timestamp[17:19])
    new_timestamp = time.mktime((year, month, day, hour, minute, second, 0, 0, 0))
    return int(new_timestamp)


def send_to_influx(payload):
    client = InfluxDBClient(host, port, username, password, db)
    client.write_points(payload, time_precision='s')


def indoor_weather(dic_to_use):
    time_to_use = dic_to_use["timestamp"]
    for measure in dic_to_use["readings"]:
        payload = [
            {"measurement": measure,
                "tags": {
                    "device": dic_to_use["nickname"],
                },
                "time": time_to_use,
                "fields": {
                    "value": dic_to_use["readings"][measure],
                }

             }
        ]
        # print(payload)
        send_to_influx(payload)


def on_connect(client, userdata, flags, rc):
    if rc==0:
        client.connected_flag=True #set flag
        print("connected OK")
    else:
        print("Bad connection Returned code=", rc)


def on_message(client, userdata, msg):
    # print('*********new message***********')
    # print(f"Message received [{msg.topic}]: {msg.payload}")
    result = json.loads(msg.payload.decode('utf-8'))
    # print(result)
    indoor_weather(result)


mqtt.Client.connected_flag = False
client = mqtt.Client(client_id)
client.username_pw_set(user_name, mqtt_password)

client.on_connect = on_connect  # bind call back function
client.on_message = on_message
client.loop_start()
print("Connecting to broker ", mqtt_server)
client.connect(mqtt_server, 1883, 60)      # connect to broker
while not client.connected_flag:  # wait in loop
    print("In wait loop")
    time.sleep(1)

print("in Main Loop")
client.subscribe(topic_sub)

while True:
    time.sleep(5)
    # print('waiting')

client.loop_stop()    # Stop loop
client.disconnect()  # disconnect

I am using this guide and trying to get data from the MQTT, it is not in the same format as in the guide so still struggling.

Pimoroni Enviro+ Project: Using Grafana to visualize data via MQTT to Node-RED to Influxdb – Lambiase Light Industries