Trouble sending MQTT from Raspberry Pico

I’ve been publishing data to my emonSD successfully from other Raspberry Pi’s running inside my home network. Now I’m trying to publish data from a Pico outside of my home and am having trouble. I’ve got my router set for port forwarding the emonPi server on 1883. On the Pico, I’m using the “umqttsimple” library and this is my micropython code:

MQTT_BROKER = 'xx.xx.xxx.169'
USER = 'emonpi' 
PASSWORD = 'emonpimqtt2016'
CLIENT_ID = 'PicoW'
print(f"Begin connection with MQTT Broker :: {MQTT_BROKER}")
time.sleep(2)
mqttClient = MQTTClient(CLIENT_ID, MQTT_BROKER, user=USER, password=PASSWORD)
mqttClient.connect()
print(f"Succes - Connected to MQTT Broker :: {MQTT_BROKER}")

The process is failing at the mqttClient.connect() step with the error IndexError: bytes index out of range

Any suggestions for fixing this?

Have you tried it inside your network?
I’ve had a Pico sending MQTT temperature data for a couple of months, so it can work.

Yes, I have had the Pico (and some ESP8266 boards) working fine inside my network using the “umqttsimple” library. The problem is I cannot connect from outside my local network. So either I’m not exposing the emonPi server correctly with my router’s port forwarding settings, or the emonPi is not configured to listen to port 1883.

On my emonPi, I added the line listener 1883 to /etc/mosquitto/mosquitto.conf but that didn’t help. I then also added per_listener_settings true to the top of mosquitto.conf but that caused the mosquitto.service to fail when I restarted it with sudo systemctl restart mosquitto.

At this point I’m giving up on MQTT for sending data from devices outside my LAN and will use http requests instead.

Got to be honest, that is pretty risky either way unless using a VPN or one of the tunneling services.

Have you check the listener with netstat?

If the Pico was getting data into your EmonPi via 1883 from inside the building, then it’s your router setup that that’s the issue.
That said, I’m with @borpin, with the state of the internet as it is, leaving devices like a Pi/Pico openly accessible from outside the building is courting disaster. Much better to OpenVPN/Wireguard/TailScale/etc them together so Shodan doesn’t come sniffing…

1 Like

I neglected to mention that I use a Dataplicity wormhole to provide the external IP address for my emonPi (Dataplicity works so seamlessly that I forgot about it!).

But before I try to get MQTT working through Dataplicity, would there be any advantage to using MQTT rather than https requests to send data from devices outside my LAN to my emonPi? Thanks.

Have you setup https to the Pi? Otherwise the main advantage of MQTT is you can consume the same data in multiple places.

Yes, I’ve got several Linux-based Pi’s outside of my LAN sending data via https requests to my emonPi. I really only need these outside devices to send data one-way to emonPi, so https requests has been sufficient. But I was exploring using microprocessors like the Pico or esp8266 as remote data collectors and I thought MQTT might be more appropriate for these devices. But I’m finding at least the “simple mqtt” for these processors is not as reliable. Plus accessing a Pico or esp8266 remotely is more difficult. So my answer to my question is that I’ll continue using Linux-based Pis (i.e., ZeroPiW) and https requests.

I’ve had a Pico with 4 DS18B20 temperature probes in the loft for a month and it hasn’t missed a beat. Before that it spent another month on my bench and worked great there too.


Perhaps you have a code issue?

Dataplicity works great as mentioned, have been using for months without fault on my Emonpi