In this project, my goal was to create an alarm similar to the one used by one of the main characters in the series Silicon Valley, known as Gilfoyle.
He used an alarm that incorporates the song ‘you suffer’ by Napalm Death to indicate when the bitcoin price has sufficiently decreased to make mining unprofitable.
To get the job done, I used the nodemcu (esp8266) microcontroller, a tiny speaker, and an NPN transistor (BC550).
The microcontroller connects to the network and sends a request to the CoinGecko API. Meanwhile, the song is encoded in hexadecimal format within the microcontroller’s memory, triggering playback when the bitcoin price falls below 26,700 euros.
#include <Arduino.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2SNoDAC.h"
#include "btc.h"
AudioGeneratorWAV *wav;
AudioFileSourcePROGMEM *file;
AudioOutputI2SNoDAC *out;
bool audioPlaying = false; // Flag
const char* ssid = "wifissid";
const char* password = "pass";
const char* bitcoinPriceUrl = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=eur";
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected");
delay(1000);
audioLogger = &Serial;
out = new AudioOutputI2SNoDAC();
wav = new AudioGeneratorWAV();
}
void loop() {
// HTTP request
static bool requestSent = false;
static bool responseReceived = false;
static String payload;
if (!requestSent && !responseReceived) {
WiFiClientSecure client;
HTTPClient http;
http.begin(client, bitcoinPriceUrl);
client.setInsecure(); // no ssl
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// JSON answer
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
float bitcoinPrice = doc["bitcoin"]["eur"];
Serial.print("Bitcoin: ");
Serial.println(bitcoinPrice, 2);
if(bitcoinPrice<=26700){
if (!audioPlaying) {
// reset the pointer
file = new AudioFileSourcePROGMEM(btc, sizeof(btc));
wav->begin(file, out);
audioPlaying = true;
}
if (wav->isRunning()) {
if (!wav->loop()) {
wav->stop();
audioPlaying = false;
}
}
}
} else {
Serial.println("Error-JSON.");
}
responseReceived = true;
} else {
Serial.printf("Error in HTTP request . Code: %d\n", httpCode);
responseReceived = true;
}
http.end();
requestSent = true;
}
if (wav->isRunning()) {
if (!wav->loop()) {
wav->stop();
audioPlaying = false;
}
}
//wait a minute
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 60000) {
previousMillis = currentMillis;
requestSent = false;
responseReceived = false;
}
}
TV series scene: