How to distinguish the reasons for Wifi not connecting?

Is there an implemented way to make the photon could detect why the connection isn’t happening ?

I am intetested in distinguish:

  • incorrect password
  • wifi network not available.

With WiFi.scan() I could dduce the last one, but if I try to connect to a hidden wifi it is not useful.

Yes there is thanks to @rickkas7

//get the connection error 1007, 1024, etc.
int32_t errorno = getNetworkConnectionError();

// Based upon @rickkas7 code - get network connection error code from WICED
uint32_t getNetworkConnectionError()
{
	int32_t value;
	if (!getSystemDiagValue(DIAG_ID_NETWORK_CONNECTION_ERROR_CODE, value)) {value = 0;}
	return value;
}
//get any system diagnostic value returns false if error or true and value if OK
bool getSystemDiagValue(uint16_t id, int32_t &value)
{
    union Data {
        struct __attribute__((packed)) {
            uint16_t idSize;
            uint16_t valueSize;
            uint16_t id;
            int32_t     value;
            size_t offset;
        } d;
        uint8_t b[10];
    };
    Data data;
    data.d.offset = data.d.value = 0;
    struct {
        static bool appender(void* appender, const uint8_t* data, size_t size) {
            Data *d = (Data *)appender;
            if ((d->d.offset + size) <= sizeof(Data::b)) {
                memcpy(&d->b[d->d.offset], data, size);
                d->d.offset += size;
            }
            return true;
        }
    } Callback;
    system_format_diag_data(&id, 1, 1, Callback.appender, &data, nullptr);
    value = data.d.value;
    return (data.d.offset == sizeof(Data::b));
}
1 Like

I guess the error codes should be these:

But testing some errors I didn’t get the expected. For example with a bad password I’ve got a 1062 code…

In the end I ditched the approach of trying to interpret the WICED error code because I found like you that the error codes aren’t always consistent to the known cause. I now just hold a couple of global bool flags to know; Bad credentials or not, WiFi was connected(at some stage) or not, and then just use WiFi.ready() [although some say this isn’t reliable hence you need to test for local IP address not zero]. From these and when the connection test one can effectively decide if no WAP connection whether bad credentials OR whether WAP out of range.