switched mqtt client; implemented persistence
This commit is contained in:
parent
861d8b527b
commit
a6fbbfab73
@ -21,6 +21,7 @@ lib_deps =
|
||||
AutoConnect@^1.1.7
|
||||
AsyncMqttClient@^0.8.2
|
||||
ArduinoJson@^6.15.2
|
||||
MQTT@^2.4.7
|
||||
; PubSubClient@^2.8
|
||||
; MQTT@^2.4.7
|
||||
PubSubClient@^2.8
|
||||
ArduinoNvs@^2.5
|
||||
; ESPRandom@^1.3.3
|
||||
@ -1,5 +1,6 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <MQTT.h>
|
||||
// #include <MQTT.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <header.h>
|
||||
|
||||
extern "C" {
|
||||
@ -23,7 +24,8 @@ WiFiClient client;
|
||||
AutoConnectConfig Config;
|
||||
// Config.autoReconnect = true;
|
||||
// WiFi.config(Config);
|
||||
MQTTClient mqttClient;
|
||||
// MQTTClient mqttClient;
|
||||
PubSubClient mqttClient(client);
|
||||
|
||||
void connectWiFi() {
|
||||
Serial.println("Start WiFi...");
|
||||
@ -33,15 +35,15 @@ void connectWiFi() {
|
||||
}
|
||||
|
||||
void mqttLoop(void *parameter) {
|
||||
bool x;
|
||||
do {
|
||||
x = mqttClient.loop();
|
||||
mqttClient.loop();
|
||||
delay(50);
|
||||
} while (mqttClient.connected());
|
||||
|
||||
Serial.println("Disconnected from MQTT.");
|
||||
if (!mqttClient.connected()) {
|
||||
Serial.println("Disconnected from MQTT.");
|
||||
Serial.println("Checking WiFi Connection.");
|
||||
if (WiFi.isConnected()) {
|
||||
Serial.println("Starting reconnect timer for MQTT.");
|
||||
xTimerStart(mqttReconnectTimer, 0);
|
||||
}
|
||||
}
|
||||
@ -50,14 +52,15 @@ void mqttLoop(void *parameter) {
|
||||
|
||||
void connectMQTT() {
|
||||
Serial.println("Connecting to MQTT...");
|
||||
mqttClient.begin(MQTT_HOST, MQTT_PORT, client);
|
||||
// mqttClient.begin(MQTT_HOST, MQTT_PORT, client);
|
||||
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
|
||||
mqttClient.connect(MQTT_DEVICE_ID);
|
||||
if (mqttClient.connected()) {
|
||||
Serial.println("Connected!");
|
||||
} else {
|
||||
Serial.println("NOT Connected!");
|
||||
}
|
||||
mqttClient.subscribe(MQTT_PATH_SUB, 2);
|
||||
mqttClient.subscribe(MQTT_PATH_SUB);
|
||||
Serial.print("subscribed to: ");
|
||||
Serial.println(MQTT_PATH_SUB);
|
||||
xTaskCreate(
|
||||
@ -88,7 +91,8 @@ void WiFiEvent(WiFiEvent_t event) {
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload_length) {
|
||||
// void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload_length) {
|
||||
void onMqttMessage(char *topic, byte *payload, unsigned int payload_length) {
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
@ -105,7 +109,8 @@ void onMqttMessage(MQTTClient *client, char topic[], char payload[], int payload
|
||||
if (strcmp(topic, MQTT_SOIL_PROPERTIES) == 0) {
|
||||
Serial.println("receiving soil thresholds...");
|
||||
Serial.println(topic);
|
||||
Serial.println(payload);
|
||||
//Serial.println(reinterpret_cast<const char *>(payload));
|
||||
// Serial.println(payload);
|
||||
// const int capacity = JSON_OBJECT_SIZE(3) + 2 * JSON_OBJECT_SIZE(1);
|
||||
StaticJsonDocument<1024> doc;
|
||||
DeserializationError err = deserializeJson(doc, payload);
|
||||
@ -133,11 +138,13 @@ void setupConnections() {
|
||||
|
||||
WiFi.onEvent(WiFiEvent);
|
||||
|
||||
mqttClient.onMessageAdvanced(onMqttMessage);
|
||||
// mqttClient.onMessageAdvanced(onMqttMessage);
|
||||
mqttClient.setCallback(onMqttMessage);
|
||||
|
||||
connectWiFi();
|
||||
}
|
||||
|
||||
void publishMessage(const char *topic, const char *msg) {
|
||||
mqttClient.publish(topic, msg, true, 1);
|
||||
mqttClient.publish(topic, msg);
|
||||
// mqttClient.publish(topic, msg, true, 1);
|
||||
}
|
||||
|
||||
@ -74,4 +74,5 @@ extern void publishMessage(const char *topic, const char *msg);
|
||||
// sensors
|
||||
void readSensors();
|
||||
void toggleValve();
|
||||
void setSoilProperties(int FC, int PWP, int SAT);
|
||||
void setSoilProperties(int FC, int PWP, int SAT);
|
||||
void setupStore();
|
||||
@ -20,6 +20,7 @@ void setup() {
|
||||
|
||||
digitalWrite(PIN_VALVE, LOW);
|
||||
|
||||
setupStore();
|
||||
setupConnections();
|
||||
setupLightSensor();
|
||||
setupTemperatureSensor();
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include <header.h>
|
||||
#include <ArduinoNvs.h>
|
||||
|
||||
#include <string>
|
||||
//using namespace std;
|
||||
@ -85,21 +86,60 @@ void toggleValve() {
|
||||
xTaskCreate(
|
||||
valveTask, /* Task function. */
|
||||
"valveTask", /* String with name of task. */
|
||||
10000, /* Stack size in bytes. */
|
||||
512, /* Stack size in bytes. */
|
||||
NULL, /* Parameter passed as input of the task */
|
||||
1, /* Priority of the task. */
|
||||
NULL); /* Task handle. */
|
||||
}
|
||||
|
||||
void persistSoilProps(int FC, int PWP, int SAT) {
|
||||
Serial.println("persistSoilProps");
|
||||
bool f = NVS.setInt("fieldCapacity", FC);
|
||||
bool p = NVS.setInt("permanentWilt", PWP);
|
||||
bool s = NVS.setInt("soilSaturation", SAT);
|
||||
if (f && p && s) {
|
||||
Serial.println("Soil properties sucessfully stored.");
|
||||
NVS.commit();
|
||||
}
|
||||
else {
|
||||
Serial.println("error occured while trying to persist soil properties");
|
||||
}
|
||||
}
|
||||
|
||||
void restoreSoilProps() {
|
||||
Serial.println("restoreSoilProps");
|
||||
int fc = NVS.getInt("fieldCapacity");
|
||||
int pwp = NVS.getInt("permanentWilt");
|
||||
int sat = NVS.getInt("soilSaturation");
|
||||
if (fc != 0) {
|
||||
fieldCapacity = fc;
|
||||
}
|
||||
if (pwp != 0) {
|
||||
permanentWiltingPoint = pwp;
|
||||
}
|
||||
if (sat != 0) {
|
||||
soilSaturation = sat;
|
||||
}
|
||||
Serial.print(fieldCapacity);
|
||||
Serial.print(permanentWiltingPoint);
|
||||
Serial.print(soilSaturation);
|
||||
}
|
||||
|
||||
void setupStore() {
|
||||
NVS.begin("store");
|
||||
restoreSoilProps();
|
||||
}
|
||||
|
||||
void setSoilProperties(int FC, int PWP, int SAT) {
|
||||
fieldCapacity = FC;
|
||||
permanentWiltingPoint = PWP;
|
||||
soilSaturation = SAT;
|
||||
Serial.print("fieldCapacity: ");
|
||||
persistSoilProps(FC, PWP, SAT);
|
||||
Serial.print("new fieldCapacity: ");
|
||||
Serial.println(fieldCapacity);
|
||||
Serial.print("permanentWiltingPoint: ");
|
||||
Serial.print("new permanentWiltingPoint: ");
|
||||
Serial.println(permanentWiltingPoint);
|
||||
Serial.print("soilSaturation: ");
|
||||
Serial.print("new soilSaturation: ");
|
||||
Serial.println(soilSaturation);
|
||||
// TODO save to nvs nonvolatile flash storage
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user