· 6 years ago · Feb 13, 2020, 02:58 PM
1#include <FS.h> //this needs to be first, or it all crashes and burns...
2
3#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
4
5//needed for library
6#include <DNSServer.h>
7#include <ESP8266WebServer.h>
8#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
9
10#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
11
12#define TRIGGER_PIN 0
13
14//define your default values here, if there are different values in config.json, they are overwritten.
15
16char api_key[7] = "TOKEN";
17
18//flag for saving data
19bool shouldSaveConfig = false;
20
21//callback notifying us of the need to save config
22void saveConfigCallback () {
23 Serial.println("Should save config");
24 shouldSaveConfig = true;
25}
26
27void setup() {
28 // put your setup code here, to run once:
29 Serial.begin(115200);
30 Serial.println();
31 pinMode(TRIGGER_PIN, INPUT);
32
33 if (SPIFFS.begin()) {
34 Serial.println("mounted file system");
35 if (SPIFFS.exists("/config.json")) {
36 //file exists, reading and loading
37 Serial.println("reading config file");
38 File configFile = SPIFFS.open("/config.json", "r");
39 if (configFile) {
40 Serial.println("opened config file");
41 size_t size = configFile.size();
42 // Allocate a buffer to store contents of the file.
43 std::unique_ptr<char[]> buf(new char[size]);
44 configFile.readBytes(buf.get(), size);
45 DynamicJsonBuffer jsonBuffer;
46 JsonObject& json = jsonBuffer.parseObject(buf.get());
47 json.printTo(Serial);
48 if (json.success()) {
49 Serial.println("\nparsed json");
50 strcpy(api_key, json["api_token"]);
51 } else {
52 Serial.println("failed to load json config");
53 }
54 configFile.close();
55 }
56 }
57 } else {
58 Serial.println("failed to mount FS");
59 }
60
61 WiFi.mode(WIFI_STA);
62
63 WiFiManagerParameter custom_api_key("key", "api key", api_key, 7);
64
65 WiFiManager wifiManager;
66 wifiManager.setSaveConfigCallback(saveConfigCallback);
67 wifiManager.addParameter(&custom_api_key);
68
69 if (!wifiManager.autoConnect("AutoConnectAP", "password")) {
70 Serial.println("failed to connect and hit timeout");
71 delay(3000);
72 //reset and try again, or maybe put it to deep sleep
73 ESP.reset();
74 delay(5000);
75 }
76
77 //if you get here you have connected to the WiFi
78 Serial.println("connected...yeey :)");
79
80 //read updated parameters
81 strcpy(api_key, custom_api_key.getValue());
82
83 //save the custom parameters to FS
84 if (shouldSaveConfig) {
85 Serial.println("saving config");
86 DynamicJsonBuffer jsonBuffer;
87 JsonObject& json = jsonBuffer.createObject();
88
89 json["api_key"] = api_key;
90
91 File configFile = SPIFFS.open("/config.json", "w");
92 if (!configFile) {
93 Serial.println("failed to open config file for writing");
94 }
95
96 json.printTo(Serial);
97 json.printTo(configFile);
98 configFile.close();
99 //end save
100 }
101
102 Serial.println(WiFi.localIP());
103
104}
105
106void loop() {
107 if ( digitalRead(TRIGGER_PIN) == LOW ) {
108 WiFi.mode(WIFI_AP_STA);
109 WiFi.softAP("Rsenso");
110 WiFiManager wifiManager;
111
112 if (!wifiManager.startConfigPortal()) {
113 Serial.println("failed to connect and hit timeout");
114 delay(3000);
115 //reset and try again, or maybe put it to deep sleep
116 ESP.reset();
117 delay(5000);
118 }
119}