Created
August 16, 2019 09:55
-
-
Save ngocbd/dba29c233c9f5de565c68a35dce77f21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFi.h> | |
#ifndef STASSID | |
#define STASSID "HUAWEI-AnqK" | |
#define STAPSK "KKXY244m" | |
#endif | |
const char* ssid = STASSID; | |
const char* password = STAPSK; | |
const char* host = "api.etherscan.io"; | |
const uint16_t port = 80; | |
String data = ""; | |
#include <LiquidCrystal_I2C.h> | |
#include <Wire.h> | |
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); | |
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0}; // example sprite bitmap | |
void setup() { | |
lcd.begin(0,2); // In ESP8266-01, SDA=0, SCL=2 | |
lcd.backlight(); | |
lcd.createChar(1, heart); | |
Serial.begin(9600); | |
Wire.begin(D1, D0); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default, | |
would try to act as both a client and an access-point and could cause | |
network-issues with your other WiFi-devices on your WiFi-network. */ | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void loop() { | |
lcd.home(); // At column=0, row=0 | |
lcd.print("ESP8266"); | |
lcd.setCursor(0, 1); | |
lcd.print("LiquidCrystalI2C"); | |
delay(500); | |
lcd.setCursor(10, 0); // At column=10, row=0 | |
lcd.write(byte(1)); | |
delay(500); | |
lcd.setCursor(10, 0); // At column=10, row=0 | |
lcd.print(" "); // Wipe sprite | |
Serial.print("connecting to "); | |
Serial.print(host); | |
Serial.print(':'); | |
Serial.println(port); | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
if (!client.connect(host, port)) { | |
Serial.println("connection failed"); | |
delay(5000); | |
return; | |
} | |
// This will send a string to the server | |
Serial.println("sending data to server"); | |
if (client.connected()) { | |
client.println("GET /api?module=stats&action=ethprice&apikey=YourApiKeyToken HTTP/1.0"); | |
client.println("Host: api.etherscan.io"); | |
client.println(); | |
} | |
// wait for data to be available | |
unsigned long timeout = millis(); | |
while (client.available() == 0) { | |
if (millis() - timeout > 5000) { | |
Serial.println(">>> Client Timeout !"); | |
client.stop(); | |
delay(60000); | |
return; | |
} | |
} | |
// Read all the lines of the reply from server and print them to Serial | |
Serial.println("receiving from remote server"); | |
// not testing 'client.connected()' since we do not need to send data here | |
while (client.available()) { | |
char ch = static_cast<char>(client.read()); | |
data+=ch; | |
Serial.print(ch); | |
} | |
// Close the connection | |
int begin = data.indexOf("ethusd")+9; | |
int end = data.indexOf('"',begin); | |
String price = data.substring(begin,end); | |
Serial.println(); | |
Serial.println(begin); | |
Serial.println(end); | |
Serial.println(price); | |
Serial.println("closing connection"); | |
client.stop(); | |
delay(300000); // execute once every 5 minutes, don't flood remote service | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment