Hi,
I'm using a NodeMCU board to try and build an Instagram tracker.
I want to be able to setup the connection and username parameters at any given time so besides having wifimanager load up at startup if there are no credentials stored I want to have a setup button.
I've managed to get that done but for I cannot figure out how to get this on demand portal to save the additional username parameter.
What am I missing here?
Would appreciate any help
Elan
// requires the following libraries, search in Library Manager or download from github:
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include "InstagramStats.h" // https://github.com/witnessmenow/arduino-instagram-stats
#include "JsonStreamingParser.h" // https://github.com/squix78/json-streaming-parser
// these libraries are included with ESP8266 support
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
// For storing configurations
#include "FS.h"
// WiFiManager Libraries
#include <DNSServer.h> //Local DNS Server used for redirecting all rs to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#define resetConfigPin D4 //When high will manually reset the wifi manager config
WiFiClientSecure secureClient;
InstagramStats instaStats(secureClient);
unsigned long api_delay = 5 * 60000; //time between api requests (1mins)
unsigned long api_due_time;
char channelId[30]; //
int IGcounter = 0;
byte digits[] = {0, 0, 0, 0, 0, 0, 0, 0}; // an array of led digit data to be written to the display
// 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);
pinMode(resetConfigPin, INPUT);
if (!SPIFFS.begin()) {
Serial.println("Failed to mount FS");
return;
}
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
loadConfig();
WiFiManager wifiManager;
wifiManager.setSaveConfigCallback(saveConfigCallback);
WiFiManagerParameter customChannelId("channelId", "User Name", channelId, 35);
wifiManager.addParameter(&customChannelId);
// If it fails to connect it will create an Insta-Counter access point
wifiManager.autoConnect("Insta-Counter");
strcpy(channelId, customChannelId.getValue());
if (shouldSaveConfig)
{
saveConfig();
}
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
// Force Config mode if there is no channel key
if (strcmp(channelId, "") <= 0)
{
Serial.println("Forcing Config Mode");
forceConfigMode();
}
delay(500);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
// secureClient.setInsecure(); // needed for ESP8266 board version above 2.4.2
}
void loop() {
if ( digitalRead(resetConfigPin) == LOW )
{
reloadConfigPortal();
}
if (millis() > api_due_time) {
getInstagramStatsForUser();
api_due_time = millis() + api_delay;
}
}
void getInstagramStatsForUser()
{
String userName = channelId;
Serial.println("Getting instagram user stats for " + userName );
InstagramUserStats response = instaStats.getUserStats(userName);
Serial.println("Response:");
Serial.print("Number of followers: ");
Serial.println(response.followedByCount);
IGcounter = response.followedByCount;
}
bool loadConfig()
{
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (!json.success()) {
Serial.println("Failed to parse config file");
return false;
}
strcpy(channelId, json["channelId"]);
return true;
}
bool saveConfig()
{
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["channelId"] = channelId;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
return false;
}
json.printTo(configFile);
return true;
}
void forceConfigMode()
{
Serial.println("Reset");
WiFi.disconnect();
Serial.println("Dq");
delay(500);
ESP.restart();
delay(5000);
}
void reloadConfigPortal()
{
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
SPIFFS.remove("/config.json"); //delete the custom parameters saved to SPIFFS
WiFiManager wifiManager;
wifiManager.setSaveConfigCallback(saveConfigCallback);
WiFiManagerParameter customChannelId("channelId", "User Name", channelId, 35);
wifiManager.addParameter(&customChannelId);
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(120);
strcpy(channelId, customChannelId.getValue());
WiFi.disconnect();
// wifiManager.resetSettings();
wifiManager.autoConnect("Insta-Counter");
if (shouldSaveConfig)
{
Serial.println("save config on demand");
saveConfig();
}
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
ESP.restart();
}