[GH-ISSUE #549] Need help creating parameter for usable multicast port function -UPDATED #461

Closed
opened 2026-02-28 01:25:25 +03:00 by kerem · 1 comment
Owner

Originally created by @mrlightsman on GitHub (Mar 7, 2018).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/549

UPDATE: I've resolved two of my issues but still need help with this one.
GOAL:
Use WiFi Manager to set Multicast IP and Port for use with ESP-01 multicast communication and as the SSID when the ESP is in AP Mode.

PROBLEM:
User inputs a multicast port in the captive portal for use with multicasting Udp. I don't know how to get this input (char) into a usable format for Udp.beginMulticast() (unsigned int).

I've tried two approaches. First I tried:

portMulti.toInt(multiPort);
where portMulti is a parameter in the captive portal (char) and multiport is the variable for port for use in the Udp.beginMulticast() command.
and go this error

Arduino: 1.8.5 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, DIO, 1M (512K SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200"

WM_UDP_Sample.ino:174: error: request for member 'toInt' in 'portMulti', which is of non-class type 'unsigned int'

portMulti.toInt(multiPort);

       ^

exit status 1
request for member 'toInt' in 'portMulti', which is of non-class type 'unsigned int'

So then I tried:
portMulti.fromString(multiPort);

Arduino: 1.8.5 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, DIO, 1M (512K SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200"

WM_UDP_Sample.ino:172: error: request for member 'fromString' in 'portMulti', which is of non-class type 'unsigned int'

portMulti.fromString(multiPort);

       ^

exit status 1
request for member 'fromString' in 'portMulti', which is of non-class type 'unsigned int'

This parameter is for use with Udp.beginMulticast(local IP, multicast IP, multicast Port) where it will look like this:

IPAddress ipMulti(parameter 1); resolved with IPAddress ipMulti; ipMulti.fromString(multiIP);
portMulti = parameter 2; unresolved issue... char needs to be unsigned int
Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti);

The best solution would be for wifi manager and json to handle parameters in the correct format from the beginning. But, converting them later in the code can also work. If conversion is the way to go, you'll see where I would want to handle that in my code example.

Thank you all for looking at this and offering advice. It is greatly appreciated.

Here is my full code example: It is compiled and verified.

#include <FS.h>                   //this needs to be first, or it all crashes and burns...

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <WiFiUDP.h>  //Wifi UDP library from arduino master

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
WiFiClient client;
WiFiUDP Udp;

int GPIO_2 = 2; //To manually toggle device 1
int GPIO_0 = 0; //To manually toggle device 2
int GPIO_2State; //State of device 1
int GPIO_0State; //State of device 2

//Define your Box name and SSID
char ap_SSID[25] = "NewBox";

//define your default values here, if there are different values in config.json, they are overwritten.
char multiIP[16]; //used to store a Multicast IP address from user input on WiFiManager page
char multiPort[6]; //used to store a multicast port from user input on WiFi Manager page
unsigned int portMulti;  // used in multicast command below
byte packetBuffer[512];  //buffer to hold udp packets

//flag for saving data
bool shouldSaveConfig = false;

//callback notifying us of the need to save config
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}


void setup() {
    Serial.begin(115200);
    Serial.println();

  //clean FS, for testing
  //SPIFFS.format();

  //read configuration from FS json
  Serial.println("mounting FS...");
  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");

          strcpy(multiIP, json["multiIP"]);
          strcpy(multiPort, json["multiPort"]);
          strcpy(ap_SSID, json["ap_SSID"]);
          
        } else {
          Serial.println("failed to load json config");
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
  //end read


  // The extra parameters to be configured (can be either global or just in the setup)
  // After connecting, parameter.getValue() will get you the configured value
  // id/name placeholder/prompt default length

  WiFiManagerParameter custom_multiIP("multiIP", "Multicast IP", multiIP, 16);
  WiFiManagerParameter custom_multiPort("multiPort", "Multicast Port", multiPort, 6);
  WiFiManagerParameter custom_ap_SSID("ap_SSID", "SSID Identifier", ap_SSID, 25);
  
  
  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //set config save notify callback
  wifiManager.setSaveConfigCallback(saveConfigCallback);

  //add all your parameters here
  wifiManager.addParameter(&custom_multiIP);
  wifiManager.addParameter(&custom_multiPort);
  wifiManager.addParameter(&custom_ap_SSID);

  //reset settings - for testing
  //wifiManager.resetSettings();

  //set minimu quality of signal so it ignores AP's under that quality
  //defaults to 8%
  //wifiManager.setMinimumSignalQuality();
  

  //sets timeout until configuration portal gets turned off
  //useful to make it all retry or go to sleep
  //in seconds
  //wifiManager.setTimeout(120);


  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP"
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect(ap_SSID)) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");


  //read updated parameters
  strcpy(multiIP, custom_multiIP.getValue());
  strcpy(multiPort, custom_multiPort.getValue());
  strcpy(ap_SSID, custom_ap_SSID.getValue());
  
  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["multiIP"] = multiIP;
    json["multiPort"] = multiPort;
    json["ap_SSID"] = ap_SSID;
    
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }

    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
    //end save

  }

  Serial.println("Local IP");
  Serial.println(WiFi.localIP());

/////////////////////////////////////////////////////////////////////////////////////////////
//I've figured out how to makemultiIP into a usable form for Udp.beginMulticast().
//I also don't know how to convert char multiPort into an unsigned int portMulti for Udp.beginMulticast().
//I think my better bet would be to handle the WM and json differently so the user input is in the
//right format to begin with.  I just don't know how to do that.
//These few lines of code are where I really need help.
////////////////////////////////////////////////////////////////////////////////////////////

IPAddress ipMulti;
ipMulti.fromString(multiIP);
//portMulti.fromString(multiPort);
//or
//portMulti.toInt(multiPort);
portMulti = 3300; //just to get code to compile for now
Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti);
Serial.println("Multicast IP");
Serial.println(ipMulti);
Serial.println("Multicast port");
Serial.println(portMulti);
}



void loop() {

//This is just performing the simple operation of reading the udp and printing it to serial

int numBytes = Udp.parsePacket();
if (numBytes) {
  Udp.read(packetBuffer, numBytes);

  for (int i = 1; i <= numBytes; i++) {
   Serial.print(packetBuffer[i]);
  }
}

 //Get the state of all four pins
  GPIO_2State = digitalRead(GPIO_2); //Toggle button for device 1 (LOW is pressed
  GPIO_0State = digitalRead(GPIO_0); //Toggle button for device 2 (LOW is pressed



 //If GPIO_2 & GPIO_0 are LOW then reset the ESP into AP mode
    if (GPIO_2State ==LOW && GPIO_0State == LOW) {
      Serial.println("Reset invoked");
        WiFiManager wifiManager;
//        SPIFFS.format();
        Serial.println("config formatted");
        delay(1000);
        wifiManager.resetSettings();
        delay(1000);
     //reset and try again, or maybe put it to deep sleep    
     {
       ESP.reset();
       delay(2000);
        }
   }



}
Originally created by @mrlightsman on GitHub (Mar 7, 2018). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/549 UPDATE: I've resolved two of my issues but still need help with this one. GOAL: Use WiFi Manager to set Multicast IP and Port for use with ESP-01 multicast communication and as the SSID when the ESP is in AP Mode. PROBLEM: User inputs a multicast port in the captive portal for use with multicasting Udp. I don't know how to get this input (char) into a usable format for Udp.beginMulticast() (unsigned int). I've tried two approaches. First I tried: `portMulti.toInt(multiPort);` where portMulti is a parameter in the captive portal (char) and multiport is the variable for port for use in the Udp.beginMulticast() command. and go this error > Arduino: 1.8.5 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, DIO, 1M (512K SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200" > > WM_UDP_Sample.ino:174: error: request for member 'toInt' in 'portMulti', which is of non-class type 'unsigned int' > > portMulti.toInt(multiPort); > > ^ > > exit status 1 > request for member 'toInt' in 'portMulti', which is of non-class type 'unsigned int' So then I tried: `portMulti.fromString(multiPort);` > Arduino: 1.8.5 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, DIO, 1M (512K SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200" > > WM_UDP_Sample.ino:172: error: request for member 'fromString' in 'portMulti', which is of non-class type 'unsigned int' > > portMulti.fromString(multiPort); > > ^ > > exit status 1 > request for member 'fromString' in 'portMulti', which is of non-class type 'unsigned int' > This parameter is for use with Udp.beginMulticast(local IP, multicast IP, multicast Port) where it will look like this: IPAddress ipMulti(parameter 1); resolved with `IPAddress ipMulti; ipMulti.fromString(multiIP);` portMulti = parameter 2; unresolved issue... char needs to be unsigned int `Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti);` The best solution would be for wifi manager and json to handle parameters in the correct format from the beginning. But, converting them later in the code can also work. If conversion is the way to go, you'll see where I would want to handle that in my code example. Thank you all for looking at this and offering advice. It is greatly appreciated. Here is my full code example: It is compiled and verified. ``` #include <FS.h> //this needs to be first, or it all crashes and burns... #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino #include <WiFiUDP.h> //Wifi UDP library from arduino master //needed for library #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson WiFiClient client; WiFiUDP Udp; int GPIO_2 = 2; //To manually toggle device 1 int GPIO_0 = 0; //To manually toggle device 2 int GPIO_2State; //State of device 1 int GPIO_0State; //State of device 2 //Define your Box name and SSID char ap_SSID[25] = "NewBox"; //define your default values here, if there are different values in config.json, they are overwritten. char multiIP[16]; //used to store a Multicast IP address from user input on WiFiManager page char multiPort[6]; //used to store a multicast port from user input on WiFi Manager page unsigned int portMulti; // used in multicast command below byte packetBuffer[512]; //buffer to hold udp packets //flag for saving data bool shouldSaveConfig = false; //callback notifying us of the need to save config void saveConfigCallback () { Serial.println("Should save config"); shouldSaveConfig = true; } void setup() { Serial.begin(115200); Serial.println(); //clean FS, for testing //SPIFFS.format(); //read configuration from FS json Serial.println("mounting FS..."); if (SPIFFS.begin()) { Serial.println("mounted file system"); if (SPIFFS.exists("/config.json")) { //file exists, reading and loading Serial.println("reading config file"); File configFile = SPIFFS.open("/config.json", "r"); if (configFile) { Serial.println("opened config file"); size_t size = configFile.size(); // Allocate a buffer to store contents of the file. std::unique_ptr<char[]> buf(new char[size]); configFile.readBytes(buf.get(), size); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); json.printTo(Serial); if (json.success()) { Serial.println("\nparsed json"); strcpy(multiIP, json["multiIP"]); strcpy(multiPort, json["multiPort"]); strcpy(ap_SSID, json["ap_SSID"]); } else { Serial.println("failed to load json config"); } } } } else { Serial.println("failed to mount FS"); } //end read // The extra parameters to be configured (can be either global or just in the setup) // After connecting, parameter.getValue() will get you the configured value // id/name placeholder/prompt default length WiFiManagerParameter custom_multiIP("multiIP", "Multicast IP", multiIP, 16); WiFiManagerParameter custom_multiPort("multiPort", "Multicast Port", multiPort, 6); WiFiManagerParameter custom_ap_SSID("ap_SSID", "SSID Identifier", ap_SSID, 25); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //set config save notify callback wifiManager.setSaveConfigCallback(saveConfigCallback); //add all your parameters here wifiManager.addParameter(&custom_multiIP); wifiManager.addParameter(&custom_multiPort); wifiManager.addParameter(&custom_ap_SSID); //reset settings - for testing //wifiManager.resetSettings(); //set minimu quality of signal so it ignores AP's under that quality //defaults to 8% //wifiManager.setMinimumSignalQuality(); //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds //wifiManager.setTimeout(120); //fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration if (!wifiManager.autoConnect(ap_SSID)) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } //if you get here you have connected to the WiFi Serial.println("connected...yeey :)"); //read updated parameters strcpy(multiIP, custom_multiIP.getValue()); strcpy(multiPort, custom_multiPort.getValue()); strcpy(ap_SSID, custom_ap_SSID.getValue()); //save the custom parameters to FS if (shouldSaveConfig) { Serial.println("saving config"); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); json["multiIP"] = multiIP; json["multiPort"] = multiPort; json["ap_SSID"] = ap_SSID; File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { Serial.println("failed to open config file for writing"); } json.printTo(Serial); json.printTo(configFile); configFile.close(); //end save } Serial.println("Local IP"); Serial.println(WiFi.localIP()); ///////////////////////////////////////////////////////////////////////////////////////////// //I've figured out how to makemultiIP into a usable form for Udp.beginMulticast(). //I also don't know how to convert char multiPort into an unsigned int portMulti for Udp.beginMulticast(). //I think my better bet would be to handle the WM and json differently so the user input is in the //right format to begin with. I just don't know how to do that. //These few lines of code are where I really need help. //////////////////////////////////////////////////////////////////////////////////////////// IPAddress ipMulti; ipMulti.fromString(multiIP); //portMulti.fromString(multiPort); //or //portMulti.toInt(multiPort); portMulti = 3300; //just to get code to compile for now Udp.beginMulticast(WiFi.localIP(), ipMulti, portMulti); Serial.println("Multicast IP"); Serial.println(ipMulti); Serial.println("Multicast port"); Serial.println(portMulti); } void loop() { //This is just performing the simple operation of reading the udp and printing it to serial int numBytes = Udp.parsePacket(); if (numBytes) { Udp.read(packetBuffer, numBytes); for (int i = 1; i <= numBytes; i++) { Serial.print(packetBuffer[i]); } } //Get the state of all four pins GPIO_2State = digitalRead(GPIO_2); //Toggle button for device 1 (LOW is pressed GPIO_0State = digitalRead(GPIO_0); //Toggle button for device 2 (LOW is pressed //If GPIO_2 & GPIO_0 are LOW then reset the ESP into AP mode if (GPIO_2State ==LOW && GPIO_0State == LOW) { Serial.println("Reset invoked"); WiFiManager wifiManager; // SPIFFS.format(); Serial.println("config formatted"); delay(1000); wifiManager.resetSettings(); delay(1000); //reset and try again, or maybe put it to deep sleep { ESP.reset(); delay(2000); } } } ```
kerem closed this issue 2026-02-28 01:25:26 +03:00
Author
Owner

@mrlightsman commented on GitHub (Mar 10, 2018):

Resolved. Simple atoi() did it.

<!-- gh-comment-id:371993336 --> @mrlightsman commented on GitHub (Mar 10, 2018): Resolved. Simple atoi() did it.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#461
No description provided.