diff options
author | Blaster4385 <venkatesh@tablaster.dev> | 2025-04-26 16:42:46 +0530 |
---|---|---|
committer | Blaster4385 <venkatesh@tablaster.dev> | 2025-04-26 16:42:49 +0530 |
commit | 9a7fd378324a514b1baff96f0ee86b21e6406fe7 (patch) | |
tree | b6890bd79d9dcd5ff30cd2e09f69fa31c9c1201c | |
parent | 4dedd94c34e28f0363fe6ebffe9ae95703f1b486 (diff) |
- This can be read by home assistant.
-rw-r--r-- | sysmon.ino | 64 |
1 files changed, 60 insertions, 4 deletions
@@ -1,3 +1,5 @@ +#include <PubSubClient.h> +#include <ESP8266WiFi.h> #include "Adafruit_GFX.h" #include "Adafruit_ILI9341.h" #include <XPT2046_Touchscreen.h> @@ -6,12 +8,24 @@ #include "datetimeHandler.h" #include "gfx_1.h" +#define wifi_ssid "<SSID>" +#define wifi_password "<PASSWORD>" + +#define mqtt_server "<HOST>" +#define mqtt_user "<USER>" +#define mqtt_password "<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 +} |