Skip to content

Commit 86893fd

Browse files
authored
Merge pull request #15 from Qrome/1.7
1.7
2 parents ddbb050 + bd4594f commit 86893fd

File tree

7 files changed

+1666
-1420
lines changed

7 files changed

+1666
-1420
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SOFTWARE.
3030
* Configured through Web Interface
3131
* Display 3D print progress from your OctoPrint Server
3232
* Option to display random goofy advice
33+
* Option to display Bitcoin current value
3334
* Basic Authorization around Configuration web interface
3435
* Support for OTA (loading firmware over WiFi)
3536
* Video: https://youtu.be/DsThufRpoiQ

marquee/BitcoinApiClient.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/** The MIT License (MIT)
2+
3+
Copyright (c) 2018 David Payne
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
*/
23+
24+
#include "BitcoinApiClient.h"
25+
26+
BitcoinApiClient::BitcoinApiClient() {
27+
//Constructor
28+
}
29+
30+
void BitcoinApiClient::updateBitcoinData(String currencyCode) {
31+
if (currencyCode == "" || currencyCode == "NONE") {
32+
bpiData.code = "";
33+
bpiData.rate = "";
34+
bpiData.description = "";
35+
bpiData.rate_float = 0;
36+
return; // nothing to do here
37+
}
38+
HTTPClient http;
39+
40+
String apiGetData = "http://" + String(servername) + "/v1/bpi/currentprice/" + currencyCode + ".json";
41+
42+
Serial.println("Getting Bitcoin Data");
43+
Serial.println(apiGetData);
44+
http.begin(apiGetData);
45+
int httpCode = http.GET();
46+
47+
String result = "";
48+
49+
if (httpCode > 0) { // checks for connection
50+
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
51+
if(httpCode == HTTP_CODE_OK) {
52+
// get lenght of document (is -1 when Server sends no Content-Length header)
53+
int len = http.getSize();
54+
// create buffer for read
55+
char buff[128] = { 0 };
56+
// get tcp stream
57+
WiFiClient * stream = http.getStreamPtr();
58+
// read all data from server
59+
Serial.println("Start reading...");
60+
while(http.connected() && (len > 0 || len == -1)) {
61+
// get available data size
62+
size_t size = stream->available();
63+
if(size) {
64+
// read up to 128 byte
65+
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
66+
for(int i=0;i<c;i++) {
67+
result += buff[i];
68+
}
69+
70+
if(len > 0)
71+
len -= c;
72+
}
73+
delay(1);
74+
}
75+
}
76+
http.end();
77+
} else {
78+
Serial.println("connection for news data failed: " + String(apiGetData)); //error message if no client connect
79+
Serial.println();
80+
return;
81+
}
82+
//Clean dirty results
83+
result.remove(0, result.indexOf("{"));
84+
result.remove(result.lastIndexOf("}") + 1);
85+
Serial.println("Results:");
86+
Serial.println(result);
87+
Serial.println("End");
88+
89+
char jsonArray [result.length()+1];
90+
result.toCharArray(jsonArray,sizeof(jsonArray));
91+
//jsonArray[result.length() + 1] = '\0';
92+
DynamicJsonBuffer json_buf;
93+
JsonObject& root = json_buf.parseObject(jsonArray);
94+
95+
if (!root.success()) {
96+
Serial.println(F("Bitcoin Data Parsing failed!"));
97+
return;
98+
}
99+
100+
bpiData.code = (const char*)root["bpi"][String(currencyCode)]["code"];
101+
bpiData.rate = (const char*)root["bpi"][String(currencyCode)]["rate"];
102+
bpiData.description = (const char*)root["bpi"][String(currencyCode)]["description"];
103+
bpiData.rate_float = String((const char*)root["bpi"][String(currencyCode)]["rate_float"]).toFloat();
104+
105+
Serial.println("code: " + bpiData.code);
106+
Serial.println("rate: " + bpiData.rate);
107+
Serial.println("description: " + bpiData.description);
108+
Serial.println("rate_float: " + String(bpiData.rate_float));
109+
110+
Serial.println();
111+
}
112+
113+
String BitcoinApiClient::getCode() {
114+
return bpiData.code;
115+
}
116+
117+
String BitcoinApiClient::getRate() {
118+
String rate = bpiData.rate;
119+
rate.remove(rate.indexOf(".") + 3);
120+
return rate;
121+
}
122+
123+
String BitcoinApiClient::getDescription() {
124+
return bpiData.description;
125+
}
126+
127+
float BitcoinApiClient::getRateFloat() {
128+
return bpiData.rate_float;
129+
}
130+

marquee/BitcoinApiClient.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/** The MIT License (MIT)
2+
3+
Copyright (c) 2018 David Payne
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
*/
23+
24+
#pragma once
25+
#include <ESP8266WiFi.h>
26+
#include <ESP8266HTTPClient.h>
27+
#include <ArduinoJson.h>
28+
29+
class BitcoinApiClient {
30+
31+
private:
32+
33+
String myCurrency = "";
34+
35+
const char* servername = "api.coindesk.com"; // remote server we will connect to https://www.coindesk.com/api/
36+
37+
typedef struct {
38+
String code;
39+
String rate;
40+
String description;
41+
float rate_float;
42+
} bpi;
43+
44+
bpi bpiData;
45+
46+
public:
47+
BitcoinApiClient();
48+
void updateBitcoinData(String currencyCode);
49+
50+
String getCode();
51+
String getRate();
52+
String getDescription();
53+
float getRateFloat();
54+
};
55+

marquee/NewsApiClient.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void NewsApiClient::updateNews() {
3939
JsonStreamingParser parser;
4040
parser.setListener(this);
4141
HTTPClient http;
42-
WiFiClient newsClient;
4342

4443
String apiGetData = "http://" + String(servername) + "/v2/top-headlines?sources=" + mySource + "&apiKey=" + myApiKey;
4544

@@ -152,4 +151,4 @@ String NewsApiClient::cleanText(String text) {
152151
text.replace("\\\"", "'");
153152
text.replace("", "-");
154153
return text;
155-
}
154+
}

0 commit comments

Comments
 (0)