Go Down

Topic: WiFi.config() doesn't change WiFi.beginAP() result. MKR1010 AP exposes wrong IPs (Read 524 times) previous topic - next topic

Metron_Ross

Using the 1.8.9 Arduino IDE,
Using WiFiNina library version 1.3.0,
Using MKR1010 WiFi firmware version 1.0.0,
Using the program embedded below.

The bottom line is that I'm trying to tell my MKR1010 to be an access point that exposes a 192.168.192.___ class C network to the world, and instead it creates its default 192.168.4.___ network.

Using the WiFiNina library to query the WiFi module/libray's configuration, tells me that the network is the 192.168.192.___ network I tried to create.  That result/answer is not correct.

I know the configuration claims returned by the WiFiNina library call are wrong because when I connect my Windows computer to the MKR1010 AP, the address assigned to my Windows computer is a 192.168.4.___ address.  Also ping'ing 192.168.4.1 (the MKR1010's actual address) is successful.

Do I need to update my WiFiNina library, or my MKR1010 firmware?

Or is there a bug (mine, the library's, or the firmware's) that needs to be fixed?

Here's my program:
Code: [Select]

#include "WiFiNINA.h"
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include "IPAddress.h"


  WiFiUDP _udp;
  int _apChannel = 10;
  char ssid[] = "Soapbox";
  char pass[] = "password";
  IPAddress _myIpAddress = IPAddress{192, 168, 192, 252};
  IPAddress _dnsIp       = IPAddress{  8,   8,   8,   8};
  IPAddress _subnetMask  = IPAddress{255, 255, 255,   0};
  IPAddress _gatewayIp   = _myIpAddress;

  IPAddress _192_168_192_2 = IPAddress(192, 168, 192, 2);
  IPAddress _192_168_4_2 = IPAddress(192, 168, 4, 2);


void setup()
{
  Serial.begin(9600);
  while(!Serial);
 
  setupWiFiPhysical();
}


void setupWiFiPhysical()
{
  int status = WL_IDLE_STATUS;

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE)
  {
    Serial.println("\nCommunication with MKR's WiFi module failed!");
  }
  else
  {
    Serial.println("\nWiFi module exists, MKR can communicate with it.");
    printWiFiStatus(WiFi.status(), true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0")
  {
    Serial.print("\n Please upgrade the firmware from "); Serial.print(fv);
    Serial.println(" to higher than 1.0.0");
  }
  else
  {
    Serial.print("\nWiFi module's firmware is version "); Serial.println(fv);
  }
 
  printWiFiConfig();

  WiFi.config(_myIpAddress, _dnsIp, _gatewayIp, _subnetMask);
  Serial.print("\nUsed WiFi.Config to set my IP Address to  "); printDottedDecIp(_myIpAddress, true);
  Serial.print("Used WiFi.Config to set Gateway IP to  "); printDottedDecIp(_gatewayIp, true);
  Serial.print("Used WiFi.Config to set SubNet Mask to "); printDottedDecIp(_subnetMask, true);
  Serial.print("Used WiFi.Config to set DNS Address to "); printDottedDecIp(_dnsIp, true);
     
  printWiFiConfig();

  Serial.println("\nEnter while loop that attempts to have the WiFi module become an Access Point\n");
  while (status != WL_AP_LISTENING)
  {

    printWiFiStatus(WiFi.status(), true);

    if (status != WL_IDLE_STATUS)
    {
      Serial.println("\nUse end() to attempt to dump/forget any connections to users");
      //WiFi.disconnect();
      WiFi.end();
      Serial.print(" Attempt's result is "); printWiFiStatus(WiFi.status(), true);
    }
   
    Serial.print("\nInvoking to 'Wifi.beginAP()' using SSID '"); Serial.print(ssid);
    Serial.print("', and Password '"); Serial.print(pass);
    Serial.print("', on 2.4GHz WiFi Channel "); Serial.println(_apChannel);

    status = WiFi.beginAP(ssid, pass, _apChannel);
    if (status != WL_AP_LISTENING)
    {
      Serial.print("BeginAP attempt failed and returned "); Serial.println(status);
      // wait a few seconds before re-trying:
      delay(5000);
    }
    else
    {
      Serial.print("\nAfter beginAP(), "); printWiFiStatus(WiFi.status(), true);
    }
   
    printWiFiConfig();

  }
     
  WiFi.config(_myIpAddress, _dnsIp, _gatewayIp, _subnetMask);
  Serial.print("\nUsed WiFi.Config to set my IP Address to  "); printDottedDecIp(_myIpAddress, true);
  Serial.print("Used WiFi.Config to set Gateway IP to  "); printDottedDecIp(_gatewayIp, true);
  Serial.print("Used WiFi.Config to set SubNet Mask to "); printDottedDecIp(_subnetMask, true);
  Serial.print("Used WiFi.Config to set DNS Address to "); printDottedDecIp(_dnsIp, true);
 
  printWiFiConfig();
}


void doWiFiPing(IPAddress ipToPing)
{
  Serial.print("Pinging "); printDottedDecIp(ipToPing, false);
  int pingRc = WiFi.ping(ipToPing);
  if (pingRc >= 0)
  {
    Serial.print(" - Round Trip Time = ");
    Serial.print(pingRc);
    Serial.println(" ms");
  }
  else
  {
    Serial.print(" - Failed. Error code: ");
    Serial.println(pingRc);
  }
}


void printWiFiStatus(int status, boolean crlf)
{
   Serial.print("WiFi Status decoded into: ");
   switch(status)
   {
      case WL_CONNECTED:       { Serial.print("WL_CONNECTED"); break; }
      case WL_AP_CONNECTED:    { Serial.print("WL_AP_CONNECTED "); break; }
      case WL_AP_LISTENING:    { Serial.print("WL_AP_LISTENING "); break; }
      case WL_NO_SHIELD:       { Serial.print("WL_NO_SHIELD or WL_NO_MODULE"); break; }  //  WL_NO_SHIELD and WL_NO_MODULE have the same value
//      case WL_NO_MODULE:       { Serial.print("WL_NO_MODULE"); break; }
      case WL_IDLE_STATUS:     { Serial.print("WL_IDLE_STATUS"); break; }
      case WL_NO_SSID_AVAIL:   { Serial.print("WL_NO_SSID_AVAIL"); break; }
      case WL_SCAN_COMPLETED:  { Serial.print("WL_SCAN_COMPLETED"); break; }
      case WL_CONNECT_FAILED:  { Serial.print("WL_CONNECT_FAILED"); break; }
      case WL_CONNECTION_LOST: { Serial.print("WL_CONNECTION_LOST"); break; }
      case WL_DISCONNECTED:    { Serial.print("WL_DISCONNECTED"); break; }
      default: { Serial.print(" UNKNOWN ("); Serial.print(status); Serial.print(" is a surprise value/unknown value"); break; }
   }
   if (crlf) { Serial.println(); }
}


void printWiFiConfig()
{
  Serial.println ("\n=====================================================================================================\n");
  Serial.println("Query WiFi library/Chipset to learn WiFi Status");
  // print your board's MAC, IP address, and Subnet MAsk:
  byte macToPrint[6];
  WiFi.macAddress(macToPrint);  // This returns the MAC bytes in bass-ackwards order
  Serial.print("  MKR WiFi MAC: "); printWiFiMac(macToPrint);
 
  Serial.print("  MKR WiFi FW:  "); Serial.println(WiFi.firmwareVersion());
 
  // print the MAC address of the router you're attached to:
  WiFi.BSSID(macToPrint);
  Serial.print("  Router's MAC (BSSID): "); printWiFiMac(macToPrint);

  // print the SSID of the network you're attached to:
  Serial.print("  LAN/SSID: '"); Serial.print(WiFi.SSID()); Serial.println("'");

  Serial.print("  Password: '");
  for (int i=0; i<sizeof(pass); i++)
  {
    Serial.print(pass[i]);
  }
  Serial.println("'");

  // print the encryption type:
  byte encryption = WiFi.encryptionType();
  Serial.print("  Encryption Type: ");
  Serial.println(encryption, HEX);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("  Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
 
  // print your board's IP address, Subnet Mask, and the AP's Gateway IP:
  Serial.print("  AP Gateway IP: ");
  Serial.println((IPAddress)WiFi.gatewayIP());  IPAddress ip = WiFi.localIP();
 
  Serial.print("  My IP Address: ");
  Serial.println(ip);
 
  Serial.print("  My Subnet mask: ");
  Serial.println((IPAddress)WiFi.subnetMask());
 
  Serial.println ("\n=====================================================================================================\n");
}


void printWiFiMac(byte mac[]) {
  for (int i = 5; i >= 0; i--) {
    if (mac[i] < 16) {
      Serial.print("0");
    }
    Serial.print(mac[i], HEX);
    if (i > 0) {
      Serial.print(":");
    }
  }
  Serial.println();
}


void loop()
{
  while (1)
  {
    delay(10000);
//    doWiFiPing(_192_168_192_2);
//    doWiFiPing(_192_168_4_2);
//    Serial.println();
  }
}

void printDottedDecIp(uint32_t ip, boolean crlf)  // Expects the IP Address to be stored with the last Octet in the MSByte.
{
  Serial.print((byte)(ip >>  0)); Serial.print('.');
  Serial.print((byte)(ip >>  8)); Serial.print('.');
  Serial.print((byte)(ip >> 16)); Serial.print('.');
  Serial.print((byte)(ip >> 24));
  if (crlf) { Serial.println(); }
}

Juraj

WiFi.config() is only for STA mode. the AP IP can't be changed with WiFiNINA

Metron_Ross

Juraj,

Thanks for the info.

Are you one of the WiFiNINA developers? or someone who has learned this the hard way?

If you're one of the WiFiNINA developers, I have some suggestions:

A) Modify the Arduino Forum Reference WiFi.config() page to explicitly describe that behavior, instead of maybe implying it, as it does now.

B) Modify the software so that WiFi.localIP(), and other functions like it report correct results (the actual IP address the MKR1010 AP is using).

C) Modify the Arduino Forum Reference WiFi.beginAP() page to describe that WiFiConfig() doesn't affect some (any?) of the results of invoking WiFi.beginAP().  Currently comments in the example program on that page explicitly claim that WiFi.config can be used to override the default 192.168.4.1 IP address.

Juraj

Juraj,

Thanks for the info.

Are you one of the WiFiNINA developers? or someone who has learned this the hard way?

If you're one of the WiFiNINA developers, I have some suggestions:

A) Modify the Arduino Forum Reference WiFi.config() page to explicitly describe that behavior, instead of maybe implying it, as it does now.

B) Modify the software so that WiFi.localIP(), and other functions like it report correct results (the actual IP address the MKR1010 AP is using).

C) Modify the Arduino Forum Reference WiFi.beginAP() page to describe that WiFiConfig() doesn't affect some (any?) of the results of invoking WiFi.beginAP().  Currently comments in the example program on that page explicitly claim that WiFi.config can be used to override the default 192.168.4.1 IP address.
When I created my WiFiEspAT library, the WiFiNINA WiFi.h was the template. I was wondering that it can't configure the AP.

russell_roberts

There is a new version of the library 1.24 maybe that helps but this works for me

Code: [Select]



IPAddress ip(10, 10, 10, 1);
  WiFi.config(ip,ip,ip);
 
  // print the network name (SSID);
  Serial.print("Creating access point named: ");
  Serial.println(ssid);
 
  // Create open network. Change this line if you want to create an WEP network:
  status = WiFi.beginAP(ssid, pass);




ocrdu

Code: [Select]
const IPAddress APIP(192, 168, 2, 1);
const int APChannel = 13;
WiFi.config(APIP, APIP, APIP, IPAddress(255,255,255,0));
APStatus = WiFi.beginAP(ssid, pass, APChannel);

works for me with WiFiNINA (except for the channel, it ignores that).

I use a Nano 33 IoT with the latest firmware, though.

BTW how is it going to reach 8.8.8.8 if it is in AP mode?

Metron_Ross

To you guys who have recently posted "Works fine for me" posts.

A) Are you working with the 1.3.0 version, or later one, of the WiFiNINA library?

B) Are you using an MKR1010? 

C) Does the MKR1010 have 1.0.0 firmware? 

The OP is about the MKR1010 with 1.0.0 firmware, and the 1.3.0 WiFiNINA library.

If those parts of the OP aren't duplicated in your situations, please be 100% clear about that at the very beginning of your posts in topics like this one.  Please don't make readers figure out whether you are using the same.  Just please write it explicitly using words like "My configuration is different.  I'm using ... versions of  ..." in the first line(s) of your posts.

Otherwise, each "Works for me" post tends to muddy the waters (because the post doesn't really report that "it" worked for you).

Adding those one or two extra sentences will really help readers.

Thanks

Go Up