My ESP8266 (nodemcu board controled with REPL) crashes after 5 loops . See the code below.
With an ESP32, the code runs correctly.
More information :
- when I remove line 45 : <time.sleep(1)> , it works
- when I remove the line 25 : < client.subscribe(b"sol") >, its works .
Here is the code :
Code: Select all
import time
import machine
from machine import Pin, ADC
from umqtt.simple import MQTTClient
import sys
server = "farmer.cloudmqtt.com"
client_id = "client_ESP151"
def sub_cb():
print("cb")
# Connection to MQTT server
def connect_and_subscribe():
global client_id, server
client = MQTTClient(client_id, server, port = 16157, user='xxxxx', password='xxxxx')
client.set_callback(sub_cb)
client.connect()
client.subscribe(b"sol")
print('Connected to MQTT broker')
return client
# reset ESP8266_151
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(1)
machine.reset()
try:
client = connect_and_subscribe()
except Exception as e:
print(e)
restart_and_reconnect()
print ("loop start")
for i in range(0,50) :
print(i)
client.publish(b"sol", b"hello" + str(i))
time.sleep(1)
Thanks !
JP