- Why do we need the new Async AsyncHTTPRequest_Teensy41 library
- Changelog
- Prerequisites
- Installation
- Packages' Patches
- HOWTO Fix
Multiple Definitions
Linker Error - Examples
- Example AsyncHTTPRequest
- Debug Terminal Output Samples
- Debug
- Troubleshooting
- Issues
- TO DO
- DONE
- Contributions and Thanks
- Contributing
- License and credits
- Copyright
Why do we need this Async AsyncHTTPRequest_Teensy41 library
- Asynchronous HTTP Request library for Teensy 4.1 using built-in QNEthernet
- Providing a subset of HTTP.
- Relying on
Teensy41_AsyncTCP library
- Methods similar in format and usage to XmlHTTPrequest in Javascript.
- GET, POST, PUT, PATCH, DELETE and HEAD
- Request and response headers
- Chunked response
- Single String response for short (<~5K) responses (heap permitting).
- Optional onData callback.
- Optional onReadyStatechange callback.
This library adds a simple HTTP layer on top of the Teensy41_AsyncTCP library
to facilitate REST communication from a Client to a Server. The paradigm is similar to the XMLHttpRequest
in Javascript, employing the notion of a ready-state progression through the transaction request.
Synchronization can be accomplished using callbacks on ready-state change, a callback on data receipt, or simply polling for ready-state change. Data retrieval can be incremental as received, or bulk retrieved when the transaction completes provided there is enough heap to buffer the entire response.
The underlying buffering uses a new xbuf
class. It handles both character and binary data. Class xbuf
uses a chain of small (64 byte) segments that are allocated and added to the tail as data is added and de-allocated from the head as data is read, achieving the same result as a dynamic circular buffer, limited only by the size of heap. The xbuf
implements indexOf
and readUntil
functions.
For short transactions, buffer space should not be an issue. In fact, it can be more economical than other methods that use larger fixed length buffers. Data is acked when retrieved by the caller, so there is some limited flow control to limit heap usage for larger transfers.
Request and response headers are handled in the typical fashion.
Chunked responses
are recognized and handled transparently
.
This library is based on, modified from:
- Teensy 4.1 using QNEthernet Library
Arduino IDE 1.8.19+
for Arduino.Teensy core v1.57+
for Teensy 4.1.QNEthernet Library version v0.17.0+
for Teensy 4.1 built-in Ethernet.Teensy41_AsyncTCP library v1.1.0+
to use Teensy 4.1 using QNEthernet Library. To install, check
The best and easiest way is to use Arduino Library Manager
. Search for AsyncHTTPRequest_Teensy41
, then select / install the latest version. You can also use this link for more detailed instructions.
- Navigate to AsyncHTTPRequest_Teensy41 page.
- Download the latest release
AsyncHTTPRequest_Teensy41-main.zip
. - Extract the zip file to
AsyncHTTPRequest_Teensy41-main
directory - Copy the whole
AsyncHTTPRequest_Teensy41-main
folder to Arduino libraries' directory such as~/Arduino/libraries/
.
- Install VS Code
- Install PlatformIO
- Install AsyncHTTPRequest_Teensy41 library by using Library Manager. Search for AsyncHTTPRequest_Teensy41 in Platform.io Author's Libraries
- Use included platformio.ini file from examples to ensure that all dependent libraries will installed automatically. Please visit documentation for the other options and examples at Project Configuration File
To be able to compile and run on Teensy boards, you have to copy the files in Packages_Patches for Teensy directory into Teensy hardware directory (./arduino-1.8.19/hardware/teensy/avr/boards.txt).
Supposing the Arduino version is 1.8.19. These files must be copied into the directory:
./arduino-1.8.19/hardware/teensy/avr/boards.txt
./arduino-1.8.19/hardware/teensy/avr/cores/teensy/Stream.h
./arduino-1.8.19/hardware/teensy/avr/cores/teensy3/Stream.h
./arduino-1.8.19/hardware/teensy/avr/cores/teensy4/Stream.h
Whenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz These files must be copied into the directory:
./arduino-x.yy.zz/hardware/teensy/avr/boards.txt
./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy/Stream.h
./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy3/Stream.h
./arduino-x.yy.zz/hardware/teensy/avr/cores/teensy4/Stream.h
The current library implementation, using xyz-Impl.h
instead of standard xyz.cpp
, possibly creates certain Multiple Definitions
Linker error in certain use cases.
You can include this .hpp
file
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "AsyncHTTPRequest_Teensy41.hpp" //https://github.com/khoih-prog/AsyncHTTPRequest_Teensy41
in many files. But be sure to use the following .h
file in just 1 .h
, .cpp
or .ino
file, which must not be included in any other file, to avoid Multiple Definitions
Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "AsyncHTTPRequest_Teensy41.h" //https://github.com/khoih-prog/AsyncHTTPRequest_Teensy41
Check the new multiFileProject example for a HOWTO
demo.
Have a look at the discussion in Different behaviour using the src_cpp or src_h lib #80
Example AsyncHTTPRequest
1. File AsyncHTTPRequest.ino
AsyncHTTPRequest_Teensy41/examples/AsyncHTTPRequest/AsyncHTTPRequest.ino
Lines 38 to 184 in 7a4784a
2. File defines.h
AsyncHTTPRequest_Teensy41/examples/AsyncHTTPRequest/defines.h
Lines 16 to 50 in 7a4784a
1. AsyncHTTPRequest on TEENSY 4.1
Start AsyncHTTPRequest on TEENSY 4.1
AsyncHTTPRequest_Teensy41 v1.10.0
Initialize Ethernet using DHCP => Connected! IP address:192.168.2.107
**************************************
abbreviation: EST
client_ip: aaa.bbb.ccc.ddd
datetime: 2023-01-31T23:54:16.675525-05:00
day_of_week: 2
day_of_year: 31
dst: false
dst_from:
dst_offset: 0
dst_until:
raw_offset: -18000
timezone: America/Toronto
unixtime: 1675227256
utc_datetime: 2023-02-01T04:54:16.675525+00:00
utc_offset: -05:00
week_number: 5
**************************************
**************************************
abbreviation: EST
client_ip: aaa.bbb.ccc.ddd
datetime: 2023-01-31T23:55:16.675337-05:00
day_of_week: 2
day_of_year: 31
dst: false
dst_from:
dst_offset: 0
dst_until:
raw_offset: -18000
timezone: America/Toronto
unixtime: 1675227316
utc_datetime: 2023-02-01T04:55:16.675337+00:00
utc_offset: -05:00
week_number: 5
**************************************
2. AsyncCustomHeader on TEENSY 4.1
Start AsyncCustomHeader on TEENSY 4.1
AsyncHTTPRequest_Teensy41 v1.10.0
Initialize Ethernet using DHCP => Connected! IP address:192.168.2.107
Sending GET Request to http://worldtimeapi.org/api/timezone/America/Toronto.txt
**************************************
abbreviation: EST
client_ip: aaa.bbb.ccc.ddd
datetime: 2023-01-31T23:56:16.674942-05:00
day_of_week: 2
day_of_year: 31
dst: false
dst_from:
dst_offset: 0
dst_until:
raw_offset: -18000
timezone: America/Toronto
unixtime: 1675227376
utc_datetime: 2023-02-01T04:56:16.674942+00:00
utc_offset: -05:00
week_number: 5
**************************************
Sending GET Request to http://worldtimeapi.org/api/timezone/America/Toronto.txt
**************************************
abbreviation: EST
client_ip: aaa.bbb.ccc.ddd
datetime: 2023-01-31T23:57:16.675848-05:00
day_of_week: 2
day_of_year: 31
dst: false
dst_from:
dst_offset: 0
dst_until:
raw_offset: -18000
timezone: America/Toronto
unixtime: 1675227436
utc_datetime: 2023-02-01T04:57:16.675848+00:00
utc_offset: -05:00
week_number: 5
**************************************
3. AsyncDweetGET on TEENSY 4.1
Start AsyncDweetGET on TEENSY 4.1
AsyncHTTPRequest_Teensy41 v1.10.0
Initialize Ethernet using DHCP => Connected! IP address:192.168.2.107
**************************************
{"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"currentSecond","created":"2022-03-18T19:51:49.407Z","content":{"second":66},"transaction":"4dcdcae9-ace0-4767-86d0-6b830fd3fc05"}}
**************************************
"second":66
Value string: 66
Actual value: 66
4. AsyncDweetPOST on TEENSY 4.1
Start AsyncDweetPOST on TEENSY 4.1
AsyncHTTPRequest_Teensy41 v1.10.0
Initialize Ethernet using DHCP => Connected! IP address:192.168.2.107
Making new POST request
**************************************
{"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"pinA0-Read","created":"2022-03-18T19:57:17.768Z","content":{"sensorValue":1007},"transaction":"76940001-4d31-4995-a712-189d9d874cb1"}}
**************************************
"sensorValue":1007
Value string: 1007
Actual value: 1007
5. AsyncSimpleGET on TEENSY 4.1
Start AsyncSimpleGET on TEENSY 4.1
AsyncHTTPRequest_Teensy41 v1.10.0
Initialize Ethernet using DHCP => Connected! IP address:192.168.2.107
**************************************
abbreviation: EST
client_ip: aaa.bbb.ccc.ddd
datetime: 2023-01-31T23:58:16.676610-05:00
day_of_week: 2
day_of_year: 31
dst: false
dst_from:
dst_offset: 0
dst_until:
raw_offset: -18000
timezone: America/Toronto
unixtime: 1675227496
utc_datetime: 2023-02-01T04:58:16.676610+00:00
utc_offset: -05:00
week_number: 5
**************************************
**************************************
abbreviation: EST
client_ip: aaa.bbb.ccc.ddd
datetime: 2023-01-31T23:59:16.675139-05:00
day_of_week: 2
day_of_year: 31
dst: false
dst_from:
dst_offset: 0
dst_until:
raw_offset: -18000
timezone: America/Toronto
unixtime: 1675227556
utc_datetime: 2023-02-01T04:59:16.675139+00:00
utc_offset: -05:00
week_number: 5
**************************************
Debug is enabled by default on Serial.
You can also change the debugging level from 0 to 4
#define ASYNC_HTTP_DEBUG_PORT Serial
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
#define _ASYNC_HTTP_LOGLEVEL_ 1
If you get compilation errors, more often than not, you may need to install a newer version of the ESP32 / ESP8266 / STM32
core for Arduino.
Sometimes, the library will only work if you update the Teensy
core to the latest version because I am using newly added functions.
Submit issues to: AsyncHTTPRequest_Teensy41 issues
- Fix bug. Add enhancement
- Add many more examples.
- Initial porting and coding for Teensy 4.1 using built-in QNEthernet
- Add debugging features.
- Add PUT, PATCH, DELETE and HEAD besides GET and POST.
- Optimize library code by using
reference-passing
instead ofvalue-passing
- Fix long timeout if using
IPAddress
- Display only successful responseText in examples
- Improve debug messages by adding functions to display error messages instead of
cryptic error number
- Not try to reconnect to the same
host:port
after connected - Fix bug of wrong
reqStates
- Default to reconnect to the same
host:port
after connected for new HTTP sites. - Use
allman astyle
and addutils
- Fix
_parseURL()
bug. Check Bug with _parseURL() #21 - Improve
README.md
so that links can be used in other sites, such asPIO
This library is based on, modified, bug-fixed and improved from:
- Bob Lemaire's asyncHTTPrequest Library to use the better asynchronous features of these following Async TCP Libraries : (
ESPAsyncTCP
,AsyncTCP
, andTeensy41_AsyncTCP
)
⭐️ Bob Lemaire |
If you want to contribute to this project:
- Report bugs and errors
- Ask for enhancements
- Create issues and pull requests
- Tell other people about this library
- The library is licensed under GPLv3
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
Copyright (C) <2022-> Khoi Hoang