From 79a4b922faa05b19c33549680814f205b6d81df3 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 15 Dec 2015 20:38:31 +0200 Subject: [PATCH 1/6] Add "Contents" block --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index a78c655fe..fdb199842 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,16 @@ This project brings support for ESP8266 chip to the Arduino environment. It lets ESP8266 Arduino core comes with libraries to communicate over WiFi using TCP and UDP, set up HTTP, mDNS, SSDP, and DNS servers, do OTA updates, use a file system in flash memory, work with SD cards, servos, SPI and I2C peripherals. +# Contents +- Installing options: + - [Using Boards Manager](#installing-with-boards-manager) + - [Using git version](#using-git-version-) + - [Using stable version with PlatformIO](#using-stable-version-with-platformio) +- [Documentation](#documentation) +- [Issues and support](#issues-and-support) +- [Contributing](#contributing) +- [License and credits](#license-and-credits) + ### Installing with Boards Manager ### Starting with 1.6.4, Arduino allows installation of third-party platform packages using Boards Manager. We have packages available for Windows, Mac OS, and Linux (32 and 64 bit). From 7e1a04a41b1b4fa94f59a463391685ac00840f98 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 15 Dec 2015 20:45:40 +0200 Subject: [PATCH 2/6] More explanations about OTA uploading --- doc/platformio.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/platformio.md b/doc/platformio.md index a03500bb3..b086c1313 100644 --- a/doc/platformio.md +++ b/doc/platformio.md @@ -28,6 +28,8 @@ platformio boards espressif # Initialise base project # platformio init --board %TYPE%(see above) +# for example, initialise project for ESP8266 ESP-12E board (NodeMCU) +platformio init --board esp12e # The next files/directories will be created in myproject # platformio.ini - Project Configuration File. |-> PLEASE EDIT ME <-| @@ -39,16 +41,28 @@ platformio init --board %TYPE%(see above) - Place your source code to `src` directory - Build/Upload project -``` +```bash # process/build project platformio run # build+upload firmware platformio run --target upload +``` -# build+upload firmware via OTA +## OTA firmware uploading + +There are 2 options: + +1. Directly specify `--upoad-port` in command line +```bash platformio run --target upload --upload-port IP_ADDRESS_HERE ``` +2. Specify [upload_port](http://docs.platformio.org/en/latest/projectconf.html#upload-port) option in `platformio.ini` +```ini +[env:***] +... +upload_port = IP_ADDRESS_HERE +``` ## IDE Integration In addition, PlatformIO [can be integrated into the popular IDEs](http://docs.platformio.org/en/latest/ide.html). For example, initialise project for Espressif ESP8266 ESP-01 board and Eclipse IDE From 94fd0333febc180461b684a25bb7d5eed3aab1ef Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Tue, 15 Dec 2015 22:28:57 +0200 Subject: [PATCH 3/6] Add authentication and upload options --- doc/platformio.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/doc/platformio.md b/doc/platformio.md index b086c1313..c03edae7e 100644 --- a/doc/platformio.md +++ b/doc/platformio.md @@ -53,17 +53,33 @@ platformio run --target upload There are 2 options: -1. Directly specify `--upoad-port` in command line +- Directly specify `--upoad-port` in command line ```bash platformio run --target upload --upload-port IP_ADDRESS_HERE ``` -2. Specify [upload_port](http://docs.platformio.org/en/latest/projectconf.html#upload-port) option in `platformio.ini` +- Specify [upload_port](http://docs.platformio.org/en/latest/projectconf.html#upload-port) option in `platformio.ini` ```ini [env:***] ... upload_port = IP_ADDRESS_HERE ``` +### Authentication and upload options + +You can pass additional options/flags to OTA uploader using [upload_flags](http://docs.platformio.org/en/latest/projectconf.html#upload-flags) option in `platformio.ini` +```ini +[env:***] +upload_flags = --port=8266 +``` + +Availalbe flags +- `--port=ESP_PORT` ESP8266 ota Port. Default 8266 +- `--auth=AUTH` Set authentication password +- `--spiffs` Use this option to transmit a SPIFFS image and do not flash the module + +For the full list with availalbe options please run this command `~/.platformio/packages/framework-arduinoespressif/tools/espota.py -h`. + + ## IDE Integration In addition, PlatformIO [can be integrated into the popular IDEs](http://docs.platformio.org/en/latest/ide.html). For example, initialise project for Espressif ESP8266 ESP-01 board and Eclipse IDE ``` From b8769bf5ebee82a957dfe08f828cef4ca4c4ac81 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Wed, 16 Dec 2015 00:32:17 +0100 Subject: [PATCH 4/6] HTTP Client move buffer (1460 Byte) from stack to heap. --- .../ReuseConnection/ReuseConnection.ino | 1 - .../src/ESP8266HTTPClient.cpp | 105 ++++++++++-------- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 4 + 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino index 35babdc33..631b9494d 100644 --- a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino +++ b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino @@ -60,7 +60,6 @@ void loop() { } http.end(); - } delay(1000); diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 6a8d010e1..3a96e4327 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -30,7 +30,6 @@ #include "ESP8266HTTPClient.h" - /** * constractor */ @@ -117,7 +116,7 @@ void HTTPClient::begin(String url, String httpsFingerprint) { if(index >= 0) { // auth info String auth = host.substring(0, index); - host.remove(0, index +1); // remove auth part including @ + host.remove(0, index + 1); // remove auth part including @ _base64Authorization = base64::encode(auth); } @@ -336,7 +335,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { } // create buffer for read - uint8_t buff[1460] = { 0 }; + uint8_t * buff = (uint8_t *) malloc(HTTP_TCP_BUFFER_SIZE); int len = size; int bytesWritten = 0; @@ -345,34 +344,40 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { len = -1; } - // read all data from stream and send it to server - while(connected() && stream->available() && (len > 0 || len == -1)) { + if(buff) { + // read all data from stream and send it to server + while(connected() && stream->available() && (len > 0 || len == -1)) { - // get available data size - size_t s = stream->available(); + // get available data size + size_t s = stream->available(); - if(s) { - int c = stream->readBytes(buff, ((s > sizeof(buff)) ? sizeof(buff) : s)); + if(s) { + int c = stream->readBytes(buff, ((s > HTTP_TCP_BUFFER_SIZE) ? HTTP_TCP_BUFFER_SIZE : s)); - // write it to Stream - bytesWritten += _tcp->write((const uint8_t *)buff, c); + // write it to Stream + bytesWritten += _tcp->write((const uint8_t *) buff, c); - if(len > 0) { - len -= c; + if(len > 0) { + len -= c; + } + + delay(0); + } else { + delay(1); } - - delay(0); - } else { - delay(1); } - } - if(size && (int)size != bytesWritten) { - DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); - DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!"); - return HTTPC_ERROR_SEND_PAYLOAD_FAILED; + if(size && (int) size != bytesWritten) { + DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!"); + free(buff); + return HTTPC_ERROR_SEND_PAYLOAD_FAILED; + } else { + DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten); + } + free(buff); } else { - DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten); + DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE); + return HTTPC_ERROR_TOO_LESS_RAM; } // handle Server Response (Header) @@ -434,37 +439,44 @@ int HTTPClient::writeToStream(Stream * stream) { int len = _size; int bytesWritten = 0; + // create buffer for read - uint8_t buff[1460] = { 0 }; + uint8_t * buff = (uint8_t *) malloc(HTTP_TCP_BUFFER_SIZE); - // read all data from server - while(connected() && (len > 0 || len == -1)) { + if(buff) { + // read all data from server + while(connected() && (len > 0 || len == -1)) { - // get available data size - size_t size = _tcp->available(); + // get available data size + size_t size = _tcp->available(); - if(size) { - int c = _tcp->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); + if(size) { + int c = _tcp->readBytes(buff, ((size > HTTP_TCP_BUFFER_SIZE) ? HTTP_TCP_BUFFER_SIZE : size)); - // write it to Stream - bytesWritten += stream->write(buff, c); + // write it to Stream + bytesWritten += stream->write(buff, c); - if(len > 0) { - len -= c; + if(len > 0) { + len -= c; + } + + delay(0); + } else { + delay(1); } - - delay(0); - } else { - delay(1); } + + free(buff); + + DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] connection closed or file end (written: %d).\n", bytesWritten); + + if(_size && _size != bytesWritten) { + DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); + } + + } else { + DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE); } - - DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] connection closed or file end (written: %d).\n", bytesWritten); - - if(_size && _size != bytesWritten) { - DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); - } - end(); return bytesWritten; } @@ -509,12 +521,13 @@ String HTTPClient::errorToString(int error) { return String("no stream"); case HTTPC_ERROR_NO_HTTP_SERVER: return String("no HTTP server"); + case HTTPC_ERROR_TOO_LESS_RAM: + return String("too less ram"); default: return String(); } } - /** * adds Header to the request * @param name diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 2bf6d0565..5f7490f86 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -41,6 +41,10 @@ #define HTTPC_ERROR_CONNECTION_LOST (-5) #define HTTPC_ERROR_NO_STREAM (-6) #define HTTPC_ERROR_NO_HTTP_SERVER (-7) +#define HTTPC_ERROR_TOO_LESS_RAM (-8) + +/// size for the stream handling +#define HTTP_TCP_BUFFER_SIZE (1460) /// HTTP codes see RFC7231 typedef enum { From b9d0807a56a924a83d63bab57f2919bb5730c2b3 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Wed, 16 Dec 2015 00:48:35 +0100 Subject: [PATCH 5/6] only malloc needed ram if we know the response size and its less then 1460 --- .../src/ESP8266HTTPClient.cpp | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 3a96e4327..5cddaf616 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -334,8 +334,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { return HTTPC_ERROR_SEND_HEADER_FAILED; } - // create buffer for read - uint8_t * buff = (uint8_t *) malloc(HTTP_TCP_BUFFER_SIZE); + size_t buff_size = HTTP_TCP_BUFFER_SIZE; int len = size; int bytesWritten = 0; @@ -344,6 +343,15 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { len = -1; } + // if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE + if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) { + buff_size = len; + } + + // create buffer for read + uint8_t * buff = (uint8_t *) malloc(buff_size); + + if(buff) { // read all data from stream and send it to server while(connected() && stream->available() && (len > 0 || len == -1)) { @@ -352,7 +360,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { size_t s = stream->available(); if(s) { - int c = stream->readBytes(buff, ((s > HTTP_TCP_BUFFER_SIZE) ? HTTP_TCP_BUFFER_SIZE : s)); + int c = stream->readBytes(buff, ((s > buff_size) ? buff_size : s)); // write it to Stream bytesWritten += _tcp->write((const uint8_t *) buff, c); @@ -367,14 +375,16 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) { } } + free(buff); + if(size && (int) size != bytesWritten) { - DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!"); - free(buff); + DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); + DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!"); return HTTPC_ERROR_SEND_PAYLOAD_FAILED; } else { DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten); } - free(buff); + } else { DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE); return HTTPC_ERROR_TOO_LESS_RAM; @@ -439,9 +449,15 @@ int HTTPClient::writeToStream(Stream * stream) { int len = _size; int bytesWritten = 0; + size_t buff_size = HTTP_TCP_BUFFER_SIZE; + + // if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE + if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) { + buff_size = len; + } // create buffer for read - uint8_t * buff = (uint8_t *) malloc(HTTP_TCP_BUFFER_SIZE); + uint8_t * buff = (uint8_t *) malloc(buff_size); if(buff) { // read all data from server @@ -451,7 +467,7 @@ int HTTPClient::writeToStream(Stream * stream) { size_t size = _tcp->available(); if(size) { - int c = _tcp->readBytes(buff, ((size > HTTP_TCP_BUFFER_SIZE) ? HTTP_TCP_BUFFER_SIZE : size)); + int c = _tcp->readBytes(buff, ((size > buff_size) ? buff_size : size)); // write it to Stream bytesWritten += stream->write(buff, c); @@ -476,7 +492,9 @@ int HTTPClient::writeToStream(Stream * stream) { } else { DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE); + return HTTPC_ERROR_TOO_LESS_RAM; } + end(); return bytesWritten; } From 481d74ba862f6e5762d5babb7cbc1bfb71f0b749 Mon Sep 17 00:00:00 2001 From: Martin Ayotte Date: Wed, 16 Dec 2015 12:06:29 -0500 Subject: [PATCH 6/6] add show_hidden argument to scanNetworks() --- libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 4 ++-- libraries/ESP8266WiFi/src/ESP8266WiFi.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp index 84f78e590..25544f858 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -561,7 +561,7 @@ void ESP8266WiFiClass::scanDelete() _scanComplete = false; } -int8_t ESP8266WiFiClass::scanNetworks(bool async) +int8_t ESP8266WiFiClass::scanNetworks(bool async, bool show_hidden) { if(ESP8266WiFiClass::_scanStarted) { return WIFI_SCAN_RUNNING; @@ -589,7 +589,7 @@ int8_t ESP8266WiFiClass::scanNetworks(bool async) config.ssid = 0; config.bssid = 0; config.channel = 0; - config.show_hidden = 0; + config.show_hidden = show_hidden; if(wifi_station_scan(&config, reinterpret_cast(&ESP8266WiFiClass::_scanDone))) { ESP8266WiFiClass::_scanComplete = false; ESP8266WiFiClass::_scanStarted = true; diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/libraries/ESP8266WiFi/src/ESP8266WiFi.h index 0d732ab4c..2623b2fb6 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFi.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -240,7 +240,7 @@ public: * * return: Number of discovered networks */ - int8_t scanNetworks(bool async = false); + int8_t scanNetworks(bool async = false, bool show_hidden = false); /* * Return the SSID discovered during the network scan.