-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale_project.ino
203 lines (163 loc) · 5.41 KB
/
scale_project.ino
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
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <uMQTTBroker.h>
#include "fonts/NotoSansBold24pt7b.h"
#include "fonts/NotoSans14pt7b.h"
#include "fonts/NotoSans12pt7b.h"
#include "fonts/NotoSans10pt7b.h"
#include "icon.h"
// pin connection
#define TFT_DC D1
#define TFT_RST D2
#define TFT_CS D7
#define touchPin D0
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
const char* ssid = "NodeMCU"; // Access Point SSID
const char* password = "123456789"; // Access Point Password
ESP8266WebServer server(80);
String BG_COLOR = "ffd500";
int hexStringToInt(String hex) {
return (int)strtol(hex.c_str(), NULL, 16);
}
class myMQTTBroker: public uMQTTBroker {
public:
virtual bool onConnect(IPAddress addr, uint16_t client_count) {
Serial.println(addr.toString()+" connected");
return true;
}
virtual void onDisconnect(IPAddress addr, String client_id) {
Serial.println(addr.toString()+" ("+client_id+") disconnected");
}
virtual bool onAuth(String username, String password, String client_id) {
Serial.println("Username/Password/ClientId: "+username+"/"+password+"/"+client_id);
return true;
}
virtual void onData(String topic, const char *data, uint32_t length) {
char data_str[length+1];
os_memcpy(data_str, data, length);
data_str[length] = '\0';
Serial.println("received topic '"+topic+"' with data '"+(String)data_str+"'");
//printClients();
}
// Sample for the usage of the client info methods
virtual void printClients() {
for (int i = 0; i < getClientCount(); i++) {
IPAddress addr;
String client_id;
getClientAddr(i, addr);
getClientId(i, client_id);
Serial.println("Client "+client_id+" on addr: "+addr.toString());
}
}
};
myMQTTBroker myBroker;
void setupMDNS() {
if (!MDNS.begin("scale")) {
Serial.println("Error setting up mDNS");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
}
void setupWiFiAP () {
WiFi.softAP(ssid, password);
}
void setupMessaging () {
myBroker.init();
myBroker.subscribe("#");
}
void setupWebServer () {
server.on("/", rootHandler);
server.begin();
}
void startup () {
tft.init(240, 240, SPI_MODE2);
tft.setRotation(1);
tft.fillScreen(ST77XX_BLACK);
setBackground();
printWeight("230.23");
printUnit("gr");
printModeLabel();
printMode("creamer");
tft.drawBitmap(20, 30, mug_icon, 48, 48, ST77XX_BLACK);
}
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
setupWiFiAP();
setupMDNS();
setupMessaging();
setupWebServer();
startup();
}
int counter = 0;
void loop() {
MDNS.update();
int touchValue = digitalRead(touchPin);
server.handleClient();
myBroker.publish("broker/counter", (String)counter++);
delay(1000);
}
void rootHandler () {
server.send(200, "text/html", renderWebpage());
}
String renderWebpage() {
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><script src=\"https://unpkg.com/[email protected]\"></script><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #3498db;}\n";
ptr +=".button-on:active {background-color: #3498db;}\n";
ptr +=".button-off {background-color: #34495e;}\n";
ptr +=".button-off:active {background-color: #2c3e50;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>ESP8266 Web Server</h1>\n";
ptr +="<h3>Hello HTMX</h3>\n";
ptr +="<button id=\"showButton\" hx-get=\"/uikit-modal.html\" hx-target=\"#modals-here\" class=\"uk-button uk-button-primary\" _=\"on htmx:afterOnLoad wait 10ms then add .uk-open to #modal\">Open Modal</button><div id=\"modals-here\"></div>";
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
void printWeight(String number) {
tft.setTextSize(1);
tft.setFont(&NotoSansBold24pt7b);
tft.setTextColor(hexStringToInt("000000"));
tft.fillRoundRect(17, 145, 215, 36, 10, hexStringToInt(BG_COLOR));
tft.setCursor(30, 180);
tft.print(number);
}
void printUnit(String number) {
tft.setTextSize(1);
tft.setFont(&NotoSans14pt7b);
tft.setTextColor(hexStringToInt("000000"));
tft.fillRoundRect(17, 188, 100, 24, 10, hexStringToInt(BG_COLOR));
tft.setCursor(30, 209);
tft.print(number);
}
void printModeLabel() {
tft.setTextSize(1);
tft.setFont(&NotoSans10pt7b);
tft.setCursor(80, 47);
tft.setTextColor(hexStringToInt("000000"));
tft.print("mode");
}
void printMode(String number) {
tft.setTextSize(1);
tft.setFont(&NotoSans14pt7b);
tft.setTextColor(hexStringToInt("000000"));
tft.fillRect(75, 50, 120, 28, hexStringToInt(BG_COLOR));
tft.setCursor(80, 72);
tft.print(number);
}
void setBackground () {
tft.fillRoundRect(5, 5, 230, 230, 35, hexStringToInt(BG_COLOR));
}