| 1 | 1 |
old mode 100644 |
| 2 | 2 |
new mode 100755 |
| ... | ... |
@@ -1,159 +1,107 @@ |
| 1 |
-#include <FS.h> //this needs to be first, or it all crashes and burns... |
|
| 1 |
+#include <FS.h> |
|
| 2 | 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"; |
|
| 3 |
+#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson |
|
| 13 | 4 |
|
| 5 |
+//needed for library |
|
| 6 |
+#include <ESP8266WebServer.h> |
|
| 7 |
+#include <DNSServer.h> |
|
| 14 | 8 |
|
| 9 |
+// Variables |
|
| 10 |
+const char* set_mqtt_user="exwrgq"; |
|
| 11 |
+const char* set_mqtt_devid="2638"; |
|
| 15 | 12 |
|
| 16 |
-/******************************* |
|
| 17 |
-* NU EDITA MAI JOS * |
|
| 18 |
-*******************************/ |
|
| 13 |
+// Default configuration values |
|
| 14 |
+char mqtt_user[9]; //le va lua din FS |
|
| 15 |
+char mqtt_devid[5]; //le va lua din FS |
|
| 19 | 16 |
|
| 20 |
-char mqtt_user[9]; |
|
| 21 |
-char mqtt_devid[5]; |
|
| 17 |
+// Function Prototypes |
|
| 18 |
+bool readConfigFile(); |
|
| 19 |
+bool writeConfigFile(); |
|
| 22 | 20 |
|
| 21 |
+// Setup function |
|
| 23 | 22 |
void setup() {
|
| 23 |
+ // Put your setup code here, to run once |
|
| 24 | 24 |
Serial.begin(115200); |
| 25 |
+ Serial.println("\n Starting");
|
|
| 25 | 26 |
|
| 26 |
- //clean FS, for testing |
|
| 27 |
- if (comanda == "erase"){
|
|
| 28 |
- SPIFFS.format(); |
|
| 29 |
- Serial.println("am formatat !");
|
|
| 30 |
- delay(1000000); |
|
| 31 |
- } |
|
| 32 |
- readFS(); |
|
| 27 |
+ // Mount the filesystem |
|
| 28 |
+ bool result = SPIFFS.begin(); |
|
| 29 |
+ //Serial.println("SPIFFS opened: " + result);
|
|
| 33 | 30 |
|
| 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);
|
|
| 31 |
+ if (readConfigFile()) {
|
|
| 32 |
+ Serial.print("User:");
|
|
| 33 |
+ Serial.println(mqtt_user); |
|
| 34 |
+ Serial.print("devid:");
|
|
| 35 |
+ Serial.println(mqtt_devid); |
|
| 36 |
+ } |
|
| 37 |
+ //WiFi.printDiag(Serial); //Remove this line if you do not want to see WiFi password printed |
|
| 39 | 38 |
|
| 40 |
- //WiFiManager |
|
| 41 |
- //Local intialization. Once its business is done, there is no need to keep it around |
|
| 42 |
- WiFiManager wifiManager; |
|
| 39 |
+} |
|
| 43 | 40 |
|
| 44 |
- //set config save notify callback |
|
| 45 |
- //wifiManager.setSaveConfigCallback(saveConfigCallback); |
|
| 41 |
+// Loop function |
|
| 46 | 42 |
|
| 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(); |
|
| 43 |
+void loop() {
|
|
| 44 |
+ // is configuration portal requested? |
|
| 45 |
+ writeConfigFile(); |
|
| 46 |
+ delay(9999999); |
|
| 47 |
+} |
|
| 53 | 48 |
|
| 54 |
- //set minimu quality of signal so it ignores AP's under that quality |
|
| 55 |
- //defaults to 8% |
|
| 56 |
- //wifiManager.setMinimumSignalQuality(); |
|
| 49 |
+bool readConfigFile() {
|
|
| 50 |
+ // this opens the config file in read-mode |
|
| 51 |
+ File f = SPIFFS.open("/config.json", "r");
|
|
| 57 | 52 |
|
| 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(); |
|
| 53 |
+ if (!f) {
|
|
| 54 |
+ Serial.println("Configuration file not found");
|
|
| 55 |
+ return false; |
|
| 56 |
+ } else {
|
|
| 57 |
+ // we could open the file |
|
| 58 |
+ size_t size = f.size(); |
|
| 59 |
+ // Allocate a buffer to store contents of the file. |
|
| 60 |
+ std::unique_ptr<char[]> buf(new char[size]); |
|
| 61 |
+ |
|
| 62 |
+ // Read and store file contents in buf |
|
| 63 |
+ f.readBytes(buf.get(), size); |
|
| 64 |
+ // Closing file |
|
| 65 |
+ f.close(); |
|
| 66 |
+ // Using dynamic JSON buffer which is not the recommended memory model, but anyway |
|
| 67 |
+ // See https://github.com/bblanchon/ArduinoJson/wiki/Memory%20model |
|
| 68 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 69 |
+ // Parse JSON string |
|
| 70 |
+ JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 71 |
+ // Test if parsing succeeds. |
|
| 72 |
+ if (!json.success()) {
|
|
| 73 |
+ Serial.println("JSON parseObject() failed");
|
|
| 74 |
+ return false; |
|
| 75 |
+ } |
|
| 76 |
+ json.printTo(Serial); |
|
| 77 |
+ strcpy(mqtt_user, json["mqtt_user"]); |
|
| 78 |
+ strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 90 | 79 |
} |
| 80 |
+ return true; |
|
| 91 | 81 |
} |
| 92 | 82 |
|
| 93 |
-void saveFS(){
|
|
| 94 |
- //save the custom parameters to FS |
|
| 95 |
- Serial.println("saving config");
|
|
| 83 |
+bool writeConfigFile() {
|
|
| 84 |
+ Serial.println("Saving config file");
|
|
| 96 | 85 |
DynamicJsonBuffer jsonBuffer; |
| 97 | 86 |
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 | 87 |
|
| 106 |
- json.printTo(Serial); |
|
| 107 |
- json.printTo(configFile); |
|
| 108 |
- configFile.close(); |
|
| 109 |
- //end save |
|
| 110 |
-} |
|
| 88 |
+ // JSONify local configuration parameters |
|
| 89 |
+ json["mqtt_user"] = set_mqtt_user; |
|
| 90 |
+ json["mqtt_devid"] = set_mqtt_devid; |
|
| 111 | 91 |
|
| 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");
|
|
| 92 |
+ // Open file for writing |
|
| 93 |
+ File f = SPIFFS.open("/config.json", "w");
|
|
| 94 |
+ if (!f) {
|
|
| 95 |
+ Serial.println("Failed to open config file for writing");
|
|
| 96 |
+ return false; |
|
| 152 | 97 |
} |
| 153 |
- //end read |
|
| 154 |
-} |
|
| 155 | 98 |
|
| 156 |
-void loop() {
|
|
| 157 |
- Serial.println("gata!");
|
|
| 158 |
- delay(100000); |
|
| 99 |
+ json.prettyPrintTo(Serial); |
|
| 100 |
+ // Write data to file and close it |
|
| 101 |
+ json.printTo(f); |
|
| 102 |
+ f.close(); |
|
| 103 |
+ |
|
| 104 |
+ Serial.println("\nConfig file was successfully saved");
|
|
| 105 |
+ return true; |
|
| 159 | 106 |
} |
| 107 |
+ |
| 160 | 108 |
deleted file mode 100644 |
| ... | ... |
@@ -1,291 +0,0 @@ |
| 1 |
-#include <FS.h> // librarie folosita de salvarea credentialelor pe FS |
|
| 2 |
-#include <ESP8266WiFi.h> |
|
| 3 |
-#include <PubSubClient.h> |
|
| 4 |
-#include <WiFiManager.h> // librarie folosita pentru salvarea credentialelor WIFI in eprom |
|
| 5 |
-#include <ArduinoJson.h> // librarie folosita pentru salvarea credentialelor MQTT in eprom |
|
| 6 |
-#include <WiFiClient.h> |
|
| 7 |
-#include <OneWire.h> |
|
| 8 |
-#include <DallasTemperature.h> |
|
| 9 |
-#include <ESP8266HTTPClient.h> |
|
| 10 |
-#include <ESP8266httpUpdate.h> |
|
| 11 |
- |
|
| 12 |
-// variabilele user si devid sunt incarcate din eprom folosind FS |
|
| 13 |
-char mqtt_user[9]; |
|
| 14 |
-char mqtt_devid[5]; |
|
| 15 |
- |
|
| 16 |
-// variabile generate cu functia generate_vars, folosind user si devid |
|
| 17 |
-char mqttPassword[15]; |
|
| 18 |
-char mqttSUB[22]; |
|
| 19 |
-char espName[13]; |
|
| 20 |
-char mqttESP[22]; |
|
| 21 |
-char mqttTEMP[23]; |
|
| 22 |
-char mqttLWT[22]; |
|
| 23 |
- |
|
| 24 |
-const String model = "NodeMCU Dallas"; |
|
| 25 |
-const String ver = "v2.0.2"; |
|
| 26 |
-const char* mqttServer = "mqtt.clickhome.ro"; |
|
| 27 |
-const int mqttPort = 1883; |
|
| 28 |
-long loopTimer = 900000; // by default trimite temperatura la 15 minute |
|
| 29 |
-long lastMsg = 0; |
|
| 30 |
-float loopTemp = 0; |
|
| 31 |
-int inPin = 5; |
|
| 32 |
-String mqttMessage; |
|
| 33 |
- |
|
| 34 |
-#define ONE_WIRE_BUS D1 // pinul de date folosit la nodeMCU este D1 |
|
| 35 |
-OneWire oneWire(ONE_WIRE_BUS); |
|
| 36 |
-DallasTemperature sensors(&oneWire); |
|
| 37 |
-WiFiClient espClient; |
|
| 38 |
-PubSubClient client(espClient); |
|
| 39 |
- |
|
| 40 |
-void generate_vars() {
|
|
| 41 |
- strcpy (mqttPassword, "8219CH"); |
|
| 42 |
- strcat (mqttPassword, mqtt_user); |
|
| 43 |
- |
|
| 44 |
- strcpy (espName, mqtt_user); |
|
| 45 |
- strcat (espName, "-"); |
|
| 46 |
- strcat (espName, mqtt_devid); |
|
| 47 |
- |
|
| 48 |
- strcpy (mqttSUB, "cmnd/"); |
|
| 49 |
- strcat (mqttSUB, espName); |
|
| 50 |
- strcat (mqttSUB, "/ESP"); |
|
| 51 |
- |
|
| 52 |
- strcpy (mqttESP, "stat/"); |
|
| 53 |
- strcat (mqttESP, espName); |
|
| 54 |
- strcat (mqttESP, "/ESP"); |
|
| 55 |
- |
|
| 56 |
- strcpy (mqttTEMP, "stat/"); |
|
| 57 |
- strcat (mqttTEMP, espName); |
|
| 58 |
- strcat (mqttTEMP, "/TEMP"); |
|
| 59 |
- |
|
| 60 |
- strcpy (mqttLWT, "tele/"); |
|
| 61 |
- strcat (mqttLWT, espName); |
|
| 62 |
- strcat (mqttLWT, "/LWT"); |
|
| 63 |
-} |
|
| 64 |
- |
|
| 65 |
-String ipToString(IPAddress ip) {
|
|
| 66 |
- String s=""; |
|
| 67 |
- for (int i=0; i<4; i++) |
|
| 68 |
- s += i ? "." + String(ip[i]) : String(ip[i]); |
|
| 69 |
- return s; |
|
| 70 |
-} |
|
| 71 |
- |
|
| 72 |
-String getMacAddress() {
|
|
| 73 |
- byte mac[6]; |
|
| 74 |
- WiFi.macAddress(mac); |
|
| 75 |
- String cMac = ""; |
|
| 76 |
- for (int i = 0; i < 6; ++i) {
|
|
| 77 |
- cMac += String(mac[i],HEX); |
|
| 78 |
- if(i<5) |
|
| 79 |
- cMac += "-"; |
|
| 80 |
- } |
|
| 81 |
- cMac.toUpperCase(); |
|
| 82 |
- return cMac; |
|
| 83 |
-} |
|
| 84 |
- |
|
| 85 |
-void reconectez() {
|
|
| 86 |
- // daca nu am credentialele MQTT, nu fac nimic, delay 1000 de ore |
|
| 87 |
- while (String(mqtt_user)=="") {
|
|
| 88 |
- Serial.println("User MQTT invalid!");
|
|
| 89 |
- delay(3600000000); |
|
| 90 |
- } |
|
| 91 |
- // ma conectez la mqtt server |
|
| 92 |
- while (!client.connected()) {
|
|
| 93 |
- client.setServer(mqttServer, mqttPort); |
|
| 94 |
- Serial.print("AtloopTempting MQTT connection...");
|
|
| 95 |
- // Incerc sa ma reconectez cu LWT din 5 in 5 secunde |
|
| 96 |
- if (client.connect(espName, mqtt_user, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 97 |
- Serial.println("connected");
|
|
| 98 |
- client.publish(mqttLWT,"Online",TRUE); |
|
| 99 |
- // trimit informatii utile cand ma conectez |
|
| 100 |
- String esp_info = " {\"ESPMac\":\"";
|
|
| 101 |
- esp_info += getMacAddress(); |
|
| 102 |
- esp_info += "\", \"IPAddress\":\""; |
|
| 103 |
- esp_info += ipToString(WiFi.localIP()); |
|
| 104 |
- esp_info += "\"}"; |
|
| 105 |
- String netinfo = " {\"Module\":\"";
|
|
| 106 |
- netinfo += String (model); |
|
| 107 |
- netinfo += "\", \"Version\":\""; |
|
| 108 |
- netinfo += String (ver); |
|
| 109 |
- netinfo += "\"}"; |
|
| 110 |
- |
|
| 111 |
- client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 112 |
- Serial.println(netinfo); |
|
| 113 |
- client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 114 |
- Serial.println(esp_info); |
|
| 115 |
- client.publish(mqttESP, "15 minute",TRUE); // trimit intervalul default |
|
| 116 |
- client.subscribe(mqttSUB); |
|
| 117 |
- } |
|
| 118 |
- else {
|
|
| 119 |
- Serial.print("failed, rc=");
|
|
| 120 |
- Serial.print(client.state()); |
|
| 121 |
- Serial.println("Incerc din nou in 60 de secunde");
|
|
| 122 |
- delay(60000); |
|
| 123 |
- } |
|
| 124 |
- } |
|
| 125 |
-} |
|
| 126 |
- |
|
| 127 |
-void setup() {
|
|
| 128 |
- Serial.begin(115200); |
|
| 129 |
- //generez variabilele |
|
| 130 |
- readFS(); |
|
| 131 |
- WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 9);
|
|
| 132 |
- WiFiManagerParameter custom_mqtt_devid("devid", "mqtt devid", mqtt_devid, 5);
|
|
| 133 |
- // ma conectez la AP via wifi |
|
| 134 |
- WiFiManager wifi; |
|
| 135 |
- // ESP-ul asteapta ca sa fie configurat 2 minute |
|
| 136 |
- wifi.setConfigPortalTimeout(120); |
|
| 137 |
- // sta AP 3 minute apoi se reseteaza din nou |
|
| 138 |
- wifi.setTimeout(180); |
|
| 139 |
- if (!wifi.autoConnect("ClickHome")) {
|
|
| 140 |
- Serial.println("timeout - going to sleep"); // EU aici nu am inteles ce se intampla ... poate ii punem un delay!!
|
|
| 141 |
- } |
|
| 142 |
- delay(200); |
|
| 143 |
- //citesc datele salvate anterior pe FS |
|
| 144 |
- strcpy(mqtt_user, custom_mqtt_user.getValue()); |
|
| 145 |
- strcpy(mqtt_devid, custom_mqtt_devid.getValue()); |
|
| 146 |
- //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 147 |
- generate_vars(); |
|
| 148 |
- reconectez(); |
|
| 149 |
- //setez functia care parseaza mesajele venite prin mqtt |
|
| 150 |
- client.setCallback(getMessage); |
|
| 151 |
- pinMode(inPin, INPUT); |
|
| 152 |
- sensors.begin(); |
|
| 153 |
-} |
|
| 154 |
- |
|
| 155 |
-void getMessage(char* topic, byte* payload, unsigned int length) {
|
|
| 156 |
- float t = 0; |
|
| 157 |
- mqttMessage=""; |
|
| 158 |
- Serial.print("Mesaj primit pe topicul: ");
|
|
| 159 |
- Serial.println(topic); |
|
| 160 |
- Serial.print("Mesaj:");
|
|
| 161 |
- for (int i = 0; i < length; i++) {
|
|
| 162 |
- mqttMessage += (char)payload[i]; |
|
| 163 |
- } |
|
| 164 |
- Serial.println(mqttMessage); |
|
| 165 |
- |
|
| 166 |
- // daca primeste 'temp' pe mqtt, trimite temperatura |
|
| 167 |
- if (mqttMessage == "temp") {
|
|
| 168 |
- Serial.println("Trimit temperatura");
|
|
| 169 |
- sensors.setResolution(12); |
|
| 170 |
- sensors.requestTemperatures(); // Send the command to get Temperatures |
|
| 171 |
- t = sensors.getTempCByIndex(0); |
|
| 172 |
- String json = " {\"Temp\":";
|
|
| 173 |
- json += String (t); |
|
| 174 |
- json += "}"; |
|
| 175 |
- client.publish(mqttTEMP, json.c_str(),TRUE); |
|
| 176 |
- Serial.println(json); |
|
| 177 |
- } |
|
| 178 |
- |
|
| 179 |
- // daca primeste 'reset' pe mqtt, isi da reset |
|
| 180 |
- if (mqttMessage == "reset") {
|
|
| 181 |
- String lastwords="Am fost resetat ..."; |
|
| 182 |
- client.publish(mqttESP, lastwords.c_str(), TRUE); |
|
| 183 |
- delay (3000); |
|
| 184 |
- ESP.reset(); |
|
| 185 |
- delay (5000); |
|
| 186 |
- } |
|
| 187 |
- |
|
| 188 |
- // daca primeste 'update' isi face software update via WEB |
|
| 189 |
- if (mqttMessage == "update") {
|
|
| 190 |
- String msg="Software update: "; |
|
| 191 |
- t_httpUpdate_return ret; |
|
| 192 |
- //ESPhttpUpdate.rebootOnUpdate(false); |
|
| 193 |
- ret = ESPhttpUpdate.update("http://update.clickhome.ro/senzor/dallas/arduino.bin");
|
|
| 194 |
- //ret = ESPhttpUpdate.update("update.clickhome.ro", 80, "/senzor/dallas/arduino.bin");
|
|
| 195 |
- switch(ret) {
|
|
| 196 |
- case HTTP_UPDATE_FAILED: |
|
| 197 |
- msg.concat(" eroare:");
|
|
| 198 |
- msg.concat(ESPhttpUpdate.getLastError()); |
|
| 199 |
- msg.concat(" motiv:");
|
|
| 200 |
- msg.concat(ESPhttpUpdate.getLastErrorString().c_str()); |
|
| 201 |
- break; |
|
| 202 |
- case HTTP_UPDATE_NO_UPDATES: |
|
| 203 |
- msg.concat(" no update.");
|
|
| 204 |
- break; |
|
| 205 |
- case HTTP_UPDATE_OK: |
|
| 206 |
- msg.concat(" success.");
|
|
| 207 |
- break; |
|
| 208 |
- } |
|
| 209 |
- Serial.println(msg); |
|
| 210 |
- } |
|
| 211 |
- |
|
| 212 |
- // daca primeste valoarea in minute seteaza loopTimer si-l trimite in DB |
|
| 213 |
- if (mqttMessage == "debug") {
|
|
| 214 |
- loopTimer=30000; |
|
| 215 |
- client.publish(mqttESP, " {\"LoopInterval\":\"30 secunde\"}",TRUE);
|
|
| 216 |
- } |
|
| 217 |
- if (mqttMessage == "10") {
|
|
| 218 |
- loopTimer=600000; |
|
| 219 |
- client.publish(mqttESP, " {\"LoopInterval\":\"10 minute\"}",TRUE);
|
|
| 220 |
- } |
|
| 221 |
- if (mqttMessage == "15") {
|
|
| 222 |
- loopTimer=900000; |
|
| 223 |
- client.publish(mqttESP, " {\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 224 |
- } |
|
| 225 |
- if (mqttMessage == "30") {
|
|
| 226 |
- loopTimer=1800000; |
|
| 227 |
- client.publish(mqttESP, " {\"LoopInterval\":\"30 de minute\"}",TRUE);
|
|
| 228 |
- } |
|
| 229 |
- if (mqttMessage == "60") {
|
|
| 230 |
- loopTimer=3600000; |
|
| 231 |
- client.publish(mqttESP, " {\"LoopInterval\":\"o ora\"}",TRUE);
|
|
| 232 |
- } |
|
| 233 |
- if (mqttMessage == "stop") {
|
|
| 234 |
- loopTimer=3600000000; // setez sa nu trimita valori, 1000 de ore |
|
| 235 |
- client.publish(mqttESP, " {\"LoopInterval\":\"oprit\"}",TRUE);
|
|
| 236 |
- } |
|
| 237 |
-} |
|
| 238 |
- |
|
| 239 |
-void readFS() {
|
|
| 240 |
- if (SPIFFS.begin()) {
|
|
| 241 |
- //Serial.println("mounted file system");
|
|
| 242 |
- if (SPIFFS.exists("/config.json")) {
|
|
| 243 |
- //file exists, reading and loading |
|
| 244 |
- //Serial.println("reading config file");
|
|
| 245 |
- File configFile = SPIFFS.open("/config.json", "r");
|
|
| 246 |
- if (configFile) {
|
|
| 247 |
- //Serial.println("opened config file");
|
|
| 248 |
- size_t size = configFile.size(); |
|
| 249 |
- // Allocate a buffer to store contents of the file. |
|
| 250 |
- std::unique_ptr<char[]> buf(new char[size]); |
|
| 251 |
- configFile.readBytes(buf.get(), size); |
|
| 252 |
- DynamicJsonBuffer jsonBuffer; |
|
| 253 |
- JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 254 |
- //json.printTo(Serial); |
|
| 255 |
- if (json.success()) {
|
|
| 256 |
- //Serial.println("\nparsed json");
|
|
| 257 |
- strcpy(mqtt_user, json["mqtt_user"]); |
|
| 258 |
- strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 259 |
- } |
|
| 260 |
- else {
|
|
| 261 |
- //Serial.println("failed to load json config");
|
|
| 262 |
- } |
|
| 263 |
- } |
|
| 264 |
- } |
|
| 265 |
- } |
|
| 266 |
- else {
|
|
| 267 |
- //Serial.println("failed to mount FS");
|
|
| 268 |
- } |
|
| 269 |
-} |
|
| 270 |
- |
|
| 271 |
-void loop() {
|
|
| 272 |
- if (!client.connected()) {
|
|
| 273 |
- reconectez(); |
|
| 274 |
- } |
|
| 275 |
- client.loop(); |
|
| 276 |
- long now = millis(); |
|
| 277 |
- if (now - lastMsg > loopTimer) {
|
|
| 278 |
- lastMsg = now; |
|
| 279 |
- sensors.setResolution(12); |
|
| 280 |
- sensors.requestTemperatures(); |
|
| 281 |
- loopTemp = sensors.getTempCByIndex(0); |
|
| 282 |
- Serial.println(loopTemp); |
|
| 283 |
- if((loopTemp > -20) && (loopTemp <60)) {
|
|
| 284 |
- String val = " {\"Temp\":";
|
|
| 285 |
- val += String (loopTemp); |
|
| 286 |
- val += "}"; |
|
| 287 |
- client.publish(mqttESP, val.c_str(),TRUE); |
|
| 288 |
- } |
|
| 289 |
- } |
|
| 290 |
-} |
|
| 291 |
- |