Connecting ESP8266 to IBM Watson with Micropython

571 views Asked by At

I managed to connect an ESP01 using Micropython 1.9.2 to a mosquitto broker running in my computer. I also managed to simulate a device and connect a mosquitto client from my computer to the Watson Broker. But when I try to connect the ESP directly to Watson I receive a "connection refused" message

MQTTException: 5
# Full stream answered by Watson is:  b' \x02\x00\x05'

My configuration parameters are:

  • WATSON_CLIENT_ID = "d:[ORG]:ESP8266:fv_esp01s_02"
  • WATSON_BROKER_IP = "[ORG].messaging.internetofthings.ibmcloud.com"
  • WATSON_USER = "use-token-auth"
  • WATSON_PWD = b"[TOKEN]"

As I said, in Watson, I had created the defined "TLS Optional" and configured the device. I tested the connection with a mosquitto client and it worked.

Any help is more than welcomed!, Best!

1

There are 1 answers

3
Francisco On

I found the answer looking at the code revisions in umqtt.simple (the mqtt library for esp8266)

The answer is that in the umqtt examples there was one that uses hexlify( client_id) and I followed as standard:

client = MQTTClient(client_id=hexlify(MQTT_CLIENT_ID), server=MQTT_BROKER_IP, user=MQTT_USER, password=MQTT_PWD)

Apparently mosquitto broker understand this but not the Watson IBM broker. Changing to:

client = MQTTClient(client_id=MQTT_CLIENT_ID, server=MQTT_BROKER_IP, user=MQTT_USER, password=MQTT_PWD)

solves the issue. For watson the variables format is as follows:

  • CLIENT_ID = bytes
  • MQTT_BROKER_IP (or url) = string
  • MQTT_USER = string
  • MQTT_PWD = bytes

Pay attention to the topic/message formats as well.

Best!