Created
February 19, 2018 20:13
-
-
Save janhajk/8d07014d7fb25efa3956950c7cda912d to your computer and use it in GitHub Desktop.
ESP8266 Bitcointracker
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 <ArduinoJson.h> | |
#include <SSD1306Brzo.h> | |
#include <WiFiUdp.h> | |
#include <ArduinoOTA.h> | |
#include <WiFiClientSecure.h> | |
// SHA1 Fingerprint von Bitstamp.net | |
const char* fingerprint = "D0 26 AB 06 64 07 BC 88 56 6D 83 BE 0A 29 00 B5 10 E5 27 D2"; | |
// W-Lan Zugansdaten | |
const char WIFI_SSID[] = ".............."; | |
const char WIFI_PSK[] = ".............."; | |
// Die Adresse, zu welcher eine GET-Abfrage gemacht werden soll | |
const char* host = "www.bitstamp.net"; | |
const int port = 443; // bitstamp benutzt SSL | |
// Pin Definitionen | |
const int LED_PIN = LED_BUILTIN; | |
// Globale Variablen | |
//WiFiClient client; | |
WiFiClientSecure client; | |
// OLED display initialisieren | |
// D3 -> SDA | |
// D5 -> SCL | |
SSD1306Brzo display(0x3c, D3, D5); | |
int screenW = 128; | |
int screenH = 32; | |
bool firstrun = true; | |
// Setup wird beim Programmstart einmal ausgeführt | |
void setup() { | |
// Set up serial console to read web page | |
Serial.begin(115200); | |
// LED Pin wird auf OUTPUT gesetzt, damit wir sie ein und ausschalten können | |
pinMode(LED_PIN, OUTPUT); | |
display.init(); | |
display.flipScreenVertically(); | |
progress("connect to WiFi", 10); | |
// Mit dem WLAN verbinden | |
connectWiFi(); | |
// ------------------------- | |
// OTA | |
ArduinoOTA.onStart([]() { | |
String type; | |
if (ArduinoOTA.getCommand() == U_FLASH) | |
type = "sketch"; | |
else // U_SPIFFS | |
type = "filesystem"; | |
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() | |
Serial.println("Start updating " + type); | |
}); | |
ArduinoOTA.onEnd([]() { | |
Serial.println("\nEnd"); | |
}); | |
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | |
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); | |
}); | |
ArduinoOTA.onError([](ota_error_t error) { | |
Serial.printf("Error[%u]: ", error); | |
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); | |
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); | |
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); | |
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); | |
else if (error == OTA_END_ERROR) Serial.println("End Failed"); | |
}); | |
ArduinoOTA.begin(); | |
// ------------------------- | |
} | |
// Loop wird bei jedem "Systemtakt" der ESP8266 ausgeführt | |
void loop() { | |
ArduinoOTA.handle(); | |
// Versuchen, sich mit der Webseite zu verbinden | |
if ( !getPage() ) { | |
Serial.println("Fehler bei GET Abfrage"); | |
} | |
delay(5000); | |
} | |
// Mit WLAN verbinden | |
void connectWiFi() { | |
byte led_status = 0; // LED ausschalten | |
// WLAN Modus auf Client-Modus setzten | |
WiFi.mode(WIFI_STA); | |
// Verbindung mit WLAN initieren mittels SSID und WLAN-Passwort | |
WiFi.begin(WIFI_SSID, WIFI_PSK); | |
// Versuchen mit dme WLAN zu verbinden | |
// Während dem Verbindungsversuch die LED Blinken lassen | |
while ( WiFi.status() != WL_CONNECTED ) { | |
digitalWrite(LED_PIN, led_status); | |
led_status ^= 0x01; // XOR Operation zum umschalten zwischen 0 und 1 | |
delay(100); // Intervall in Millisekunden | |
} | |
// Wenn die Verbindung erfolgreich ist, dann LED einschalten | |
digitalWrite(LED_PIN, LOW); | |
progress("WiFi connected", 40); | |
} | |
// Eine HTTP GET Abfrage ausführen | |
bool getPage() { | |
if (firstrun) { | |
progress("WiFi connected", 50); | |
} | |
Serial.print("Verbindung zu "); | |
Serial.println(host); | |
// Versuchen mit der Adresse zu verbinden | |
if ( !client.connect(host, port) ) { | |
return false; | |
} | |
// Wird nur bei SSL verwendet | |
if (client.verify(fingerprint, host)) { | |
Serial.println("Zertifikate stimmen überein"); | |
} else { | |
Serial.println("Zertifikate stimmen nicht überein!"); | |
return false; | |
} | |
// HTTP GET request ausführen | |
// index.html kann man mit dem Pfad ersetzen, z.B. /api/v1/temperatur | |
client.println("GET /api/v2/ticker/btcusd HTTP/1.1"); | |
client.print("Host: "); | |
client.println(host); | |
client.println("Connection: close"); | |
client.println(); | |
while (client.connected()) { | |
String line = client.readStringUntil('\n'); | |
if (line == "\r") { | |
Serial.println("headers received"); | |
break; | |
} | |
} | |
if (firstrun) { | |
progress("WiFi connected", 60); | |
} | |
String line = client.readStringUntil('\n'); | |
Serial.println("Antwort von host:"); | |
Serial.println("=========="); | |
Serial.println(line); | |
Serial.println("=========="); | |
StaticJsonBuffer<400> jsonBuffer; | |
Serial.print("Letzter Bitcoin Preis:"); | |
JsonObject& root = jsonBuffer.parseObject(line); | |
String data = root["last"]; | |
Serial.println(data); | |
if (firstrun) { | |
progress("WiFi connected", 100); | |
} | |
outputBitcoin(data); | |
if (firstrun) { | |
firstrun = false; | |
} | |
return true; | |
} | |
// Statusbar zeichnen | |
void progress(String text, uint8_t progress) { | |
display.clear(); | |
display.setTextAlignment(TEXT_ALIGN_CENTER); | |
display.setFont(ArialMT_Plain_16); | |
display.drawString(screenW/2, 5, text); | |
display.drawProgressBar(screenW/2-2*screenW/3/2, 50, screenW*2/3, 10, progress); | |
display.display(); | |
} | |
// Bitcoin Preis ausgeben | |
void outputBitcoin(String text) { | |
display.clear(); | |
display.setTextAlignment(TEXT_ALIGN_LEFT); | |
display.setFont(ArialMT_Plain_24); | |
display.drawString(10, 0, "Bitstamp"); | |
display.drawString(10, 25, text); | |
display.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment