-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3d-printer-lcd-box.ino
218 lines (170 loc) · 4.63 KB
/
3d-printer-lcd-box.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <ArduinoJson.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include "LiquidCrystal_I2C.h"
#include <Adafruit_HTU21DF.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Sensitive.h"
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
const uint16_t LCD_TIMEOUT = 900000;
boolean needToPublish = false;
volatile boolean backlightOn = false;
const byte INT_PIN_A = 12;
const byte INT_PIN_B = 13;
volatile boolean positionA = 12;
volatile boolean positionB = 13;
uint32_t lastAction;
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD);
Adafruit_MQTT_Publish buttonPress = Adafruit_MQTT_Publish(&mqtt, "relay");
Adafruit_MQTT_Subscribe messages = Adafruit_MQTT_Subscribe(&mqtt, "display");
const char* messagePayload() {
String value = "{\"relayA\": " + String(positionA) + ", \"relayB\": " + String(positionB) + "}";
return value.c_str();
}
void publishMessage() {
// send message that switch was toggled
const char* payload = messagePayload();
buttonPress.publish(payload);
}
void messageCallback(char *data, uint16_t len) {
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
JsonObject& msg = jsonBuffer.parseObject(data);
const char* line1 = msg["line1"];
const char* line2 = msg["line2"];
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}
// check for status of switch A
void publishA() {
needToPublish = true;
if(digitalRead(INT_PIN_A) == HIGH){
positionA = true;
} else {
positionA = false;
}
}
// check for status of switch B
void publishB() {
needToPublish = true;
if(digitalRead(INT_PIN_B) == HIGH) {
positionB = true;
} else {
positionB = false;
}
}
// update line 2 of the LCD (todo: update messageCallback to only update line 1)
void updateLineTwo(float temp, float humid) {
lcd.setCursor(0, 1);
lcd.print("T: ");
lcd.print(round(temp));
lcd.print(" | ");
lcd.print("H: ");
lcd.print(round(humid));
}
void setup()
{
// attach interrupt for Pin A
pinMode(INT_PIN_A, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INT_PIN_A), publishA, CHANGE);
// attach interrupt for Pin B
pinMode(INT_PIN_B, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INT_PIN_B), publishB, CHANGE);
// check initial status of Pin A
if(digitalRead(INT_PIN_A) == HIGH) {
positionA = true;
} else {
positionA = false;
}
// check initial status of Pin B
if(digitalRead(INT_PIN_B) == HIGH) {
positionB = true;
} else {
positionB = false;
}
// initialize the LCD
lcd.begin();
lcd.clear();
// turn on the blacklight and print a message.
lcd.backlight();
lcd.print("Connecting...");
// initiate wifi connection
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
lcd.clear();
lcd.print("Still connecting...");
}
lcd.clear();
lcd.print("Connected!");
delay(1000);
lcd.clear();
lcd.print(WiFi.localIP());
delay(500);
// set callback for incoming messages
messages.setCallback(messageCallback);
mqtt.subscribe(&messages);
// initialize HTU sensor
htu.begin();
// set flag to pubish initial state
needToPublish = true;
}
void loop() {
// get current temp and humidity readings
float temp = (htu.readTemperature() * 1.8) + 32;
float humidity = htu.readHumidity();
// update line 2 with the current values
updateLineTwo(temp, humidity);
// check if backlight should be turned on or off
if(lastAction < millis() - LCD_TIMEOUT && backlightOn == true) {
lcd.noBacklight();
backlightOn = false;
}
if(lastAction > millis() - LCD_TIMEOUT && backlightOn == false) {
lcd.backlight();
backlightOn = true;
}
// check if we need to publish any new messages (from switches)
if(needToPublish) {
needToPublish = false;
publishMessage();
}
// todo: do we want this in loop()?
MQTT_connect();
// set receiving MQTT stuff
mqtt.processPackets(100);
if(!mqtt.ping()) {
mqtt.disconnect();
}
}
void MQTT_connect() {
int8_t ret;
if(mqtt.connected()) {
return;
}
lcd.clear();
lcd.print("Connecting MQTT");
uint8_t retries = 3;
while((ret = mqtt.connect()) != 0) {
lcd.clear();
lcd.print("Retries left: ");
lcd.print(retries);
mqtt.disconnect();
delay(10000);
retries--;
if(retries == 0){
lcd.clear();
lcd.print("Could not connect");
while(1);
}
}
lcd.clear();
lcd.print("MQTT Connected!");
}