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
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)
Thanks