-
Notifications
You must be signed in to change notification settings - Fork 0
/
epapernametag.ino
230 lines (199 loc) · 7.74 KB
/
epapernametag.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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#define ENABLE_GxEPD2_GFX 0
#include <GxEPD2_BW.h>
#include <GxEPD2_3C.h>
#include <Fonts/FreeSans12pt7b.h>
#include "waveshare-demo-epd42.h" // combined e-Paper driver and 4.2" display code
// ***** for mapping of Waveshare e-Paper ESP8266 Driver Board *****
// select one , can use full buffer size (full HEIGHT)
//GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
// can use only half buffer size
GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
int prev_stations; // Connected wifi clients
String getContentType(String filename); // convert the file extension to the MIME type
bool handleFileRead(String path); // send the right file to the client (if it exists)
void setup(void) {
Serial.begin(115200);
Serial.println();
// e-paper display SPI initialization has to come before SPIFFS or it'll reset
pinMode(CS_PIN , OUTPUT);
pinMode(RST_PIN , OUTPUT);
pinMode(DC_PIN , OUTPUT);
pinMode(BUSY_PIN, INPUT);
SPI.begin();
// then GxEPD
display.init();
SPIFFS.begin(); // Start the SPI Flash Files System
pinMode(10, INPUT_PULLUP); //SD3 / GPIO10 is safe to use because our flash is DIO
if (digitalRead(10) == LOW)
{
SPIFFS.remove("/wifiap.txt");
}
Serial.print("Checking for wifi credentials file ... ");
if (!SPIFFS.exists("/wifiap.txt")) {
Serial.println("NOT FOUND!");
long randSSID = secureRandom(1000, 10000); // four digit HW PRNG random number for a semi-unique SSID
long randPW = secureRandom(10000000L, 100000000L); // eight digit HW PRNG random number password
File f = SPIFFS.open("/wifiap.txt", "w");
if (!f) {
Serial.println("Failed to create wifi credential file!");
}
f.print("epapernametag-");
f.println(randSSID);
f.println(randPW);
f.close();
Serial.println("epaper credentials start");
display.setRotation(0);
display.setFont(&FreeSans12pt7b);
display.setTextColor(GxEPD_BLACK);
display.setFullWindow();
display.firstPage();
do
{
display.fillScreen(GxEPD_WHITE);
display.setCursor(0, 0);
display.println("");
display.println("To upload a name tag, connect to:");
display.println("");
display.print("Wifi SSID: epapernametag-");
display.println(randSSID);
display.println("");
display.print("Wifi password: ");
display.println(randPW);
display.println("");
display.println("http://epapernametag.local (iOS/Mac)");
display.println("http://192.168.4.1 (Android/Win)");
display.println("");
display.println("Jumper SD3 to GND to reset wifi");
}
while (display.nextPage());
display.powerOff();
Serial.println("epaper credentials end");
} else {
Serial.println("FOUND!");
}
File f = SPIFFS.open("/wifiap.txt", "r");
if (!f) {
Serial.println("Failed to open wifi credential file!");
}
String ssid = f.readStringUntil('\n');
String password = f.readStringUntil('\n');
f.close();
ssid.trim();
password.trim();
char __ssid[ssid.length() + 1];
char __password[password.length() + 1];
ssid.toCharArray(__ssid, sizeof(__ssid));
password.toCharArray(__password, sizeof(__password));
Serial.print("SSID = ");
Serial.println(__ssid);
Serial.print("Password = ");
Serial.println(__password);
Serial.println(WiFi.softAP(__ssid, __password) ? "Wifi AP ready" : "Wifi AP failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
Serial.print("Stations connected = ");
Serial.println(prev_stations = WiFi.softAPgetStationNum());
if (MDNS.begin("epapernametag")) {
Serial.println("MDNS responder started");
}
server.on("/LOAD", EPD_Load);
server.on("/EPD", EPD_Init);
server.on("/NEXT", EPD_Next);
server.on("/SHOW", EPD_Show);
server.onNotFound([]() { // If the client requests any URI
if (!handleFileRead(server.uri())) // send it if it exists
server.send(404, "text/plain", "404: Not Found"); // otherwise, respond with a 404 (Not Found) error
});
server.begin(); // Actually start the server
Serial.println("HTTP server started");
}
void loop(void) {
int curr_stations = WiFi.softAPgetStationNum();
if (curr_stations != prev_stations) {
Serial.printf("Stations connected = %d\n", curr_stations);
prev_stations = curr_stations;
}
server.handleClient();
}
void EPD_Init()
{
EPD_dispIndex = ((int)server.arg(0)[0] - 'a') + (((int)server.arg(0)[1] - 'a') << 4);
// Print log message: initialization of e-Paper (e-Paper's type)
Serial.printf("EPD %s\r\n", EPD_dispMass[EPD_dispIndex].title);
// Initialization
EPD_dispInit();
server.send(200, "text/plain", "Init ok\r\n");
}
void EPD_Load()
{
//server.arg(0) = data+data.length+'LOAD'
String p = server.arg(0);
if (p.endsWith("LOAD")) {
int index = p.length() - 8;
int L = ((int)p[index] - 'a') + (((int)p[index + 1] - 'a') << 4) + (((int)p[index + 2] - 'a') << 8) + (((int)p[index + 3] - 'a') << 12);
if (L == (p.length() - 8)) {
Serial.println("LOAD");
// if there is loading function for current channel (black or red)
// Load data into the e-Paper
if (EPD_dispLoad != 0) EPD_dispLoad();
}
}
server.send(200, "text/plain", "Load ok\r\n");
}
void EPD_Next()
{
Serial.println("NEXT");
// Instruction code for for writting data into
// e-Paper's memory
int code = EPD_dispMass[EPD_dispIndex].next;
// If the instruction code isn't '-1', then...
if (code != -1)
{
// Do the selection of the next data channel
EPD_SendCommand(code);
delay(2);
}
// Setup the function for loading choosen channel's data
EPD_dispLoad = EPD_dispMass[EPD_dispIndex].chRd;
server.send(200, "text/plain", "Next ok\r\n");
}
void EPD_Show()
{
Serial.println("SHOW");
// Show results and Sleep
EPD_dispMass[EPD_dispIndex].show();
server.send(200, "text/plain", "Show ok\r\n");
}
String getContentType(String filename){
if(filename.endsWith(".htm")) return "text/html";
else if(filename.endsWith(".html")) return "text/html";
else if(filename.endsWith(".css")) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript";
else if(filename.endsWith(".png")) return "image/png";
else if(filename.endsWith(".gif")) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon";
else if(filename.endsWith(".xml")) return "text/xml";
else if(filename.endsWith(".pdf")) return "application/x-pdf";
else if(filename.endsWith(".zip")) return "application/x-zip";
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
bool handleFileRead(String path) { // send the right file to the client (if it exists)
Serial.println("handleFileRead: " + path);
if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file
String contentType = getContentType(path); // Get the MIME type
if (SPIFFS.exists(path)) { // If the file exists
File file = SPIFFS.open(path, "r"); // Open it
size_t sent = server.streamFile(file, contentType); // And send it to the client
file.close(); // Then close the file again
return true;
}
Serial.println("\tFile Not Found");
return false; // If the file doesn't exist, return false
}