-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathD1Mini_OLED_MQTT.ino
241 lines (228 loc) · 6.65 KB
/
D1Mini_OLED_MQTT.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <PersWiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266SSDP.h>
#include <PubSubClient.h>
#include <SPIFFSReadServer.h>
#include <DNSServer.h>
#include <FS.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include "Wemos_Mini_OLED.h"
#define DEVICE_NAME "ESP8266 WiFi"
SPIFFSReadServer server(80);
DNSServer dnsServer;
WiFiClient espClient;
PubSubClient client(espClient);
PersWiFiManager persWM(server, dnsServer);
Wemos_Mini_OLED display(0);
const char* mqtt_server = "XXXXXXXXXXX.com"; // Your MQTT server
const int port = 1883; // You may need to change this
const char* user = "XXXXXXXXXXX"; // Your MQTT yourname
const char* pass = "XXXXXXXXXXXXXX"; // Your MQTT password
bool internet = false;
String s;
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Loading..");
display.display();
//allows serving of files from SPIFFS
SPIFFS.begin();
//sets network name for AP mode
persWM.setApCredentials(DEVICE_NAME);
//persWM.setApCredentials(DEVICE_NAME, "password"); optional WiFi AP password
persWM.onConnect([]() {
client.setServer(mqtt_server, port); // You may need to change the port
client.setCallback(callback);
internet = true;
display.clearDisplay();
display.setCursor(0, 0);
display.println("Ready :)");
display.display();
});
persWM.onAp([]() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("setup\ncomplete!");
display.println("Connect to\n\"ESP8266\nWiFi\"");
display.display();
});
persWM.begin();
server.on("/serial", []() {
//build json object of program data
StaticJsonDocument<500> json;
long CurrentTime = millis();
int sc = CurrentTime / 1000;
int mn = sc / 60;
int hr = mn / 60;
char ch[20];
sprintf (ch, "%02d", sc % 60);
json["sec"] = ch;
sprintf (ch, "%02d", mn % 60);
json["min"] = ch;
sprintf (ch, "%02d", hr % 60);
json["hr"] = ch;
json["s"] = s;
s = "";
char jsonchar[500];
serializeJson(json, jsonchar); //print to char array, takes more memory but sends in one piece
server.send(200, "application/json", jsonchar);
}); //server.on serial
//SSDP makes device visible on windows network
server.on("/description.xml", HTTP_GET, []() {
SSDP.schema(server.client());
});
SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort(80);
SSDP.setName(DEVICE_NAME);
SSDP.setModelName("ESP8266 WiFi device");
SSDP.setModelURL("/");
SSDP.setManufacturer("HA4ever ^_*");
SSDP.setManufacturerURL("https://github.com/HA4ever37");
SSDP.setSerialNumber("No1");
SSDP.setModelNumber("1");
SSDP.begin();
SSDP.setDeviceType("upnp:rootdevice");
server.begin();
s += ("<br>Setup complete.");
} //void setup
void loop() {
dnsServer.processNextRequest();
server.handleClient();
if (internet)
if (!client.connected())
reconnect();
client.loop();
} //void loop
void callback(char* topic, byte* payload, unsigned int length) {
display.clearDisplay();
display.setCursor(0, 0);
s += ("<br>Message arrived [");
s += (topic);
s += ("]: ");
for (int i = 0; i < length; i++) {
s += ((char)payload[i]);
}
if ((char)payload[0] == '*') {
msgConfrim("Blink led");
for (byte b = 0; b < 25; b++) { //blink the led for 5 seconds
client.loop();
server.handleClient();
digitalWrite(BUILTIN_LED, LOW);
delay(100);
digitalWrite(BUILTIN_LED, HIGH);
delay(100);
}
}
else if ((char)payload[0] == '#') {
WiFiClient client;
HTTPClient http;
String payload = "Error!";
if (http.begin(client, "http://api.ipify.org")) {
if (http.GET() > 0)
payload = http.getString();
}
char ip[16];
payload.toCharArray(ip, 16);
msgConfrim(ip);
}
else if ((char)payload[0] == '0') {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
msgConfrim("Led is OFF");
}
else if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
msgConfrim("Led is ON");
}
else if ((char)payload[0] == '2') {
display.setTextSize(2);
msgConfrim("Set large font");
}
else if ((char)payload[0] == '3') {
display.setTextSize(1);
msgConfrim("Set small font");
}
else if ((char)payload[0] == '4') {
for (byte i = 0; i < 5; i++) {
display.ssd1306_command(SSD1306_DISPLAYOFF); // To switch display off
delay(500);
display.ssd1306_command(SSD1306_DISPLAYON); // To switch display back on
delay(500);
}
msgConfrim("Blink screen 5 times");
}
else if ((char)payload[0] == '5') {
display.ssd1306_command(SSD1306_DISPLAYOFF);
msgConfrim("Screen off");
}
else if ((char)payload[0] == '6') {
display.ssd1306_command(SSD1306_DISPLAYON);
msgConfrim("Screen on");
}
else if ((char)payload[0] == ':') {
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
String oTime = "Online time: ";
char ch[4];
sprintf (ch, "%02d", hr % 60);
oTime += ch;
oTime += ':';
sprintf (ch, "%02d", min % 60);
oTime += ch;
oTime += ':';
sprintf (ch, "%02d", sec % 60);
oTime += ch;
msgConfrim(strdup(oTime.c_str()));
}
else {
display.ssd1306_command(SSD1306_DISPLAYON);
display.clearDisplay();
display.setCursor(0, 0);
for (int i = 0; i < length; i++) {
display.print((char)payload[i]);
display.display();
yield();
}
msgConfrim("New message is received!");
}
}
void msgConfrim(char* msg) {
s += ("<br>Publish message: ");
s += (msg);
client.publish("outTopic", msg, 1);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
s += ("<br>Attempting MQTT connection...");
// Create a random client ID
String clientId = DEVICE_NAME;
// Attempt to connect
if (client.connect(clientId.c_str(), user, pass)) {
s += ("<br>Connected");
// Once connected, publish an announcement...
client.publish("outTopic", "ESP8266 WiFi is on!");
client.subscribe("inTopic");
}
else {
display.clearDisplay();
display.setCursor(0, 0);
s += ("Failed, rc=");
display.print("Failed, rc=");
s += (client.state());
display.println(client.state());
s += ("<br>Try again in 5 seconds");
display.println("Trying\nagain..");
display.display();
// Wait 5 seconds before retrying
delay(5000);
}
}
}