1
1
#include " price_notify.hpp"
2
2
3
- const char *wsServerPrice = " wss://ws.coincap.io/prices?assets=bitcoin " ;
3
+ const char *wsServerPrice = " wss://ws.kraken.com/v2 " ;
4
4
5
5
WebSocketsClient webSocket;
6
6
uint currentPrice = 90000 ;
@@ -14,7 +14,7 @@ void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length);
14
14
15
15
void setupPriceNotify ()
16
16
{
17
- webSocket.beginSSL (" ws.coincap.io " , 443 , " /prices?assets=bitcoin " );
17
+ webSocket.beginSSL (" ws.kraken.com " , 443 , " /v2 " );
18
18
webSocket.onEvent ([](WStype_t type, uint8_t * payload, size_t length) {
19
19
onWebsocketPriceEvent (type, payload, length);
20
20
});
@@ -32,21 +32,30 @@ void onWebsocketPriceEvent(WStype_t type, uint8_t * payload, size_t length) {
32
32
case WStype_CONNECTED:
33
33
{
34
34
Serial.println (" Connected to " + String (wsServerPrice));
35
- priceNotifyInit = true ;
35
+
36
+ JsonDocument doc;
37
+ doc[" method" ] = " subscribe" ;
38
+ JsonObject params = doc[" params" ].to <JsonObject>();
39
+ params[" channel" ] = " ticker" ;
40
+ params[" symbol" ][0 ] = " BTC/USD" ;
41
+
42
+ webSocket.sendTXT (doc.as <String>().c_str ());
36
43
break ;
37
44
}
38
45
case WStype_TEXT:
39
46
{
40
47
JsonDocument doc;
41
48
deserializeJson (doc, (char *)payload);
42
49
43
- if (doc[" bitcoin " ].is <JsonObject>())
50
+ if (doc[" data " ][ 0 ].is <JsonObject>())
44
51
{
45
- if (currentPrice != doc[" bitcoin" ].as <long >())
52
+ float price = doc[" data" ][0 ][" last" ].as <float >();
53
+ uint roundedPrice = round (price);
54
+ if (currentPrice != roundedPrice)
46
55
{
47
- processNewPrice (doc[ " bitcoin " ]. as < long >() , CURRENCY_USD);
56
+ processNewPrice (roundedPrice , CURRENCY_USD);
48
57
}
49
- }
58
+ }
50
59
break ;
51
60
}
52
61
case WStype_BIN:
0 commit comments