記錄一次使用 ESP8266 做比特幣視價(jià)器

前言

2018年開始投資比特幣等數(shù)字貨幣谊迄,不料一路暴跌您觉,血虧之后拙寡,卸載所有 app 。

計(jì)劃屯幣到2020年琳水,等比特幣再次減半,希望到時(shí)能回本或者小賺般堆。

總得做點(diǎn)什么紀(jì)念這次失敗的炒幣行動(dòng)在孝,作為一個(gè)程序員,只能用代碼來表示了淮摔。

決定做一個(gè)簡單的比特幣示價(jià)器私沮,隨時(shí)隨地可以查看價(jià)格。


前期準(zhǔn)備

硬件部分

- ESP8266 NodeMcu WIFI??

? ? 這是一款類似 Arduino 一樣操作硬件IO的設(shè)備和橙,支持Nodejs 類似語法仔燕,而且集成了一個(gè)超低成本的 wifi 模塊。非常適合我們用來做一些小玩意魔招。

- 0.96寸OLED顯示屏

?????一個(gè)非常小的單色顯示器晰搀,不支持中文顯示,優(yōu)點(diǎn)僅僅是便宜办斑!

- 母對母杜邦線

? ? 為了連接芯片和顯示器用的外恕。

軟件部分

由于我用的是 macbook 所以杆逗,下載地址都是 mac 版本的。其他平臺(tái)請到官網(wǎng)下載相應(yīng)版本

- Arduino

? ?下載地址:https://www.arduino.cc/en/Main/Software

- CH340/CH341的USB轉(zhuǎn)串口驅(qū)動(dòng)程序

? 下載地址:http://www.wch.cn/download/CH341SER_MAC_ZIP.html

連接方式:


引腳連接圖

最終效果

代碼

/*******************************************************************

? ? A project to display crypto currency prices using an ESP8266

? ? Main Hardware:

? ? - NodeMCU Development Board (Any ESP8266 dev board will work)

? ? - OLED I2C Display (SH1106)

? ? Written by Brian Lough

? ? https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA

*******************************************************************/

// ----------------------------

// Standard Libraries - Already Installed if you have ESP8266 set up

// ----------------------------

#include <ESP8266WiFi.h>

#include <WiFiClientSecure.h>

#include <Wire.h>

// ----------------------------

// Additional Libraries - each one of these will need to be installed.

// ----------------------------

#include <CoinMarketCapApi.h>

// For Integrating with the CoinMarketCap.com API

// Available on the library manager (Search for "CoinMarket")

// https://github.com/witnessmenow/arduino-coinmarketcap-api

#include "SH1106.h"

// The driver for the OLED display

// Available on the library manager (Search for "oled ssd1306")

// https://github.com/squix78/esp8266-oled-ssd1306

#include <ArduinoJson.h>

// !! NOTE !!: When installing this select an older version than V6 from the drop down

// Required by the CoinMarketCapApi Library for parsing the response

// Available on the library manager (Search for "arduino json")

// https://github.com/squix78/esp8266-oled-ssd1306

// ----------------------------

// Configurations - Update these

// ----------------------------

char ssid[] = " wifi 名";? ? ? // your network SSID (name)

char password[] = " wifi 密碼";? // your network key

// Pins based on your wiring

#define SCL_PIN D5

#define SDA_PIN D3

// CoinMarketCap's limit is "no more than 10 per minute"

// Make sure to factor in if you are requesting more than one coin.

// We'll request a new value just before we change the screen so it's the most up to date

unsigned long screenChangeDelay = 3500; // Every 10 seconds

// Have tested up to 10, can probably do more

#define MAX_HOLDINGS 10

#define CURRENCY "usd" //See CoinMarketCap.com for currency options (usd, gbp etc)

#define CURRENCY_SYMBOL "$" // Euro doesn't seem to work, $ and £ do

// You also need to add your crypto currecnies in the setup function

// ----------------------------

// End of area you need to change

// ----------------------------

WiFiClientSecure client;

CoinMarketCapApi api(client);

SH1106 display(0x3c, SDA_PIN, SCL_PIN);

unsigned long screenChangeDue;

struct Holding {

? String tickerId;

? float amount;

? bool inUse;

? CMCTickerResponse lastResponse;

};

Holding holdings[MAX_HOLDINGS];

int currentIndex = -1;

String ipAddressString;

void addNewHolding(String tickerId, float amount = 0) {

? int index = getNextFreeHoldingIndex();

? if (index > -1) {

? ? holdings[index].tickerId = tickerId;

? ? holdings[index].amount = amount;

? ? holdings[index].inUse = true;

? }

}

void setup() {

? Serial.begin(115200);

? // ----------------------------

? // Holdings - Add your currencies here

? // ----------------------------

? // Go to the currencies coinmarketcap.com page

? // and take the tickerId from the URL (use bitcoin or ethereum as an example)


? addNewHolding("bitcoin");

? addNewHolding("eos");

? addNewHolding("ethereum");

? addNewHolding("huobi-token");

? // ----------------------------

? // Everything below can be thinkered with if you want but should work as is!

? // ----------------------------

? // Initialising the display

? display.init();

? display.setTextAlignment(TEXT_ALIGN_CENTER);

? display.setFont(ArialMT_Plain_16);

? display.drawString(64, 0, F("BTC ETH EOS"));

? display.setFont(ArialMT_Plain_24);

? display.drawString(64, 18, F("DaZhang!!"));

? display.setFont(ArialMT_Plain_10);

? display.drawString(64, 42, F("desigen By zjm"));

? display.display();


? // Set WiFi to station mode and disconnect from an AP if it was Previously

? // connected

? WiFi.mode(WIFI_STA);

? WiFi.disconnect();

? delay(100);

? // Attempt to connect to Wifi network:

? Serial.print("Connecting Wifi: ");

? Serial.println(ssid);

? WiFi.begin(ssid, password);

? while (WiFi.status() != WL_CONNECTED) {

? ? Serial.print(".");

? ? delay(500);

? }

? Serial.println("");

? Serial.println("WiFi connected");

? Serial.println("IP address: ");

? IPAddress ip = WiFi.localIP();

? Serial.println(ip);

? ipAddressString = ip.toString();

}

int getNextFreeHoldingIndex() {

? for (int i = 0; i < MAX_HOLDINGS; i++) {

? ? if (!holdings[i].inUse) {

? ? ? return i;

? ? }

? }

? return -1;

}

int getNextIndex() {

? for (int i = currentIndex + 1; i < MAX_HOLDINGS; i++) {

? ? if (holdings[i].inUse) {

? ? ? return i;

? ? }

? }

? for (int j = 0; j <= currentIndex; j++) {

? ? if (holdings[j].inUse) {

? ? ? return j;

? ? }

? }

? return -1;

}

void displayHolding(int index) {

? CMCTickerResponse response = holdings[index].lastResponse;

? display.clear();

? display.setTextAlignment(TEXT_ALIGN_CENTER);

? display.setFont(ArialMT_Plain_16);

? display.drawString(64, 0, response.symbol);

? display.setFont(ArialMT_Plain_24);

? double price = response.price_currency;

? if (price == 0) {

? ? price = response.price_usd;

? }

? display.drawString(64, 20, formatCurrency(price));

? display.setFont(ArialMT_Plain_16);

//? display.setTextAlignment(TEXT_ALIGN_CENTER);

//? display.drawString(64, 48, " 1h:" + String(response.percent_change_1h) + "%");

? display.setTextAlignment(TEXT_ALIGN_CENTER);

? display.drawString(64, 48, "24h: " + String(response.percent_change_24h) + "%");

? display.display();

}

void displayMessage(String message){

? display.clear();

? display.setFont(ArialMT_Plain_10);

? display.setTextAlignment(TEXT_ALIGN_LEFT);

? display.drawStringMaxWidth(0, 0, 128, message);

? display.display();

}

String formatCurrency(float price) {

? String formattedCurrency = CURRENCY_SYMBOL;

? int pointsAfterDecimal = 6;

? if (price > 100) {

? ? pointsAfterDecimal = 2;

? } else if (price > 1) {

? ? pointsAfterDecimal = 4;

? }

? formattedCurrency.concat(String(price, pointsAfterDecimal));

? return formattedCurrency;

}

bool loadDataForHolding(int index) {

? int nextIndex = getNextIndex();

? if (nextIndex > -1 ) {

? ? holdings[index].lastResponse = api.GetTickerInfo(holdings[index].tickerId, CURRENCY);

? ? return holdings[index].lastResponse.error == "";

? }

? return false;

}

void loop() {

? unsigned long timeNow = millis();

? if ((timeNow > screenChangeDue))? {

? ? currentIndex = getNextIndex();

? ? if (currentIndex > -1) {

? ? ? if (loadDataForHolding(currentIndex)) {

? ? ? ? displayHolding(currentIndex);

? ? ? } else {

? ? ? ? displayMessage(F("Error loading data."));

? ? ? }

? ? } else {

? ? ? displayMessage(F("No funds to display. Edit the setup to add them"));

? ? }

? ? screenChangeDue = timeNow + screenChangeDelay;

? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末鳞疲,一起剝皮案震驚了整個(gè)濱河市罪郊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尚洽,老刑警劉巖悔橄,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異腺毫,居然都是意外死亡癣疟,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進(jìn)店門拴曲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來争舞,“玉大人,你說我怎么就攤上這事澈灼【捍ǎ” “怎么了?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵叁熔,是天一觀的道長委乌。 經(jīng)常有香客問我,道長荣回,這世上最難降的妖魔是什么遭贸? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮心软,結(jié)果婚禮上壕吹,老公的妹妹穿的比我還像新娘。我一直安慰自己删铃,他們只是感情好耳贬,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布丹锹。 她就那樣靜靜地躺著侧漓,像睡著了一般。 火紅的嫁衣襯著肌膚如雪饲齐。 梳的紋絲不亂的頭發(fā)上诫隅,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天腐魂,我揣著相機(jī)與錄音,去河邊找鬼逐纬。 笑死蛔屹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的风题。 我是一名探鬼主播判导,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼嫉父,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了眼刃?” 一聲冷哼從身側(cè)響起绕辖,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎擂红,沒想到半個(gè)月后仪际,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡昵骤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年树碱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片变秦。...
    茶點(diǎn)故事閱讀 39,722評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡成榜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蹦玫,到底是詐尸還是另有隱情赎婚,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布樱溉,位于F島的核電站挣输,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏福贞。R本人自食惡果不足惜撩嚼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望挖帘。 院中可真熱鬧完丽,春花似錦、人聲如沸拇舀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽你稚。三九已至,卻和暖如春朱躺,著一層夾襖步出監(jiān)牢的瞬間刁赖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工长搀, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留宇弛,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓源请,卻偏偏與公主長得像枪芒,于是被迫代替她去往敵國和親彻况。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容