Issues about the NetworkDown event

Hey, guys, I am using the wifi_rs21 module, but I am confused about the NetworkDown event.

Once the InitializeNetwork() method finishes, which enables wifi_rs21 to find the network to join and succeeds in retrieving the time from network, the Network Down event is raised. This means that the network connection is not correctly configured, thus cannot receive any messages, but inside the event handler, I can still get the time from network. This is really confusing. Can somebody help me?

Here is the code:

        void ProgramStarted()

        void InitializeNetwork()
            // setup events
            wifi_RS21.NetworkDown += wifi_NetworkDown;
            wifi_RS21.NetworkUp += wifi_NetworkUp;
            wifi_RS21.Interface.NetworkAddressChanged += Interface_NetworkAddressChanged;

            // use the router's DHCP server to set my network info

            // to use hold the network to connect to
            WiFiNetworkInfo targetNetwork = TargetNetwork(wifi_RS21.Interface.Scan(), targetSSID);

            if (targetNetwork != null)
                wifi_RS21.Interface.Join(targetNetwork, password);
                Debug.Print("Network joined");
                Debug.Print("Connected: " + wifi_RS21.IsNetworkConnected.ToString());
                Debug.Print("Up: " + wifi_RS21.IsNetworkUp.ToString());
                Debug.Print("Current Time: " + NtpClient.GetNetworkTime().ToString("dd/MM/yyyy HH:mm:ss"));

        void wifi_NetworkDown(GTM.Module.NetworkModule sender,
            GT.Modules.Module.NetworkModule.NetworkState state)
            Debug.Print("Current Time: " + NtpClient.GetNetworkTime().ToString("dd/MM/yyyy HH:mm:ss"));
            Debug.Print("Network Down Event");

I’m on a different interface but the way it works for me is I initialize the network then wait for the network up event. After initialize I get a network down event followed by a network up event. I use the up event to start everything running including a call to NTP for the date time setup.

I first propose that you have a coding logic error. Why would you try to read from the network when you’re being told it’s down? (Yes, I know you’re somewhat arbitrarily doing this, but don’t ! :wink: )

The network down handler fires at a point in time. Whether the network then establishes connection while your time request is waiting, or if the time code actually caches this from RTC, who knows. If you want to find out more, I think you’re going to need to dig deeper into code. Or, use the DOWN handler to do whatever is needed so you don’t try to read from the network, flash the big red LED so people know your app isn’t working, etc etc, and as Cowboy says use the UP handler to then get everything back in working order.

@ Brett - The reason I want to read the time from Internet in down handler is that I want to check whether the network is connected. Thank you for the reply.

@ Cowboy - Thank you for the reply. But for me, after the down event, there is no up event.

That’s not the right place to do that. The handler is just there to let you know that you need to change your app behavior because it can’t rely on the network being up, because it’s not…

The right thing to diagnose now for you is why does your network not work, why does your network never recover and fire the network up handler.

Can you give us a reminder about your networking on this device, have you ever had it working reliably ?

@ Brett - Sorry about the late reply.

Now I am using Connectify to convert my laptop to a wireless router. The WIFI_RS21 module directly connects to my laptop.

I have never seen there has been a network up event, to be honest. I complete the main logic of the codes immediately after the wifi_rs21.interface.join() method instead of inside the network up event handler.