Browse code

invatam

Andrei Bucur authored on 01/03/2018 13:51:12
Showing 1 changed files
1 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
+