Skip to content

Commit 19e563a

Browse files
author
MA
committed
Basic GET and POST requests are working.
1 parent 996185e commit 19e563a

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

examples/get/get.ino

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
#include "recipes/WiFi.h"
12
#include "ArduinoFetch.h"
3+
4+
#define SSID WIFISSID
5+
#define PASSPHRASE WIFIPASSPHRASE
26

37
void setup() {
4-
Serial.begin(9600);
8+
Serial.begin(9200);
9+
connectWiFi(SSID, PASSPHRASE);
510

611
RequestOptions options;
7-
options.method = "GET";
8-
Response response = fetch("https://api.grandeur.tech/", options);
12+
options.method = "POST";
13+
options.headers.contentType = "application/json";
14+
options.body = "{\"email\": \"[email protected]\", \"password\": \"test:80\"}";
15+
16+
Response response = fetch("https://api.grandeur.tech/auth/login/?apiKey=grandeurkywxmoy914080rxf9dh05n7e", options);
917

1018
Serial.println(response.text());
1119
}

examples/get/recipes/Blink.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#ifndef BLINK_H
2+
#define BLINK_H
3+
4+
#define LEDPIN 2
5+
#define OFF 1
6+
#define ON 0
7+
8+
#include <Arduino.h>
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 */

examples/get/recipes/WiFi.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef WIFI_H
2+
#define WIFI_H
3+
4+
#include <Arduino.h>
5+
#include <ESP8266WiFi.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+
blinkN(10);
24+
}
25+
// Stop the LED blinking, showing the WiFi is successfully connected.
26+
blinkStop();
27+
// Print debug logs to Serial.
28+
Serial.println("WiFi connected");
29+
Serial.print("IP Address: ");
30+
Serial.println(WiFi.localIP()); // Local IP Address of the ESP8266 device.
31+
}
32+
33+
#endif /* WIFI_H */

0 commit comments

Comments
 (0)