working mosquitto
This commit is contained in:
parent
7612cd4666
commit
b590561c0d
35
src/main.ino
35
src/main.ino
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue