From 9a7fd378324a514b1baff96f0ee86b21e6406fe7 Mon Sep 17 00:00:00 2001 From: Blaster4385 Date: Sat, 26 Apr 2025 16:42:46 +0530 Subject: feat: Transmit sensor data over mqtt - This can be read by home assistant. --- sysmon.ino | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/sysmon.ino b/sysmon.ino index 1a6080d..fcfcc77 100644 --- a/sysmon.ino +++ b/sysmon.ino @@ -1,3 +1,5 @@ +#include +#include #include "Adafruit_GFX.h" #include "Adafruit_ILI9341.h" #include @@ -6,12 +8,24 @@ #include "datetimeHandler.h" #include "gfx_1.h" +#define wifi_ssid "" +#define wifi_password "" + +#define mqtt_server "" +#define mqtt_user "" +#define mqtt_password "" + +#define humidity_topic "homeassistant/sensor/humidity" +#define temperature_topic "homeassistant/sensor/temperature" + #define TFT_DC D4 #define TFT_CS D2 #define TFT_RESET D3 #define DHTPIN D8 #define DHTTYPE DHT22 +WiFiClient espClient; +PubSubClient client(espClient); Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); DHT dht(DHTPIN, DHTTYPE); @@ -21,6 +35,9 @@ static uint32_t displayTime = 0; void setup() { Serial.begin(115200); + delay(1000); + setup_wifi(); + client.setServer(mqtt_server, 1883); initHardware(); gfx1_layout(); } @@ -28,7 +45,9 @@ void setup() { void update(String command) { if (!isnan(dht.readTemperature())) { localHum = dht.readHumidity(); + client.publish(humidity_topic, String(localHum).c_str(), true); localTemp = dht.readTemperature() - 4; + client.publish(temperature_topic, String(localTemp).c_str(), true); } String keys[] = { "CpuTemp", "CpuUsage", "RamMax", "RamFree", "Time", "OS", "Kernel", "Uptime" }; @@ -45,9 +64,6 @@ void update(String command) { } for (int i = 0; i < std::size(values); i++) { - Serial.print(keys[i]); - Serial.print(" = "); - Serial.println(values[i]); switch (i) { case 0: if (values[i] != "") @@ -87,11 +103,51 @@ void update(String command) { } void loop() { + if (!client.connected()) { + reconnect(); + } + client.loop(); String command = Serial.readString(); command.trim(); update(command); } +void setup_wifi() { + // We start by connecting to a WiFi network + Serial.print("Hello WiFi"); + Serial.println(); + Serial.print("Connecting to "); + Serial.println(wifi_ssid); + + WiFi.begin(wifi_ssid, wifi_password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) { + Serial.println("connected"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + void initHardware() { pinMode(TFT_RESET, OUTPUT); digitalWrite(TFT_RESET, LOW); @@ -101,4 +157,4 @@ void initHardware() { tft.begin(); tft.setRotation(3); dht.begin(); -} \ No newline at end of file +} -- cgit