Menu

WiFiMulti + AP_STA Mode

2022-11-22
2023-07-19
  • Marco Gulino

    Marco Gulino - 2022-11-22

    Hi,
    I've been using the Arduino Nano version of myFP2 for quite some time now, and I'm now migrating to the ESP32 version.
    I had to create some patches to enable some features I needed, firstly I managed to get it running on an ESP32-S2 mini without any issues (I can send a patch if interested, but it involves defining a new custom board with my own pinout, not sure if it's of interest for general audience), but more importantly I added a method for using WiFi multi (allowing an undefined number of stations), plus AP+STA mode (useful if no APs are found).

    In short, this will allow to define an indefinite (more or less, memory constraints apply) number of access points the firmware will try to connect to. Additionally, a flag can be set so that the firmware will run its own access point, in case none of the preconfigured access points is available.
    CONTROLLERMODE needs to be set to STATIONMODE in controller_config.h

    If the custom configuration file is not present, the firmware will work using the regular workflow.

    This is the new method I implemented (add before the setup method):

    #include <WiFiMulti.h>
    WiFiMulti wifiMulti;
    
    bool wifiMultiSuccessful() {
      const String filename = "/wifimulticonfig.jsn";
      if ( filesystemloaded == false )                    // check if SPIFFS was started
      {
        return false;
      }
    
      File f = SPIFFS.open(filename, "r");
      if (!f)
      {
        DEBUG_println("wifiMulti() file not found");
      }
      else
      {
        // Using JSON assistant - https://arduinojson.org/v5/assistant/
        // 188 * 2
        DynamicJsonDocument doc(2000);
        DeserializationError jerror = deserializeJson(doc, f);
        f.close();
        if (jerror)
        {
          DEBUG_println("wifiMulti() deserialise error");
          DEBUG_println(jerror.c_str());
          return false;
        }
        bool ap_sta = doc["ap_sta"] | true;
        if(ap_sta) {
          WiFi.mode(WIFI_MODE_APSTA);
          WiFi.softAP(ControllerData->get_devicename().c_str(), doc["ap_sta_psk"] | "");
        } else {
          WiFi.mode(WIFI_MODE_STA);
        }
        auto accessPoints = doc["APs"].as<JsonArray>();
        for(JsonVariant accessPoint: accessPoints) {
          DEBUG_print("Adding accessPoint: SSID=");
          DEBUG_print(accessPoint["ssid"].as<String>());
          DEBUG_print(", psk=");
          DEBUG_println(accessPoint["psk"].as<String>());
          wifiMulti.addAP(accessPoint["ssid"], accessPoint["psk"]);
        }
        auto result = wifiMulti.run();
        DEBUG_print("WiFiMulti success="); 
        DEBUG_println(result==WL_CONNECTED);
        if(result == WL_CONNECTED) {
          DEBUG_print("Connected to SSID=");
          DEBUG_println(WiFi.SSID());
        }
        return result == WL_CONNECTED || ap_sta;
      }
      return false;
    }
    

    In setup(), this line

      //-------------------------------------------------
      // STATIONMODE START
      // Setup Stationmode, as a station connecting to an existing wifi network
      //-------------------------------------------------
      if (myfp2esp32mode == STATIONMODE)
    

    becomes

        //-------------------------------------------------
      // STATIONMODE START
      // Setup Stationmode, as a station connecting to an existing wifi network
      //-------------------------------------------------
      if (myfp2esp32mode == STATIONMODE && ! wifiMultiSuccessful())
    

    Finally, you need to upload a wifimulticonfig.jsn in the data partition, with this format:

    {
        "ap_sta": true, // True for AP+STA mode, false otherwise
        "ap_sta_psk": "My AP Password",
        "APs": [
            {
                "ssid": "My First AP",
                "psk": "My First AP psk"
            },
            {
                "ssid": "My Second AP",
                "psk": "My Second AP psk"
            },
            .........
        ]
    }
    

    I've estimated the DynamicJSONDocument size for roughly 10 APs, I guess it could be increased further.
    To save memory, it's suggested to compact the configuration json.

    Hope it's useful.

    Marco

     

    Last edit: Marco Gulino 2022-11-26
    • brownrb

      brownrb - 2023-07-17

      Confirming that support for

      // Select EITHER of the following WiFi Config types, READWIFICONFIG or MULTIAP
      // Or you can leave both commented out
      // To enable reading SSID and PASSWORD from file wificonfig.json at
      // boot time, uncomment the following file
      #define WIFICONFIGTYPE    READWIFICONFIG
      
      // To enable a multi-ap controller, uncomment the following line
      //#define WIFICONFIGTYPE    MULTIAP
      

      is in the next release 306-02

       
  • brownrb

    brownrb - 2023-02-17

    Hi Marco
    Finally got back to reading this, I do have a question though, why is there a need for multiple AP's or stations? What scenario would make this a requirement?

    Regards
    Robert

     
    • Marco Gulino

      Marco Gulino - 2023-03-25

      Hi,
      Well, the first answer that comes to mind is, "why not?" If you can have
      this capability, without adding much in term of complexity, I'd say it's
      just a bonus.

      In my scenario, I have several wifi APs, depending on where the telescope
      is (main one indoors for testing, garden repeater, portable repeater if I'm
      further back in the garden, or if I'm outdoors, phone hotspot) so it's
      convenient to have all of them already preconfigured for all necessities

      Thanks,
      Marco

      On Fri, 17 Feb 2023 at 10:56, brownrb brownrb@users.sourceforge.net wrote:

      Hi Marco
      Finally got back to reading this, I do have a question though, why is
      there a need for multiple AP's or stations? What scenario would make this a
      requirement?

      Regards
      Robert


      WiFiMulti + AP_STA Mode
      https://sourceforge.net/p/myfocuserpro2-esp32/discussion/general/thread/fa3d01f47a/?limit=25#fa7c


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/myfocuserpro2-esp32/discussion/general/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.