| ... | ... |
@@ -1,24 +1,23 @@ |
| 1 |
+#include <FS.h> //this needs to be first, e folosit de salvarea credentialelor pe FS |
|
| 1 | 2 |
#include <ESP8266WiFi.h> |
| 2 | 3 |
#include <PubSubClient.h> |
| 3 | 4 |
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
| 5 |
+#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson pentru salvarea credentialelor in eprom |
|
| 4 | 6 |
#include <WiFiClient.h> |
| 5 | 7 |
#include <OneWire.h> |
| 6 | 8 |
#include <DallasTemperature.h> |
| 7 | 9 |
|
| 8 |
-// Modifica cu datele tale |
|
| 9 |
-const char* mqttUser = "rqx958t"; |
|
| 10 |
-const char* mqttPassword = "8219CHrqx958t"; |
|
| 11 |
-const char* mqttSUB = "cmnd/rqx958t-1460/ESP"; |
|
| 12 |
-const char* mqttESP = "stat/rqx958t-1460/ESP"; |
|
| 13 |
-const char* mqttTEMP = "stat/rqx958t-1460/TEMP"; |
|
| 14 |
-const char* mqttLWT = "tele/rqx958t-1460/LWT"; |
|
| 15 |
-const char* espName ="rqx958t-1460"; |
|
| 16 |
- |
|
| 17 |
-//####################################################################### |
|
| 18 |
-// NU EDITA MAI JOS |
|
| 19 |
-//####################################################################### |
|
| 10 |
+char mqtt_user[9]; //le va lua din FS |
|
| 11 |
+char mqtt_devid[5]; //le va lua din FS |
|
| 12 |
+char mqttPassword[15]; //urmatoarele le va genera cu functia gen |
|
| 13 |
+char mqttSUB[22]; |
|
| 14 |
+char espName[13]; |
|
| 15 |
+char mqttESP[22]; |
|
| 16 |
+char mqttTEMP[23]; |
|
| 17 |
+char mqttLWT[22]; |
|
| 18 |
+ |
|
| 20 | 19 |
const String model = "NodeMCU Dallas"; |
| 21 |
-const String ver = "v1.0.5"; |
|
| 20 |
+const String ver = "v2.0.0"; |
|
| 22 | 21 |
const char* mqttServer = "mqtt.clickhome.ro"; |
| 23 | 22 |
const int mqttPort = 1883; |
| 24 | 23 |
long loopTimer = 900000; // miliseconds - by default trimite la 15 minute |
| ... | ... |
@@ -37,6 +36,30 @@ DallasTemperature sensors(&oneWire); |
| 37 | 37 |
WiFiClient espClient; |
| 38 | 38 |
PubSubClient client(espClient); |
| 39 | 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 |
+} |
|
| 40 | 64 |
|
| 41 | 65 |
String ipToString(IPAddress ip){
|
| 42 | 66 |
String s=""; |
| ... | ... |
@@ -58,12 +81,18 @@ String getMacAddress() {
|
| 58 | 58 |
return cMac; |
| 59 | 59 |
} |
| 60 | 60 |
|
| 61 |
-void reconnect() {
|
|
| 62 |
- // Loop until we're reconnected |
|
| 61 |
+void reconectez() {
|
|
| 62 |
+ while (String(mqtt_user)==""){
|
|
| 63 |
+ Serial.println("Invalid user!");
|
|
| 64 |
+ delay(99999999); |
|
| 65 |
+ } |
|
| 66 |
+ |
|
| 67 |
+ // ma conectez la mqtt server |
|
| 63 | 68 |
while (!client.connected()) {
|
| 69 |
+ client.setServer(mqttServer, mqttPort); |
|
| 64 | 70 |
Serial.print("AtloopTempting MQTT connection...");
|
| 65 | 71 |
// Incerc sa ma reconectez cu LWT din 5 in 5 secunde |
| 66 |
- if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 72 |
+ if (client.connect(espName, mqtt_user, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 67 | 73 |
Serial.println("connected");
|
| 68 | 74 |
client.publish(mqttLWT,"Online",TRUE); |
| 69 | 75 |
// trimit informatii utile cand ma conectez |
| ... | ... |
@@ -88,7 +117,7 @@ void reconnect() {
|
| 88 | 88 |
Serial.print("failed, rc=");
|
| 89 | 89 |
Serial.print(client.state()); |
| 90 | 90 |
Serial.println(" try again in 60 seconds");
|
| 91 |
- // Wait 5 seconds before retrying |
|
| 91 |
+ // Wait 60 seconds before retrying |
|
| 92 | 92 |
delay(60000); |
| 93 | 93 |
} |
| 94 | 94 |
} |
| ... | ... |
@@ -97,50 +126,30 @@ void reconnect() {
|
| 97 | 97 |
void setup() |
| 98 | 98 |
{
|
| 99 | 99 |
Serial.begin(115200); |
| 100 |
+ //generez variabilele |
|
| 101 |
+ readFS(); |
|
| 102 |
+ // The extra parameters to be configured (can be either global or just in the setup) |
|
| 103 |
+ // After connecting, parameter.getValue() will get you the configured value |
|
| 104 |
+ // id/name placeholder/prompt default length |
|
| 105 |
+ |
|
| 106 |
+ WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 9);
|
|
| 107 |
+ WiFiManagerParameter custom_mqtt_devid("devid", "mqtt devid", mqtt_devid, 5);
|
|
| 100 | 108 |
|
| 101 | 109 |
// ma conectez la AP via wifi |
| 102 | 110 |
WiFiManager wifi; |
| 111 |
+ wifi.setConfigPortalTimeout(120); // a timeout so the ESP doesn't hang waiting to be configured, for instance after a power failure |
|
| 103 | 112 |
wifi.setTimeout(180); // sta AP 3 minute apoi se reseteaza din nou |
| 104 | 113 |
if (!wifi.autoConnect("ClickHome")) {
|
| 105 | 114 |
Serial.println("timeout - going to sleep");
|
| 106 | 115 |
} |
| 107 | 116 |
delay(200); |
| 108 |
- |
|
| 109 |
- // ma conectez la mqtt server |
|
| 110 |
- client.setServer(mqttServer, mqttPort); |
|
| 111 |
- while (!client.connected()) {
|
|
| 112 |
- Serial.println("Connecting to MQTT...");
|
|
| 113 |
- // fac connect cu LWT |
|
| 114 |
- if (client.connect(espName, mqttUser, mqttPassword, mqttLWT, 1, 1, "Offline")) {
|
|
| 115 |
- // trimit starea Online pe topic LWT cand ma conectez |
|
| 116 |
- client.publish(mqttLWT,"Online",TRUE); |
|
| 117 |
- // trimit informatii utile cand ma conectez |
|
| 118 |
- String esp_info = "{\"ESPMac\":\"";
|
|
| 119 |
- esp_info += getMacAddress(); |
|
| 120 |
- esp_info += "\", \"IPAddress\":\""; |
|
| 121 |
- esp_info += ipToString(WiFi.localIP()); |
|
| 122 |
- esp_info += "\"}"; |
|
| 123 |
- String netinfo = "{\"Module\":\"";
|
|
| 124 |
- netinfo += String (model); |
|
| 125 |
- netinfo += "\", \"Version\":\""; |
|
| 126 |
- netinfo += String (ver); |
|
| 127 |
- netinfo += "\"}"; |
|
| 128 |
- client.publish(mqttESP, netinfo.c_str(),TRUE); |
|
| 129 |
- Serial.println(netinfo); |
|
| 130 |
- client.publish(mqttESP, esp_info.c_str(),TRUE); |
|
| 131 |
- Serial.println(esp_info); |
|
| 132 |
- client.publish(mqttESP, "{\"LoopInterval\":\"15 minute\"}",TRUE);
|
|
| 133 |
- Serial.println("connected");
|
|
| 134 |
- client.subscribe(mqttSUB); |
|
| 135 |
- } |
|
| 136 |
- else {
|
|
| 137 |
- Serial.print("failed with state ");
|
|
| 138 |
- Serial.print(client.state()); |
|
| 139 |
- delay(2000); |
|
| 140 |
- } |
|
| 141 |
- } |
|
| 142 |
- // astept mesajul de la serverul MQTT |
|
| 143 |
- client.setCallback(getMessage); |
|
| 117 |
+ |
|
| 118 |
+ strcpy(mqtt_user, custom_mqtt_user.getValue()); //citesc datele salvate anterior pe FS |
|
| 119 |
+ strcpy(mqtt_devid, custom_mqtt_devid.getValue()); |
|
| 120 |
+ generate_vars(); //genereaza topicurile de mqtt in baza mqtt_user si mqtt_devid |
|
| 121 |
+ reconectez(); |
|
| 122 |
+ client.setCallback(getMessage); //setez functia care parseaza mesajele venite prin mqtt |
|
| 123 |
+ |
|
| 144 | 124 |
pinMode(inPin, INPUT); |
| 145 | 125 |
sensors.begin(); |
| 146 | 126 |
} |
| ... | ... |
@@ -204,10 +213,46 @@ void getMessage(char* topic, byte* payload, unsigned int length) {
|
| 204 | 204 |
} |
| 205 | 205 |
} |
| 206 | 206 |
|
| 207 |
+void readFS(){
|
|
| 208 |
+ //read configuration from FS json |
|
| 209 |
+ //Serial.println("mounting FS...");
|
|
| 210 |
+ |
|
| 211 |
+ if (SPIFFS.begin()) {
|
|
| 212 |
+ //Serial.println("mounted file system");
|
|
| 213 |
+ if (SPIFFS.exists("/config.json")) {
|
|
| 214 |
+ //file exists, reading and loading |
|
| 215 |
+ //Serial.println("reading config file");
|
|
| 216 |
+ File configFile = SPIFFS.open("/config.json", "r");
|
|
| 217 |
+ if (configFile) {
|
|
| 218 |
+ //Serial.println("opened config file");
|
|
| 219 |
+ |
|
| 220 |
+ size_t size = configFile.size(); |
|
| 221 |
+ // Allocate a buffer to store contents of the file. |
|
| 222 |
+ std::unique_ptr<char[]> buf(new char[size]); |
|
| 223 |
+ |
|
| 224 |
+ configFile.readBytes(buf.get(), size); |
|
| 225 |
+ DynamicJsonBuffer jsonBuffer; |
|
| 226 |
+ JsonObject& json = jsonBuffer.parseObject(buf.get()); |
|
| 227 |
+ //json.printTo(Serial); |
|
| 228 |
+ if (json.success()) {
|
|
| 229 |
+ //Serial.println("\nparsed json");
|
|
| 230 |
+ strcpy(mqtt_user, json["mqtt_user"]); |
|
| 231 |
+ strcpy(mqtt_devid, json["mqtt_devid"]); |
|
| 232 |
+ } else {
|
|
| 233 |
+ //Serial.println("failed to load json config");
|
|
| 234 |
+ } |
|
| 235 |
+ } |
|
| 236 |
+ } |
|
| 237 |
+ } else {
|
|
| 238 |
+ //Serial.println("failed to mount FS");
|
|
| 239 |
+ } |
|
| 240 |
+ //end read |
|
| 241 |
+} |
|
| 242 |
+ |
|
| 207 | 243 |
void loop() |
| 208 | 244 |
{
|
| 209 | 245 |
if (!client.connected()) {
|
| 210 |
- reconnect(); |
|
| 246 |
+ reconectez(); |
|
| 211 | 247 |
} |
| 212 | 248 |
client.loop(); |
| 213 | 249 |
long now = millis(); |