123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #include <FS.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <DNSServer.h>
- #include <WiFiManager.h>
- #include <ArduinoJson.h>
- bool flag_SaveConfig = false;
- #define TRIGGER_PIN 12
- Ticker tkConfig ;
- bool flag_ConfigPortal = false;
- int timeout = 30000;
- char ESP_NAME[40];
- char UDP_PORT[10] = "12345";
- char UDP_IP[16] = "239.0.0.57";
- void saveConfigCallback () {
- Serial.println("Should save config");
- flag_SaveConfig = true;
- }
- void CheckTriggerPin () {
-
- 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(ESP_NAME, json["ESP_NAME"]);
- strcpy(UDP_PORT, json["UDP_PORT"]);
- strcpy(UDP_IP, json["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["ESP_NAME"] = ESP_NAME;
- json["UDP_PORT"] = UDP_PORT;
- json["UDP_IP"] = 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();
- Serial.println();
-
- }
- }
- void StartConfigAP(){
-
- if (flag_ConfigPortal) {
-
- flag_ConfigPortal = false;
-
-
- redLedState (1, 500);
- blueLedState (1,500);
-
- tkConfig.detach();
- delay (500);
-
-
-
-
-
-
-
- WiFiManagerParameter custom_ESP_NAME("name", "ESP NAME", ESP_NAME, 40);
- WiFiManagerParameter custom_UDP_PORT("port", " UDP port", UDP_PORT, 10);
- WiFiManagerParameter custom_UDP_IP("ip", "UDP IP", UDP_IP, 32);
-
-
- WiFiManager wifiManager;
-
-
- wifiManager.setSaveConfigCallback(saveConfigCallback);
-
- wifiManager.addParameter(&custom_ESP_NAME);
- wifiManager.addParameter(&custom_UDP_PORT);
- wifiManager.addParameter(&custom_UDP_IP);
-
-
-
-
- delay(1000);
-
-
-
- redLedState (1, 100);
- blueLedState (-1, 100);
- if (!wifiManager.startConfigPortal(ESP_NAME)) {
- Serial.println("failed to connect, restarting config portal");
- delay(2000);
-
- redLedState (-1, 100);
- blueLedState (-1, 100);
- ESP.reset();
- delay(3000);
- }
-
-
-
- Serial.println("connected to wifi");
- blueLedState(0,500);
-
- strcpy(ESP_NAME, custom_ESP_NAME.getValue());
- strcpy(UDP_PORT, custom_UDP_PORT.getValue());
- strcpy(UDP_IP, custom_UDP_IP.getValue());
- WriteConfig();
- Serial.println("Restarting");
- delay(500);
- ESP.reset();
- delay(3000);
- }
- }
- void setupWifi() {
- pinMode(TRIGGER_PIN, INPUT_PULLUP);
- blueLedState (-1, 300);
-
- ReadConfig();
- Serial.print("Connecting");
- WiFi.mode(WIFI_STA);
- WiFi.begin("sniperAP","razorshark");
-
- int start_time = millis();
- while(WiFi.status()!=3){
- Serial.print(".");
- delay(1000);
- if (millis()-start_time > timeout) {
- Serial.println("timeout, starting config portal");
- flag_ConfigPortal = true;
- StartConfigAP();
- }
- }
- Serial.println();
- Serial.print("connected to ");
- Serial.println(WiFi.SSID());
- blueLedState(1,500);
-
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- tkConfig.attach(5, CheckTriggerPin);
-
-
-
-
-
- }
|