replaced device_id with UUID
This commit is contained in:
parent
93561d4273
commit
5136a46197
@ -25,4 +25,4 @@ lib_deps =
|
|||||||
; MQTT@^2.4.7
|
; MQTT@^2.4.7
|
||||||
PubSubClient@^2.8
|
PubSubClient@^2.8
|
||||||
ArduinoNvs@^2.5
|
ArduinoNvs@^2.5
|
||||||
; ESPRandom@^1.3.3
|
ESPRandom@^1.3.3
|
||||||
@ -8,10 +8,14 @@ extern "C" {
|
|||||||
#include "freertos/timers.h"
|
#include "freertos/timers.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MQTT_VALVE_COMMAND MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/valve"
|
// #define MQTT_VALVE_COMMAND(device_id) (MQTT_TOPIC_BASE_SUB "/" device_id "/valve")
|
||||||
#define MQTT_SOIL_PROPERTIES MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/soil"
|
// #define MQTT_SOIL_PROPERTIES(device_id) (MQTT_TOPIC_BASE_SUB "/" device_id "/soil")
|
||||||
#define MQTT_LIGHT_PROPERTIES MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/light"
|
// #define MQTT_LIGHT_PROPERTIES(device_id) (MQTT_TOPIC_BASE_SUB "/" device_id "/light")
|
||||||
#define MQTT_AUTO_PROPERTIES MQTT_TOPIC_BASE_SUB "/" MQTT_DEVICE_ID "/automatic"
|
// #define MQTT_AUTO_PROPERTIES(device_id) (MQTT_TOPIC_BASE_SUB "/" device_id "/automatic")
|
||||||
|
char MQTT_VALVE_COMMAND[72];
|
||||||
|
char MQTT_SOIL_PROPERTIES[72];
|
||||||
|
char MQTT_LIGHT_PROPERTIES[72];
|
||||||
|
char MQTT_AUTO_PROPERTIES[72];
|
||||||
|
|
||||||
TimerHandle_t mqttReconnectTimer;
|
TimerHandle_t mqttReconnectTimer;
|
||||||
TimerHandle_t wifiReconnectTimer;
|
TimerHandle_t wifiReconnectTimer;
|
||||||
@ -56,7 +60,7 @@ void connectMQTT() {
|
|||||||
// mqttClient.begin(MQTT_HOST, MQTT_PORT, client);
|
// mqttClient.begin(MQTT_HOST, MQTT_PORT, client);
|
||||||
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
||||||
mqttClient.disconnect();
|
mqttClient.disconnect();
|
||||||
mqttClient.connect(MQTT_DEVICE_ID);
|
mqttClient.connect(getDeviceIDcharArr());
|
||||||
if (mqttClient.connected()) {
|
if (mqttClient.connected()) {
|
||||||
Serial.println("Connected!");
|
Serial.println("Connected!");
|
||||||
} else {
|
} else {
|
||||||
@ -161,7 +165,32 @@ void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void constructMQTTpaths() {
|
||||||
|
strcpy(MQTT_VALVE_COMMAND, MQTT_TOPIC_BASE_SUB "/");
|
||||||
|
strcat(MQTT_VALVE_COMMAND, getDeviceIDcharArr());
|
||||||
|
strcat(MQTT_VALVE_COMMAND, "/valve");
|
||||||
|
|
||||||
|
strcpy(MQTT_SOIL_PROPERTIES, MQTT_TOPIC_BASE_SUB "/");
|
||||||
|
strcat(MQTT_SOIL_PROPERTIES, getDeviceIDcharArr());
|
||||||
|
strcat(MQTT_SOIL_PROPERTIES, "/soil");
|
||||||
|
|
||||||
|
strcpy(MQTT_LIGHT_PROPERTIES, MQTT_TOPIC_BASE_SUB "/");
|
||||||
|
strcat(MQTT_LIGHT_PROPERTIES, getDeviceIDcharArr());
|
||||||
|
strcat(MQTT_LIGHT_PROPERTIES, "/light");
|
||||||
|
|
||||||
|
strcpy(MQTT_AUTO_PROPERTIES, MQTT_TOPIC_BASE_SUB "/");
|
||||||
|
strcat(MQTT_AUTO_PROPERTIES, getDeviceIDcharArr());
|
||||||
|
strcat(MQTT_AUTO_PROPERTIES, "/automatic");
|
||||||
|
|
||||||
|
Serial.println("MQTT_COMMANDS:");
|
||||||
|
Serial.println(MQTT_VALVE_COMMAND);
|
||||||
|
Serial.println(MQTT_SOIL_PROPERTIES);
|
||||||
|
Serial.println(MQTT_LIGHT_PROPERTIES);
|
||||||
|
Serial.println(MQTT_AUTO_PROPERTIES);
|
||||||
|
}
|
||||||
|
|
||||||
void setupConnections() {
|
void setupConnections() {
|
||||||
|
constructMQTTpaths();
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println();
|
Serial.println();
|
||||||
disableCore0WDT();
|
disableCore0WDT();
|
||||||
|
|||||||
@ -55,10 +55,8 @@
|
|||||||
// MQTT
|
// MQTT
|
||||||
#define MQTT_HOST "mqtt.timovolkmann.de"
|
#define MQTT_HOST "mqtt.timovolkmann.de"
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
#define MQTT_DEVICE_ID "esp-timo"
|
|
||||||
#define MQTT_TOPIC_BASE_SUB "smartgarden/commands"
|
#define MQTT_TOPIC_BASE_SUB "smartgarden/commands"
|
||||||
#define MQTT_TOPIC_BASE_PUB "smartgarden/updates"
|
#define MQTT_TOPIC_BASE_PUB "smartgarden/updates"
|
||||||
#define MQTT_PATH_PUB MQTT_TOPIC_BASE_PUB "/" MQTT_DEVICE_ID "/"
|
|
||||||
#define MQTT_PATH_SUB MQTT_TOPIC_BASE_SUB "/#"
|
#define MQTT_PATH_SUB MQTT_TOPIC_BASE_SUB "/#"
|
||||||
// MQTT_DEVICE_ID "/#"
|
// MQTT_DEVICE_ID "/#"
|
||||||
|
|
||||||
@ -106,5 +104,9 @@ void restoreAutoProps();
|
|||||||
void persistAutoProps(bool light, bool irrigation);
|
void persistAutoProps(bool light, bool irrigation);
|
||||||
void setAutoProperties(bool light, bool irrigation);
|
void setAutoProperties(bool light, bool irrigation);
|
||||||
|
|
||||||
|
void initDeviceID();
|
||||||
|
String getDeviceID();
|
||||||
|
const char *getDeviceIDcharArr();
|
||||||
|
|
||||||
void takeSemaphore();
|
void takeSemaphore();
|
||||||
void releaseSemaphore();
|
void releaseSemaphore();
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <ArduinoNvs.h>
|
#include <ArduinoNvs.h>
|
||||||
|
#include <ESPRandom.h>
|
||||||
#include <header.h>
|
#include <header.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
//using namespace std;
|
//using namespace std;
|
||||||
@ -9,7 +11,7 @@ extern "C" {
|
|||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MQTT_SENSOR_DATA MQTT_PATH_PUB "data"
|
char MQTT_SENSOR_DATA_TOPIC[64];
|
||||||
|
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
|
|
||||||
@ -24,6 +26,8 @@ int minimumLightValueLX = 50;
|
|||||||
// switches for automatic light and irrigation control
|
// switches for automatic light and irrigation control
|
||||||
bool automaticLight = true;
|
bool automaticLight = true;
|
||||||
bool automaticIrrigation = true;
|
bool automaticIrrigation = true;
|
||||||
|
// make sure device irrigates until fieldcapacity is reached
|
||||||
|
bool irrigateUntilFC = false;
|
||||||
|
|
||||||
void readSensors() {
|
void readSensors() {
|
||||||
Serial.println();
|
Serial.println();
|
||||||
@ -40,12 +44,7 @@ void readSensors() {
|
|||||||
int mstValue = readCapacitiveSoilMoistureSensor();
|
int mstValue = readCapacitiveSoilMoistureSensor();
|
||||||
Serial.print("Soil moisture: ");
|
Serial.print("Soil moisture: ");
|
||||||
Serial.println(mstValue);
|
Serial.println(mstValue);
|
||||||
// if (mstValue > fieldCapacity) {
|
|
||||||
// Serial.println("Field capacity reached. No irrigation needed. ");
|
|
||||||
// }
|
|
||||||
// if (mstValue < permanentWiltingPoint) {
|
|
||||||
// Serial.println("Permanent wilting point reached. Plant needs irrigation. ");
|
|
||||||
// }
|
|
||||||
if (automaticIrrigation) {
|
if (automaticIrrigation) {
|
||||||
toggleValve(true);
|
toggleValve(true);
|
||||||
}
|
}
|
||||||
@ -64,7 +63,7 @@ void readSensors() {
|
|||||||
|
|
||||||
doc["temperature"] = temperatureValue;
|
doc["temperature"] = temperatureValue;
|
||||||
serializeJson(doc, buffer);
|
serializeJson(doc, buffer);
|
||||||
publishMessage(MQTT_SENSOR_DATA, buffer);
|
publishMessage(MQTT_SENSOR_DATA_TOPIC, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool openValve() {
|
bool openValve() {
|
||||||
@ -83,16 +82,18 @@ bool closeValve() {
|
|||||||
|
|
||||||
void valveTask(void *parameter) {
|
void valveTask(void *parameter) {
|
||||||
takeSemaphore();
|
takeSemaphore();
|
||||||
bool isAutomatic = (bool)parameter;
|
bool isAutomatic = (bool) parameter;
|
||||||
Serial.println("Valve task triggered.");
|
Serial.print(isAutomatic);
|
||||||
|
Serial.println(" Valve task triggered.");
|
||||||
unsigned long valveTimeoutTimer = millis();
|
unsigned long valveTimeoutTimer = millis();
|
||||||
bool valveOpen = false;
|
bool valveOpen = false;
|
||||||
int initialSoilMoisture = readCapacitiveSoilMoistureSensor();
|
int initialSoilMoisture = readCapacitiveSoilMoistureSensor();
|
||||||
|
|
||||||
if (initialSoilMoisture > permanentWiltingPoint) {
|
if (initialSoilMoisture <= permanentWiltingPoint || irrigateUntilFC) {
|
||||||
Serial.println("Soil contains enough water. No irrigation needed. ");
|
|
||||||
} else {
|
|
||||||
valveOpen = openValve();
|
valveOpen = openValve();
|
||||||
|
irrigateUntilFC = true;
|
||||||
|
} else {
|
||||||
|
Serial.println("Soil contains enough water. No irrigation needed. ");
|
||||||
}
|
}
|
||||||
|
|
||||||
while (valveOpen) {
|
while (valveOpen) {
|
||||||
@ -102,6 +103,7 @@ void valveTask(void *parameter) {
|
|||||||
if (mstValue > fieldCapacity) { // && isAutomatic
|
if (mstValue > fieldCapacity) { // && isAutomatic
|
||||||
Serial.println("Field capacity reached. No irrigation needed. ");
|
Serial.println("Field capacity reached. No irrigation needed. ");
|
||||||
valveOpen = closeValve();
|
valveOpen = closeValve();
|
||||||
|
irrigateUntilFC = false;
|
||||||
}
|
}
|
||||||
if (millis() - valveTimeoutTimer >= MAX_VALVE_TIMEOUT) {
|
if (millis() - valveTimeoutTimer >= MAX_VALVE_TIMEOUT) {
|
||||||
Serial.println("Irrigation timeout reached. close valve. ");
|
Serial.println("Irrigation timeout reached. close valve. ");
|
||||||
@ -161,10 +163,18 @@ void restoreSoilProps() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setupStore() {
|
void setupStore() {
|
||||||
|
|
||||||
NVS.begin("store");
|
NVS.begin("store");
|
||||||
|
initDeviceID();
|
||||||
restoreSoilProps();
|
restoreSoilProps();
|
||||||
restoreLightProps();
|
restoreLightProps();
|
||||||
restoreAutoProps();
|
restoreAutoProps();
|
||||||
|
|
||||||
|
strcpy(MQTT_SENSOR_DATA_TOPIC, MQTT_TOPIC_BASE_PUB "/");
|
||||||
|
strcat(MQTT_SENSOR_DATA_TOPIC, getDeviceIDcharArr());
|
||||||
|
strcat(MQTT_SENSOR_DATA_TOPIC, "/data");
|
||||||
|
Serial.println("MQTT_SENSOR_DATA_TOPIC:");
|
||||||
|
Serial.println(MQTT_SENSOR_DATA_TOPIC);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSoilProperties(int FC, int PWP, int SAT) {
|
void setSoilProperties(int FC, int PWP, int SAT) {
|
||||||
@ -248,3 +258,29 @@ void setAutoProperties(bool light, bool irrigation) {
|
|||||||
Serial.println(automaticLight);
|
Serial.println(automaticLight);
|
||||||
Serial.println(automaticIrrigation);
|
Serial.println(automaticIrrigation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String DEVICE_ID = "";
|
||||||
|
|
||||||
|
void initDeviceID() {
|
||||||
|
DEVICE_ID = NVS.getString("UUID");
|
||||||
|
if (!DEVICE_ID.isEmpty()) {
|
||||||
|
Serial.print("Fetched Device ID from NVS: ");
|
||||||
|
Serial.println(DEVICE_ID);
|
||||||
|
} else {
|
||||||
|
uint8_t uuid[16];
|
||||||
|
ESPRandom::uuid(uuid);
|
||||||
|
DEVICE_ID = ESPRandom::uuidToString(uuid);
|
||||||
|
NVS.setString("UUID", DEVICE_ID);
|
||||||
|
NVS.commit();
|
||||||
|
Serial.print("New Device ID: ");
|
||||||
|
Serial.println(DEVICE_ID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String getDeviceID() {
|
||||||
|
return DEVICE_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* getDeviceIDcharArr() {
|
||||||
|
return DEVICE_ID.c_str();
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user