Esp32 reload program after wifi cut

hi all
i have uploaded a micropython program in my esp32 , it works well , the main issue the board doesn't come to execute the boot.py and the main.py after a power loss or wifi loss ...advise plz how to get the progrma working without a hard reset

Hello @romdhane2030!
Technically a 'hard reset' (pushing the RESET button) is handled as a power loss by the CPU, so I'm not sure what is the difference between those.
Can you give us some more info on your setup? I'd like to better understand:

  • what software have you used to program the board?
  • how is the board powered when in use?
  • what happens when there is a failure (do the board LEDs fade like after a reset?)
  • what is the procedure you describe as a "hard reset"?

PS: Wifi loss is totally different, and you should handle that in your sketch. The CPU does not reset in that case :slight_smile:
BR,
Luca

:point_up_2: :point_up_2: This! :point_up_2: :point_up_2:

yes , you are totally right , wifi loss is already handled ....in the micropython program ....however the problem persists for the power loss case , i.e. the ship doesn't come to execute the script itself....regards

yes , you are totally right , wifi loss is already handled ....in the micropython program ....however the problem persists for the power loss case , i.e. the ship doesn't come to execute the script itself....
i mean by hard reset , the local button with "RST" label ......by the way , i'm using the program to connect to hivemq broker in order to send a msg to the ship ....is it possible to upload a server program in my esp32 board , so i can get and post coammands to and from the board ...in case of affirmative , would you provide a github ling for that project ....i can provide more details if needed ....best regards

@romdhane2030

the if the board does not execute the boot.py and main.py after a power loss it means that it's not really a power loss.

you should try to disconnect the board from power manually and make sure you don't have some sort of capacitor on the power line which might keep it alive when the power is cut for a fraction of a second.

try this in your board

  • add a print('boot.py') in boot.py
  • add a print('main.py) in main.py

keep a REPL open and issue the following

import machine
machine.soft_reset()

this should reset the board and show those two print lines.
If it doesn't happen your boot.py or main.py might be corrupted by copying/saving them to the machine.

To make a web server on the MicroPython board search for esp32 micropython web server GET and find any of the examples.
Make sure they're not older than 1 year because some stuff might have changed :slight_smile:

1 Like

ok that's good enough for this topic ...thank you , juste one more thing , as my board is far away and need to update the firmware or a librairy to keep the device working ( as you said stuff keep changing) what's the way to do that and even obviously ch

import machine

< >>> machine.soft_reset()
MPY: soft reboot
connected to wifi
boot.py

/>
Connection lost -- EOF

Use Stop/Restart to reconnect.

Process ended with exit code 1.
that's what im getting after unplugging and plug back the usb cable

could you paste the content of your boot.py and main.py here for us to take a look and test?
I'm more than happy to run the code on a few of my boards :slight_smile:

main.py

sorry, @romdhane2030

you'll need to paste the content of the files.
if the content of your main.py is simply

main.py

it will throw an error.

Which editor are you using?

  • open the file
  • select all the code
  • copy
  • create a code block by adding 3 back-ticks ( ` )
  • paste the content
  • close the code block with 3 more back-ticks ( ` )

this is a screenshot to make it clearer

CleanShot 2023-09-05 at 08.07.33@2x

main.py


from umqtt.simple import MQTTClient

import machine

# Configuration MQTT
mqtt_server = "[broker.hivemq.com](http://broker.hivemq.com/)"
mqtt_port = 1883
mqtt_topic_commande = b"pump" # Use bytes instead of a string
mqtt_topic_confirmation = b"pompe/confirmation" # Use bytes instead of a string
mqtt_topic_ship = b"ship/control" # Use bytes instead of a string

client_id = b"esp32" # Use bytes instead of a string
client = MQTTClient(client_id, mqtt_server, mqtt_port)

def pump_callback(topic, msg):
if topic == mqtt_topic_commande:
if msg == b"start": # Use bytes instead of a string
# DĂ©marrage de la pompe
print("Pompe démarrée")
# Envoyer un message de confirmation
client.publish(mqtt_topic_confirmation, b"Pompe démarrée") # Use bytes instead of a string
elif msg == b"stop": # Use bytes instead of a string
# ArrĂȘt de la pompe
print("Pompe arrĂȘtĂ©e")
# Envoyer un message de confirmation
client.publish(mqtt_topic_confirmation, b"Pompe arrĂȘtĂ©e") # Use bytes instead of a string

def ship_callback(topic, msg):
# Handle ship control messages here
print("Received ship control message:", msg.decode("utf-8")) # Convert bytes to string for display

client.set_callback(pump_callback) # Set the callback for pump control
client.connect()
print("Connected to HIVEMQBroker")
client.subscribe(mqtt_topic_commande)
client.subscribe(mqtt_topic_ship) # Subscribe to the ship control topic
client.publish(mqtt_topic_confirmation, b"hello", 1) # Use bytes instead of a string

try:
while True:
client.check_msg()
except KeyboardInterrupt:
client.disconnect()
print("DĂ©connexion")

boot.py

main.py

import machine

# Configuration MQTT
mqtt_server = "[broker.hivemq.com](http://broker.hivemq.com/)"
mqtt_port = 1883
mqtt_topic_commande = b"pump" # Use bytes instead of a string
mqtt_topic_confirmation = b"pompe/confirmation" # Use bytes instead of a string
mqtt_topic_ship = b"ship/control" # Use bytes instead of a string

client_id = b"esp32" # Use bytes instead of a string
client = MQTTClient(client_id, mqtt_server, mqtt_port)

def pump_callback(topic, msg):
if topic == mqtt_topic_commande:
if msg == b"start": # Use bytes instead of a string
# DĂ©marrage de la pompe
print("Pompe démarrée")
# Envoyer un message de confirmation
client.publish(mqtt_topic_confirmation, b"Pompe démarrée") # Use bytes instead of a string
elif msg == b"stop": # Use bytes instead of a string
# ArrĂȘt de la pompe
print("Pompe arrĂȘtĂ©e")
# Envoyer un message de confirmation
client.publish(mqtt_topic_confirmation, b"Pompe arrĂȘtĂ©e") # Use bytes instead of a string

def ship_callback(topic, msg):
# Handle ship control messages here
print("Received ship control message:", msg.decode("utf-8")) # Convert bytes to string for display

client.set_callback(pump_callback) # Set the callback for pump control
client.connect()
print("Connected to HIVEMQBroker")
client.subscribe(mqtt_topic_commande)
client.subscribe(mqtt_topic_ship) # Subscribe to the ship control topic
client.publish(mqtt_topic_confirmation, b"hello", 1) # Use bytes instead of a string

try:
while True:
client.check_msg()
except KeyboardInterrupt:
client.disconnect()
print("DĂ©connexion")

@romdhane2030 This is what the <CODE> button in the toolbar does for you:

image

It's described in How to get the best out of this forum - see under "Code Problems"

2 Likes

is this the correct indentation?

from umqtt.simple import MQTTClient

import machine

# Configuration MQTT
mqtt_server = "[broker.hivemq.com](http://broker.hivemq.com/)"
mqtt_port = 1883
mqtt_topic_commande = b"pump" # Use bytes instead of a string
mqtt_topic_confirmation = b"pompe/confirmation" # Use bytes instead of a string
mqtt_topic_ship = b"ship/control" # Use bytes instead of a string

client_id = b"esp32" # Use bytes instead of a string
client = MQTTClient(client_id, mqtt_server, mqtt_port)

def pump_callback(topic, msg):
    if topic == mqtt_topic_commande:
        if msg == b"start": # Use bytes instead of a string
        # DĂ©marrage de la pompe
            print("Pompe démarrée")
            # Envoyer un message de confirmation
            client.publish(mqtt_topic_confirmation, b"Pompe démarrée") # Use bytes instead of a string
        elif msg == b"stop": # Use bytes instead of a string
            # ArrĂȘt de la pompe
            print("Pompe arrĂȘtĂ©e")
            # Envoyer un message de confirmation
        client.publish(mqtt_topic_confirmation, b"Pompe arrĂȘtĂ©e") # Use bytes instead of a string

def ship_callback(topic, msg):
    # Handle ship control messages here
    print("Received ship control message:", msg.decode("utf-8")) # Convert bytes to string for display

client.set_callback(pump_callback) # Set the callback for pump control
client.connect()

print("Connected to HIVEMQBroker")
client.subscribe(mqtt_topic_commande)
client.subscribe(mqtt_topic_ship) # Subscribe to the ship control topic
client.publish(mqtt_topic_confirmation, b"hello", 1) # Use bytes instead of a string

try:
    while True:
        client.check_msg()
except KeyboardInterrupt:
    client.disconnect()
    print("DĂ©connexion")

I got some code to connect to a locally running MQTT server (Mosquitto 2.0.17)

I think most of your problems in that code is that

  • board is not connected to a network
  • MQTT Server won't accept connection

network_credentials.py

ssid = "WIFI SSID NAME"
password = "WIFI PASSWORD"

main.py

from umqtt.simple import MQTTClient
import network_credentials as nc

import machine

import network

n_if = network.WLAN(network.STA_IF)
n_if.active(True)
n_if.connect(nc.ssid, nc.password)
while not n_if.isconnected():
    print('connecting')
    
print(f"network connection is {n_if.isconnected()}")


# Configuration MQTT
mqtt_server = "10.0.1.17"
mqtt_port = 1883
mqtt_topic_commande = b"pump" # Use bytes instead of a string
mqtt_topic_confirmation = b"pompe/confirmation" # Use bytes instead of a string
mqtt_topic_ship = b"ship/control" # Use bytes instead of a string

client_id = "my_esp32" # Use bytes instead of a string
client = MQTTClient(client_id, mqtt_server, mqtt_port)

def pump_callback(topic, msg):
    if topic == mqtt_topic_commande:
        if msg == b"start": # Use bytes instead of a string
        # Dmarrage de la pompe
            print("Pompe dmarre")
            # Envoyer un message de confirmation
            client.publish(mqtt_topic_confirmation, b"Pompe dmarre") # Use bytes instead of a string
        elif msg == b"stop": # Use bytes instead of a string
            # Arrt de la pompe
            print("Pompe arrte")
            # Envoyer un message de confirmation
        client.publish(mqtt_topic_confirmation, b"Pompe arrte") # Use bytes instead of a string

def ship_callback(topic, msg):
    # Handle ship control messages here
    print("Received ship control message:", msg.decode("utf-8")) # Convert bytes to string for display

client.set_callback(pump_callback) # Set the callback for pump control
client.connect()

print("Connected Broker")
client.subscribe(mqtt_topic_commande)
client.subscribe(mqtt_topic_ship) # Subscribe to the ship control topic
client.publish(mqtt_topic_confirmation, b"hello", 1) # Use bytes instead of a string

try:
    while True:
        client.check_msg()
except KeyboardInterrupt:
    client.disconnect()
    print("Disconnection")

great ...this is really working good without any errors ...thank you so much ..

glad to hear that :slight_smile:

you might want to use a single callback rather than two (see in my code that I only issue one set_callback()) and filter in there.

good luck
u.