Skip to content

Commit 710e02f

Browse files
committed
Add Arduino Network demo.
1 parent ac7b674 commit 710e02f

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <WiFi.h>
2+
#include <ArduinoJson.h>
3+
#include <FastLED.h>
4+
#include <cstdint>
5+
6+
// WiFi variables
7+
#ifndef STASSID
8+
#define STASSID "SSID"
9+
#define STAPSK "PASSWORD"
10+
#endif // !STASSID
11+
12+
const char* ssid = STASSID;
13+
const char* password = STAPSK;
14+
15+
const char* host = "corquaid.github.io";
16+
const uint16_t port = 443;
17+
18+
WiFiMulti multi;
19+
20+
// LED variables
21+
#define NUM_LEDS 1
22+
#define DATA_PIN PIN_NEOPIXEL
23+
#define LED_TYPE WS2812
24+
#define COLOR_ORDER GRB
25+
#define LED_BRIGHTNESS 30
26+
27+
CRGB leds[NUM_LEDS];
28+
29+
void setup() {
30+
Serial.begin(115200);
31+
32+
// Uncomment to wait for serial monitor
33+
// while(!Serial) { delay(100);};
34+
35+
// Setup RGB LED.
36+
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
37+
FastLED.setBrightness(LED_BRIGHTNESS);
38+
39+
// Set the LED Red while connecting to WiFi.
40+
FastLED.showColor(CRGB::Red);
41+
42+
Serial.print("Connecting to ");
43+
Serial.println(ssid);
44+
45+
// Connect to a WiFi network.
46+
multi.addAP(ssid, password);
47+
48+
if(multi.run() != WL_CONNECTED) {
49+
Serial.println(F("Unable to connect to network, rebooting in 10 seconds..."));
50+
delay(10000);
51+
rp2040.reboot();
52+
}
53+
54+
Serial.println(F("Connected to network"));
55+
Serial.println(F("IP address: "));
56+
Serial.println(WiFi.localIP());
57+
58+
// Create a WiFi clent. It has to be a "secure" client because the server we are connecting to uses HTTPS.
59+
BearSSL::WiFiClientSecure client;
60+
client.setTimeout(10000);
61+
// Doesn't validate the chain, just accept whatever is given. VERY INSECURE!
62+
client.setInsecure();
63+
// Connect to server.
64+
if(!client.connect(host, port)) {
65+
Serial.println(F("Connection failed"));
66+
delay(5000);
67+
return;
68+
}
69+
70+
Serial.println(F("Connected!"));
71+
72+
// Set the LED to blue while downloading data.
73+
FastLED.showColor(CRGB::Blue);
74+
75+
Serial.println(F("Downloading list of astronauts currently in space..."));
76+
77+
// Send HTTP GET request
78+
client.println(F("GET /international-space-station-APIs/JSON/people-in-space.json HTTP/1.0"));
79+
client.println(F("Host: corquaid.github.io"));
80+
client.println(F("Connection: close"));
81+
82+
if(client.println() == 0) {
83+
Serial.println(F("Failed to send request"));
84+
client.stop();
85+
return;
86+
}
87+
88+
// Check HTTP status
89+
char status[32] = {0};
90+
client.readBytesUntil('\r', status, sizeof(status));
91+
if(strncmp(status + 9, "200 OK", sizeof(status)) != 0) {
92+
Serial.print(F("Unexpected response: "));
93+
Serial.println(status);
94+
client.stop();
95+
return;
96+
}
97+
98+
// Skip HTTP headers
99+
const char endOfHeaders[] = "\r\n\r\n";
100+
if(!client.find(endOfHeaders)) {
101+
Serial.println(F("Invalid response"));
102+
client.stop();
103+
return;
104+
}
105+
106+
// Parse JSON
107+
JsonDocument doc;
108+
DeserializationError error = deserializeJson(doc, client);
109+
if(error) {
110+
Serial.print(F("deserializeJson() failed: "));
111+
Serial.println(error.f_str());
112+
client.stop();
113+
return;
114+
}
115+
116+
// Print number of astronauts in space and their names.
117+
int numberOfPeople = doc[F("number")];
118+
119+
Serial.print(F("There are "));
120+
Serial.print(numberOfPeople);
121+
Serial.println(F(" astronauts in space."));
122+
123+
for(JsonObject people_item : doc[F("people")].as<JsonArray>()) {
124+
int people_item_id = people_item[F("id")];
125+
const char* people_item_name = people_item[F("name")];
126+
Serial.print(people_item_id);
127+
Serial.print(F(": "));
128+
Serial.println(people_item_name);
129+
}
130+
131+
// Set the LED to green to indicate success.
132+
FastLED.showColor(CRGB::Green);
133+
}
134+
135+
void loop() {}

0 commit comments

Comments
 (0)