Project 12: Assignment - Price Alerting System

Hi guys, I’m trying to write a python code to create a buzzer alert if the bitcoin value in INR crosses the threshold value I have fixed in the code below. After installing all the required library files on the server I’m still facing an error saying that “NameError: name ‘requests’ is not defined”. I’m new to python, please do mention if you find any syntax or any other mistakes in my code. Thanks a lot for the help!!

CODE

from boltiot import Bolt

def get_bitcoin_price():
URL = “https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=INR
response = requests.request(“GET”, URL)
response = json.loads(response.text)
current_price = response[“IND”]
return current_price

threshold = 864999.52
price = get_bitcoin_price()
if price >= threshold:
print(“Price greater than threshold”)
count = 1
message = price
print(“Price of 1 bitcoin is:”, get_bitcoin_price)
api_key = “b147d655-cf93-4494-90df-c717e027d748”
device_id = “BOLT14000099”
mybolt = Bolt(api_key, device_id)
while count == 1:
output = mybolt.digitalWrite(‘0’,‘HIGH’)
print(output)

time.sleep(30)

@mabhijit.ec17 You have to import requests when dealing with https. Try the below code and let me know if it works.

import requests
from boltiot import Bolt
import json, time

api_key = “your API key”          #Do NOT mention your API key and BOLT ID while posting querry
device_id = “your BOLT ID”        #Do NOT mention your API key and BOLT ID while posting querry
mybolt = Bolt(api_key, device_id)

selling_price = 864999.52

def get_bitcoin_price():
   URL = "https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=INR" 
   response = requests.request("GET", URL)
   response = json.loads(response.text)
   current_price = response["INR"]
   return current_price

while True:
    print ("Obtaining current price")
    current_price = get_bitcoin_price()
    print ("Current Bitcoin price is:" + str(current_price) +  "INR")
    try:
        if current_price > selling_price :
           print ("Current Bitcoin price has surpassed the set selling price 864999.52 INR")
           mybolt.digitalWrite('0','HIGH')
           time.sleep(5)
           mybolt.digitalWrite('0', 'LOW')
    except Exception as e:
        print ("Error occured: Below are the details")
        print (e)
    time.sleep(10)
1 Like