| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,159 +0,0 @@ |
| 1 |
-#include <FS.h> //this needs to be first, or it all crashes and burns... |
|
| 2 |
-#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino |
|
| 3 |
-#include <DNSServer.h> |
|
| 4 |
-#include <ESP8266WebServer.h> |
|
| 5 |
-#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
|
| 6 |
-#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson pentru salvarea credentialelor in eprom |
|
| 7 |
- |
|
| 8 |
- |
|
| 9 |
- |
|
| 10 |
-const char* comanda="save"; //valori posibile: save, erase |
|
| 11 |
-char* set_mqtt_user="exwrgq"; |
|
| 12 |
-char* set_mqtt_devid="2638"; |
|
| 13 |
- |
|
| 14 |
- |
|
| 15 |
- |
|
| 16 |
-/******************************* |
|
| 17 |
-* NU EDITA MAI JOS * |
|
| 18 |
-*******************************/ |
|
| 19 |
- |
|
| 20 |
-char mqtt_user[9]; |
|
| 21 |
-char mqtt_devid[5]; |
|
| 22 |
- |
|
| 23 |
-void setup() {
|
|
| 24 |
- Serial.begin(115200); |
|
| 25 |
- |
|
| 26 |
- //clean FS, for testing |
|
| 27 |
- if (comanda == "erase"){
|
|
| 28 |
- SPIFFS.format(); |
|
| 29 |
- Serial.println("am formatat !");
|
|
| 30 |
- delay(1000000); |
|
| 31 |
- } |
|
| 32 |
- readFS(); |
|
| 33 |
- |
|
| 34 |
- // The extra parameters to be configured (can be either global or just in the setup) |
|
| 35 |
- // After connecting, parameter.getValue() will get you the configured value |
|
| 36 |
- // id/name placeholder/prompt default length |
|
| 37 |
- WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 9);
|
|
| 38 |
- WiFiManagerParameter custom_mqtt_devid("devid", "mqtt devid", mqtt_devid, 5);
|
|
| 39 |
- |
|
| 40 |
- //WiFiManager |
|
| 41 |
- //Local intialization. Once its business is done, there is no need to keep it around |
|
| 42 |
- WiFiManager wifiManager; |
|
| 43 |
- |
|
| 44 |
- //set config save notify callback |
|
| 45 |
- //wifiManager.setSaveConfigCallback(saveConfigCallback); |
|
| 46 |
- |
|
| 47 |
- //add all your parameters here |
|
| 48 |
- //sters wifiManager.addParameter(&custom_mqtt_user); //apare campul in interfata web alaturi de SSID si pass de la WIfi |
|
| 49 |
- //sters wifiManager.addParameter(&custom_mqtt_devid); |
|
| 50 |
- |
|
| 51 |
- //reset settings - for testing |
|
| 52 |
- //wifiManager.resetSettings(); |
|
| 53 |
- |
|
| 54 |
- //set minimu quality of signal so it ignores AP's under that quality |
|
| 55 |
- //defaults to 8% |
|
| 56 |
- //wifiManager.setMinimumSignalQuality(); |
|
| 57 |
- |
|
| 58 |
- //sets timeout until configuration portal gets turned off |
|
| 59 |
- //useful to make it all retry or go to sleep |
|
| 60 |
- //in seconds |
|
| 61 |
- //wifiManager.setTimeout(120); |
|
| 62 |
- |
|
| 63 |
- //fetches ssid and pass and tries to connect |
|
| 64 |
- //if it does not connect it starts an access point with the specified name |
|
| 65 |
- //here "AutoConnectAP" |
|
| 66 |
- //and goes into a blocking loop awaiting configuration |
|
| 67 |
- /* |
|
| 68 |
- if (!wifiManager.autoConnect("ClickHOME")) {
|
|
| 69 |
- Serial.println("failed to connect and hit timeout");
|
|
| 70 |
- delay(3000); |
|
| 71 |
- //reset and try again, or maybe put it to deep sleep |
|
| 72 |
- ESP.reset(); |
|
| 73 |
- delay(5000); |
|
| 74 |
- } |
|
| 75 |
- */ |
|
| 76 |
- |
|
| 77 |
- //read updated parameters |
|
| 78 |
- strcpy(mqtt_user, custom_mqtt_user.getValue()); |
|
| 79 |
- strcpy(mqtt_devid, custom_mqtt_devid.getValue()); |
|
| 80 |
- |
|
| 81 |
- Serial.print("setup read updated mqtt_user:");
|
|
| 82 |
- Serial.print(mqtt_user); |
|
| 83 |
- Serial.println("read mqtt_devid:");
|
|
| 84 |
- Serial.println(mqtt_devid); |
|
| 85 |
- |
|
| 86 |
- if (comanda == "save"){
|
|
| 87 |
- strcpy(mqtt_user, set_mqtt_user); |
|
| 88 |
- strcpy(mqtt_devid, set_mqtt_devid); |
|
| 89 |
- saveFS(); |
|
| 90 |
- } |
|
| 91 |
-} |
|
| 92 |
- |
|
| 93 |
-void saveFS(){
|
|
| 94 |
- //save the custom parameters to FS |
|
| 95 |
- Serial.println("saving config");
|
|
| 96 |
- DynamicJsonBuffer jsonBuffer; |
|
| 97 |
- JsonObject& json = jsonBuffer.createObject(); |
|
| 98 |
- json["mqtt_user"] = mqtt_user; |
|
| 99 |
- json["mqtt_devid"] = mqtt_devid; |
|
| 100 |
- |
|
| 101 |
- File configFile = SPIFFS.open("/config.json", "w");
|
|
| 102 |
- if (!configFile) {
|
|
| 103 |
- Serial.println("failed to open config file for writing");
|
|
| 104 |
- } |
|
| 105 |
- |
|
| 106 |
- json.printTo(Serial); |
|
| 107 |
- json.printTo(configFile); |
|
| 108 |
- configFile.close(); |
|
| 109 |
- //end save |
|
| 110 |
-} |
|
| 111 |
- |
|
| 112 |
-void readFS(){
|
|
| 113 |
- //read configuration from FS json |
|
| 114 |
- Serial.println("mounting FS...");
|
|
| 115 |
- |
|
| 116 |
- if (SPIFFS.begin()) {
|
|
| 117 |
- Serial.println("mounted file system");
|
|
| 118 |
- if (SPIFFS.exists("/config.json")) {
|
|
| 119 |
- //file exists, reading and loading |
|
| 120 |
- Serial.println("reading config file");
|
|
| 121 |
- File configFile = SPIFFS.open("/config.json", "r");
|
|
| 122 |
- if (configFile) {
|
|
| 123 |
- Serial.println("opened config file");
|
|
| 124 |
- |
|
| 125 |
- size_t size = configFile.size(); |
|
| 126 |
- // Allocate a buffer to store contents of the file. |
|
| 127 |
- std::unique_ptr<char[]> buf(new char[size]); |
|
| 128 |
- |
|
| 129 |
- configFile.readBytes(buf.get(), size); |
|
| 130 |
- DynamicJsonBuffer jsonBuffer; |
|
| 131 |
- JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 132 |
- json.printTo(Serial); |
|
| 133 |
- if (json.success()) {
|
|
| 134 |
- Serial.println("\nparsed json");
|
|
| 135 |
- |
|
| 136 |
- strcpy(mqtt_user, json["mqtt_user"]); |
|
| 137 |
- strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 138 |
- |
|
| 139 |
- Serial.print("read from FS updated mqtt_user:");
|
|
| 140 |
- Serial.println(mqtt_user); |
|
| 141 |
- Serial.print("read mqtt_devid:");
|
|
| 142 |
- Serial.println(mqtt_devid); |
|
| 143 |
- |
|
| 144 |
- |
|
| 145 |
- } else {
|
|
| 146 |
- Serial.println("failed to load json config");
|
|
| 147 |
- } |
|
| 148 |
- } |
|
| 149 |
- } |
|
| 150 |
- } else {
|
|
| 151 |
- Serial.println("failed to mount FS");
|
|
| 152 |
- } |
|
| 153 |
- //end read |
|
| 154 |
-} |
|
| 155 |
- |
|
| 156 |
-void loop() {
|
|
| 157 |
- Serial.println("gata!");
|
|
| 158 |
- delay(100000); |
|
| 159 |
-} |
| 160 | 1 |
new file mode 100755 |
| ... | ... |
@@ -0,0 +1,116 @@ |
| 0 |
+#include <FS.h> |
|
| 1 |
+#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino |
|
| 2 |
+#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson |
|
| 3 |
+ |
|
| 4 |
+//needed for library |
|
| 5 |
+#include <ESP8266WebServer.h> |
|
| 6 |
+#include <DNSServer.h> |
|
| 7 |
+ |
|
| 8 |
+// Constants |
|
| 9 |
+const int TRIGGER_PIN = 0; // D3 on NodeMCU and WeMos. |
|
| 10 |
+ |
|
| 11 |
+// Variables |
|
| 12 |
+const char* set_mqtt_user="exwrgq"; |
|
| 13 |
+const char* set_mqtt_devid="2638"; |
|
| 14 |
+ |
|
| 15 |
+// Default configuration values |
|
| 16 |
+char mqtt_user[9]; //le va lua din FS |
|
| 17 |
+char mqtt_devid[5]; //le va lua din FS |
|
| 18 |
+ |
|
| 19 |
+// Function Prototypes |
|
| 20 |
+bool readConfigFile(); |
|
| 21 |
+bool writeConfigFile(); |
|
| 22 |
+ |
|
| 23 |
+// Setup function |
|
| 24 |
+void setup() {
|
|
| 25 |
+ // Put your setup code here, to run once |
|
| 26 |
+ Serial.begin(115200); |
|
| 27 |
+ Serial.println("\n Starting");
|
|
| 28 |
+ |
|
| 29 |
+ // Initialize trigger pins |
|
| 30 |
+ pinMode(TRIGGER_PIN, INPUT_PULLUP); |
|
| 31 |
+ |
|
| 32 |
+ // Mount the filesystem |
|
| 33 |
+ bool result = SPIFFS.begin(); |
|
| 34 |
+ //Serial.println("SPIFFS opened: " + result);
|
|
| 35 |
+ |
|
| 36 |
+ if (readConfigFile()) {
|
|
| 37 |
+ Serial.print("User:");
|
|
| 38 |
+ Serial.println(mqtt_user); |
|
| 39 |
+ Serial.print("devid:");
|
|
| 40 |
+ Serial.println(mqtt_devid); |
|
| 41 |
+ } |
|
| 42 |
+ //WiFi.printDiag(Serial); //Remove this line if you do not want to see WiFi password printed |
|
| 43 |
+ |
|
| 44 |
+} |
|
| 45 |
+ |
|
| 46 |
+// Loop function |
|
| 47 |
+ |
|
| 48 |
+void loop() {
|
|
| 49 |
+ // is configuration portal requested? |
|
| 50 |
+ if (digitalRead(TRIGGER_PIN) == LOW) {
|
|
| 51 |
+ Serial.println("Configuration portal requested");
|
|
| 52 |
+ writeConfigFile(); |
|
| 53 |
+ Serial.println("writeconfigfile...done");
|
|
| 54 |
+ } |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+bool readConfigFile() {
|
|
| 58 |
+ // this opens the config file in read-mode |
|
| 59 |
+ File f = SPIFFS.open("/config.json", "r");
|
|
| 60 |
+ |
|
| 61 |
+ if (!f) {
|
|
| 62 |
+ Serial.println("Configuration file not found");
|
|
| 63 |
+ return false; |
|
| 64 |
+ } else {
|
|
| 65 |
+ // we could open the file |
|
| 66 |
+ size_t size = f.size(); |
|
| 67 |
+ // Allocate a buffer to store contents of the file. |
|
| 68 |
+ std::unique_ptr<char[]> buf(new char[size]); |
|
| 69 |
+ |
|
| 70 |
+ // Read and store file contents in buf |
|
| 71 |
+ f.readBytes(buf.get(), size); |
|
| 72 |
+ // Closing file |
|
| 73 |
+ f.close(); |
|
| 74 |
+ // Using dynamic JSON buffer which is not the recommended memory model, but anyway |
|
| 75 |
+ // See https://github.com/bblanchon/ArduinoJson/wiki/Memory%20model |
|
| 76 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 77 |
+ // Parse JSON string |
|
| 78 |
+ JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 79 |
+ // Test if parsing succeeds. |
|
| 80 |
+ if (!json.success()) {
|
|
| 81 |
+ Serial.println("JSON parseObject() failed");
|
|
| 82 |
+ return false; |
|
| 83 |
+ } |
|
| 84 |
+ json.printTo(Serial); |
|
| 85 |
+ strcpy(mqtt_user, json["mqtt_user"]); |
|
| 86 |
+ strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 87 |
+ } |
|
| 88 |
+ return true; |
|
| 89 |
+} |
|
| 90 |
+ |
|
| 91 |
+bool writeConfigFile() {
|
|
| 92 |
+ Serial.println("Saving config file");
|
|
| 93 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 94 |
+ JsonObject& json = jsonBuffer.createObject(); |
|
| 95 |
+ |
|
| 96 |
+ // JSONify local configuration parameters |
|
| 97 |
+ json["mqtt_user"] = set_mqtt_user; |
|
| 98 |
+ json["mqtt_devid"] = set_mqtt_devid; |
|
| 99 |
+ |
|
| 100 |
+ // Open file for writing |
|
| 101 |
+ File f = SPIFFS.open("/config.json", "w");
|
|
| 102 |
+ if (!f) {
|
|
| 103 |
+ Serial.println("Failed to open config file for writing");
|
|
| 104 |
+ return false; |
|
| 105 |
+ } |
|
| 106 |
+ |
|
| 107 |
+ json.prettyPrintTo(Serial); |
|
| 108 |
+ // Write data to file and close it |
|
| 109 |
+ json.printTo(f); |
|
| 110 |
+ f.close(); |
|
| 111 |
+ |
|
| 112 |
+ Serial.println("\nConfig file was successfully saved");
|
|
| 113 |
+ return true; |
|
| 114 |
+} |
|
| 115 |
+ |