[GH-ISSUE #759] No matter wo i did ,still get "invalid auth token" #634

Closed
opened 2026-02-28 01:26:19 +03:00 by kerem · 4 comments
Owner

Originally created by @giordanoshai on GitHub (Oct 26, 2018).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/759

Sparkfun esp8266 thing dev
WiFiManager 0.14.0

Here is the code:

#include <FS.h>                   //this needs to be first, or it all crashes and burns...
#define BLYNK_PRINT Serial
#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 <BlynkSimpleEsp8266.h>
#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 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(blynk_token, json["blynk_token"]);

        } else {
          Serial.println("failed to load json config");
        }
        configFile.close();
      }
    }
  } 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_blynk_token("blynk", "blynk token", blynk_token, 33);

  //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_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(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["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());
  Blynk.config (custom_blynk_token.getValue(), "cceaglobal.com", 8080);

}


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

}

And here is the serial

*WM: Request redirected to captive portal
*WM: Handle root
*WM: WiFi save
*WM: Parameter
*WM: blynk
*WM: a864011d64f3464b8738a84a1c5bbe9d
*WM: Sent wifi save page
*WM: Connecting to new AP
*WM: Connecting as wifi client...
*WM: Connection result: 
*WM: 3
Should save config
connected...yeey :)
saving config
failed to open config file for writing
{"blynk_token":"a864011d64f3464b8738a84a1c5bbe9d"}local ip
192.168.1.8
[66370] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.3 on Esp Thing Dev

  Give Blynk a Github star! => https://github.com/blynkkk/blynk-library

*WM: freeing allocated params!
[66395] Connecting to cceaglobal.com:8080
[66506] Invalid auth token
[71465] Connecting to cceaglobal.com:8080
[71556] Invalid auth token
[76513] Connecting to cceaglobal.com:8080
[76607] Invalid auth token
[81565] Connecting to cceaglobal.com:8080
[81652] Invalid auth token

If i replace the custom_blynk_token.getValue() to Bynk auth token a864011d64f3464b8738a84a1c5bbe9d, the program gose well.

Originally created by @giordanoshai on GitHub (Oct 26, 2018). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/759 Sparkfun esp8266 thing dev WiFiManager 0.14.0 ------------------------------------------------------------------------------------------------------------------- ### Here is the code: ``` #include <FS.h> //this needs to be first, or it all crashes and burns... #define BLYNK_PRINT Serial #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 <BlynkSimpleEsp8266.h> #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 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(blynk_token, json["blynk_token"]); } else { Serial.println("failed to load json config"); } configFile.close(); } } } 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_blynk_token("blynk", "blynk token", blynk_token, 33); //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_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(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["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()); Blynk.config (custom_blynk_token.getValue(), "cceaglobal.com", 8080); } void loop() { // put your main code here, to run repeatedly: Blynk.run(); } ``` ### And here is the serial ``` *WM: Request redirected to captive portal *WM: Handle root *WM: WiFi save *WM: Parameter *WM: blynk *WM: a864011d64f3464b8738a84a1c5bbe9d *WM: Sent wifi save page *WM: Connecting to new AP *WM: Connecting as wifi client... *WM: Connection result: *WM: 3 Should save config connected...yeey :) saving config failed to open config file for writing {"blynk_token":"a864011d64f3464b8738a84a1c5bbe9d"}local ip 192.168.1.8 [66370] ___ __ __ / _ )/ /_ _____ / /__ / _ / / // / _ \/ '_/ /____/_/\_, /_//_/_/\_\ /___/ v0.5.3 on Esp Thing Dev Give Blynk a Github star! => https://github.com/blynkkk/blynk-library *WM: freeing allocated params! [66395] Connecting to cceaglobal.com:8080 [66506] Invalid auth token [71465] Connecting to cceaglobal.com:8080 [71556] Invalid auth token [76513] Connecting to cceaglobal.com:8080 [76607] Invalid auth token [81565] Connecting to cceaglobal.com:8080 [81652] Invalid auth token ``` If i replace the `custom_blynk_token.getValue()` to Bynk auth token `a864011d64f3464b8738a84a1c5bbe9d,` the program gose well.
kerem closed this issue 2026-02-28 01:26:19 +03:00
Author
Owner

@tablatronix commented on GitHub (Oct 26, 2018):

You need to store the token yourself, amd get it yourself, once wm goes out of scope so do the params, so they are probably null

<!-- gh-comment-id:433570445 --> @tablatronix commented on GitHub (Oct 26, 2018): You need to store the token yourself, amd get it yourself, once wm goes out of scope so do the params, so they are probably null
Author
Owner

@tablatronix commented on GitHub (Oct 26, 2018):

The code copies it already into blynk_token

<!-- gh-comment-id:433570712 --> @tablatronix commented on GitHub (Oct 26, 2018): The code copies it already into blynk_token
Author
Owner

@giordanoshai commented on GitHub (Oct 27, 2018):

You need to store the token yourself, amd get it yourself, once wm goes out of scope so do the params, so they are probably null

and how to do that?
pls help!
thank you!

<!-- gh-comment-id:433624485 --> @giordanoshai commented on GitHub (Oct 27, 2018): > You need to store the token yourself, amd get it yourself, once wm goes out of scope so do the params, so they are probably null and how to do that? pls help! thank you!
Author
Owner

@tablatronix commented on GitHub (Oct 27, 2018):

Your using the wrong variable

<!-- gh-comment-id:433636978 --> @tablatronix commented on GitHub (Oct 27, 2018): Your using the wrong variable
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#634
No description provided.