diff --git a/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino b/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino new file mode 100644 index 0000000..5583358 --- /dev/null +++ b/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino @@ -0,0 +1,108 @@ +/**************************************************************************************************************************** + AsyncCustomHeader_STM32.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet + + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) + + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer + + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) + + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic + Licensed under MIT license + + Copyright (C) <2018> + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). + *****************************************************************************************************************************/ + +#include "defines.h" + +// Select a test server address +char GET_ServerAddress[] = "192.168.2.110/"; +//char GET_ServerAddress[] = "http://worldtimeapi.org/api/timezone/America/Toronto.txt"; + +// 600s = 10 minutes to not flooding +#define HTTP_REQUEST_INTERVAL_MS 600000 + +#include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic + +#include // https://github.com/sstaub/Ticker + +AsyncHTTPRequest request; + +void sendRequest(void); + +// Repeat forever, millis() resolution +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); + +void sendRequest(void) +{ + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + Serial.println("\nSending GET Request to " + String(GET_ServerAddress)); + + request.open("GET", GET_ServerAddress); + request.setReqHeader("X-CUSTOM-HEADER", "custom_value"); + request.send(); + } +} + +void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) +{ + if (readyState == readyStateDone) + { + Serial.println("\n**************************************"); + Serial.println(request->responseText()); + Serial.println("**************************************"); + + request->setDebug(false); + } +} + +void setup(void) +{ + Serial.begin(115200); + while (!Serial); + + Serial.println("\nStart AsyncCustomHeader_STM32 on " + String(BOARD_NAME)); + + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); + + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); + + request.setDebug(false); + + // 5s timeout + request.setTimeout(5); + + request.onReadyStateChange(requestCB); + + sendHTTPRequest.start(); //start the ticker. + + // Send first request now + delay(10000); + sendRequest(); +} + +void loop(void) +{ + sendHTTPRequest.update(); +} diff --git a/examples/AsyncCustomHeader_STM32/defines.h b/examples/AsyncCustomHeader_STM32/defines.h new file mode 100644 index 0000000..27afabb --- /dev/null +++ b/examples/AsyncCustomHeader_STM32/defines.h @@ -0,0 +1,140 @@ +/**************************************************************************************************************************** + defines.h + + Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet + + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) + + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer + + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) + + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic + Licensed under MIT license + + Copyright (C) <2018> + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). + *****************************************************************************************************************************/ +/* + Currently support + 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : + - Nucleo-144 (F429ZI, F767ZI) + - Discovery (STM32F746G-DISCOVERY) + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) + 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) + 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields +*/ + +#ifndef defines_h +#define defines_h + +#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#endif + +#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 defined(STM32F0) + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" +#elif defined(STM32F1) + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" +#elif defined(STM32F2) + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" +#elif defined(STM32F3) + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" +#elif defined(STM32F4) + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" +#elif defined(STM32F7) + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" +#elif defined(STM32L0) + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" +#elif defined(STM32L1) + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" +#elif defined(STM32L4) + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" +#elif defined(STM32H7) + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" +#elif defined(STM32G0) + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" +#elif defined(STM32G4) + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" +#elif defined(STM32WB) + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" +#elif defined(STM32MP1) + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" +#else + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" +#endif + +#ifndef BOARD_NAME + #define BOARD_NAME BOARD_TYPE +#endif + +#include +#include + +//#include + +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, +}; + +// Select the static IP address according to your local network +IPAddress ip(192, 168, 2, 232); + +#endif //defines_h diff --git a/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino b/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino new file mode 100644 index 0000000..bdef64f --- /dev/null +++ b/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino @@ -0,0 +1,147 @@ +/**************************************************************************************************************************** + AsyncDweetGET_STM32.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet + + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) + + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer + + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) + + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic + Licensed under MIT license + + Copyright (C) <2018> + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). + *****************************************************************************************************************************/ + +/** + Connects to dweet.io once every 1 minutes, sends a GET request and a request body. + + Shows how to use Strings to assemble path and parse content from response. + dweet.io expects: https://dweet.io/dweet/for/thingName + + For more on dweet.io, see https://dweet.io/play/ + * */ + +#include "defines.h" + +// Select a test server address +const char GET_ServerAddress[] = "dweet.io"; + +// use your own thing name here +String dweetName = "/dweet/for/currentSecond?second="; + +// 10s = 10 seconds to not flooding the server +#define HTTP_REQUEST_INTERVAL_MS 10000 + +#include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic + +#include // https://github.com/sstaub/Ticker + +AsyncHTTPRequest request; + +void sendRequest(void); + +// Repeat forever, millis() resolution +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); + +void sendRequest(void) +{ + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + request.open("GET", (GET_ServerAddress + dweetName + String(millis()/1000)).c_str() ); + request.send(); + } +} + +void parseResponse(String responseText) +{ + /* + Typical response is: + {"this":"succeeded", + "by":"getting", + "the":"dweets", + "with":[{"thing":"my-thing-name", + "created":"2016-02-16T05:10:36.589Z", + "content":{"sensorValue":456}}]} + + You want "content": numberValue + */ + // now parse the response looking for "content": + int labelStart = responseText.indexOf("content\":"); + // find the first { after "content": + int contentStart = responseText.indexOf("{", labelStart); + // find the following } and get what's between the braces: + int contentEnd = responseText.indexOf("}", labelStart); + String content = responseText.substring(contentStart + 1, contentEnd); + + Serial.println(content); + + // now get the value after the colon, and convert to an int: + int valueStart = content.indexOf(":"); + String valueString = content.substring(valueStart + 1); + int number = valueString.toInt(); + + Serial.print("Value string: "); + Serial.println(valueString); + Serial.print("Actual value: "); + Serial.println(number); +} + +void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) +{ + if (readyState == readyStateDone) + { + String responseText = request->responseText(); + + Serial.println("\n**************************************"); + //Serial.println(request->responseText()); + Serial.println(responseText); + Serial.println("**************************************"); + + parseResponse(responseText); + + request->setDebug(false); + } +} + +void setup(void) +{ + Serial.begin(115200); + while (!Serial); + + Serial.println("\nStart AsyncDweetGET_STM32 on " + String(BOARD_NAME)); + + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); + + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + sendHTTPRequest.start(); //start the ticker. +} + +void loop(void) +{ + sendHTTPRequest.update(); +} diff --git a/examples/AsyncDweetGet_STM32/defines.h b/examples/AsyncDweetGet_STM32/defines.h new file mode 100644 index 0000000..27afabb --- /dev/null +++ b/examples/AsyncDweetGet_STM32/defines.h @@ -0,0 +1,140 @@ +/**************************************************************************************************************************** + defines.h + + Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet + + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) + + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer + + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) + + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic + Licensed under MIT license + + Copyright (C) <2018> + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). + *****************************************************************************************************************************/ +/* + Currently support + 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : + - Nucleo-144 (F429ZI, F767ZI) + - Discovery (STM32F746G-DISCOVERY) + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) + 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) + 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields +*/ + +#ifndef defines_h +#define defines_h + +#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#endif + +#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 defined(STM32F0) + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" +#elif defined(STM32F1) + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" +#elif defined(STM32F2) + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" +#elif defined(STM32F3) + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" +#elif defined(STM32F4) + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" +#elif defined(STM32F7) + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" +#elif defined(STM32L0) + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" +#elif defined(STM32L1) + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" +#elif defined(STM32L4) + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" +#elif defined(STM32H7) + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" +#elif defined(STM32G0) + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" +#elif defined(STM32G4) + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" +#elif defined(STM32WB) + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" +#elif defined(STM32MP1) + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" +#else + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" +#endif + +#ifndef BOARD_NAME + #define BOARD_NAME BOARD_TYPE +#endif + +#include +#include + +//#include + +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, +}; + +// Select the static IP address according to your local network +IPAddress ip(192, 168, 2, 232); + +#endif //defines_h diff --git a/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino b/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino new file mode 100644 index 0000000..046e26b --- /dev/null +++ b/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino @@ -0,0 +1,146 @@ +/**************************************************************************************************************************** + AsyncDweetPOST_STM32.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet + + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) + + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer + + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) + + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic + Licensed under MIT license + + Copyright (C) <2018> + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). + *****************************************************************************************************************************/ + + // Dweet.io POST client. Connects to dweet.io once every ten seconds, sends a POST request and a request body. + // Shows how to use Strings to assemble path and body + +#include "defines.h" + +// Select a test server address +const char POST_ServerAddress[] = "dweet.io"; + +// use your own thing name here +String dweetName = "/dweet/for/pinA0-Read?"; + +// 10s = 10 seconds to not flooding the server +#define HTTP_REQUEST_INTERVAL_MS 10000 + +#include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic + +#include // https://github.com/sstaub/Ticker + +AsyncHTTPRequest request; + +void sendRequest(void); + +// Repeat forever, millis() resolution +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); + +void sendRequest(void) +{ + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + String postData = "sensorValue="; + postData += analogRead(A0); + + Serial.println("\nMaking new POST request"); + + request.open("POST", (POST_ServerAddress + dweetName + postData).c_str() ); + request.send(); + } +} + +void parseResponse(String responseText) +{ + /* + Typical response is: + {"this":"succeeded", + "by":"getting", + "the":"dweets", + "with":[{"thing":"my-thing-name", + "created":"2016-02-16T05:10:36.589Z", + "content":{"sensorValue":456}}]} + + You want "content": numberValue + */ + // now parse the response looking for "content": + int labelStart = responseText.indexOf("content\":"); + // find the first { after "content": + int contentStart = responseText.indexOf("{", labelStart); + // find the following } and get what's between the braces: + int contentEnd = responseText.indexOf("}", labelStart); + String content = responseText.substring(contentStart + 1, contentEnd); + + Serial.println(content); + + // now get the value after the colon, and convert to an int: + int valueStart = content.indexOf(":"); + String valueString = content.substring(valueStart + 1); + int number = valueString.toInt(); + + Serial.print("Value string: "); + Serial.println(valueString); + Serial.print("Actual value: "); + Serial.println(number); +} + +void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) +{ + if (readyState == readyStateDone) + { + String responseText = request->responseText(); + + Serial.println("\n**************************************"); + //Serial.println(request->responseText()); + Serial.println(responseText); + Serial.println("**************************************"); + + parseResponse(responseText); + + request->setDebug(false); + } +} + +void setup(void) +{ + Serial.begin(115200); + while (!Serial); + + Serial.println("\nStart AsyncDweetPOST_STM32 on " + String(BOARD_NAME)); + + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); + + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + sendHTTPRequest.start(); //start the ticker. +} + +void loop(void) +{ + sendHTTPRequest.update(); +} diff --git a/examples/AsyncDweetPost_STM32/defines.h b/examples/AsyncDweetPost_STM32/defines.h new file mode 100644 index 0000000..27afabb --- /dev/null +++ b/examples/AsyncDweetPost_STM32/defines.h @@ -0,0 +1,140 @@ +/**************************************************************************************************************************** + defines.h + + Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet + + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) + + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer + + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) + + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic + Licensed under MIT license + + Copyright (C) <2018> + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). + *****************************************************************************************************************************/ +/* + Currently support + 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : + - Nucleo-144 (F429ZI, F767ZI) + - Discovery (STM32F746G-DISCOVERY) + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) + 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) + 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields +*/ + +#ifndef defines_h +#define defines_h + +#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#endif + +#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 defined(STM32F0) + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" +#elif defined(STM32F1) + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" +#elif defined(STM32F2) + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" +#elif defined(STM32F3) + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" +#elif defined(STM32F4) + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" +#elif defined(STM32F7) + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" +#elif defined(STM32L0) + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" +#elif defined(STM32L1) + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" +#elif defined(STM32L4) + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" +#elif defined(STM32H7) + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" +#elif defined(STM32G0) + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" +#elif defined(STM32G4) + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" +#elif defined(STM32WB) + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" +#elif defined(STM32MP1) + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" +#else + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" +#endif + +#ifndef BOARD_NAME + #define BOARD_NAME BOARD_TYPE +#endif + +#include +#include + +//#include + +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, +}; + +// Select the static IP address according to your local network +IPAddress ip(192, 168, 2, 232); + +#endif //defines_h diff --git a/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino b/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino new file mode 100644 index 0000000..3e89c05 --- /dev/null +++ b/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino @@ -0,0 +1,101 @@ +/**************************************************************************************************************************** + AsyncSimpleGET_STM32.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet + + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) + + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer + + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) + + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic + Licensed under MIT license + + Copyright (C) <2018> + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). + *****************************************************************************************************************************/ + +#include "defines.h" + +// Select a test server address +//char GET_ServerAddress[] = "ipv4bot.whatismyipaddress.com/"; +char GET_ServerAddress[] = "http://worldtimeapi.org/api/timezone/America/Toronto.txt"; + +// 600s = 10 minutes to not flooding +#define HTTP_REQUEST_INTERVAL_MS 600000 + +#include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic + +#include // https://github.com/sstaub/Ticker + +AsyncHTTPRequest request; + +void sendRequest(void); + +// Repeat forever, millis() resolution +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); + +void sendRequest(void) +{ + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + request.open("GET", GET_ServerAddress); + request.send(); + } +} + +void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) +{ + if (readyState == readyStateDone) + { + Serial.println("\n**************************************"); + Serial.println(request->responseText()); + Serial.println("**************************************"); + + request->setDebug(false); + } +} + +void setup(void) +{ + Serial.begin(115200); + while (!Serial); + + Serial.println("\nStart AsyncSimpleGET_STM32 on " + String(BOARD_NAME)); + + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); + + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + sendHTTPRequest.start(); //start the ticker. + + // Send first request now + delay(10000); + sendRequest(); +} + +void loop(void) +{ + sendHTTPRequest.update(); +} diff --git a/examples/AsyncSimpleGET_STM32/defines.h b/examples/AsyncSimpleGET_STM32/defines.h new file mode 100644 index 0000000..c171fde --- /dev/null +++ b/examples/AsyncSimpleGET_STM32/defines.h @@ -0,0 +1,140 @@ +/**************************************************************************************************************************** + defines.h + + Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet + + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) + + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer + + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) + + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic + Licensed under MIT license + + Copyright (C) <2018> + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . + + Version: 1.0.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). + *****************************************************************************************************************************/ +/* + Currently support + 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : + - Nucleo-144 (F429ZI, F767ZI) + - Discovery (STM32F746G-DISCOVERY) + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) + 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) + 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields +*/ + +#ifndef defines_h +#define defines_h + +#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) +#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#endif + +#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 defined(STM32F0) +#warning STM32F0 board selected +#define BOARD_TYPE "STM32F0" +#elif defined(STM32F1) +#warning STM32F1 board selected +#define BOARD_TYPE "STM32F1" +#elif defined(STM32F2) +#warning STM32F2 board selected +#define BOARD_TYPE "STM32F2" +#elif defined(STM32F3) +#warning STM32F3 board selected +#define BOARD_TYPE "STM32F3" +#elif defined(STM32F4) +#warning STM32F4 board selected +#define BOARD_TYPE "STM32F4" +#elif defined(STM32F7) +#warning STM32F7 board selected +#define BOARD_TYPE "STM32F7" +#elif defined(STM32L0) +#warning STM32L0 board selected +#define BOARD_TYPE "STM32L0" +#elif defined(STM32L1) +#warning STM32L1 board selected +#define BOARD_TYPE "STM32L1" +#elif defined(STM32L4) +#warning STM32L4 board selected +#define BOARD_TYPE "STM32L4" +#elif defined(STM32H7) +#warning STM32H7 board selected +#define BOARD_TYPE "STM32H7" +#elif defined(STM32G0) +#warning STM32G0 board selected +#define BOARD_TYPE "STM32G0" +#elif defined(STM32G4) +#warning STM32G4 board selected +#define BOARD_TYPE "STM32G4" +#elif defined(STM32WB) +#warning STM32WB board selected +#define BOARD_TYPE "STM32WB" +#elif defined(STM32MP1) +#warning STM32MP1 board selected +#define BOARD_TYPE "STM32MP1" +#else +#warning STM32 unknown board selected +#define BOARD_TYPE "STM32 Unknown" +#endif + +#ifndef BOARD_NAME +#define BOARD_NAME BOARD_TYPE +#endif + +#include +#include + +//#include + +// Enter a MAC address and IP address for your controller below. +#define NUMBER_OF_MAC 20 + +byte mac[][NUMBER_OF_MAC] = +{ + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, +}; + +// Select the static IP address according to your local network +IPAddress ip(192, 168, 2, 232); + +#endif //defines_h