Skip to content

Commit f67a3b7

Browse files
committed
[Initial commit after move]
Moved from standard location Create your own WifiConfig.h defining ssid and password #define ssid "your ssid" #define password "your password" Signed-off-by: Arrie Dacious <[email protected]>
0 parents  commit f67a3b7

23 files changed

+1477
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
WifiConfig.h
2+
*~

Config.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef __CONFIG_H__
2+
#define __CONFIG_H__
3+
struct config {
4+
char m1[255] = "http://192.168.1.4:8888/test1/{val}";
5+
char m2[255] = "http://192.168.1.4:8888/test2/{val}";
6+
};
7+
8+
extern struct config cfg;
9+
10+
#endif

FS.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Filesystem stuff
2+
#include <FS.h>
3+
4+
#include "Config.h"
5+
6+
7+
void format() {
8+
if (SPIFFS.begin()) {
9+
SPIFFS.format();
10+
SPIFFS.end();
11+
}
12+
}
13+
14+
void loadConfig() {
15+
if (SPIFFS.begin()) {
16+
if (SPIFFS.exists("/config.bin")) {
17+
File f = SPIFFS.open("/config.bin", "r");
18+
if (f) {
19+
size_t size = f.size();
20+
if (size == sizeof(cfg)) {
21+
f.readBytes((char*)&cfg, size);
22+
f.close();
23+
}
24+
}
25+
}
26+
SPIFFS.end();
27+
}
28+
}
29+
30+
void saveConfig() {
31+
if (SPIFFS.begin()) {
32+
File f = SPIFFS.open("/config.bin","w");
33+
if (f) {
34+
f.write((uint8_t*)&cfg, sizeof(cfg));
35+
f.close();
36+
}
37+
SPIFFS.end();
38+
}
39+
}
40+

HttpMethods.cpp

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#include "HttpMethods.h"
2+
3+
#include <ESP8266HTTPClient.h>
4+
#include <FS.h>
5+
6+
#define DBG_OUTPUT_PORT Serial
7+
8+
void listFiles() {
9+
Dir dir = SPIFFS.openDir("/");
10+
while (dir.next()) {
11+
String fileName = dir.fileName();
12+
size_t fileSize = dir.fileSize();
13+
DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
14+
}
15+
DBG_OUTPUT_PORT.printf("\n");
16+
}
17+
18+
19+
20+
void httpRequest(char *url) {
21+
// Declare an object of class HTTPClient
22+
HTTPClient http;
23+
// Specify request destination
24+
http.begin(url);
25+
// Send the request
26+
int httpCode = http.GET();
27+
// Check the returning code
28+
if (httpCode > 0) {
29+
// Get the request response payload
30+
String payload = http.getString();
31+
}
32+
// Close connection
33+
http.end();
34+
}
35+
36+
void httpPost(char *url, char *msg) {
37+
HTTPClient http; //Declare object of class HTTPClient
38+
39+
http.begin(url); //Specify request destination
40+
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
41+
42+
int httpCode = http.POST(msg); //Send the request
43+
String payload = http.getString(); //Get the response payload
44+
45+
Serial.println(httpCode); //Print HTTP return code
46+
Serial.println(payload); //Print request response payload
47+
48+
http.end(); //Close connection
49+
}
50+
51+
52+
//format bytes
53+
String formatBytes(size_t bytes) {
54+
if (bytes < 1024) {
55+
return String(bytes) + "B";
56+
} else if (bytes < (1024 * 1024)) {
57+
return String(bytes / 1024.0) + "KB";
58+
} else if (bytes < (1024 * 1024 * 1024)) {
59+
return String(bytes / 1024.0 / 1024.0) + "MB";
60+
} else {
61+
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB";
62+
}
63+
}
64+
65+
String getContentType(String filename) {
66+
if (server.hasArg("download")) {
67+
return "application/octet-stream";
68+
} else if (filename.endsWith(".htm")) {
69+
return "text/html";
70+
} else if (filename.endsWith(".html")) {
71+
return "text/html";
72+
} else if (filename.endsWith(".css")) {
73+
return "text/css";
74+
} else if (filename.endsWith(".js")) {
75+
return "application/javascript";
76+
} else if (filename.endsWith(".png")) {
77+
return "image/png";
78+
} else if (filename.endsWith(".gif")) {
79+
return "image/gif";
80+
} else if (filename.endsWith(".jpg")) {
81+
return "image/jpeg";
82+
} else if (filename.endsWith(".ico")) {
83+
return "image/x-icon";
84+
} else if (filename.endsWith(".xml")) {
85+
return "text/xml";
86+
} else if (filename.endsWith(".pdf")) {
87+
return "application/x-pdf";
88+
} else if (filename.endsWith(".zip")) {
89+
return "application/x-zip";
90+
} else if (filename.endsWith(".gz")) {
91+
return "application/x-gzip";
92+
}
93+
return "text/plain";
94+
}
95+
96+
bool handleFileRead(String path) {
97+
DBG_OUTPUT_PORT.println("handleFileRead: " + path);
98+
if (path.endsWith("/")) {
99+
path += "index.htm";
100+
}
101+
String contentType = getContentType(path);
102+
String pathWithGz = path + ".gz";
103+
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
104+
if (SPIFFS.exists(pathWithGz)) {
105+
path += ".gz";
106+
}
107+
File file = SPIFFS.open(path, "r");
108+
server.streamFile(file, contentType);
109+
file.close();
110+
return true;
111+
}
112+
return false;
113+
}
114+
115+
//holds the current upload
116+
File fsUploadFile;
117+
118+
void handleFileUpload() {
119+
if (server.uri() != "/edit") {
120+
return;
121+
}
122+
HTTPUpload& upload = server.upload();
123+
if (upload.status == UPLOAD_FILE_START) {
124+
String filename = upload.filename;
125+
if (!filename.startsWith("/")) {
126+
filename = "/" + filename;
127+
}
128+
DBG_OUTPUT_PORT.print("handleFileUpload Name: "); DBG_OUTPUT_PORT.println(filename);
129+
fsUploadFile = SPIFFS.open(filename, "w");
130+
filename = String();
131+
} else if (upload.status == UPLOAD_FILE_WRITE) {
132+
//DBG_OUTPUT_PORT.print("handleFileUpload Data: "); DBG_OUTPUT_PORT.println(upload.currentSize);
133+
if (fsUploadFile) {
134+
fsUploadFile.write(upload.buf, upload.currentSize);
135+
}
136+
} else if (upload.status == UPLOAD_FILE_END) {
137+
if (fsUploadFile) {
138+
fsUploadFile.close();
139+
}
140+
DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize);
141+
}
142+
}
143+
144+
void handleFileDelete() {
145+
if (server.args() == 0) {
146+
return server.send(500, "text/plain", "BAD ARGS");
147+
}
148+
String path = server.arg(0);
149+
DBG_OUTPUT_PORT.println("handleFileDelete: " + path);
150+
if (path == "/") {
151+
return server.send(500, "text/plain", "BAD PATH");
152+
}
153+
if (!SPIFFS.exists(path)) {
154+
return server.send(404, "text/plain", "FileNotFound");
155+
}
156+
SPIFFS.remove(path);
157+
server.send(200, "text/plain", "");
158+
path = String();
159+
}
160+
161+
void handleFileCreate() {
162+
if (server.args() == 0) {
163+
return server.send(500, "text/plain", "BAD ARGS");
164+
}
165+
String path = server.arg(0);
166+
DBG_OUTPUT_PORT.println("handleFileCreate: " + path);
167+
if (path == "/") {
168+
return server.send(500, "text/plain", "BAD PATH");
169+
}
170+
if (SPIFFS.exists(path)) {
171+
return server.send(500, "text/plain", "FILE EXISTS");
172+
}
173+
File file = SPIFFS.open(path, "w");
174+
if (file) {
175+
file.close();
176+
} else {
177+
return server.send(500, "text/plain", "CREATE FAILED");
178+
}
179+
server.send(200, "text/plain", "");
180+
path = String();
181+
}
182+
183+
void handleFileList() {
184+
if (!server.hasArg("dir")) {
185+
server.send(500, "text/plain", "BAD ARGS");
186+
return;
187+
}
188+
189+
String path = server.arg("dir");
190+
DBG_OUTPUT_PORT.println("handleFileList: " + path);
191+
Dir dir = SPIFFS.openDir(path);
192+
path = String();
193+
194+
String output = "[";
195+
while (dir.next()) {
196+
File entry = dir.openFile("r");
197+
if (output != "[") {
198+
output += ',';
199+
}
200+
bool isDir = false;
201+
output += "{\"type\":\"";
202+
output += (isDir) ? "dir" : "file";
203+
output += "\",\"name\":\"";
204+
output += String(entry.name()).substring(1);
205+
output += "\"}";
206+
entry.close();
207+
}
208+
209+
output += "]";
210+
server.send(200, "text/json", output);
211+
}
212+
213+

HttpMethods.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef __HTTP_METHODS_H__
2+
#define __HTTP_METHODS_H__
3+
4+
#include <ESP8266WebServer.h>
5+
6+
extern ESP8266WebServer server;
7+
8+
void listFiles();
9+
10+
void httpRequest(char *url);
11+
12+
void httpPost(char *url, char *msg);
13+
14+
15+
void handleFileDelete();
16+
17+
void handleFileCreate();
18+
19+
void handleFileList();
20+
21+
void handleFileUpload();
22+
23+
bool handleFileRead(String path);
24+
25+
String getContentType(String filename);
26+
27+
String formatBytes(size_t bytes);
28+
29+
#endif

I2CScanner.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* I2C Scanner routines
3+
*/
4+
#include <Wire.h>
5+
#include <Arduino.h> // for Serial
6+
7+
uint8_t portArray[] = {16, 5, 4, 0, 2, 14, 12, 13};
8+
//String portMap[] = {"D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7"}; //for Wemos
9+
String portMap[] = {"GPIO16", "GPIO5", "GPIO4", "GPIO0", "GPIO2", "GPIO14", "GPIO12", "GPIO13"};
10+
11+
void scanI2C() {
12+
byte error, address;
13+
int nDevices;
14+
nDevices = 0;
15+
for (address = 1; address < 127; address++ ) {
16+
// The i2c_scanner uses the return value of
17+
// the Write.endTransmisstion to see if
18+
// a device did acknowledge to the address.
19+
Wire.beginTransmission(address);
20+
error = Wire.endTransmission();
21+
22+
if (error == 0){
23+
Serial.print("I2C device found at address 0x");
24+
if (address < 16)
25+
Serial.print("0");
26+
Serial.print(address, HEX);
27+
Serial.println(" !");
28+
29+
nDevices++;
30+
} else if (error == 4) {
31+
Serial.print("Unknow error at address 0x");
32+
if (address < 16)
33+
Serial.print("0");
34+
Serial.println(address, HEX);
35+
}
36+
} //for loop
37+
if (nDevices == 0)
38+
Serial.println("No I2C devices found");
39+
else
40+
Serial.println("**********************************\n");
41+
//delay(1000); // wait 1 seconds for next scan, did not find it necessary
42+
}
43+
44+
45+
void scanAllI2C() {
46+
for (uint8_t i = 0; i < sizeof(portArray); i++) {
47+
for (uint8_t j = 0; j < sizeof(portArray); j++) {
48+
if (i != j){
49+
Serial.print("Scanning (SDA : SCL) - " + portMap[i] + " : " + portMap[j] + " - ");
50+
Wire.begin(portArray[i], portArray[j]);
51+
scanI2C();
52+
}
53+
}
54+
}
55+
}
56+
57+

I2CScanner.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef __I2C_SCANNER_H__
2+
#define __I2C_SCANNER_H__
3+
4+
/*
5+
* Function that tries different pin combinations for I2C bus configuration
6+
*/
7+
void scanAllI2C();
8+
9+
/*
10+
* Function that scans for I2C devices on current configured bus
11+
*/
12+
void scanI2C();
13+
14+
#endif

0 commit comments

Comments
 (0)