Pi Pico W won't auto run main.py or boot.py

Thanks a lot @Frida !
I am not familiar with that dialect of python with the c. usage. Any chance you can explain what you are doing to check the connection and re-connection? I like the idea of checking for a periodic message. I can use that to stop sending the publish messages. Does the ip address go to '0.0.0.0' when the internet is lost? It seems like I have an IP even if the web is down but the model is live and I have local wifi.

As I recall there was a one time code that I ran to give the esp8266 network and password which it then remembered.

import esp, network

network.wifi_mode(esp.STA_MODE + esp.AP_MODE)
nic = network.WLAN()

print(nic.ifconfig())
nic.connect('SSID', 'Password')
print(nic.ifconfig())

During startup, the network is 0.0.0.0, so I tested until the network was up and running. lines 20 and 21

Then I made a function ''reconnect()"
In this function I test that the network is different from 0.0.0.0 and then I test if I can connect to the MQTT server.

When I can, I switch to "CONNECTED", where I test whether there is still a connection, otherwise it's all over again.

Hope this is useful?

Just wondering if it goes to 0's if the connection is dropped.

I won't be using Discord. Their stupid bot closed and locked my post. I don't know why, but they are not my resource.

Just out of interest - but slightly off topic - are you trying to send MQTT to an off premises MQTT server somewhere - or is this to a local device ?

If it is somewhere on the internet you want to nail it down pretty tightly at the firewall level and maybe read up on Linux Firewalls etc so you can restrict what (and from where) data can ingress to your MQTT server (as it appears you are not doing any form of secure connection from the Pico-W)

Craig

In one of your posts above, you mentioned the... c. dialect of Python or the c. usage.

The 'c' character is simply the instance name for the object that has been declared. Any name/string could have been used. I tend to use names that mean something to me, like this...

mqtt_client = connect_to_mqtt(mqtt_client_id, mqtt_server, mqtt_port, mqtt_user, mqtt_password)

Note: Just incase you are wondering, the character 'c' has nothing to do with the C or C++ programming languages.

Thanks! Still getting used to the python syntax. @dynamicdave

I am using the 8883 port with login and password on both node-red and hiveMQ. Do I need more? The code listed here was just for testing.

I was able to cobble together some working code in micropython that cleanly re-connects to the internet after signal is lost. I will be adding the node-red connection soon and plant to have Node-Red send a heart-beat message every 10 seconds and will not publish to hiveMQ until I get the heartbeat back.

import network
import time
import ntptime

# Replace with your network credentials
ssid = "xxxxxxx"
password = "xxxxxxx"

# Create a station interface
wlan = network.WLAN(network.STA_IF)

def connect():
    # Activate the interface
    wlan.active(True)
    # Connect to the network
    wlan.connect(ssid, password)
    # Check for successful connection
    while wlan.ifconfig()[0] == '0.0.0.0':
        time.sleep(1)

def check_connection():
    if wlan.isconnected():
        return True
    else:
        print("Lost connection. Attempting to reconnect...")
        connect()
        if wlan.isconnected():
            print("Reconnected.")
            return True
        else:
            print("Failed to reconnect.")
            return False
        
def check_internet():
    try:
        ntptime.settime()  # Try to set the time using an NTP server
        print("Internet is available. Current time:", time.localtime())
        return True
    except:
        print("Unable to reach the NTP server. Internet might not be available.")
        return False

connect()

# Main loop
while True:
    if check_connection():
        if check_internet():
            print("Connected to Internet")

    time.sleep(10)


You may have an Internet connection even if ntptime.settime() fail thus returning "False" in your code.
Most of times, at least in my case, settime() requires some time to answer leading to failures.
As an alternative, I get my local epoch from here and parse it to my needs.

Yes - the issue is that anyone/everyone else on the internet also has access to your system to hammer it and try to break it.

At the very minimum you want some form of firewall to restrict connections from only your address - are you only doing outbound requests to HiveMQ - nothing is making requests or opening connections inbound to your Node Red - or do you have Node red out on the internet somewhere ?

Craig