Page 1 of 1

Command not executed MQTT boot.py

Posted: Sun Jan 06, 2019 2:43 pm
by fizzytaken
Hi,
I have a problem with my boot.py file, I created a file with a different "def" for init network, mqtt, lib, etc ...
The ESP32 starts and starts to boot devices, but it will appear that it does not execute all commands:

My boot.py file runs:

print ("INIT NETWORK")
init_network()
print ("UPDATE")
update()
print ("INIT MQTT")
init_mqtt()

My serial port:

(...)
UPDATE
### LIBS DOWNLOADED ###
INIT MQTT
### CONNECTED TO INTERNET ###
MQTT CONFIG
I (6164) wifi: pm start, type:0

Traceback (most recent call last):
File "boot.py", line 92, in <module>
File "boot.py", line 37, in init_mqtt
NameError: name 'mac' is not defined
OSError: [Errno 2] ENOENT
MicroPython v1.9.4 on 2018-05-11; ESP32 module with ESP32
Type "help()" for more information.
>>>
(Nothing is received by the MQTT broker)

But i can pas commands one by one in shell and all works great

Anyone can help me ?
Regards


THE DEF COMMANDS IN THE BOOT.PY FILE

Code: Select all

# -INIT NETWORK- #
def init_network():
	ssid = "XXX"
	w_pass = "XXX"
	sta_if = network.WLAN(network.STA_IF)
	sta_if.active(True)
	sta_if.connect(ssid,w_pass)
	mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
	if (debug == 1):print(mac)
	time.sleep(5)
	return 0

# -UPDATE- #
def update():

	if (install_lib == 1):upip.install('micropython-umqtt.simple')
	from umqtt.simple import MQTTClient
	if (set_time == 1):settime() #Update Time

	if (debug == 1):print("### LIBS DOWNLOADED ###")
	return 0

# -INIT MQTT- #
def init_mqtt():
	if (debug == 1):print("### CONNECTED TO INTERNET ###")

	if (debug == 1):print("MQTT CONFIG")
	from umqtt.simple import MQTTClient
	mqtt = MQTTClient("device_id", "XXX",user="XXX", password="XXX", port="1883")
	time.sleep(5)
	mqtt.connect()
	mqtt.publish(mac, VERSION)
	return 0

Re: Command not executed MQTT boot.py

Posted: Sun Jan 06, 2019 5:24 pm
by dhylands
The variable mac is local to your init_network function. I think you need to add a "global mac" statement to init_network