ESP 8266 Wifi Manager tzapu - using custom parameters in main code? how?

I’m trying to combine tzapu/WiFiManager to use with some other code… I’m using an example from the git hub that allows custom Parameters to be configured. I want to be able to be able to set the channel number from the wifi manager.

const int channel = 1

So assuming im changing the name of mqtt_port to channel in the below code for input. How do I set the const int channel to pull the number form char channel

Thanks for your help team!

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

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//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

//define your default values here, if there are different values in config.json, they are overwritten.
char mqtt_server[40];
char mqtt_port[6] = "8080";
char blynk_token[34] = "YOUR_BLYNK_TOKEN";

//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() {
  // put your setup code here, to run once:
  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(mqtt_server, json["mqtt_server"]);
          strcpy(mqtt_port, json["mqtt_port"]);
          strcpy(blynk_token, json["blynk_token"]);

        } 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_mqtt_server("server", "mqtt server", mqtt_server, 40);
  WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);

  //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);

  //set static ip
  wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
  
  //add all your parameters here
  wifiManager.addParameter(&custom_mqtt_server);
  wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_blynk_token);

  //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("AutoConnectAP", "password")) {
    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(mqtt_server, custom_mqtt_server.getValue());
  strcpy(mqtt_port, custom_mqtt_port.getValue());
  strcpy(blynk_token, custom_blynk_token.getValue());

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["mqtt_server"] = mqtt_server;
    json["mqtt_port"] = mqtt_port;
    json["blynk_token"] = blynk_token;

    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());

}

void loop() {
  // put your main code here, to run repeatedly:


}

my code

So I want to use the custom input parametr

How do I set the const int channel to pull the number form char channel

You don't know what the const keyword means, do you?

You get ONE opportunity to set the value of a const variable - when you declare it.

You can NOT set the value of a const "variable" based on something read from a file.

"char channel" doesn't appear anywhere in your code, nor does "const int channel". In fact, channel never appears in your code. So, it's really hard to understand what you are talking about.

Hi Paul

Very new to programming, and playing with a project while reading through books and tutorials.

So I need to change my const int to just int to start with?

The above code is an example from the git hub for WiFiManager with custom parameters.

I'm trying to edit the code just to have one input parameter called channel..

So
char mqtt_port[6] = "8080"; will become char channel [6] = "1234"

And for int channel to equal the value or char channel..

Does that make sense?

So I need to change my const int to just int to start with?

Maybe. Probably, even.

And for int channel to equal the value or char channel..

Does that make sense?

Yes, and no. It makes sense that you want to extract a value for the string channel from the configuration file. It makes sense that you want to convert the string to an int. atoi() does that.

It does not make sense that you want to have two variables called channel, of different types and sizes.

So yes I just want one variable called channel that can be defined using the wifi manager with configuration.

I’ve tried using atoi() - but I must be using it wrong.

I’m a newbie to this and I keep hitting a wall trying to combine the code.

This is my current sketch, it uses wifi manager to config the wifi, but now I’m trying to use the custom parameter sketch (Above) to configure the channel and universe value.

I would really appreciate it if someone can show me how to convert the char from above code to an int to be used in the main sketch.

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <E131.h> // Copyright (c) 2015 Shelby Merrick http://www.forkineye.com


//////////////////////////////////Configurating Stuff////////////////////////////////////////////
// select which pin will trigger the configuration portal when set to LOW
// ESP-01 users please note: the only pins available (0 and 2), are shared 
// with the bootloader, so always set them HIGH at power-up
#define TRIGGER_PIN 14

const int  universe = 1; // want to make this a custom parameter 
const int channel = 1;   //want to make this a custom parameter 


// channel number used by the output.
const int channel_1_red = channel + 0; // the channel number to link to output 1 red.
const int channel_1_green = channel + 1; // the channel number to link to output 1 green.
const int channel_1_blue = channel + 2; // the channel number to link to output 1 blue.
const int channel_1_white = channel + 3; // the channel number to link to output 1 white. 

// pin numbers to use as outputs.
const int output_1_red = 4; // the pin to use as output 1 red (D8).
const int output_1_green = 5; // the pin to use as output 1 green (D5).
const int output_1_blue = 12; // the pin to use as output 1 blue (D6).
const int output_1_white = 13; // the pin to use as output 1 white (D7). 

E131 e131;

void setup() {
  // set up code
  Serial.begin(115200);
  Serial.println("\n Starting");

  pinMode(TRIGGER_PIN, INPUT); //sets mode for reset pin

  // set the pins chosen above as outputs.
  pinMode(output_1_red, OUTPUT);
  pinMode(output_1_green, OUTPUT);
  pinMode(output_1_blue, OUTPUT);
  pinMode(output_1_white, OUTPUT);
  \
  
  // set the pins chosen above to low / off.
  analogWrite(output_1_red, 0);
  analogWrite(output_1_green, 0);
  analogWrite(output_1_blue, 0);
  analogWrite(output_1_white, 0);
 
  WiFiManager wifiManager;
  wifiManager.autoConnect("Pixie Setup ");
  wifiManager.setAPStaticIPConfig(IPAddress(192, 168, 1, 1), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));

   e131.beginMulticast(WiFi.SSID().c_str(), WiFi.psk().c_str(), universe); //pulls data from eprom and changes it to a str//thanks to pert on arduino forum for this one. 
}


void loop() {

    

    
  // is configuration portal requested?
  if ( digitalRead(TRIGGER_PIN) == HIGH ) {
    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;

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

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

    //it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration

    //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
    //WiFi.mode(WIFI_STA);
    
   // if (!wifiManager.startConfigPortal("OnDemandAP")) {
   if (!wifiManager.startConfigPortal("Fairy")) {
      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 :)");
  }


  // put your main code here, to run repeatedly:

    uint16_t num_channels = e131.parsePacket();

  /* Process channel data if we have it */
  if (num_channels) {
  Serial.println("receiving Sacn");

    Serial.println(e131.data[channel_1_red -1]);
    Serial.println(e131.data[channel_1_green -1]);
    Serial.println(e131.data[channel_1_blue -1]);
    Serial.println(e131.data[channel_1_white -1]);    
   
  // set the outputs to the data value.
  analogWrite(output_1_red, e131.data[channel_1_red -1] *4 );
  analogWrite(output_1_green, e131.data[channel_1_green -1] *4);
  analogWrite(output_1_blue, e131.data[channel_1_blue -1] *4 );
  analogWrite(output_1_white, e131.data[channel_1_white -1] *4 );


  }

} //loop

I'm trying to use the custom parameter sketch (Above) to configure the channel and universe value.

You haven't a hope in hell of assigning new values to channel and universe, since you made them const.

So yes I just want one variable called channel that can be defined using the wifi manager with configuration.

So, is it one - channel - or not one - channel and universe? I really don't care which it is, but, damn it, you need to be clear just what you are trying to do.

The const int channel_1_XXX variables clearly can't be const, either, since you want to configure channel, which they depend on. So, you need to assign them values AFTER you assign channel a value, which doesn't happen at compile time.

   e131.beginMulticast(WiFi.SSID().c_str(), WiFi.psk().c_str(), universe); //pulls data from eprom and changes it to a str//thanks to pert on arduino forum for this one.

The c_str() method does NOT get data from EEPROM. It doesn't change anything to anything else. It simply exposes the string that the String instance wraps.

void loop() {

   

   
  // is configuration portal requested?

I have never understood putting the { on the line with the function statement or the if statement or the for statement. The ONLY argument that makes any sense is that doing so saves on the number of lines in the program. Of course, you just shot that argument all to shit with all those useless blank lines.

Put EVERY { on line BY ITSELF. Use Tools + Auto Indent to properly indent your code.