MQTT connection stopes main.py

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
rp346@njit.edu
Posts: 18
Joined: Sun Sep 16, 2018 6:33 pm

MQTT connection stopes main.py

Post by rp346@njit.edu » Sat Dec 08, 2018 2:09 am

I have this simple code (main.py) running on ESP32 with micropython.

This works perfectly, but if just add MQTT part by uncommented the commented lines, things doesn't work nothing gets pushed to MQTT nor the relay connected to PIN 12 triggers.

If i run

Code: Select all

ampy -p /dev/tty.SLAB_USBtoUART run main.py
it works too.

Code: Select all

import machine
import utime
import ujson as json

import sensor
import identity
import mqtt
import ubinascii
from umqtt.robust import MQTTClient
import gc
gc.collect()

config = json.loads(open('config.json').read())
SERVER = config['mqtt2']['hostname']
PORT = config['mqtt2']['port']
USER = config['mqtt2']['username']
PASSWORD = config['mqtt2']['password']
DEVICE_ID = ubinascii.hexlify(machine.unique_id()).decode('utf-8')
PIN12 = machine.Pin(12, machine.Pin.OUT)

SLEEP_TIME = int(config['system']['sleep_time'])

data = {}

client = MQTTClient(DEVICE_ID, SERVER, user=USER, password=PASSWORD, port=PORT)
#client.connect()

while True:
  data['date'] = identity.local_date()
  data['lux'] = sensor.read_lux()
  #client.publish(str(DEVICE_ID,'utf-8')+"/data", json.dumps(data))

  if PIN12.value() == 1:
      PIN12.value(0)
  else:
      PIN12.value(1)

  utime.sleep(SLEEP_TIME)
How can I simply this to fix the issue I am having ?

Thanks
Last edited by rp346@njit.edu on Mon Dec 17, 2018 10:03 pm, edited 1 time in total.
- roy

david_syd_au
Posts: 2
Joined: Thu Dec 13, 2018 10:04 pm

Re: MQTT connection stopes main.py

Post by david_syd_au » Fri Dec 14, 2018 9:39 am

You should call
client.check_msg()
in your main loop so that the MQTT can handle messaging.

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: MQTT connection stopes main.py

Post by cefn » Sat Dec 15, 2018 1:34 pm

Have you tried running each of these commands or code blocks through the REPL?

This means you can interactively see any errors or blocking statements (which don't return to the REPL prompt) and figure out what is behind the error.

Another thing I do within the boards I use is connect over serial, then press RST (Reset) to get the project to start again, meaning I can see any startup errors logged to the console.

Post Reply