123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- bool flag_SaveConfig = false;
- #define TRIGGER_PIN 12
- Ticker tkConfig ;
- bool flag_ConfigPortal = false;
- char UKI_NAME[40];
- char UKI_UDP_PORT[6] = "9000";
- char UKI_UDP_IP[34] = "192.168.10.100";
- void saveConfigCallback () {
- Serial.println("Should save config");
- flag_SaveConfig = true;
- }
- void CheckTriggerPin () {
- Serial.println("Config check");
- if ( digitalRead(TRIGGER_PIN) == LOW) {
- flag_ConfigPortal = true;
- }
- }
- void ReadConfig() {
-
-
- 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(UKI_NAME, json["UKI_NAME"]);
- strcpy(UKI_UDP_PORT, json["UKI_UDP_PORT"]);
- strcpy(UKI_UDP_IP, json["UKI_UDP_IP"]);
- } else {
- Serial.println("failed to load json config");
- }
- }
- }
- } else {
- Serial.println("failed to mount FS");
- }
-
- }
- void WriteConfig() {
-
- if (flag_SaveConfig) {
-
- flag_SaveConfig = false ;
- Serial.println("saving config");
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.createObject();
- json["UKI_NAME"] = UKI_NAME;
- json["UKI_UDP_PORT"] = UKI_UDP_PORT;
- json["UKI_UDP_IP"] = UKI_UDP_IP;
- 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();
-
- }
- }
- void StartConfigAP(){
-
- if (flag_ConfigPortal) {
-
- flag_ConfigPortal = false;
-
-
- redLedState (1, 500);
- blueLedState (1,500);
- detachOTA();
- tkConfig.detach();
- delay (500);
- ReadConfig() ;
-
-
-
-
-
-
- WiFiManagerParameter custom_UKI_NAME("name", "UKI NAME", UKI_NAME, 40);
- WiFiManagerParameter custom_UKI_UDP_PORT("port", "UKI udp port", UKI_UDP_PORT, 5);
- WiFiManagerParameter custom_UKI_UDP_IP("ip", "UKI udp IP", UKI_UDP_IP, 32);
-
-
- WiFiManager wifiManager;
-
-
- wifiManager.setSaveConfigCallback(saveConfigCallback);
-
- wifiManager.addParameter(&custom_UKI_NAME);
- wifiManager.addParameter(&custom_UKI_UDP_PORT);
- wifiManager.addParameter(&custom_UKI_UDP_IP);
-
-
-
- redLedState (0, 500);
- blueLedState (-1, 100);
- delay(1000);
- if (!wifiManager.startConfigPortal("ESP_UKI_AP")) {
- Serial.println("failed to connect, restarting");
-
- redLedState (-1, 100);
- blueLedState (-1, 100);
- ESP.reset();
- delay(3000);
- }
-
- Serial.println("connected to UKI wifi");
- blueLedState(1,500);
-
- strcpy(UKI_NAME, custom_UKI_NAME.getValue());
- strcpy(UKI_UDP_PORT, custom_UKI_UDP_PORT.getValue());
- strcpy(UKI_UDP_IP, custom_UKI_UDP_IP.getValue());
- WriteConfig();
-
-
-
- }
- }
- void setupWifi() {
- pinMode(TRIGGER_PIN, INPUT);
- tkConfig.attach(5, CheckTriggerPin);
- WiFi.begin();
-
-
-
- }
|