0

I'm trying to build a basic MQTT publisher using a nodemcu v3 and a dht11 to send temperature data. I'm using ESPlorer and when I try to upload my code it tells me that the paho module does not exist. My code is as follows:

import time
import network
import paho.mqtt.client as mqtt

sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
sta_if.connect('<MySSID>', '<MyPW>')

mqtt = mqtt.Client()
mqtt.connect("randomIPaddress")

pin = machine.Pin(4)
temp_instance = dht11.DHT11(pin)
result = temp_instance.read()

print("Temperature is: %d C" % result.temperature)
print("Humidity is: %d %%" % result.humidity)
message = result.temperature 
mqtt.publish("base/dht11/temp", message)  
mqtt.loop_forever()

I'm still very confused by how MQTT publishing works, and I can't seem to find any sources that agree with each other on this. Everywhere I look has a different solution for my problem.

Does anyone have an idea why ESPLorer keeps telling me that the paho module doesn't exist? I've already tried installing the module as shown in the documentation but that got me nowhere.

Edit: https://pypi.python.org/pypi/paho-mqtt/1.1 These where the instructions I followed to install paho.

Variomatic
  • 21
  • 1
  • 5
  • 2
    Can you please post the instructions you followed to install `paho`? – killthrush Mar 04 '18 at 20:56
  • Welcome to SO. Your code looks like Python so I added the corresponding tag. To get more detailed answers, consider adding the exact error you are getting to the question. Also, as @killthrush suggested, can you add how you installed paho? – m00am Mar 05 '18 at 09:16
  • https://pypi.python.org/pypi/paho-mqtt/1.1 Those are the instructions I followed to install paho. Where they the wrong ones? – Variomatic Mar 05 '18 at 11:26

1 Answers1

5

The paho MQTT client has been written for regular Python. It is unlikely that it would run under MicroPython.

MicroPython includes its own MQTT client called umqtt. There are two versions, umqtt.simple and umqtt.robust.

You can see an example that uses it here.

larsks
  • 277,717
  • 41
  • 399
  • 399