working mosquitto

This commit is contained in:
Flinner Yuu 2022-12-10 23:48:24 +03:00
parent 7612cd4666
commit b590561c0d
Signed by: flinner
GPG Key ID: 95CE0DA7F0E58CA6
1 changed files with 21 additions and 14 deletions

View File

@ -9,12 +9,10 @@
#define CHARGE_PIN 5 #define CHARGE_PIN 5
#define MEASURING_PIN 36 #define MEASURING_PIN 36
#define RESISTOR_VALUE 1e6 #define RESISTOR_VALUE 1e6
#define DEVICE "ESP32"
#define WIFI_SSID "SSID_HERE" // Network Name
#define WIFI_PASSWORD "PASSWORD_HERE" // Network Password
#define MQTT_SERVER "test.mosquitto.org" #define MQTT_SERVER "test.mosquitto.org"
#define MQTT_PORT 1883 #define MQTT_PORT 1883
#define MQTT_SUBSCRIBE_PUMP "plant/1/pump" #define MQTT_TOPIC_CAPCITANCE "pipe_system/capacitance"
#define DEVICE_ID "PIPE_01"
@ -39,11 +37,19 @@ void setup()
void loop() void loop()
{ {
float cap; float cap;
setup_wifi();
setup_mqtt(); // Check WiFi connection and reconnect if needed
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
if (!client.connected()) {
reconnect_mqtt();
}
cap = capacitance(DISCHARGE_PIN, CHARGE_PIN, MEASURING_PIN); cap = capacitance(DISCHARGE_PIN, CHARGE_PIN, MEASURING_PIN);
publish_mqtt("capacitance", cap);
} }
// =============== LOOP END ============ // =============== LOOP END ============
@ -92,7 +98,7 @@ void setup_wifi() {
Serial.print(WIFI_SSID); Serial.print(WIFI_SSID);
Serial.println(" Successfully"); Serial.println(" Successfully");
Serial.print(WiFi.localIP()); Serial.print(WiFi.localIP());
Serial.println(" isn the local IP"); Serial.println(" is the local IP");
} }
void setup_mqtt() { void setup_mqtt() {
@ -112,8 +118,8 @@ void callback(char *topic, byte *message, unsigned int length) {
} }
Serial.println(); Serial.println();
if (String(topic) == MQTT_SUBSCRIBE_PUMP) { if (String(topic) == "SUBSCRIBE_STRING") {
Serial.print("Starting Pump for: "); Serial.print("a message!");
} }
} }
@ -122,10 +128,10 @@ void reconnect_mqtt() {
while (!client.connected()) { while (!client.connected()) {
Serial.print("Attempting MQTT connection..."); Serial.print("Attempting MQTT connection...");
// Attempt to connect // Attempt to connect
if (client.connect("ESP32-1")) { if (client.connect(DEVICE_ID)) {
Serial.println("connected"); Serial.println("connected");
// Subscribe // Subscribe
client.subscribe(MQTT_SUBSCRIBE_PUMP); client.subscribe("SUB_STRING");
} else { } else {
Serial.print("failed, rc="); Serial.print("failed, rc=");
Serial.print(client.state()); Serial.print(client.state());
@ -136,12 +142,13 @@ void reconnect_mqtt() {
} }
} }
void publish_mqtt(const char *topic, const char *sensor, float value) { //void publish_mqtt(const char *topic, const char *sensor, float value) {
void publish_mqtt(const char *sensor, float value) {
char buffer[40]; char buffer[40];
sprintf(buffer, "plant1 %s=%.0f", sensor, value); sprintf(buffer, "%s %s=%.0f", DEVICE_ID, sensor, value);
Serial.print("Publishing "); Serial.print("Publishing ");
Serial.println(buffer); Serial.println(buffer);
client.publish(topic, buffer); client.publish(MQTT_TOPIC_CAPCITANCE, buffer);
} }