diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..30fa040 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 bolsonitro + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..68218c1 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Blockclock for M5Stick CPlus + +Este é um relógio de blocos que funciona conectado à wifi, puxando dados da mempool.space à cada 1 minuto. + +![M5StickC running blobkclock](https://raw.githubusercontent.com/bolsonitro/Blockclock_M5StickC_Plus/master/m5stickblockclock.jpg) + +## Instalando + +1. Baixe a ultima versão do Arduino IDE +2. Siga o [tutorial da M5Stack](https://docs.m5stack.com/en/quick_start/m5stickc_plus/arduino) +3. Clone o repósitório e abra o arquivo blockclock.ino com a IDE do arduino +4. Selecione a porta COM correspondente à sua M5Stick e o modelo +5. Grave o código (Seta para a direita no canto superior esquerdo) \ No newline at end of file diff --git a/blockclock.ino b/blockclock.ino new file mode 100644 index 0000000..65bec25 --- /dev/null +++ b/blockclock.ino @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +#include +#include + +#include "WiFi.h" + +WiFiMulti wifiMulti; +HTTPClient http; + +String blockHeightGlobal; +int bat; +const char* SSID = "WIFISSID"; +const char* PASSWD = "WIFIPASSWORD"; + +void setup() { + M5.begin(true, true, false); + + M5.Lcd.setTextSize(2); + M5.Lcd.setRotation(1); + M5.Lcd.setCursor(10, 10); + M5.Lcd.println("BLOCKCLOCK"); + M5.Lcd.setCursor(10, 30); + M5.Lcd.println("Connecting Wifi"); + wifiMulti.addAP(SSID, PASSWD); + + M5.Lcd.setCursor(10, 50); + + if (wifiMulti.run() == WL_CONNECTED) { + M5.Lcd.println("Wifi connected"); + M5.Lcd.setCursor(10, 70); + delay(500); + } else { + M5.Lcd.println("Connection failed"); + } +} + +void loop() { + String blockheight; + + if (wifiMulti.run() == WL_CONNECTED) { + blockheight = getBlockHeight(); + if (blockheight != blockHeightGlobal) { + blockHeightGlobal = blockheight; + clearScreen(); + printInfo(); + } + } + if (!isCharging()) { + printBattery(); + } + delay(60000); +} + +String getBlockHeight() { + http.begin("https://mempool.space/api/blocks/tip/height"); + int httpCode = http.GET(); + if (httpCode == 200) { + return http.getString(); + } + return "ERR " + httpCode; +} + +void printInfo() { + M5.Lcd.setCursor(5, 10); + M5.Lcd.setTextSize(3); + M5.Lcd.print("Block Height:"); + M5.Lcd.setCursor(60, 80); + M5.Lcd.print(blockHeightGlobal); +} + +bool isCharging() { + float batteryCurrent = M5.Axp.GetBatCurrent(); + if (batteryCurrent < 0) return false; + return true; +} + +void printBattery() { + M5.Lcd.setTextSize(2); + bat = calculateBatteryPercentage(M5.Axp.GetBatVoltage()); + M5.Lcd.setCursor(185, 115); + if (bat > 100) { + M5.Lcd.print("100%"); + return; + } + M5.Lcd.print(String(bat) + "%"); +} + +int calculateBatteryPercentage(float voltage) { + // https://forum.micropython.org/viewtopic.php?f=2&t=7615#p43401 + return (int)trunc((voltage - 3.7) * (100 - 0) / (4.1 - 3.7) + 0); +} + +void clearScreen() { M5.Lcd.fillRect(0, 0, 240, 135, BLACK); } diff --git a/m5stickblockclock.jpg b/m5stickblockclock.jpg new file mode 100644 index 0000000..908bb15 Binary files /dev/null and b/m5stickblockclock.jpg differ