Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Simple Async HTTP Request library, supporting GET, POST, PUT, PATCH, DELETE and HEAD, on top of Teensy41_AsyncTCP for Teensy 4.1 using QNEthernet. This library is one of the current or future Async libraries to support Teensy 4.1 using QNEthernet, such as AsyncHTTPRequest_Generic, AsyncHTTPSRequest_Generic, AsyncMQTT_Generic, Teensy41_AsyncWebSe…

License

Notifications You must be signed in to change notification settings

khoih-prog/AsyncHTTPRequest_Teensy41

Repository files navigation

AsyncHTTPRequest_Teensy41 Library

arduino-library-badge GitHub release contributions welcome GitHub issues

Donate to my libraries using BuyMeACoffee



Table of Contents



Why do we need this Async AsyncHTTPRequest_Teensy41 library

Features

  1. Asynchronous HTTP Request library for Teensy 4.1 using built-in QNEthernet
  2. Providing a subset of HTTP.
  3. Relying on Teensy41_AsyncTCP library
  4. Methods similar in format and usage to XmlHTTPrequest in Javascript.

Supports

  1. GET, POST, PUT, PATCH, DELETE and HEAD
  2. Request and response headers
  3. Chunked response
  4. Single String response for short (<~5K) responses (heap permitting).
  5. Optional onData callback.
  6. Optional onReadyStatechange callback.

Principles of operation

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:

  1. Bob Lemaire's asyncHTTPrequest Library

Currently Supported Boards

  1. Teensy 4.1 using QNEthernet Library


Prerequisites

  1. Arduino IDE 1.8.19+ for Arduino. GitHub release
  2. Teensy core v1.57+ for Teensy 4.1. GitHub release
  3. QNEthernet Library version v0.17.0+ for Teensy 4.1 built-in Ethernet.
  4. Teensy41_AsyncTCP library v1.1.0+ to use Teensy 4.1 using QNEthernet Library. To install, check arduino-library-badge


Installation

Use Arduino Library Manager

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 arduino-library-badge for more detailed instructions.

Manual Install

  1. Navigate to AsyncHTTPRequest_Teensy41 page.
  2. Download the latest release AsyncHTTPRequest_Teensy41-main.zip.
  3. Extract the zip file to AsyncHTTPRequest_Teensy41-main directory
  4. Copy the whole AsyncHTTPRequest_Teensy41-main folder to Arduino libraries' directory such as ~/Arduino/libraries/.

VS Code & PlatformIO

  1. Install VS Code
  2. Install PlatformIO
  3. Install AsyncHTTPRequest_Teensy41 library by using Library Manager. Search for AsyncHTTPRequest_Teensy41 in Platform.io Author's Libraries
  4. 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


Packages' Patches

1. For Teensy boards

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


HOWTO Fix Multiple Definitions Linker Error

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



Examples

  1. AsyncCustomHeader
  2. AsyncDweetGet
  3. AsyncDweetPost
  4. AsyncHTTPRequest
  5. AsyncSimpleGET
  6. multiFileProject

#include "defines.h"
#define ASYNC_HTTP_REQUEST_TEENSY41_VERSION_MIN_TARGET "AsyncHTTPRequest_Teensy41 v1.10.0"
#define ASYNC_HTTP_REQUEST_TEENSY41_VERSION_MIN 1010000
// Uncomment for certain HTTP site to optimize
//#define NOT_SEND_HEADER_AFTER_CONNECTED true
// Level from 0-4
#define ASYNC_HTTP_DEBUG_PORT Serial
#define _ASYNC_HTTP_LOGLEVEL_ 2
// 600s = 10 minutes to not flooding, 60s in testing
#define HTTP_REQUEST_INTERVAL_MS 10000 //600000
// Seconds for timeout, default is 3s
#define DEFAULT_RX_TIMEOUT 10
// 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
#include <Ticker.h> // https://github.com/sstaub/Ticker
AsyncHTTPRequest request;
void sendRequest();
// Repeat forever, millis() resolution
Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS);
void sendRequest()
{
static bool requestOpenResult;
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
{
//requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt");
requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt");
if (requestOpenResult)
{
// Only send() if open() returns true, or crash
request.send();
}
else
{
Serial.println("Can't send bad request");
}
}
else
{
Serial.println("Can't send request");
}
}
void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState)
{
(void) optParm;
if (readyState == readyStateDone)
{
AHTTP_LOGDEBUG(F("\n**************************************"));
AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString());
if (request->responseHTTPcode() == 200)
{
Serial.println(F("\n**************************************"));
Serial.println(request->responseText());
Serial.println(F("**************************************"));
}
}
}
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.print("\nStart AsyncHTTPRequest on ");
Serial.println(BOARD_NAME);
Serial.println(ASYNC_HTTP_REQUEST_TEENSY41_VERSION);
#if defined(ASYNC_HTTP_REQUEST_TEENSY41_VERSION_MIN)
if (ASYNC_HTTP_REQUEST_TEENSY41_VERSION_INT < ASYNC_HTTP_REQUEST_TEENSY41_VERSION_MIN)
{
Serial.print("Warning. Must use this example on Version equal or later than : ");
Serial.println(ASYNC_HTTP_REQUEST_TEENSY41_VERSION_MIN_TARGET);
}
#endif
delay(500);
#if USING_DHCP
// Start the Ethernet connection, using DHCP
Serial.print("Initialize Ethernet using DHCP => ");
Ethernet.begin();
#else
// Start the Ethernet connection, using static IP
Serial.print("Initialize Ethernet using static IP => ");
Ethernet.begin(myIP, myNetmask, myGW);
Ethernet.setDNSServerIP(mydnsServer);
#endif
if (!Ethernet.waitForLocalIP(5000))
{
Serial.println(F("Failed to configure Ethernet"));
if (!Ethernet.linkStatus())
{
Serial.println(F("Ethernet cable is not connected."));
}
// Stay here forever
while (true)
{
delay(1);
}
}
else
{
Serial.print(F("Connected! IP address:"));
Serial.println(Ethernet.localIP());
}
#if USING_DHCP
delay(1000);
#else
delay(2000);
#endif
request.setDebug(false);
request.onReadyStateChange(requestCB);
sendHTTPRequest.start(); //start the ticker.
// Send first request now
//delay(60);
sendRequest();
}
void loop()
{
sendHTTPRequest.update();
}


2. File defines.h

#ifndef defines_h
#define defines_h
#if !( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) )
#error Only Teensy 4.1 supported
#endif
// Debug Level from 0 to 4
#define _TEENSY41_ASYNC_TCP_LOGLEVEL_ 1
#define _AWS_TEENSY41_LOGLEVEL_ 1
#define SHIELD_TYPE "Teensy4.1 QNEthernet"
#if (_AWS_TEENSY41_LOGLEVEL_ > 3)
#warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error
#endif
#define USING_DHCP true
//#define USING_DHCP false
#if !USING_DHCP
// Set the static IP address to use if the DHCP fails to assign
IPAddress myIP(192, 168, 2, 222);
IPAddress myNetmask(255, 255, 255, 0);
IPAddress myGW(192, 168, 2, 1);
//IPAddress mydnsServer(192, 168, 2, 1);
IPAddress mydnsServer(8, 8, 8, 8);
#endif
#include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet
using namespace qindesign::network;
//#include <AsyncHTTPRequest_Teensy41.h>
#endif //defines_h



Debug Terminal Output Samples

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

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

Troubleshooting

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.


Issues

Submit issues to: AsyncHTTPRequest_Teensy41 issues


TO DO

  1. Fix bug. Add enhancement
  2. Add many more examples.

DONE

  1. Initial porting and coding for Teensy 4.1 using built-in QNEthernet
  2. Add debugging features.
  3. Add PUT, PATCH, DELETE and HEAD besides GET and POST.
  4. Optimize library code by using reference-passing instead of value-passing
  5. Fix long timeout if using IPAddress
  6. Display only successful responseText in examples
  7. Improve debug messages by adding functions to display error messages instead of cryptic error number
  8. Not try to reconnect to the same host:port after connected
  9. Fix bug of wrong reqStates
  10. Default to reconnect to the same host:port after connected for new HTTP sites.
  11. Use allman astyle and add utils
  12. Fix _parseURL() bug. Check Bug with _parseURL() #21
  13. Improve README.md so that links can be used in other sites, such as PIO


Contributions and Thanks

This library is based on, modified, bug-fixed and improved from:

  1. Bob Lemaire's asyncHTTPrequest Library to use the better asynchronous features of these following Async TCP Libraries : ( ESPAsyncTCP, AsyncTCP, and Teensy41_AsyncTCP )
boblemaire
⭐️ Bob Lemaire


Contributing

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

License and credits

  • The library is licensed under GPLv3

Copyright

Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>

Copyright (C) <2022-> Khoi Hoang

About

Simple Async HTTP Request library, supporting GET, POST, PUT, PATCH, DELETE and HEAD, on top of Teensy41_AsyncTCP for Teensy 4.1 using QNEthernet. This library is one of the current or future Async libraries to support Teensy 4.1 using QNEthernet, such as AsyncHTTPRequest_Generic, AsyncHTTPSRequest_Generic, AsyncMQTT_Generic, Teensy41_AsyncWebSe…

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published