Private Sub ConnectToNetwork
    Dim b(4) As Byte = GlobalStore.Slot1
    If b(0) = 192 And b(1) = 168 Then
        Log("Fast WiFi re-connection try...")
        Dim mask(4) As Byte = GlobalStore.Slot2    'Array As Byte(255, 255, 255, 0)
        Dim gateway(4) As Byte = GlobalStore.Slot3    'Array As Byte(192, 168, 1, 1)
        Dim dns(4) As Byte = GlobalStore.Slot4 'Array As Byte(192, 168, 1, 1)
        others.ESPex.ConfigNetwork(b, gateway, mask, dns, dns)
    End If
    Dim counter As UInt
    Do Until counter > 1000
        others.Get_WiFiStatus
        If others.Wifistatus = 3 Then    'Connected !
            Exit
        End If
        Delay(5)
        counter = counter + 1
    Loop
    Log("Fast reconnection counter = ", (counter * 5) , " ms")
    Log("others.WiFistatus = ", others.Wifistatus)
    If others.Wifistatus = 3 Then    'wifi connected
        others.Check_Internet
    End If
    If counter > 1000 Then
        launchSlowConnect
    End If
   
    GlobalStore.Put(0, others.ESPex.SSID)
    Dim ip(4) As Byte
    others.ESPex.GetLocalIP(ip)
    GlobalStore.Put(1, ip)
    Log("WiFi connected: " , others.ESPex.SSID, "; my ip: ", others.ESPex.IP2str(ip))
   
    others.ESPex.GetSubnetMask(ip)
    GlobalStore.Put(2, ip)
    Log("Subnet Mask: ", others.ESPex.IP2str(ip))
   
    others.ESPex.GetGatwayIP(ip)
    GlobalStore.Put(3, ip)    'gateway
    GlobalStore.Put(4, ip)    'DNS
    Log("Gateway: ", others.ESPex.IP2str(ip))
   
       
    others.Save_Settings
    Log("Wifi, %: ", others.Get_WiFI_RSSI)
    If others.Wifistatus = 3 Then    'wifi connected
        others.Check_Internet
    End If
    Log("TODO: HTTP requests before DeepSleep...")
    others.Go_Sleep(10000)
End Sub
Sub launchSlowConnect
    others.SetupWiFi
    Log("Slow connecting to: ", others.SSID)
    wifi.Connect2(others.SSID, others.Password)
    Dim counter2 As UInt
    Do Until counter2 > 20
        others.Get_WiFiStatus
        If others.Wifistatus = 3 Then    'Connected !
            Exit
        End If
        Delay(500)
        counter2 = counter2 + 1
    Loop
    Log("Slow connection counter = ", (counter2 * 500) , " ms")
    If counter2 > 20 Then
        Log("Slow Wifi connecting is timed-out. Rebooting...")
        Delay(10)
        others.ESP.Restart
        Return
    End If
    Log("WiFi connected and credentials saved")
    Log("others.Wifistatus = ", others.Wifistatus)
    others.Check_Internet
End Sub