-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibraGetBalance.ino
86 lines (65 loc) · 2.22 KB
/
LibraGetBalance.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* LibraGetBalance.ino clone from BasicHTTPClient.ino
*
* Created on: 22/09/2019
*
* Modify by: Mr.Boonsanti Tungisarawuttigul
*/
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
const String LIBRA_ADDRESS = "f86a39a0e53a111251e9769099d18cc04053c7813e4ebf0b0e554eb196a320f4";
const String GET_BALANCE_ADDRESS = "https://libraservice2.kulap.io/getBalance";
void setup() {
Serial.begin(115200);
setup_wifi();
Serial.println();
Serial.println("Getting Balance.. " + GET_BALANCE_ADDRESS + "/" + LIBRA_ADDRESS);
}
void setup_wifi() {
delay(10);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.write("Wifi connecting.\n");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.write(".");
}
Serial.write("Wifi connected.\n");
}
void loop() {
StaticJsonBuffer<100> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
char jsonMessage[100];
root["address"] = LIBRA_ADDRESS;
root.printTo(jsonMessage);
// wait for WiFi connection
if((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
// configure traged server and url
http.begin(GET_BALANCE_ADDRESS); //HTTP
http.addHeader("Content-Type", "application/json");
// start connection and send HTTP header
int httpCode = http.POST(jsonMessage);
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
char charBuf[100];
payload.toCharArray(charBuf, 100);
StaticJsonBuffer<100> jb;
JsonObject& obj = jb.parseObject(charBuf);
const String balance = obj["balance"];
Serial.println("Balance: " + balance);
}
} else {
Serial.printf("POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(1000);
}