Skip to content

Commit 4a186de

Browse files
committed
Fetch is now asynchronous. You can pass a callback to fetch and fetch will pass the response to your callback.
1 parent 5b359b2 commit 4a186de

File tree

26 files changed

+843
-6
lines changed

26 files changed

+843
-6
lines changed

examples/esp32/async/get/get.ino

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "recipes/WiFi.h"
2+
#include "Fetch.h"
3+
4+
#define SSID YourWiFiSSID
5+
#define PASSPHRASE YourWiFiPassphrase
6+
// Certificate will expire and might need to be updated before running this sketch.
7+
#define CACert "-----BEGIN CERTIFICATE-----\n\
8+
MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n\
9+
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n\
10+
d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n\
11+
ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL\n\
12+
MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\n\
13+
LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug\n\
14+
RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm\n\
15+
+9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW\n\
16+
PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM\n\
17+
xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB\n\
18+
Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3\n\
19+
hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg\n\
20+
EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF\n\
21+
MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA\n\
22+
FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec\n\
23+
nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z\n\
24+
eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF\n\
25+
hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2\n\
26+
Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\n\
27+
vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\n\
28+
+OkuE6N36B9K\n\
29+
-----END CERTIFICATE-----"
30+
31+
// Fetch when used in async mode returns a client that listens for the response.
32+
FetchClient client;
33+
34+
// Function that handles the response when it's received.
35+
void handleResponse(Response response) {
36+
Serial.println("Response received:");
37+
// Printing response body as plain text.
38+
Serial.println();
39+
Serial.println(response.text());
40+
}
41+
42+
void setup() {
43+
Serial.begin(9600);
44+
connectWiFi(SSID, PASSPHRASE);
45+
46+
RequestOptions options;
47+
options.method = "GET";
48+
options.caCert = CACert;
49+
50+
Serial.println("Sending the request...");
51+
52+
// Fetch sends the response, when received, to our handleResponse function, instead of blocking the rest of the code until the response is received.
53+
54+
client = fetch("https://api.github.com/", options, handleResponse);
55+
56+
Serial.println("Request Sent!");
57+
Serial.println("Waiting for the response...");
58+
}
59+
60+
void loop() {
61+
// This is crucial, it listens on the connection for the response, until it's received.
62+
client.loop();
63+
}

examples/esp32/async/post/post.ino

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "recipes/WiFi.h"
2+
#include "Fetch.h"
3+
4+
#define SSID YourWiFiSSID
5+
#define PASSPHRASE YourWiFiPassphrase
6+
// Certificate will expire and might need to be updated before running this sketch.
7+
#define CACert "-----BEGIN CERTIFICATE-----\n\
8+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n\
9+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n\
10+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n\
11+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n\
12+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n\
13+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n\
14+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+\n\
15+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U\n\
16+
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW\n\
17+
T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH\n\
18+
B5T0Y3HsLuJvW5iB4YlcNHlsdu87kG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5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n\
34+
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n\
35+
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n\
36+
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n\
37+
-----END CERTIFICATE-----"
38+
39+
// Fetch when used in async mode returns a client that listens for the response.
40+
FetchClient client;
41+
42+
// Function that handles the response when it's received.
43+
void handleResponse(Response response) {
44+
Serial.println("Response received:");
45+
// Printing response.
46+
Serial.println(response);
47+
// Printing respons headers.
48+
Serial.printf("Connection Header: \"%s\"\n", response.headers["Content-Type"].c_str());
49+
Serial.printf("Connection Header: \"%s\"\n", response.headers["Connection"].c_str());
50+
}
51+
52+
void setup() {
53+
Serial.begin(9600);
54+
connectWiFi(SSID, PASSPHRASE);
55+
56+
RequestOptions options;
57+
options.method = "POST";
58+
options.headers["Content-Type"] = "application/json";
59+
options.body = "{\"email\": \"[email protected]\", \"password\": \"test:80\"}";
60+
options.caCert = CACert;
61+
62+
Serial.println("Sending the request...");
63+
64+
// Fetch sends the response, when received, to our handleResponse function, instead of blocking the rest of the code until the response is received.
65+
client = fetch("https://api.grandeur.tech/auth/login/?apiKey=grandeurkywxmoy914080rxf9dh05n7e", options, handleResponse);
66+
67+
Serial.println("Request Sent!");
68+
Serial.println("Waiting for the response...");
69+
}
70+
71+
void loop() {
72+
// This is crucial, it listens on the connection for the response, until it's received.
73+
client.loop();
74+
}

examples/esp32/get/get.ino renamed to examples/esp32/sync/get/get.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#define SSID YourWiFiSSID
55
#define PASSPHRASE YourWiFiPassphrase
6+
// Certificate will expire and might need to be updated before running this sketch.
67
#define CACert "-----BEGIN CERTIFICATE-----\n\
78
MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n\
89
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n\
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <Arduino.h>
2+
3+
#ifndef BLINK_H
4+
#define BLINK_H
5+
6+
#define LEDPIN 2
7+
#define OFF 1
8+
#define ON 0
9+
10+
// Makes the LED blink once every 1 second.
11+
void blinkOnce() {
12+
pinMode(LEDPIN, OUTPUT);
13+
// Turning ON.
14+
digitalWrite(LEDPIN, ON);
15+
delay(1000);
16+
// Turning OFF.
17+
digitalWrite(LEDPIN, OFF);
18+
delay(1000);
19+
}
20+
21+
// Makes the LED blink twice every 1 second.
22+
void blinkTwice() {
23+
pinMode(LEDPIN, OUTPUT);
24+
// Turning ON.
25+
digitalWrite(LEDPIN, ON);
26+
delay(500);
27+
// Turning OFF.
28+
digitalWrite(LEDPIN, OFF);
29+
delay(500);
30+
}
31+
32+
// Makes the LED blink n-times every 1 second.
33+
void blinkN(size_t n) {
34+
pinMode(LEDPIN, OUTPUT);
35+
// Turning ON.
36+
digitalWrite(LEDPIN, ON);
37+
delay(1000/n);
38+
// Turning OFF.
39+
digitalWrite(LEDPIN, OFF);
40+
delay(1000/n);
41+
}
42+
43+
// Makes the LED stop blinking.
44+
void blinkStop() {
45+
pinMode(LEDPIN, OUTPUT);
46+
digitalWrite(LEDPIN, OFF);
47+
}
48+
49+
// Makes the LED fade in and out once every 1 second.
50+
void fadeOnce() {
51+
pinMode(LEDPIN, OUTPUT);
52+
// Fading in.
53+
for(int i = 0; i < 1024; i+=1024/20) {
54+
analogWrite(LEDPIN, i);
55+
delay(100);
56+
}
57+
// Fading out.
58+
for(int i = 1023; i > 0; i-=1024/20) {
59+
analogWrite(LEDPIN, i);
60+
delay(100);
61+
}
62+
}
63+
64+
// Makes the LED fade in and out twice every 1 second.
65+
void fadeTwice() {
66+
pinMode(LEDPIN, OUTPUT);
67+
// Fading in.
68+
for(int i = 0; i < 1024; i+=1024/20) {
69+
analogWrite(LEDPIN, i);
70+
delay(50);
71+
}
72+
// Fading out.
73+
for(int i = 1023; i > 0; i-=1024/20) {
74+
analogWrite(LEDPIN, i);
75+
delay(50);
76+
}
77+
}
78+
79+
// Makes the LED fade in and out n-times every 1 second.
80+
void fadeN(size_t n) {
81+
pinMode(LEDPIN, OUTPUT);
82+
// Fading in.
83+
for(int i = 0; i < 1024; i+=1024/20) {
84+
analogWrite(LEDPIN, i);
85+
delay(100/n);
86+
}
87+
// Fading out.
88+
for(int i = 1023; i > 0; i-=1024/20) {
89+
analogWrite(LEDPIN, i);
90+
delay(100/n);
91+
}
92+
}
93+
94+
#endif /* BLINK_H */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef WIFI_H
2+
#define WIFI_H
3+
4+
#include <Arduino.h>
5+
#include <WiFi.h>
6+
// #include "blink.h"
7+
8+
// Makes the device connect to a WiFi router or hotspot.
9+
void connectWiFi(const char* ssid, const char* passphrase) {
10+
// Disconnecting WiFi if it"s already connected.
11+
WiFi.disconnect();
12+
// Setting it to Station mode which basically scans for nearby WiFi routers or hotspots.
13+
WiFi.mode(WIFI_STA);
14+
// Begin connecting to WiFi using the provided ssid and passphrase.
15+
WiFi.begin(ssid, passphrase);
16+
// Print a debug log to Serial.
17+
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
18+
// Keep looping until the WiFi is not connected.
19+
while (WiFi.status() != WL_CONNECTED) {
20+
// Print dots in a horizontal line to the Serial, showing the WiFi is trying to connect.
21+
Serial.print(".");
22+
delay(1000);
23+
// Blink LED very fast, showing the WiFi is trying to connect.
24+
// blinkN(10);
25+
}
26+
// Stop the LED blinking, showing the WiFi is successfully connected.
27+
// blinkStop();
28+
// Print debug logs to Serial.
29+
Serial.println("WiFi connected");
30+
Serial.print("IP Address: ");
31+
Serial.println(WiFi.localIP()); // Local IP Address of the ESP8266 device.
32+
}
33+
34+
#endif /* WIFI_H */

examples/esp32/post/post.ino renamed to examples/esp32/sync/post/post.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#define SSID YourWiFiSSID
55
#define PASSPHRASE YourWiFiPassphrase
6+
// Certificate will expire and might need to be updated before running this sketch.
67
#define CACert "-----BEGIN CERTIFICATE-----\n\
78
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n\
89
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n\
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef WIFI_H
2+
#define WIFI_H
3+
4+
#include <Arduino.h>
5+
#include <WiFi.h>
6+
// #include "blink.h"
7+
8+
// Makes the device connect to a WiFi router or hotspot.
9+
void connectWiFi(const char* ssid, const char* passphrase) {
10+
// Disconnecting WiFi if it"s already connected.
11+
WiFi.disconnect();
12+
// Setting it to Station mode which basically scans for nearby WiFi routers or hotspots.
13+
WiFi.mode(WIFI_STA);
14+
// Begin connecting to WiFi using the provided ssid and passphrase.
15+
WiFi.begin(ssid, passphrase);
16+
// Print a debug log to Serial.
17+
Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", ssid, passphrase);
18+
// Keep looping until the WiFi is not connected.
19+
while (WiFi.status() != WL_CONNECTED) {
20+
// Print dots in a horizontal line to the Serial, showing the WiFi is trying to connect.
21+
Serial.print(".");
22+
// Blink LED very fast, showing the WiFi is trying to connect.
23+
delay(1000);
24+
// blinkN(10);
25+
}
26+
// Stop the LED blinking, showing the WiFi is successfully connected.
27+
// blinkStop();
28+
// Print debug logs to Serial.
29+
Serial.println("WiFi connected");
30+
Serial.print("IP Address: ");
31+
Serial.println(WiFi.localIP()); // Local IP Address of the ESP8266 device.
32+
}
33+
34+
#endif /* WIFI_H */

examples/esp8266/async/get/get.ino

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "recipes/WiFi.h"
2+
#include "Fetch.h"
3+
4+
#define SSID YourWiFiSSID
5+
#define PASSPHRASE YourWiFiPassphrase
6+
// Fingerprint will expire and might need to be updated before running this sketch.
7+
#define FINGERPRINT "29 70 30 74 CA 3C 48 F5 4A 79 C6 2D 11 57 A2 41 2A 2D 7D 5C"
8+
9+
// Fetch when used in async mode returns a client that listens for the response.
10+
FetchClient client;
11+
12+
// Function that handles the response when it's received.
13+
void handleResponse(Response response) {
14+
Serial.println("Response received:");
15+
// Printing response body as plain text.
16+
Serial.println();
17+
Serial.println(response.text());
18+
}
19+
20+
void setup() {
21+
Serial.begin(9600);
22+
connectWiFi(SSID, PASSPHRASE);
23+
24+
RequestOptions options;
25+
options.method = "GET";
26+
options.fingerprint = FINGERPRINT;
27+
28+
Serial.println("Sending the request...");
29+
30+
// Fetch sends the response, when received, to our handleResponse function, instead of blocking the rest of the code until the response is received.
31+
client = fetch("https://api.github.com/", options, handleResponse);
32+
33+
Serial.println("Request Sent!");
34+
Serial.println("Waiting for the response...");
35+
}
36+
37+
void loop() {
38+
// This is crucial, it listens on the connection for the response, until it's received.
39+
client.loop();
40+
}

0 commit comments

Comments
 (0)