-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathesp32_chatterbox.ino
114 lines (96 loc) · 2.65 KB
/
esp32_chatterbox.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
#include <WiFi.h>
#include <DNSServer.h>
#include <WebServer.h>
#include <SPIFFS.h>
#include <FS.h>
#include "HTMLPAGE.h"
#define FORMAT_SPIFFS_IF_FAILED true
#define RECORD_SEP "\x1E"
const byte DNS_PORT = 53;
IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
WebServer server(80);
const char* filename = "/posts.txt";
void sendPage(){
Serial.println("GET /");
server.send(200,"text/html",HTMLPAGE);
}
void sendMessages(){
Serial.println("GET /posts");
File file = SPIFFS.open(filename, FILE_READ);
if(!file){
Serial.println("- failed to open file for reading");
}
server.streamFile(file,"text/plain");
file.close();
}
void receiveMessage(){
Serial.println("POST /post");
int argCount = server.args();
if (argCount == 0){
Serial.println("zero args?");
}
File file = SPIFFS.open(filename, FILE_APPEND);
if(!file){
Serial.println("- failed to open file for writing");
}
if(argCount == 1){
String line = server.arg(0);
line.replace(String(RECORD_SEP),String(""));
file.print(line);
file.print(RECORD_SEP);
Serial.print("New message: ");
Serial.println(line);
}
file.close();
server.send(200,"text.plain","");
}
void setup() {
Serial.begin(115200);
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
Serial.println("SPIFFS Mount Failed");
return;
}
// initialize file (not totally sure this is necessary but YOLO)
File file = SPIFFS.open(filename, FILE_READ);
if(!file){
file.close();
File file_write = SPIFFS.open(filename, FILE_WRITE);
if(!file_write){
Serial.println("- failed to create file?!?");
}
else{
file_write.print("");
file_write.close();
}
}else{
file.close();
}
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP("ChatterBox");
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
dnsServer.start(DNS_PORT, "*", apIP);
// init http server
server.on("/", HTTP_GET, sendPage);
server.on("/index.html", HTTP_GET, sendPage);
server.on("/posts", HTTP_GET, sendMessages);
server.on("/post", HTTP_POST, receiveMessage);
server.begin();
}
void loop() {
dnsServer.processNextRequest();
server.handleClient();
// type a "c" in the serial monitor to clean the database
if (Serial.available() > 0) {
int incomingByte = Serial.read();
if(incomingByte == 99){
Serial.println("overwriting file...");
File file_write = SPIFFS.open(filename, FILE_WRITE);
file_write.print("");
file_write.close();
Serial.println("file overwritten :)");
}
}
}