O'Reilly logo

ESP8266 Home Automation Projects by Catalin Batrinu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required