[SOLVED] LoPy mqtt cannot subscribe

hi there! i follow this tutorial to connect my lopy to ubidots

I managed to published the data to ubidots but I cant subscribe from ubidots. I want to control the LED from ubidots. I am new to micropython/python and cant find any references. Is anyone here also using pycom board or micropython?

here is my code:

from network import WLAN
import urequests as requests
import machine
import time

TOKEN = "xxx"
DELAY = 1  # Delay in seconds
wlan = WLAN(mode=WLAN.STA)
wlan.antenna(WLAN.INT_ANT)
wlan.connect("xxx", auth=(WLAN.WPA2, "xxx"), timeout=5000)
while not wlan.isconnected(): 
     machine.idle()
print("Connected to Wifi\n")

#subscribe
var mqtt = require("mqtt");
var client  = mqtt.connect('mqtt://things.ubidots.com', {username:'{i put token value here}', password:""});
client.subscribe({"/v1.6/devices/pycom/Switch": 1}, function(err, granted) {
  console.log(granted);
});
client.on('message', function(topic, message, packet) {
    #here you can process updates from the broker
     while True:
     try
     print('getting status from Ubidots')
     print(switch)
     except:
          pass
     time.sleep(10)
     print status[0]['value']
     if status[0]['value']:
     print "Switch is ON"

     if status[0]['value']:
     print("Switch is ON")
     control_variable.save_value({"value":0})
     else:
     print("Switch is OFF")
});

please help me. thank you so much!

Dear @Vickyh,

As first, I recomnend you test the basic example provided by pycom to get you board working over MQTT (publish example), with this example you can check if the configurations of the Ubidots MQTT broker are right, plus to check if you are able to talk with Ubidots. For the Ubidots MQTT documentation, please reference to the MQTT documentation.

Once you are able to talk with Ubidots, take a look of the subscribe Ubidots documentation, and verify if you are setting up the subscription to the right topic:

image

I hope this would help you!

All the best,
Maria C.

Hi Maria! you are the author of that tutorial.

I have tried the basic example and I am able to publish data to both adafruit and ubidots, therefore I can sure that my configurations are right.

I am confused while reading the references from MQTT documentation and subscribe ubidots documentation. I am not sure I should follow the right hand side or left hand side, however, I have put it in to my coding but it doesn’t work.

any suggestion? thanks! your tutorial is helpful.

Hey @Vickyh!

If you are able to run the basic example and publish data to Ubidots, means that the Ubidots MQTT Broker configuration are the right one, so you pass one step! :wink:

The inconvenience should be presented in the structure of the topic to be subscribed, if you desire to received just the last value from a variable the topic should be:

/v1.6/devices/{label_device}/{label_variable}/lv

Also, this guide can help you to get a better idea how to structure it!

All the best,
Maria C.