• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 micropython stops working when WiFi is down !!

Q&A Forum › Category: ESP32 › ESP32 micropython stops working when WiFi is down !!
0 Vote Up Vote Down
Philippe asked 2 weeks ago

In order to watering my garden I use an ESP32 with a micropython script. This equipment works in standalone, with no external action. it has all necessary parameters in a stored file (JSON form).
I made a little Windows application to change these parameters using my Mosquitto broker and Paho-MQTT.
The ESP32 stops working if the Wi-Fi is down (return OK when WiFi is OK), this is a bit annoying if it happens while a solenoid valve is on.
Does it exist a solution to this issue ?

7 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 1 week ago

Hi.
I’m sorry for taking so long to get back to you. I missed your question.
 
What do you mean by “stops working”? What happens exactly? Does it turn off or does it disconnect from your router?
Do you want it to try to reconnect to the broker as soon as Wi-Fi is available?
 
Regards,
Sara

0 Vote Up Vote Down
Philippe answered 1 week ago

Hi Sara,
Don’t worry for the delay, thank you for your answer.
If, for any reason the wifi disappear, the python script continue running but is in a wifi error loop and the rest of my script is ignored.
Very annoying if a solenoid valve is on.
When the wifi returns all is OK.
I just wish my script be executed even when the wifi is down. Is there a way to do this, perhaps with another wifi manager ??

0 Vote Up Vote Down
Sara Santos Staff answered 1 week ago

Hi.
If the watering system works without any external action, you can try to make your Wi-Fi connection non-blocking.
How are you handling the Wi-Fi connection?

Regards,
Sara
 
 

0 Vote Up Vote Down
Philippe answered 6 days ago

Hi Sara,
 
That’s how I handling the Wi-Fi connection :

import time
import ntptime
import esp
import network
from network import WLAN #AJOUT
from umqtt.simple import MQTTClient
import ubinascii
import machine
from machine import RTC
from machine import WDT
import micropython
import ujson
import sys
import random
import gc
gc.collect()
esp.osdebug(None)
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
    time.sleep(2)
    # wdt.feed()
    print(“Pas de connexion WIFi”)
    pass
print(‘Connection successful’)
print(station.ifconfig())

Please teach me how to make my Wi-Fi connection non-blocking that’s what I look for !!
My script is standalone working, time to time I send via MQTT a new set of parameters which is stored in a data file in the ESP32 memory.
Thanks in advance for your help.
Best regards
Philippe

0 Vote Up Vote Down
Sara Santos Staff answered 6 days ago

Hi.
You can create a function that connects to Wi-Fi. Inside that function, define a number of attempts to connect to wi-fi, after setting that it is not connected.
For example, a function called connect_wifi() that tries to connect to Wi-Fi and returns true if wi-fi is connected or false otherwise.

Then, have another function that checks whether Wi-Fi is connected or not. It can be called check_wifi(), for example.

Then, in your main loop, you can check the wi-fi connection every 10 seconds, for example by calling the check:wifi() function. If it is not connected, call the connect_wifi() to try to connect again..

After this, do all the other tasks for your code.

So, in summary, in you main loop:
– check whether Wi-Fi is connected or not (check_wifi() function) every 10 seconds for example (use timers instead of delays)
– if wi-fi not connected, try to connect to wi-fi, call the connect_wifi() function, save in a variable, whether connection was successful or not
– do your other tasks

– once 10 seconds have passed, it will check the Wi-Fi connection again.
– (the loop repeats)

I hope this helps giving you some guidance to rearrange your code.

Alternatively, you can try to use asynchronous programming. You can check this tutorial: https://randomnerdtutorials.com/micropython-esp32-esp8266-asynchronous-programming/
Regards,
Sara

0 Vote Up Vote Down
Philippe answered 3 days ago

Hi Sara,
 
Many thanks for your explanations, I will try and be back later.
I use PAHO-MQTT and I suppose this library use asyncio, I will also try this afterr reading of your link
See you soon for the results.
Good afternoon.
Best regards.
Philippe

0 Vote Up Vote Down
Sara Santos Staff answered 3 days ago

Great!
Then, let me know your results.
 
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.