Repository navigation
Expand file tree
/
Copy pathlcd-display.ino
More file actions
276 lines (236 loc) · 6.36 KB
/
Copy pathlcd-display.ino
File metadata and controls
276 lines (236 loc) · 6.36 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
ESP8266 Blink by Simon Peter
Blink the blue LED on the ESP-01 module
This example code is in the public domain
The blue LED on the ESP-01 module is connected to GPIO1
(which is also the TXD pin; so we cannot use Serial.print() at the same time)
Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
*/
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <PubSubClient.h>
#define BUZZER_PIN 14
LiquidCrystal_I2C lcd(0x3F, 16, 2);
byte up_arrow[8] = {
B00100,
B01110,
B10101,
B00100,
B00100,
B00100,
B00100,
};
byte down_arrow[8] = {
B00100,
B00100,
B00100,
B00100,
B10101,
B01110,
B00100,
};
const char* ssid = "my-ssid";
const char* password = "";
const char* mqtt_server = "";
const int mqtt_port = 1883;
byte beeps = 0;
byte rolling_pos = 0;
const char rolling[] = "|/-\\";
String path = "nest/displays/";
unsigned long last_message_time = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String str(" ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
str[i] = (char)payload[i];
}
Serial.println();
String subpath(topic);
subpath = subpath.substring(path.length() - 1);
Serial.println("subpath: " + subpath);
if (subpath != 0 && subpath.equals("backlight")) {
if ((char)payload[0] == '1') {
lcd.backlight();
} else if ((char)payload[0] == '0') {
lcd.noBacklight();
}
}
if (subpath != 0 && subpath.equals("r0")) {
last_message_time = millis();
lcd.setCursor(0, 0);
lcd.print(str);
}
if (subpath != 0 && subpath.equals("r1")) {
last_message_time = millis();
lcd.setCursor(0, 1);
lcd.print(str);
}
if (subpath != 0 && subpath.equals("r2")) {
last_message_time = millis();
lcd.setCursor(0, 2);
lcd.print(str);
}
if (subpath != 0 && subpath.equals("r3")) {
last_message_time = millis();
lcd.setCursor(0, 3);
lcd.print(str);
}
if (subpath != 0 && subpath.equals("clear")) {
last_message_time = millis();
lcd.clear();
}
if (subpath != 0 && subpath.equals("beep")) {
last_message_time = millis();
beeps = 4;
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
Serial.printf("Attempting MQTT connection to %s:%d from my ip\n", mqtt_server, mqtt_port);
Serial.println(WiFi.localIP());
lcd.setCursor(0, 0);
lcd.print("Connecting MQTT:");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(mqtt_server);
/*
lcd.print("as client: ");
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(clientId);
*/
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
lcd.setCursor(0, 0);
lcd.print("Connected to: ");
// Once connected, publish an announcement...
client.publish("nest/new_displays", path.c_str());
// ... and resubscribe
client.subscribe(path.c_str());
Serial.println("Subscribed to " + path);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
lcd.setCursor(0, 0);
lcd.print("failed ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(client.state());
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
delay(10);
Serial.println("Setup( )");
pinMode(BUZZER_PIN, OUTPUT);
byte count = 0;
Wire.begin();
for (byte i = 1; i < 120; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
lcd.init(); // initializing the LCD
lcd.createChar(1, up_arrow);
lcd.createChar(2, down_arrow);
lcd.backlight(); // Enable or Turn On the backlight
lcd.setCursor(0, 0);
lcd.print("SSID \0");
lcd.setCursor(0, 1);
lcd.print("MAC: \1");
WiFi.persistent(false);
WiFi.setOutputPower(0);
WiFi.begin(ssid, password);
delay(1000);
lcd.setCursor(0, 0);
lcd.print(ssid);
lcd.setCursor(0, 1);
lcd.print(WiFi.macAddress());
Serial.println("LCD started");
byte i = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
i++;
Serial.printf("Trying to connect to %s with pass %s\n", ssid, password);
lcd.setCursor(15, 0);
lcd.print(rolling[rolling_pos++]);
if (rolling_pos >= sizeof(rolling)-2) {
rolling_pos = 0;
}
if (i > 10) {
Serial.println("Resetting wifi");
WiFi.mode(WIFI_OFF); // this is a temporary line, to be removed after SDK update to 1.5.4
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
i = 0;
Serial.println("wifi reset done.");
}
}
lcd.setCursor(15, 0);
lcd.print(" ");
Serial.print(F("My IP is: "));
Serial.println(WiFi.localIP());
lcd.setCursor(0,0);
lcd.print("Connected. IP: ");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(WiFi.localIP());
path += WiFi.macAddress() + "/#";
randomSeed(micros());
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
last_message_time = millis();
}
// the loop function runs over and over again forever
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (beeps > 0) {
beeps--;
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
client.loop();
delay(100);
}
if (last_message_time + 120 * 1000 < millis()) {
lcd.setCursor(0, 0);
lcd.print("no data ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
}