Skip to content

Commit 45d12db

Browse files
committed
Code refactoring, add ESP8266HTTPClient and WiFiClientSecureBearSSL libs
1 parent 7661ac1 commit 45d12db

File tree

3 files changed

+35
-40
lines changed

3 files changed

+35
-40
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"C_Cpp.errorSquiggles": "Disabled"
3+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Connection diagram for ESP8266 firmware:
5353
|VCC|5v |
5454
|SDA|A4 |
5555
|SCL|A5 |
56+
5657
Update Arduino Uno firmware:
5758

5859
1. Open the **/main/main.ino** file

api/api.ino

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
#include <ArduinoJson.h>
22
#include <ESP8266WiFi.h>
3+
#include <ESP8266HTTPClient.h>
4+
#include <WiFiClientSecureBearSSL.h>
35

46
const char* ssid = "TP-Link_408";
57
const char* password = "84570550";
68

7-
#define CD_API "/v1/bpi/currentprice.json"
8-
#define CD_URL "api.coindesk.com"
9-
10-
static char respBuffer[4096];
11-
WiFiClient client;
9+
#define API "https://api.coindesk.com/v1/bpi/currentprice.json"
1210

1311
void setup() {
1412
Serial.begin(115200);
1513
delay(10);
1614
Serial.print("Connecting to ");
1715
Serial.println(ssid);
1816
WiFi.begin(ssid, password);
17+
1918
while (WiFi.status() != WL_CONNECTED) {
2019
delay(500);
2120
Serial.print(".");
2221
}
22+
2323
Serial.println("WiFi connected");
2424
Serial.print("IP address: ");
2525
Serial.println(WiFi.localIP());
@@ -31,41 +31,32 @@ void loop() {
3131
}
3232

3333
void getData() {
34-
const char request[] =
35-
"GET " CD_API " HTTP/1.1\r\n"
36-
"User-Agent: ESP8266/0.1\r\n"
37-
"Accept: */*\r\n"
38-
"Host: " CD_URL "\r\n"
39-
"Connection: close\r\n"
40-
"\r\n";
41-
delay(100);
42-
43-
if (!client.connect(CD_URL, 80)) {
44-
Serial.println("Connection failed");
45-
return;
46-
}
47-
48-
client.print(request);
49-
client.flush();
50-
delay(1000);
51-
uint16_t index = 0;
52-
while(client.connected() || client.available()) {
53-
if(client.available()) {
54-
respBuffer[index++] = client.read();
55-
delay(1);
34+
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
35+
36+
client->setInsecure();
37+
HTTPClient http;
38+
39+
if (WiFi.status() == WL_CONNECTED) {
40+
41+
HTTPClient http;
42+
43+
http.begin(*client, API);
44+
int httpCode = http.GET();
45+
46+
if (httpCode > 0) {
47+
48+
String payload = http.getString();
49+
String json_str = String(payload);
50+
DynamicJsonDocument doc(1024);
51+
DeserializationError error = deserializeJson(doc, payload);
52+
53+
JsonObject bpi = doc["bpi"];
54+
JsonObject usd = bpi["USD"];
55+
String rate_float = usd["rate_float"];
56+
57+
Serial.print(rate_float);
5658
}
59+
60+
http.end();
5761
}
58-
59-
char * json = strchr(respBuffer,'{');
60-
String json_str = String(json);
61-
uint16_t idx_d = json_str.lastIndexOf('d');
62-
json_str.remove(idx_d,3);
63-
DynamicJsonDocument doc(1024);
64-
DeserializationError error = deserializeJson(doc, json);
65-
66-
JsonObject bpi = doc["bpi"];
67-
JsonObject usd = bpi["USD"];
68-
String rate_float = usd["rate_float"];
69-
70-
Serial.print(rate_float);
7162
}

0 commit comments

Comments
 (0)