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

Commit 8c53d95

Browse files
authored
Add more examples
1 parent 77e1683 commit 8c53d95

File tree

8 files changed

+1062
-0
lines changed

8 files changed

+1062
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/****************************************************************************************************************************
2+
AsyncCustomHeader_STM32.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
3+
4+
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
5+
6+
AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
7+
8+
Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest)
9+
10+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic
11+
Licensed under MIT license
12+
13+
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
14+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
15+
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
16+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18+
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
Version: 1.0.0
21+
22+
Version Modified By Date Comments
23+
------- ----------- ---------- -----------
24+
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
25+
*****************************************************************************************************************************/
26+
27+
#include "defines.h"
28+
29+
// Select a test server address
30+
char GET_ServerAddress[] = "192.168.2.110/";
31+
//char GET_ServerAddress[] = "http://worldtimeapi.org/api/timezone/America/Toronto.txt";
32+
33+
// 600s = 10 minutes to not flooding
34+
#define HTTP_REQUEST_INTERVAL_MS 600000
35+
36+
#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic
37+
38+
#include <Ticker.h> // https://github.com/sstaub/Ticker
39+
40+
AsyncHTTPRequest request;
41+
42+
void sendRequest(void);
43+
44+
// Repeat forever, millis() resolution
45+
Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS);
46+
47+
void sendRequest(void)
48+
{
49+
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
50+
{
51+
Serial.println("\nSending GET Request to " + String(GET_ServerAddress));
52+
53+
request.open("GET", GET_ServerAddress);
54+
request.setReqHeader("X-CUSTOM-HEADER", "custom_value");
55+
request.send();
56+
}
57+
}
58+
59+
void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState)
60+
{
61+
if (readyState == readyStateDone)
62+
{
63+
Serial.println("\n**************************************");
64+
Serial.println(request->responseText());
65+
Serial.println("**************************************");
66+
67+
request->setDebug(false);
68+
}
69+
}
70+
71+
void setup(void)
72+
{
73+
Serial.begin(115200);
74+
while (!Serial);
75+
76+
Serial.println("\nStart AsyncCustomHeader_STM32 on " + String(BOARD_NAME));
77+
78+
// start the ethernet connection and the server
79+
// Use random mac
80+
uint16_t index = millis() % NUMBER_OF_MAC;
81+
82+
// Use Static IP
83+
//Ethernet.begin(mac[index], ip);
84+
// Use DHCP dynamic IP and random mac
85+
Ethernet.begin(mac[index]);
86+
87+
Serial.print(F("AsyncHTTPRequest @ IP : "));
88+
Serial.println(Ethernet.localIP());
89+
Serial.println();
90+
91+
request.setDebug(false);
92+
93+
// 5s timeout
94+
request.setTimeout(5);
95+
96+
request.onReadyStateChange(requestCB);
97+
98+
sendHTTPRequest.start(); //start the ticker.
99+
100+
// Send first request now
101+
delay(10000);
102+
sendRequest();
103+
}
104+
105+
void loop(void)
106+
{
107+
sendHTTPRequest.update();
108+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/****************************************************************************************************************************
2+
defines.h
3+
4+
Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
5+
6+
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
7+
8+
AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
9+
10+
Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest)
11+
12+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic
13+
Licensed under MIT license
14+
15+
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
16+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
17+
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
18+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20+
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
22+
Version: 1.0.0
23+
24+
Version Modified By Date Comments
25+
------- ----------- ---------- -----------
26+
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
27+
*****************************************************************************************************************************/
28+
/*
29+
Currently support
30+
1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as :
31+
- Nucleo-144 (F429ZI, F767ZI)
32+
- Discovery (STM32F746G-DISCOVERY)
33+
- STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet,
34+
- See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1)
35+
2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false)
36+
3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields
37+
*/
38+
39+
#ifndef defines_h
40+
#define defines_h
41+
42+
#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \
43+
defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \
44+
defined(STM32WB) || defined(STM32MP1) )
45+
#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting.
46+
#endif
47+
48+
#define ASYNC_HTTP_DEBUG_PORT Serial
49+
50+
// Use from 0 to 4. Higher number, more debugging messages and memory usage.
51+
#define _ASYNC_HTTP_LOGLEVEL_ 1
52+
53+
54+
#if defined(STM32F0)
55+
#warning STM32F0 board selected
56+
#define BOARD_TYPE "STM32F0"
57+
#elif defined(STM32F1)
58+
#warning STM32F1 board selected
59+
#define BOARD_TYPE "STM32F1"
60+
#elif defined(STM32F2)
61+
#warning STM32F2 board selected
62+
#define BOARD_TYPE "STM32F2"
63+
#elif defined(STM32F3)
64+
#warning STM32F3 board selected
65+
#define BOARD_TYPE "STM32F3"
66+
#elif defined(STM32F4)
67+
#warning STM32F4 board selected
68+
#define BOARD_TYPE "STM32F4"
69+
#elif defined(STM32F7)
70+
#warning STM32F7 board selected
71+
#define BOARD_TYPE "STM32F7"
72+
#elif defined(STM32L0)
73+
#warning STM32L0 board selected
74+
#define BOARD_TYPE "STM32L0"
75+
#elif defined(STM32L1)
76+
#warning STM32L1 board selected
77+
#define BOARD_TYPE "STM32L1"
78+
#elif defined(STM32L4)
79+
#warning STM32L4 board selected
80+
#define BOARD_TYPE "STM32L4"
81+
#elif defined(STM32H7)
82+
#warning STM32H7 board selected
83+
#define BOARD_TYPE "STM32H7"
84+
#elif defined(STM32G0)
85+
#warning STM32G0 board selected
86+
#define BOARD_TYPE "STM32G0"
87+
#elif defined(STM32G4)
88+
#warning STM32G4 board selected
89+
#define BOARD_TYPE "STM32G4"
90+
#elif defined(STM32WB)
91+
#warning STM32WB board selected
92+
#define BOARD_TYPE "STM32WB"
93+
#elif defined(STM32MP1)
94+
#warning STM32MP1 board selected
95+
#define BOARD_TYPE "STM32MP1"
96+
#else
97+
#warning STM32 unknown board selected
98+
#define BOARD_TYPE "STM32 Unknown"
99+
#endif
100+
101+
#ifndef BOARD_NAME
102+
#define BOARD_NAME BOARD_TYPE
103+
#endif
104+
105+
#include <LwIP.h>
106+
#include <STM32Ethernet.h>
107+
108+
//#include <AsyncUDP_STM32.h>
109+
110+
// Enter a MAC address and IP address for your controller below.
111+
#define NUMBER_OF_MAC 20
112+
113+
byte mac[][NUMBER_OF_MAC] =
114+
{
115+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 },
116+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 },
117+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 },
118+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 },
119+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 },
120+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 },
121+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 },
122+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 },
123+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 },
124+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A },
125+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B },
126+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C },
127+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D },
128+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E },
129+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F },
130+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 },
131+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 },
132+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 },
133+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 },
134+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 },
135+
};
136+
137+
// Select the static IP address according to your local network
138+
IPAddress ip(192, 168, 2, 232);
139+
140+
#endif //defines_h
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/****************************************************************************************************************************
2+
AsyncDweetGET_STM32.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
3+
4+
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
5+
6+
AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
7+
8+
Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest)
9+
10+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic
11+
Licensed under MIT license
12+
13+
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
14+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
15+
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
16+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18+
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
Version: 1.0.0
21+
22+
Version Modified By Date Comments
23+
------- ----------- ---------- -----------
24+
1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc).
25+
*****************************************************************************************************************************/
26+
27+
/**
28+
Connects to dweet.io once every 1 minutes, sends a GET request and a request body.
29+
30+
Shows how to use Strings to assemble path and parse content from response.
31+
dweet.io expects: https://dweet.io/dweet/for/thingName
32+
33+
For more on dweet.io, see https://dweet.io/play/
34+
* */
35+
36+
#include "defines.h"
37+
38+
// Select a test server address
39+
const char GET_ServerAddress[] = "dweet.io";
40+
41+
// use your own thing name here
42+
String dweetName = "/dweet/for/currentSecond?second=";
43+
44+
// 10s = 10 seconds to not flooding the server
45+
#define HTTP_REQUEST_INTERVAL_MS 10000
46+
47+
#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic
48+
49+
#include <Ticker.h> // https://github.com/sstaub/Ticker
50+
51+
AsyncHTTPRequest request;
52+
53+
void sendRequest(void);
54+
55+
// Repeat forever, millis() resolution
56+
Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS);
57+
58+
void sendRequest(void)
59+
{
60+
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
61+
{
62+
request.open("GET", (GET_ServerAddress + dweetName + String(millis()/1000)).c_str() );
63+
request.send();
64+
}
65+
}
66+
67+
void parseResponse(String responseText)
68+
{
69+
/*
70+
Typical response is:
71+
{"this":"succeeded",
72+
"by":"getting",
73+
"the":"dweets",
74+
"with":[{"thing":"my-thing-name",
75+
"created":"2016-02-16T05:10:36.589Z",
76+
"content":{"sensorValue":456}}]}
77+
78+
You want "content": numberValue
79+
*/
80+
// now parse the response looking for "content":
81+
int labelStart = responseText.indexOf("content\":");
82+
// find the first { after "content":
83+
int contentStart = responseText.indexOf("{", labelStart);
84+
// find the following } and get what's between the braces:
85+
int contentEnd = responseText.indexOf("}", labelStart);
86+
String content = responseText.substring(contentStart + 1, contentEnd);
87+
88+
Serial.println(content);
89+
90+
// now get the value after the colon, and convert to an int:
91+
int valueStart = content.indexOf(":");
92+
String valueString = content.substring(valueStart + 1);
93+
int number = valueString.toInt();
94+
95+
Serial.print("Value string: ");
96+
Serial.println(valueString);
97+
Serial.print("Actual value: ");
98+
Serial.println(number);
99+
}
100+
101+
void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState)
102+
{
103+
if (readyState == readyStateDone)
104+
{
105+
String responseText = request->responseText();
106+
107+
Serial.println("\n**************************************");
108+
//Serial.println(request->responseText());
109+
Serial.println(responseText);
110+
Serial.println("**************************************");
111+
112+
parseResponse(responseText);
113+
114+
request->setDebug(false);
115+
}
116+
}
117+
118+
void setup(void)
119+
{
120+
Serial.begin(115200);
121+
while (!Serial);
122+
123+
Serial.println("\nStart AsyncDweetGET_STM32 on " + String(BOARD_NAME));
124+
125+
// start the ethernet connection and the server
126+
// Use random mac
127+
uint16_t index = millis() % NUMBER_OF_MAC;
128+
129+
// Use Static IP
130+
//Ethernet.begin(mac[index], ip);
131+
// Use DHCP dynamic IP and random mac
132+
Ethernet.begin(mac[index]);
133+
134+
Serial.print(F("AsyncHTTPRequest @ IP : "));
135+
Serial.println(Ethernet.localIP());
136+
Serial.println();
137+
138+
request.setDebug(false);
139+
140+
request.onReadyStateChange(requestCB);
141+
sendHTTPRequest.start(); //start the ticker.
142+
}
143+
144+
void loop(void)
145+
{
146+
sendHTTPRequest.update();
147+
}

0 commit comments

Comments
 (0)