From b493f01b394581a70be50e230e9e91ce243d7cd1 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 22 May 2015 14:56:39 +0300 Subject: [PATCH 1/6] Update README.md Mention the new pubsubclient library. [ci skip] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 65ed84f9d..373b645a4 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ See attached example and library README file for details. Libraries that don't rely on low-level access to AVR registers should work well. Here are a few libraries that were verified to work: - [aREST](https://github.com/marcoschwartz/aREST) REST API handler library. -- [PubSubClient](https://github.com/knolleary/pubsubclient) MQTT library. Use this [sample](https://gist.github.com/igrr/7f7e7973366fc01d6393) to get started. +- [PubSubClient](https://github.com/Imroy/pubsubclient) MQTT library by @Imroy. - [DHT11](https://github.com/adafruit/DHT-sensor-library) - initialize DHT as follows: ```DHT dht(DHTPIN, DHTTYPE, 15);``` - [DallasTemperature](https://github.com/milesburton/Arduino-Temperature-Control-Library.git) - [NeoPixelBus](https://github.com/Makuna/NeoPixelBus) - Arduino NeoPixel library compatible with esp8266. From 21d50e104cf29fe6b0dbe3b00aee26949ad34b2c Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 22 May 2015 16:19:08 +0300 Subject: [PATCH 2/6] Web server: disconnect at the end of callback, not after sendContent (#304) --- libraries/ESP8266WebServer/src/ESP8266WebServer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp index b96043772..c851a7cfb 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp @@ -172,10 +172,6 @@ void ESP8266WebServer::sendContent(String content) { break; } } - uint16_t maxWait = HTTP_MAX_CLOSE_WAIT; - while(_currentClient.connected() && maxWait--) { - delay(1); - } } String ESP8266WebServer::arg(const char* name) { @@ -245,6 +241,10 @@ void ESP8266WebServer::_handleRequest() { } } + uint16_t maxWait = HTTP_MAX_CLOSE_WAIT; + while(_currentClient.connected() && maxWait--) { + delay(1); + } _currentClient = WiFiClient(); _currentUri = String(); } From 8fdb824e11d4d74100f3f43bd5cecb42b8894da4 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 22 May 2015 17:57:30 +0300 Subject: [PATCH 3/6] Add setContentLength method to web server, update examples related to #304 --- .../examples/FSWebServer/FSWebServer.ino | 65 ++++++++++++------- .../examples/SDWebServer/SDWebServer.ino | 13 +--- .../ESP8266WebServer/src/ESP8266WebServer.cpp | 9 ++- .../ESP8266WebServer/src/ESP8266WebServer.h | 33 ++++------ 4 files changed, 62 insertions(+), 58 deletions(-) diff --git a/libraries/ESP8266WebServer/examples/FSWebServer/FSWebServer.ino b/libraries/ESP8266WebServer/examples/FSWebServer/FSWebServer.ino index fccf2b3eb..12d7a20c2 100644 --- a/libraries/ESP8266WebServer/examples/FSWebServer/FSWebServer.ino +++ b/libraries/ESP8266WebServer/examples/FSWebServer/FSWebServer.ino @@ -106,36 +106,54 @@ void handleFileUpdate(){ } void handleFileDelete(){ - if(server.args() == 0) return server.send(500, "text/plain", "BAD ARGS"); + if(server.args() == 0) { + server.send(500, "text/plain", "BAD ARGS"); + return; + } String path = server.arg(0); - if(path == "/") - return server.send(500, "text/plain", "BAD PATH"); - if(!FS.exists((char *)(path.c_str()))) - return server.send(404, "text/plain", "FileNotFound"); + if(path == "/") { + server.send(500, "text/plain", "BAD PATH"); + return; + } + if(!FS.exists((char *)(path.c_str()))) { + server.send(404, "text/plain", "FileNotFound"); + return; + } FS.remove((char *)path.c_str()); server.send(200, "text/plain", ""); path = String(); } void handleFileCreate(){ - if(server.args() == 0) - return server.send(500, "text/plain", "BAD ARGS"); + if(server.args() == 0) { + server.send(500, "text/plain", "BAD ARGS"); + return; + } String path = server.arg(0); - if(path == "/") - return server.send(500, "text/plain", "BAD PATH"); - if(FS.exists((char *)path.c_str())) - return server.send(500, "text/plain", "FILE EXISTS"); + if(path == "/") { + server.send(500, "text/plain", "BAD PATH"); + return; + } + if(FS.exists((char *)path.c_str())) { + server.send(500, "text/plain", "FILE EXISTS"); + return; + } FSFile file = FS.open((char *)path.c_str(), FSFILE_OVERWRITE); if(file) file.close(); - else - return server.send(500, "text/plain", "CREATE FAILED"); + else { + server.send(500, "text/plain", "CREATE FAILED"); + return; + } server.send(200, "text/plain", ""); path = String(); } void handleFileList() { - if(!server.hasArg("dir")) return server.send(500, "text/plain", "BAD ARGS"); + if(!server.hasArg("dir")) { + server.send(500, "text/plain", "BAD ARGS"); + return; + } String path = server.arg("dir"); FSFile entry; @@ -148,19 +166,21 @@ void handleFileList() { } dir.rewindDirectory(); - WiFiClient client = server.client(); - client.print("HTTP/1.1 200 OK\r\nContent-Type: text/json\r\nConnection: close\r\n\r\n"); String output = "["; while(true){ entry = dir.openNextFile(); - if (!entry) break; + if (!entry) + break; + if(!FS.exists(entry.name())){ os_printf("Entry[%s] Not Exists!\n", entry.name()); entry.remove(); entry.close(); continue; } - if(output != "[") output += ','; + + if(output != "[") + output += ','; output += "{\"type\":\""; output += (entry.isDirectory())?"dir":"file"; output += "\",\"name\":\""; @@ -169,14 +189,9 @@ void handleFileList() { entry.close(); } dir.close(); - + output += "]"; - client.write(output.c_str(), output.length()); - output = String(); - uint16_t maxWait = HTTP_MAX_CLOSE_WAIT; - while(client.connected() && maxWait--) { - delay(1); - } + server.send(200, "text/json", output); } void setup(void){ diff --git a/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino b/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino index d0e4e1d35..557b0788b 100644 --- a/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino +++ b/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino @@ -85,16 +85,9 @@ bool loadFromSdCard(String path){ if (!dataFile) return false; - if(server.hasArg("download")) dataType = "application/octet-stream"; + if (server.hasArg("download")) dataType = "application/octet-stream"; - server.sendHeader("Content-Length", String(dataFile.size())); - server.sendHeader("Connection", "close"); - server.sendHeader("Access-Control-Allow-Origin", "*"); - server.send(200, dataType.c_str(), ""); - - WiFiClient client = server.client(); - size_t totalSize = dataFile.size(); - if (client.write(dataFile, HTTP_DOWNLOAD_UNIT_SIZE) != totalSize) { + if (server.streamFile(dataFile, dataType) != dataFile.size()) { DBG_OUTPUT_PORT.println("Sent less data than expected!"); } @@ -187,7 +180,7 @@ void printDirectory() { return returnFail("NOT DIR"); } dir.rewindDirectory(); - + server.setContentSize(CONTENT_SIZE_UNKNOWN); server.send(200, "text/json", ""); WiFiClient client = server.client(); diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp index c851a7cfb..5c94ee818 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.cpp @@ -111,6 +111,7 @@ void ESP8266WebServer::handleClient() } _currentClient = client; + _contentLength = CONTENT_LENGTH_NOT_SET; _handleRequest(); } @@ -138,9 +139,13 @@ void ESP8266WebServer::send(int code, const char* content_type, String content) if (!content_type) content_type = "text/html"; - String len(content.length()); sendHeader("Content-Type", content_type, true); - sendHeader("Content-Length", len.c_str()); + if (_contentLength != CONTENT_LENGTH_UNKNOWN) { + size_t length = (_contentLength == CONTENT_LENGTH_NOT_SET) ? + content.length() : _contentLength; + String lengthStr(length); + sendHeader("Content-Length", lengthStr.c_str()); + } sendHeader("Connection", "close"); sendHeader("Access-Control-Allow-Origin", "*"); diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 0b553b518..d787e4e3f 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -34,6 +34,9 @@ enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END }; #define HTTP_MAX_DATA_WAIT 1000 //ms to wait for the client to send the request #define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection +#define CONTENT_LENGTH_UNKNOWN ((size_t) -1) +#define CONTENT_LENGTH_NOT_SET ((size_t) -2) + typedef struct { HTTPUploadStatus status; String filename; @@ -78,32 +81,19 @@ public: void send(int code, char* content_type, String content); void send(int code, String content_type, String content); + void setContentLength(size_t contentLength) { _contentLength = contentLength; } void sendHeader(String name, String value, bool first = false); void sendContent(String content); template size_t streamFile(T &file, String contentType){ - String head = "HTTP/1.1 200 OK\r\nContent-Type: "; - head += contentType; - head += "\r\nContent-Length: "; - head += file.size(); - head += "\r\nConnection: close"; - head += "\r\nAccess-Control-Allow-Origin: *"; - if( - String(file.name()).endsWith(".gz") && - contentType != "application/x-gzip" && - contentType != "application/octet-stream" - ){ - head += "\r\nContent-Encoding: gzip"; + setContentLength(file.size()); + if (String(file.name()).endsWith(".gz") && + contentType != "application/x-gzip" && + contentType != "application/octet-stream"){ + sendHeader("Content-Encoding", "gzip"); } - head += "\r\n\r\n"; - _currentClient.print(head); - head = String(); - size_t res = _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE); - uint16_t maxWait = HTTP_MAX_CLOSE_WAIT; - while(_currentClient.connected() && maxWait--) { - delay(1); - } - return res; + send(200, contentType, ""); + return _currentClient.write(file, HTTP_DOWNLOAD_UNIT_SIZE); } protected: @@ -131,6 +121,7 @@ protected: RequestArgument* _currentArgs; HTTPUpload _currentUpload; + size_t _contentLength; String _responseHeaders; RequestHandler* _firstHandler; From f7dbb35c922125960e6bc74b50817181277baf70 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 22 May 2015 18:11:09 +0300 Subject: [PATCH 4/6] Fix case of WiFi.channel() method --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 4 ++-- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 8 ++++---- libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 1ec6f8b36..5a3db6143 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -251,7 +251,7 @@ uint8_t* ESP8266WiFiClass::BSSID(void) return reinterpret_cast(conf.bssid); } -int32_t ESP8266WiFiClass::Channel(void) { +int32_t ESP8266WiFiClass::channel(void) { return wifi_get_channel(); } @@ -353,7 +353,7 @@ uint8_t * ESP8266WiFiClass::BSSID(uint8_t i) return it->bssid; } -int32_t ESP8266WiFiClass::Channel(uint8_t i) +int32_t ESP8266WiFiClass::channel(uint8_t i) { struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); if (!it) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index f1a2977c3..1a3a7b366 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -160,7 +160,7 @@ public: * * return: channel */ - int32_t Channel(void); + int32_t channel(void); /* * Return the current network RSSI. Note: this is just a stub, there is no way to @@ -214,11 +214,11 @@ public: uint8_t * BSSID(uint8_t networkItem); /** - * return Channel of scanned wifi + * return channel of scanned wifi * @param networkItem specify from which network item want to get the information - * @return uint32_t Channel of scanned wifi + * @return uint32_t channel of scanned wifi */ - int32_t Channel(uint8_t networkItem); + int32_t channel(uint8_t networkItem); /** * return if the scanned wifi is Hidden (no SSID) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp index 7aab82ae0..e0a7ddeb2 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp @@ -123,7 +123,7 @@ wl_status_t ESP8266WiFiMulti::run(void) { DEBUG_WIFI_MULTI("[WIFI] SSID: %s\n", WiFi.SSID()); DEBUG_WIFI_MULTI("[WIFI] IP: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]); DEBUG_WIFI_MULTI("[WIFI] MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - DEBUG_WIFI_MULTI("[WIFI] Channel: %d\n", WiFi.Channel()); + DEBUG_WIFI_MULTI("[WIFI] Channel: %d\n", WiFi.channel()); break; case WL_NO_SSID_AVAIL: DEBUG_WIFI_MULTI("[WIFI] Connecting Faild AP not found.\n"); From 3422294a7f0c524d207230085f336756c5371500 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 22 May 2015 18:16:44 +0300 Subject: [PATCH 5/6] Fix typo --- libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino b/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino index 557b0788b..4e63f68e5 100644 --- a/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino +++ b/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino @@ -180,7 +180,7 @@ void printDirectory() { return returnFail("NOT DIR"); } dir.rewindDirectory(); - server.setContentSize(CONTENT_SIZE_UNKNOWN); + server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/json", ""); WiFiClient client = server.client(); From 8bd0ad465390a68a29c696320268488181b955bc Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 22 May 2015 18:40:34 +0300 Subject: [PATCH 6/6] Update change log --- changes.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/changes.md b/changes.md index afaa2a9b1..32fabfd53 100644 --- a/changes.md +++ b/changes.md @@ -1,5 +1,21 @@ # Change log +## Current version + +### Core + +- Updated I2C library to better handle repeated start for certain devices. + +### Libraries + +- ESP8266WebServer: add gzip streaming, fix sendContent behaviour, + add setContentSize method. +- ESP8266WiFi: add BSSID, channel, isHidden methods, fix AP/STA mode + selection (#28). + +### Tools + + ## 1.6.4-673-g8cd3697 May 22, 2015