1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <XPT2046_Touchscreen.h>
#include "DHT.h"
#include "cpu.h"
#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);
static uint32_t tempTime = 0;
static uint32_t rtcTime = 0;
static uint32_t displayTime = 0;
void setup() {
Serial.begin(115200);
delay(1000);
setup_wifi();
client.setServer(mqtt_server, 1883);
initHardware();
gfx1_layout();
}
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" };
String values[] = { "", "", "", "", "", "", "", "" };
for (int i = 0; i < std::size(keys); i++) {
int index = command.indexOf(keys[i]);
if (index != -1) {
String value = command.substring(index + keys[i].length() + 1, command.indexOf(",", index + keys[i].length() + 1));
values[i] = value;
}
}
for (int i = 0; i < std::size(values); i++) {
switch (i) {
case 0:
if (values[i] != "")
setCpuTemp(values[i]);
break;
case 1:
if (values[i] != "")
setCpuUsage(values[i]);
break;
case 2:
if (values[i] != "")
setRamMax(values[i]);
break;
case 3:
if (values[i] != "")
setRamFree(values[i]);
break;
case 4:
if (values[i] != "")
setTime(values[i]);
break;
case 5:
if (values[i] != "")
OS = values[i];
break;
case 6:
if (values[i] != "")
KernelVer = values[i];
break;
case 7:
if (values[i] != "")
Uptime = values[i];
break;
}
}
gfx1_udpate();
}
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);
delay(1000);
digitalWrite(TFT_RESET, HIGH);
delay(1000);
tft.begin();
tft.setRotation(3);
dht.begin();
}
|