-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiStat_ESP.cpp
More file actions
204 lines (195 loc) · 6.37 KB
/
Copy pathpiStat_ESP.cpp
File metadata and controls
204 lines (195 loc) · 6.37 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// =================================================
// Script for controlling SSD1306 display and fan
// for Microlab stats, based on readings from JSON
// generated by another Python script.
// =================================================
// @author MYCELIUM
// =================================================
#include <WiFi.h>
#include "wifi_secrets.h"
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/Org_01.h>
// =================================================
// ==== ==== ==== ==== WIFI CONF ==== ==== ==== ====
// =================================================
const char* FLASK = SECRET_FLASK;
const char* WIFI_SSID = SECRET_SSID;
const char* WIFI_PASS = SECRET_PASS;
const char* API_URL = SECRET_APIL;
// =================================================
// ==== ==== ==== ==== OLED CONF ==== ==== ==== ====
// =================================================
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// =================================================
// ==== ==== ==== ==== GLOBALS ==== ==== ==== ====
// =================================================
unsigned long lastUpdate = 0;
const int LED_PIN = 2;
const int SWC_PIN = 15;
bool lastFanState = -1;
JsonDocument doc;
// =================================================
// ==== ==== ==== ==== WIFI FUN ==== ==== ==== ====
// =================================================
// Trys to connect to wifi, visually shows blink on
// blue LED and draw text with dots on OLED screen
// if more than 40 attempts has been made -
// restart whole ESP module.
// =================================================
void setupWiFi()
{
// Show text on screen while connecting.
display.clearDisplay();
display.setFont(&Org_01);
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(0, 10);
display.print("Connecting");
display.display();
// Starts actual connection attempt
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Counts atempts and blinks LED.
int attempt = 0;
while(WiFi.status() != WL_CONNECTED && attempt < 40)
{
digitalWrite(LED_PIN, LOW);
delay(500);
digitalWrite(LED_PIN, HIGH);
display.print(".");
display.display();
attempt++;
}
pinMode(LED_PIN, OUTPUT);
// Restarts ESP module if counter reaches 40
if(WiFi.status() != WL_CONNECTED) ESP.restart();
}
// =================================================
// ==== ==== ==== ==== MAIN FUN ==== ==== ==== ====
// =================================================
void setup()
{
pinMode(SWC_PIN, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) for(;;);
setupWiFi();
}
// =================================================
// ==== ==== ==== ==== OLED FUN ==== ==== ==== ====
// =================================================
// Displays stats from fetched JSON on OLED display.
// For each stat it uses separate line with
// optimised font and conversation of smaller values
// into bigger, like 1000KBs = 1MBs
// so no pixels are wasted.
// =================================================
void updateDisplay()
{
// Prepare screen, font and colour.
display.clearDisplay();
display.setFont(&Org_01);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
// ==== ROW 1: CPU ====
display.setCursor(0, 10);
display.printf("%.1fC | %.0f%%", (float)doc["cpu_temp"], (float)doc["cpu_percent"]);
display.drawFastHLine(0, 14, 128, SSD1306_WHITE);
// ==== ROW 2: RAM ====
display.setCursor(0, 24);
display.printf("%.1f | %.1fGB", (float)doc["ram_used_gb"], (float)doc["ram_total_gb"]);
// ==== ROW 3: SSD ====
display.setCursor(0, 42);
display.printf("%.1f | %.0fGB", (float)doc["nvme_used"], (float)doc["nvme_total"]);
// ==== ROW 4: NET ====
float down = (float)doc["lan_download"];
float up = (float)doc["lan_upload"];
// Download speed with triangle down
display.fillTriangle(0, 56, 6, 56, 3, 61, SSD1306_WHITE);
display.setCursor(10, 60);
if(down >= 100.0) display.printf("%.1fMB", down / 1024.0);
else display.printf("%.0fKB", down);
// Upload speed with triangle up
display.fillTriangle(60, 61, 66, 61, 63, 56, SSD1306_WHITE);
display.setCursor(70, 60);
if (up >= 100.0) display.printf("%.1fMB", up / 1024.0);
else display.printf("%.0fKB", up);
// ==== FOOTER ====
display.drawFastHLine(0, 63, 128, SSD1306_WHITE);
display.display();
}
// =================================================
// ==== ==== ==== ==== FAN FUN ==== ==== ==== ====
// =================================================
void handleFan()
{
float currentTemp = doc["cpu_temp"] | 0.0;
if(currentTemp >= 45.0)
{
digitalWrite(SWC_PIN, LOW);
}
else if(currentTemp <= 35.0)
{
digitalWrite(SWC_PIN, HIGH);
}
}
// =================================================
// ==== ==== ==== == FAN STATE FUN = ==== ==== ====
// =================================================
void statusFan() {
if(WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(SECRET_FLASK);
http.addHeader("Content-Type", "application/json");
bool fanIsOn = (digitalRead(SWC_PIN) == LOW);
JsonDocument statusDoc;
statusDoc["fan_status"] = fanIsOn ? "ON" : "OFF";
String requestBody;
serializeJson(statusDoc, requestBody);
int httpResponseCode = http.POST(requestBody);
http.end();
}
}
// =================================================
// ==== ==== ==== ==== MAIN LOOP ==== ==== ==== ====
// =================================================
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - lastUpdate >= 1000 || lastUpdate == 0)
{
if(WiFi.status() == WL_CONNECTED)
{
HTTPClient http;
http.begin(API_URL);
int httpCode = http.GET();
if(httpCode == 200)
{
deserializeJson(doc, http.getString());
updateDisplay();
digitalWrite(LED_PIN, HIGH);
handleFan();
}
else
{
// Prepare screen, font and colour.
display.clearDisplay();
display.setFont(&Org_01);
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.print("Error in data fetching");
display.display();
}
http.end();
lastUpdate = currentMillis;
}
else
{
setupWiFi();
}
}
}