123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #include <FS.h>
- #include <WiFiManager.h>
- #include <ArduinoJson.h>
- #ifdef ESP32
- #include <SPIFFS.h>
- #endif
- char mqtt_server[40];
- char mqtt_port[6] = "8080";
- char api_token[32] = "YOUR_API_TOKEN";
- char static_ip[16] = "10.0.1.56";
- char static_gw[16] = "10.0.1.1";
- char static_sn[16] = "255.255.255.0";
- bool shouldSaveConfig = false;
- void saveConfigCallback () {
- Serial.println("Should save config");
- shouldSaveConfig = true;
- }
- void setupSpiffs(){
-
-
-
- Serial.println("mounting FS...");
- if (SPIFFS.begin()) {
- Serial.println("mounted file system");
- if (SPIFFS.exists("/config.json")) {
-
- Serial.println("reading config file");
- File configFile = SPIFFS.open("/config.json", "r");
- if (configFile) {
- Serial.println("opened config file");
- size_t size = configFile.size();
-
- 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(api_token, json["api_token"]);
-
-
-
-
-
-
-
-
-
- } else {
- Serial.println("failed to load json config");
- }
- }
- }
- } else {
- Serial.println("failed to mount FS");
- }
-
- }
- void setup() {
-
- Serial.begin(115200);
- Serial.println();
- setupSpiffs();
-
- WiFiManager wm;
-
- wm.setSaveConfigCallback(saveConfigCallback);
-
-
-
-
-
- WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
- WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
- WiFiManagerParameter custom_api_token("api", "api token", "", 32);
-
- wm.addParameter(&custom_mqtt_server);
- wm.addParameter(&custom_mqtt_port);
- wm.addParameter(&custom_api_token);
-
-
-
-
-
-
-
-
-
-
-
-
- if (!wm.autoConnect("AutoConnectAP", "password")) {
- Serial.println("failed to connect and hit timeout");
- delay(3000);
-
- ESP.restart();
- delay(5000);
- }
-
-
-
-
- Serial.println("connected...yeey :)");
-
- strcpy(mqtt_server, custom_mqtt_server.getValue());
- strcpy(mqtt_port, custom_mqtt_port.getValue());
- strcpy(api_token, custom_api_token.getValue());
-
- if (shouldSaveConfig) {
- Serial.println("saving config");
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.createObject();
- json["mqtt_server"] = mqtt_server;
- json["mqtt_port"] = mqtt_port;
- json["api_token"] = api_token;
-
-
-
- File configFile = SPIFFS.open("/config.json", "w");
- if (!configFile) {
- Serial.println("failed to open config file for writing");
- }
- json.prettyPrintTo(Serial);
- json.printTo(configFile);
- configFile.close();
-
- shouldSaveConfig = false;
- }
- Serial.println("local ip");
- Serial.println(WiFi.localIP());
- Serial.println(WiFi.gatewayIP());
- Serial.println(WiFi.subnetMask());
- }
- void loop() {
-
- }
|