umqtt.simple trouble

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Tillmario
Posts: 18
Joined: Mon Nov 19, 2018 2:06 pm

umqtt.simple trouble

Post by Tillmario » Fri Nov 23, 2018 7:05 pm

Hi,
as you guys suggested i looked into umqtt.simple to send data from my ESP2866 to my Raspberry Pi 3.

Here is what i did:
On the ESP2866 running Micropython i created the folder "umqtt" and put the simple.py from inside. (https://github.com/micropython/micropyt ... qtt.simple)
From github I also used the example_pub.py.

Code: Select all

from umqtt.simple import MQTTClient

# Test reception e.g. with:
# mosquitto_sub -t foo_topic

def main(server="localhost"):
    c = MQTTClient("umqtt_client", server)
    c.connect()
    c.publish(b"foo_topic", b"hello")
    c.disconnect()

if __name__ == "__main__":
    main()
I didn't know if it was wrong or what the best way to do this is, but i saved it as main.py and used WebREPL to transfer it to the board.

As suggested in the file itself i ran the command

Code: Select all

mosquitto_sub -t foo_topic
on my RPi-Terminal (Connected via VNC-Viewer) and it was ready to receive messages. (mosquitto and mosquitto-clients previously installed) Via Putty i also connected to my Pi and sent the message

Code: Select all

mosquitto_pub -t foo_topic -m Hello
which was received. Hello was printed on VNC-Viewer.

I thought restarting the ESP2866 would execute main.py and also print "Hello", but nothing happened. I tried to implement my RPi's IP into the

Code: Select all

c = MQTTClient("umqtt_client", server)

I tried

Code: Select all

c = MQTTClient("umqtt_client", 192.168.178.63)
or

Code: Select all

c = MQTTClient("[code]c = MQTTClient("umqtt_client", server)
I don't really know what to do and the documentation couldn't help me but that was my best guess.
Can someone give me any help?

Greetings, Tom

User avatar
Mike Teachman
Posts: 155
Joined: Mon Jun 13, 2016 3:19 pm
Location: Victoria, BC, Canada

Re: umqtt.simple trouble

Post by Mike Teachman » Sat Nov 24, 2018 4:53 am

Darn frustrating. I might be able to help. Please take a look at my github MQTT examples.

https://github.com/MikeTeachman/micropy ... tt-esp8266
https://github.com/MikeTeachman/micropy ... tt-esp8266

They are for Adafruit IO and Thingspeak. In classes I teach on uPy they have worked 100%.

I recently gave a demo at a Raspberry Pi MeetUp where I published sensor data to a moquitto broker.

Here is the code:

Code: Select all

#
#   Raspberry Pi Meetup
#
#   Micropython code to Publish light sensor data to a Mosquitto broker using the MQTT protocol
#
#
# Hardware used:
# - Adafruit Huzzah ESP8266 Feather running micropython
# - Adafruit TSL2561 lux breakout board
# -- copy TSL2561 driver to board:  ampy -pCOMx -d1 put tsl2561.py
# - ssd1306 OLED display
#
#   Tested with Micropython release v1.9.3 (Nov 1, 2017)
#


# prerequisites:
# - Mosquitto MQTT server

import machine
import network
from umqtt.robust import MQTTClient
import time
import os
import tsl2561
import ssd1306 

#
#   blink LED for specified number of milliseconds
#
def blinkLed(blinkDurationInMs):
    builtInLed.off()
    time.sleep_ms(blinkDurationInMs)
    builtInLed.on()    
    
#
#   configure ESP8266 for I2C communication
#   GPIO4 pin is set to I2C SDA (data) 
#   GPIO5 pin is set to I2C SCL (clock) 
#
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

#
#   configure the built-in LED
#
builtInLed = machine.Pin(2, machine.Pin.OUT)
builtInLed.on()
blinkLed(500)

#   OLED display
oled = ssd1306.SSD1306_I2C(128, 32, i2c)

oled.fill(0)
oled.text('RPi Meetup', 0, 15)
oled.show()

#   light sensor
light_sensor = tsl2561.TSL2561(i2c)
light_sensor.gain(1)
light_sensor.integration_time(13)

#
#   WIFI connection information
#   
#   replace WIFI-SSID and WIFI-PASSWORD with access information for the WIFI network you will connect to
#
#wifiSSID = "WIFI-SSID"             # EDIT 1 - enter name of WiFi connection point
#wifiPassword = "WIFI-PASSWORD"     # EDIT 2 - enter WiFi password 

#
#   turn off the WiFi Access Point
# 
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)

#
#   create an interface that can connnect to a WIFI network
#
wifi = network.WLAN(network.STA_IF)

#
#   activate the WIFI interface
#
wifi.active(True)

#
#   connect the ESP8266 device to the WIFI network
#
wifi.connect(wifiSSID, wifiPassword)

#
#   wait until the ESP8266 has successfully connected to the WIFI network
# 
while not wifi.isconnected():
  pass
  
oled.fill(0)
oled.text('WiFi Connected', 0, 15)
oled.show()
  

blinkLed(1000)  
#
#   define IO connection information
#   create a random clientID 
#
randomNum = int.from_bytes(os.urandom(3), 'little')
myMqttClient = bytes("client"+str(randomNum), 'utf-8')

#
#   Edit MQTT server and topic
#
mosquittoUrl = b"<URL>"     # EDIT 3 - URL of Mosquitto server
mqttTopic = b"lux"                  # EDIT 4 - enter Topic name

#
#   configure a MQTT client
#
client = MQTTClient(client_id=myMqttClient, server=mosquittoUrl)#, port=1883)
                    
#
#   connect ESP8266 to Mosquitto broker IO using MQTT
#
client.connect()

#
#   continuously publish light sensor readings to Mosquitto broker using MQTT
#
while True:
    #
    #   read light from sensor
    #
    lux_reading = light_sensor.read()
    
    client.publish(mqttTopic, bytes(str(lux_reading), 'utf-8'))
    
    oled.fill(0)
    oled.text('{} lux'.format(lux_reading), 0, 0)
    oled.show()
    
    
    blinkLed(100)
    time.sleep(1)  # delay between each publish, in seconds

client.disconnect() 
Good luck !

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: umqtt.simple trouble

Post by pythoncoder » Sat Nov 24, 2018 5:59 am

@Tillmario What you did at the start (with server="localhost") should work. However putting it in main.py results in a problem because the script tends to run before the ESP8266 has had a chance to connect to the WiFi. I suggest you try

Code: Select all

import utime
from umqtt.simple import MQTTClient
utime.sleep(5)  # Give WiFi a chance to come up

# Test reception e.g. with:
# mosquitto_sub -t foo_topic

c = MQTTClient("umqtt_client", "localhost")
c.connect()
c.publish(b"foo_topic", b"hello")
c.disconnect()
as main.py. Note you don't need

Code: Select all

if __name__ == "__main__":
As a general point, development is easier if your main.py simply imports and runs your module rather than containing the application code. This means you can develop the module at the REPL. When it's working, main.py can be adapted to import it at boot.
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: umqtt.simple trouble

Post by kevinkk525 » Sat Nov 24, 2018 9:18 am

Well this:

Code: Select all

c = MQTTClient("umqtt_client", "localhost")
can't work on the esp8266 because the broker is not running on localhost, which would be the esp8266 itself. It has to be the IP-Adress of the mqtt broker.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Tillmario
Posts: 18
Joined: Mon Nov 19, 2018 2:06 pm

Re: umqtt.simple trouble

Post by Tillmario » Sat Nov 24, 2018 11:32 pm

kevinkk525 wrote:
Sat Nov 24, 2018 9:18 am
Well this:

Code: Select all

c = MQTTClient("umqtt_client", "localhost")
can't work on the esp8266 because the broker is not running on localhost, which would be the esp8266 itself. It has to be the IP-Adress of the mqtt broker.
Thanks, that was my initial suspicion but I forgot the " " :roll: :oops: Now it works, thanks.
Mike Teachman wrote:
Sat Nov 24, 2018 4:53 am
I might be able to help. Please take a look at my github MQTT examples.

Thanks for the links, i might check them out later on. My focus now is to understand what is happening. There are a lot of tutorials but the programs they write, like yours, are often too complex for me to get every step. That's why I just wanted to send a Hello from ESP to RPi.
Next step is Reading DHT22 Data and sending it to the RPi and then trying to save it somewhere on the Pi and possibly showing it in a graph. I will try figure it out.
pythoncoder wrote:
Sat Nov 24, 2018 5:59 am
As a general point, development is easier if your main.py simply imports and runs your module rather than containing the application code. This means you can develop the module at the REPL. When it's working, main.py can be adapted to import it at boot.
Thanks, I tried that and it worked. :)

Post Reply