Adding parameters to the WiFiManager setup page and saving them into the file

So far, the only parameters saved from the WiFiManager page were the SSID and password for your network. Let's add other parameters such as the MQTT server, port, user, password and topic and save them to a configuration file located in the SPIFFS:

#include <FS.h> 
#include <ESP8266WiFi.h> 
#include <PubSubClient.h> 
#include <DNSServer.h> 
#include <ESP8266WebServer.h> 
#include <WiFiManager.h> 
#include <ArduinoJson.h> 

Define the CLIENT_ID that will be used to connect to the MQTT broker later:

#define CLIENT_ID         "ESP_%06X" 

Define your default values here; if there are different values in config.json, they are overwritten:

char mqtt_server[40]; char mqtt_port[6] = "1883"; ...

Get ESP8266 Home Automation Projects now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.