Skip to content

Instantly share code, notes, and snippets.

@robertoostenveld
robertoostenveld / interface.c
Created September 3, 2017 20:17
snippet of code to be inserted in interface.c to replace gethostbyname
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(host, "http", &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
// server.on("/debug", HTTP_GET, dirList);
// server.on("/debug", HTTP_PUT, dirList);
// server.on("/debug", HTTP_POST, dirList);
void handleDebug() {
String message = "HTTP Request\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
@robertoostenveld
robertoostenveld / handleDirList.cpp
Last active January 8, 2017 10:07
ESP8266WebServer handler that returns SPIFFS directory list
// server.on("/dir", HTTP_GET, handleDirList);
void handleDirList() {
SPIFFS.begin();
String str = "";
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
str += dir.fileName();
str += " ";
str += dir.fileSize();
@robertoostenveld
robertoostenveld / handleNotFound.cpp
Last active January 8, 2017 10:12
ESP8266WebServer handler that serves static files from SPIFFS
// this serves all URLs that can be resolved to a file on the SPIFFS filesystem
// server.onNotFound(handleNotFound);
static String getContentType(const String& path) {
if (path.endsWith(".html")) return "text/html";
else if (path.endsWith(".htm")) return "text/html";
else if (path.endsWith(".css")) return "text/css";
else if (path.endsWith(".txt")) return "text/plain";
else if (path.endsWith(".js")) return "application/javascript";
else if (path.endsWith(".png")) return "image/png";