Sneha Raju
Published © GPL3+

Bitcoin Alert System

This system alerts the user about the bitcoin prices by sending notifications to your mobile and updates the latest prices to your telegram

BeginnerShowcase (no instructions)566
Bitcoin Alert System

Things used in this project

Hardware components

Buzzer
Buzzer
×1
Bolt IoT connecting wires
×1
Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1

Software apps and online services

Maker service
IFTTT Maker service
Telegram

Story

Read more

Schematics

Circuit Connections

Code

Python code for bitcoin alert system

Python
import requests,conf
import time
from datetime import datetime
from boltiot import Bolt

BITCOIN_PRICE_THRESHOLD = 9500
mybolt = Bolt(conf.bolt_api_key,conf.device_id)
BITCOIN_API_URL = 'https://api.coinmarketcap.com/v1/ticker/bitcoin/'
IFTTT_WEBHOOKS_URL = 'https://maker.ifttt.com/trigger/{}/with/key/{your key}'


def buzzer_on_off(pin,state):
    if state == "on":
        mybolt.analogWrite(pin,'255')
    elif state == "off":
        mybolt.analogWrite(pin,'0')   

def get_latest_bitcoin_price():
    response = requests.get(BITCOIN_API_URL)
    response_json = response.json()
    # Convert the price to a floating point number
    return float(response_json[0]['price_usd'])


def post_ifttt_webhook(event, value):
    # The payload that will be sent to IFTTT service
    data = {'value1': value}
    # inserts our desired event
    ifttt_event_url = IFTTT_WEBHOOKS_URL.format(event)
    # Sends a HTTP POST request to the webhook URL
    requests.post(ifttt_event_url, json=data)

def format_bitcoin_history(bitcoin_history):
    rows = []
    for bitcoin_price in bitcoin_history:
        # Formats the date into a string: '24.02.2018 15:09'
        date = bitcoin_price['date'].strftime('%d.%m.%Y %H:%M')
        price = bitcoin_price['price']
        row = '{}: $<b>{}</b>'.format(date, price)
        rows.append(row)

    
    
    return '<br>'.join(rows)

def main():
    bitcoin_history = []
    while True:
        price = get_latest_bitcoin_price()
        date = datetime.now()
        bitcoin_history.append({'date': date, 'price': price})

        # Send an emergency notification
        if price < BITCOIN_PRICE_THRESHOLD:
            post_ifttt_webhook('bitcoin_price_emergency', price)


        else:
            post_ifttt_webhook('bitcoin_alert_message',price)
            buzzer_on_off(0,"on")
            time.sleep(1)
            buzzer_on_off(0,"off")
            time.sleep(1)
            

        # Send a Telegram notification
        
        if len(bitcoin_history) == 5:
            post_ifttt_webhook('bitcoin_price_update', 
                               format_bitcoin_history(bitcoin_history))
            # Reset the history
            bitcoin_history = []

        
        time.sleep(2*60)
   

if __name__ == '__main__':
    main()

Credits

Sneha Raju

Sneha Raju

1 project • 0 followers

Comments