-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWIFI_MANAGER.ino
138 lines (114 loc) · 5.16 KB
/
WIFI_MANAGER.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// https://github.com/tzapu/WiFiManager/blob/master/examples/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino
// https://github.com/tzapu/WiFiManager/issues/63
// the bottom two links still to be tested - migration from version 5 to 6 (I am running on V5)
// https://github.com/tzapu/WiFiManager,
// https://github.com/bblanchon/ArduinoJson/issues/1276
//...........................................................................................................................
//
//...........................................................................................................................
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
//...........................................................................................................................
//
//...........................................................................................................................
void WiFi_Manager(){
//clean FS, for testing
//SPIFFS.format();
//read configuration from FS json
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
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(blynk_token, json["blynk_token"]);
//strcpy(PushsaferKey, json["PushsaferKey"]);
} else {
Serial.println("failed to load json config");
}
configFile.close();
}
}
} else {
Serial.println("failed to mount FS");
}
//end read
// The extra parameters to be configured (can be either global or just in the setup)
// After connecting, parameter.getValue() will get you the configured value
// id/name placeholder/prompt default length
WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
// WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 32);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//set static ip
//wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
//add all your parameters here
wifiManager.addParameter(&custom_blynk_token);
//reset settings - for testing
// Press and hold the button to erase all the credentials
if (digitalRead(Flash_Button) == LOW) {
digitalWrite(Status_LED, HIGH); // set LED OFF
Serial.println("reset wifi and blynk token settings");
for(int i = 0; i < 35; i++)blynk_token[i] = 0;
wifiManager.resetSettings();
delay(500);
digitalWrite(Status_LED, LOW); // set LED ON
}
//set minimu quality of signal so it ignores AP's under that quality
//defaults to 8%
//wifiManager.setMinimumSignalQuality();
//Configuration Portal Timeout
//If you need to set a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure, you can add
wifiManager.setConfigPortalTimeout(120);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("Geyser Connect", "admin")) {
Serial.println("failed to connect after a power failure and timeout, reset and try again"); //on the second attampt your WiFi may now be connected to the internet
delay(3000);
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
//E2_write(Login_Set,1); //Valid Login
//read updated parameters
strcpy(blynk_token, custom_blynk_token.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["blynk_token"] = blynk_token;
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();
//end save
}
Serial.print("local ip = "); Serial.println(WiFi.localIP());
Serial.print("blynk_token = "); Serial.println(blynk_token);
}