Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for receive and send TCP packages #1317

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0ba7c0b
Create fork for esphome
OttoWinter Oct 14, 2019
22420ca
Merge branch 'master' of https://github.com/me-no-dev/ESPAsyncWebServer
OttoWinter Oct 17, 2019
abb2204
Bump version to 1.2.4
OttoWinter Oct 17, 2019
3f098db
Use AsyncTCP until AsyncTCP-esphome is published
OttoWinter Oct 19, 2019
62cdde3
Bump version to 1.2.5
OttoWinter Oct 19, 2019
6c873b2
Update to use AsyncTCP-esphome
OttoWinter Nov 2, 2019
ae1258f
Bump version to 1.2.6
OttoWinter Nov 2, 2019
40c37b9
Fix EventSource ::send (#1)
OttoWinter Jul 29, 2020
32ab7bd
Bump version to v1.2.7
OttoWinter Jul 29, 2020
36e4fa0
Make LinkedList iterator safe against removal of cur item
depau Mar 7, 2021
c7b7faf
Fix build when using latest arduino-esp32 master due to IDF update
blackhack Jun 5, 2021
8a43124
Fix build when using WebSockets
blackhack Jun 6, 2021
c6e6462
Explicitly type cast to uint32_t to avoid ambiguity
agners Jul 22, 2021
468ac18
Merge remote-tracking branch 'depau/mem-corruption' into fix-build-wi…
agners Jul 23, 2021
5200b97
Update Author and URL in library.json
agners Jul 26, 2021
84e0ad4
Bump version to v1.3.0
agners Jul 26, 2021
57a77e1
Merge pull request #2 from agners/fix-build-with-arduino-esp32-2.0.0-rc1
OttoWinter Jul 26, 2021
e796381
Merge pull request #3 from agners/bump-properties
OttoWinter Jul 26, 2021
f2a0213
Fix CI
agners Jul 26, 2021
ef429f3
Merge pull request #4 from agners/fix-ci
OttoWinter Jul 26, 2021
762fdb9
Add owner fields to library.json
OttoWinter Oct 22, 2021
83ce0c6
Add dist to gitignore
OttoWinter Oct 22, 2021
2403369
Bump version to 1.3.1
OttoWinter Oct 22, 2021
ccd1ed0
Remove SPIFFSEditor
OttoWinter Oct 22, 2021
f4622e9
Bump version to 2.0.0
OttoWinter Oct 22, 2021
41ee322
ESP8266 Add ESP8266WiFi library requirement
OttoWinter Oct 22, 2021
fc585ca
Bump version to 2.0.1
OttoWinter Oct 22, 2021
ac0ee8d
Don't delete handlers when they're removed (#6)
oxan Nov 11, 2021
aa107a5
Bump version to 2.1.0
jesserockz Nov 11, 2021
d9bfc20
Adapt for LibreTuya compatibility (#7)
kuba2k2 Aug 9, 2022
4fd0a1f
Bump version to 3.0.0
jesserockz Aug 9, 2022
c4ced25
Allow user customization WS_MAX_QUEUED_MESSAGES (#10)
asjdf Feb 16, 2023
c22f062
fix typo on "if (!client->send();) return 0;" (#17)
bmedici Aug 17, 2023
4524dca
CI updates (#8)
jesserockz Sep 1, 2023
8c60a28
Rename LibreTuya to LibreTiny (#23)
kuba2k2 Sep 2, 2023
f2a65ff
Bump version to 3.1.0
jesserockz Sep 4, 2023
9c2034d
Add mutex for queue processing. (#24)
Fabian-Schmidt Mar 31, 2024
7f4759c
Switch ESPAsyncTCP-esphome to esphome owner (#27)
jesserockz May 6, 2024
d3b0639
Bump version to 3.1.1
jesserockz May 6, 2024
cf47c2b
Bump version to 3.2.0
jesserockz May 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/scripts/install-arduino-core-esp32.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ if [ ! -d "$ARDUINO_ESP32_PATH" ]; then
cd "$ARDUINO_USR_PATH/hardware/espressif"

echo "Installing Python Serial ..."
pip install pyserial > /dev/null
pip3 install pyserial > /dev/null

if [ "$OS_IS_WINDOWS" == "1" ]; then
echo "Installing Python Requests ..."
pip install requests > /dev/null
pip3 install requests > /dev/null
fi

if [ "$GITHUB_REPOSITORY" == "espressif/arduino-esp32" ]; then
Expand All @@ -28,7 +28,7 @@ if [ ! -d "$ARDUINO_ESP32_PATH" ]; then
git submodule update --init --recursive > /dev/null 2>&1

echo "Installing Platform Tools ..."
cd tools && python get.py
cd tools && python3 get.py
cd $script_init_path

echo "ESP32 Arduino has been installed in '$ARDUINO_ESP32_PATH'"
Expand Down
6 changes: 3 additions & 3 deletions .github/scripts/install-arduino-core-esp8266.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ mkdir -p "$ARDUINO_USR_PATH/hardware/esp8266com"
cd "$ARDUINO_USR_PATH/hardware/esp8266com"

echo "Installing Python Serial ..."
pip install pyserial > /dev/null
pip3 install pyserial > /dev/null

if [ "$OS_IS_WINDOWS" == "1" ]; then
echo "Installing Python Requests ..."
pip install requests > /dev/null
pip3 install requests > /dev/null
fi

echo "Cloning Core Repository ..."
Expand All @@ -22,7 +22,7 @@ git submodule update --init --recursive > /dev/null 2>&1

echo "Installing Platform Tools ..."
cd tools
python get.py > /dev/null
python3 get.py > /dev/null
cd $script_init_path

echo "ESP8266 Arduino has been installed in '$ARDUINO_USR_PATH/hardware/esp8266com'"
Expand Down
4 changes: 2 additions & 2 deletions .github/scripts/install-arduino-ide.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ if [ ! -d "$ARDUINO_IDE_PATH" ]; then
echo "Installing Arduino IDE on $OS_NAME ..."
echo "Downloading 'arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT' to 'arduino.$ARCHIVE_FORMAT' ..."
if [ "$OS_IS_LINUX" == "1" ]; then
wget -O "arduino.$ARCHIVE_FORMAT" "https://www.arduino.cc/download.php?f=/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
wget -O "arduino.$ARCHIVE_FORMAT" "https://downloads.arduino.cc/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..."
tar xf "arduino.$ARCHIVE_FORMAT" > /dev/null
mv arduino-nightly "$ARDUINO_IDE_PATH"
else
curl -o "arduino.$ARCHIVE_FORMAT" -L "https://www.arduino.cc/download.php?f=/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
curl -o "arduino.$ARCHIVE_FORMAT" -L "https://downloads.arduino.cc/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..."
unzip "arduino.$ARCHIVE_FORMAT" > /dev/null
if [ "$OS_IS_MACOS" == "1" ]; then
Expand Down
6 changes: 3 additions & 3 deletions .github/scripts/install-platformio.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

echo "Installing Python Wheel ..."
pip install wheel > /dev/null 2>&1
pip3 install wheel > /dev/null 2>&1

echo "Installing PlatformIO ..."
pip install -U platformio > /dev/null 2>&1
pip3 install -U platformio > /dev/null 2>&1

echo "PlatformIO has been installed"
echo ""
Expand All @@ -23,7 +23,7 @@ function build_pio_sketch(){ # build_pio_sketch <board> <path-to-ino> <build-fla
local sketch_dir=$(dirname "$sketch")
echo ""
echo "Compiling '"$(basename "$sketch")"' ..."
python -m platformio ci -l '.' --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv" --project-option="build_flags=$buildFlags"
python3 -m platformio ci -l '.' --board "$board" "$sketch_dir" --project-option="board_build.partitions = huge_app.csv" --project-option="build_flags=$buildFlags"
}

function count_sketches() # count_sketches <examples-path>
Expand Down
10 changes: 5 additions & 5 deletions .github/scripts/on-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if [ "$BUILD_PIO" -eq 0 ]; then
source ./.github/scripts/install-arduino-ide.sh

echo "Installing ESPAsyncWebServer ..."
cp -rf "$GITHUB_WORKSPACE" "$ARDUINO_USR_PATH/libraries/ESPAsyncWebServer"
cp -af "$GITHUB_WORKSPACE/src" "$ARDUINO_USR_PATH/libraries/ESPAsyncWebServer"
echo "Installing ArduinoJson ..."
git clone https://github.com/bblanchon/ArduinoJson "$ARDUINO_USR_PATH/libraries/ArduinoJson" > /dev/null 2>&1

Expand All @@ -53,18 +53,18 @@ else
# PlatformIO Test
source ./.github/scripts/install-platformio.sh

python -m platformio lib --storage-dir "$GITHUB_WORKSPACE" install
python3 -m platformio lib --storage-dir "$GITHUB_WORKSPACE" install
echo "Installing ArduinoJson ..."
python -m platformio lib -g install https://github.com/bblanchon/ArduinoJson.git > /dev/null 2>&1
python3 -m platformio lib -g install https://github.com/bblanchon/ArduinoJson.git > /dev/null 2>&1
if [[ "$TARGET_PLATFORM" == "esp32" ]]; then
BOARD="esp32dev"
echo "Installing AsyncTCP ..."
python -m platformio lib -g install https://github.com/me-no-dev/AsyncTCP.git > /dev/null 2>&1
python3 -m platformio lib -g install https://github.com/me-no-dev/AsyncTCP.git > /dev/null 2>&1
echo "BUILDING ESP32 EXAMPLES"
else
BOARD="esp12e"
echo "Installing ESPAsyncTCP ..."
python -m platformio lib -g install https://github.com/me-no-dev/ESPAsyncTCP.git > /dev/null 2>&1
python3 -m platformio lib -g install https://github.com/me-no-dev/ESPAsyncTCP.git > /dev/null 2>&1
echo "BUILDING ESP8266 EXAMPLES"
fi
build_pio_sketches "$BOARD" "$GITHUB_WORKSPACE/examples"
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ jobs:
name: Arduino for ${{ matrix.board }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
board: [esp32, esp8266]
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3.0.2
- name: Build Tests
run: bash ./.github/scripts/on-push.sh ${{ matrix.board }} 0 1

build-pio:
name: PlatformIO for ${{ matrix.board }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
board: [esp32, esp8266]
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3.0.2
- name: Build Tests
run: bash ./.github/scripts/on-push.sh ${{ matrix.board }} 1 1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.vscode
.DS_Store
/dist/

109 changes: 3 additions & 106 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# ESPAsyncWebServer
# ESPAsyncWebServer
[![Build Status](https://travis-ci.org/me-no-dev/ESPAsyncWebServer.svg?branch=master)](https://travis-ci.org/me-no-dev/ESPAsyncWebServer) ![](https://github.com/me-no-dev/ESPAsyncWebServer/workflows/ESP%20Async%20Web%20Server%20CI/badge.svg) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/395dd42cfc674e6ca2e326af3af80ffc)](https://www.codacy.com/manual/me-no-dev/ESPAsyncWebServer?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=me-no-dev/ESPAsyncWebServer&amp;utm_campaign=Badge_Grade)

A fork of the [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) library by [@me-no-dev](https://github.com/me-no-dev) for [ESPHome](https://esphome.io).

For help and support [![Join the chat at https://gitter.im/me-no-dev/ESPAsyncWebServer](https://badges.gitter.im/me-no-dev/ESPAsyncWebServer.svg)](https://gitter.im/me-no-dev/ESPAsyncWebServer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Async HTTP and WebSocket Server for ESP8266 Arduino
Expand Down Expand Up @@ -524,56 +526,6 @@ response->addHeader("Server","ESP Async Web Server");
request->send(response);
```

### Respond with content coming from a File
```cpp
//Send index.htm with default content type
request->send(SPIFFS, "/index.htm");

//Send index.htm as text
request->send(SPIFFS, "/index.htm", "text/plain");

//Download index.htm
request->send(SPIFFS, "/index.htm", String(), true);
```

### Respond with content coming from a File and extra headers
```cpp
//Send index.htm with default content type
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/index.htm");

//Send index.htm as text
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/index.htm", "text/plain");

//Download index.htm
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, "/index.htm", String(), true);

response->addHeader("Server","ESP Async Web Server");
request->send(response);
```

### Respond with content coming from a File containing templates
Internally uses [Chunked Response](#chunked-response).

Index.htm contents:
```
%HELLO_FROM_TEMPLATE%
```

Somewhere in source files:
```cpp
String processor(const String& var)
{
if(var == "HELLO_FROM_TEMPLATE")
return F("Hello world!");
return String();
}

// ...

//Send index.htm with template processor function
request->send(SPIFFS, "/index.htm", String(), false, processor);
```

### Respond with content using a callback
```cpp
//send 128 bytes as plain text
Expand Down Expand Up @@ -771,41 +723,6 @@ response->setLength();
request->send(response);
```

## Serving static files
In addition to serving files from SPIFFS as described above, the server provide a dedicated handler that optimize the
performance of serving files from SPIFFS - ```AsyncStaticWebHandler```. Use ```server.serveStatic()``` function to
initialize and add a new instance of ```AsyncStaticWebHandler``` to the server.
The Handler will not handle the request if the file does not exists, e.g. the server will continue to look for another
handler that can handle the request.
Notice that you can chain setter functions to setup the handler, or keep a pointer to change it at a later time.

### Serving specific file by name
```cpp
// Serve the file "/www/page.htm" when request url is "/page.htm"
server.serveStatic("/page.htm", SPIFFS, "/www/page.htm");
```

### Serving files in directory
To serve files in a directory, the path to the files should specify a directory in SPIFFS and ends with "/".
```cpp
// Serve files in directory "/www/" when request url starts with "/"
// Request to the root or none existing files will try to server the defualt
// file name "index.htm" if exists
server.serveStatic("/", SPIFFS, "/www/");

// Server with different default file
server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("default.html");
```

### Serving static files with authentication

```cpp
server
.serveStatic("/", SPIFFS, "/www/")
.setDefaultFile("default.html")
.setAuthentication("user", "pass");
```

### Specifying Cache-Control header
It is possible to specify Cache-Control header value to reduce the number of calls to the server once the client loaded
the files. For more information on Cache-Control values see [Cache-Control](https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9)
Expand Down Expand Up @@ -918,20 +835,6 @@ Two filter callback are provided for convince:
* `ON_STA_FILTER` - return true when requests are made to the STA (station mode) interface.
* `ON_AP_FILTER` - return true when requests are made to the AP (access point) interface.

### Serve different site files in AP mode
```cpp
server.serveStatic("/", SPIFFS, "/www/").setFilter(ON_STA_FILTER);
server.serveStatic("/", SPIFFS, "/ap/").setFilter(ON_AP_FILTER);
```

### Rewrite to different index on AP
```cpp
// Serve the file "/www/index-ap.htm" in AP, and the file "/www/index.htm" on STA
server.rewrite("/", "index.htm");
server.rewrite("/index.htm", "index-ap.htm").setFilter(ON_AP_FILTER);
server.serveStatic("/", SPIFFS, "/www/");
```

### Serving different hosts
```cpp
// Filter callback using request host
Expand Down Expand Up @@ -1358,9 +1261,6 @@ void setup(){
}
});

// attach filesystem root at URL /fs
server.serveStatic("/fs", SPIFFS, "/");

// Catch-All Handlers
// Any request that can not find a Handler that canHandle it
// ends in the callbacks below.
Expand Down Expand Up @@ -1443,9 +1343,6 @@ Example of OTA code
```cpp
// OTA callbacks
ArduinoOTA.onStart([]() {
// Clean SPIFFS
SPIFFS.end();

// Disable client connections
ws.enable(false);

Expand Down
Loading