| 1 | 1 |
old mode 100644 |
| 2 | 2 |
new mode 100755 |
| ... | ... |
@@ -19,7 +19,7 @@ char mqttTEMP[23]; |
| 19 | 19 |
char mqttLWT[22]; |
| 20 | 20 |
|
| 21 | 21 |
const String model = "NodeMCU Dallas"; |
| 22 |
-const String ver = "v2.0.1"; |
|
| 22 |
+const String ver = "v2.1.0"; |
|
| 23 | 23 |
const char* mqttServer = "mqtt.clickhome.ro"; |
| 24 | 24 |
const int mqttPort = 1883; |
| 25 | 25 |
long loopTimer = 900000; // miliseconds - by default trimite la 15 minute |
| ... | ... |
@@ -37,6 +37,8 @@ DallasTemperature sensors(&oneWire); |
| 37 | 37 |
|
| 38 | 38 |
WiFiClient espClient; |
| 39 | 39 |
PubSubClient client(espClient); |
| 40 |
+bool readConfigFile(); |
|
| 41 |
+ |
|
| 40 | 42 |
|
| 41 | 43 |
void generate_vars(){
|
| 42 | 44 |
strcpy (mqttPassword, "8219CH"); |
| ... | ... |
@@ -124,19 +126,18 @@ void reconectez() {
|
| 124 | 124 |
} |
| 125 | 125 |
} |
| 126 | 126 |
} |
| 127 |
- |
|
| 127 |
+ |
|
| 128 | 128 |
void setup() |
| 129 | 129 |
{
|
| 130 | 130 |
Serial.begin(115200); |
| 131 |
- //generez variabilele |
|
| 132 |
- readFS(); |
|
| 133 |
- // The extra parameters to be configured (can be either global or just in the setup) |
|
| 134 |
- // After connecting, parameter.getValue() will get you the configured value |
|
| 135 |
- // id/name placeholder/prompt default length |
|
| 136 |
- |
|
| 137 |
- WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 9);
|
|
| 138 |
- WiFiManagerParameter custom_mqtt_devid("devid", "mqtt devid", mqtt_devid, 5);
|
|
| 139 | 131 |
|
| 132 |
+ // Mount the filesystem |
|
| 133 |
+ bool result = SPIFFS.begin(); |
|
| 134 |
+ //Serial.println("SPIFFS opened: " + result);
|
|
| 135 |
+ |
|
| 136 |
+ readConfigFile(); //citesc user si devid din memorie |
|
| 137 |
+ generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 138 |
+ |
|
| 140 | 139 |
// ma conectez la AP via wifi |
| 141 | 140 |
WiFiManager wifi; |
| 142 | 141 |
wifi.setConfigPortalTimeout(120); // a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure |
| ... | ... |
@@ -146,9 +147,6 @@ void setup() |
| 146 | 146 |
} |
| 147 | 147 |
delay(200); |
| 148 | 148 |
|
| 149 |
- strcpy(mqtt_user, custom_mqtt_user.getValue()); //citesc datele salvate anterior pe FS |
|
| 150 |
- strcpy(mqtt_devid, custom_mqtt_devid.getValue()); |
|
| 151 |
- generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 152 | 149 |
reconectez(); |
| 153 | 150 |
client.setCallback(getMessage); //setez functia care parseaza mesajele venite prin mqtt |
| 154 | 151 |
|
| ... | ... |
@@ -248,42 +246,41 @@ void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 248 | 248 |
} |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 |
-void readFS(){
|
|
| 252 |
- //read configuration from FS json |
|
| 253 |
- //Serial.println("mounting FS...");
|
|
| 254 |
- |
|
| 255 |
- if (SPIFFS.begin()) {
|
|
| 256 |
- //Serial.println("mounted file system");
|
|
| 257 |
- if (SPIFFS.exists("/config.json")) {
|
|
| 258 |
- //file exists, reading and loading |
|
| 259 |
- //Serial.println("reading config file");
|
|
| 260 |
- File configFile = SPIFFS.open("/config.json", "r");
|
|
| 261 |
- if (configFile) {
|
|
| 262 |
- //Serial.println("opened config file");
|
|
| 263 |
- |
|
| 264 |
- size_t size = configFile.size(); |
|
| 265 |
- // Allocate a buffer to store contents of the file. |
|
| 266 |
- std::unique_ptr<char[]> buf(new char[size]); |
|
| 251 |
+bool readConfigFile() {
|
|
| 252 |
+ // this opens the config file in read-mode |
|
| 253 |
+ File f = SPIFFS.open("/config.json", "r");
|
|
| 254 |
+ |
|
| 255 |
+ if (!f) {
|
|
| 256 |
+ Serial.println("Configuration file not found");
|
|
| 257 |
+ return false; |
|
| 258 |
+ } else {
|
|
| 259 |
+ // we could open the file |
|
| 260 |
+ size_t size = f.size(); |
|
| 261 |
+ // Allocate a buffer to store contents of the file. |
|
| 262 |
+ std::unique_ptr<char[]> buf(new char[size]); |
|
| 267 | 263 |
|
| 268 |
- configFile.readBytes(buf.get(), size); |
|
| 269 |
- DynamicJsonBuffer jsonBuffer; |
|
| 270 |
- JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 271 |
- //json.printTo(Serial); |
|
| 272 |
- if (json.success()) {
|
|
| 273 |
- //Serial.println("\nparsed json");
|
|
| 274 |
- strcpy(mqtt_user, json["mqtt_user"]); |
|
| 275 |
- strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 276 |
- } else {
|
|
| 277 |
- //Serial.println("failed to load json config");
|
|
| 278 |
- } |
|
| 279 |
- } |
|
| 264 |
+ // Read and store file contents in buf |
|
| 265 |
+ f.readBytes(buf.get(), size); |
|
| 266 |
+ // Closing file |
|
| 267 |
+ f.close(); |
|
| 268 |
+ // Using dynamic JSON buffer which is not the recommended memory model, but anyway |
|
| 269 |
+ // See https://github.com/bblanchon/ArduinoJson/wiki/Memory%20model |
|
| 270 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 271 |
+ // Parse JSON string |
|
| 272 |
+ JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 273 |
+ // Test if parsing succeeds. |
|
| 274 |
+ if (!json.success()) {
|
|
| 275 |
+ Serial.println("JSON parseObject() failed");
|
|
| 276 |
+ return false; |
|
| 280 | 277 |
} |
| 281 |
- } else {
|
|
| 282 |
- //Serial.println("failed to mount FS");
|
|
| 278 |
+ json.printTo(Serial); |
|
| 279 |
+ strcpy(mqtt_user, json["mqtt_user"]); |
|
| 280 |
+ strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 283 | 281 |
} |
| 284 |
- //end read |
|
| 282 |
+ return true; |
|
| 285 | 283 |
} |
| 286 | 284 |
|
| 285 |
+ |
|
| 287 | 286 |
void loop() |
| 288 | 287 |
{
|
| 289 | 288 |
if (!client.connected()) {
|
| ... | ... |
@@ -306,3 +303,4 @@ void loop() |
| 306 | 306 |
} |
| 307 | 307 |
} |
| 308 | 308 |
} |
| 309 |
+ |