Project 12:Bitcoin Alert System

I have written the following code-

from boltiot import Bolt
api_key = “d231b44f-9474-4879-bc4f-9a95463a42ba”
device_id = “BOLT294186”
mybolt = Bolt(api_key, device_id)

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

selling_price=7400
while True:
#Step 1- Fetch price from API
price_fetched=get_bitcoin_price()
print(“The current price is”,price_fetched)
print(“Your selling Price is”,selling_price)
#Step 2- Comaparing with selling price
if price_fetched > selling_price:
print(“The current price is greater than the selling price”)
buzzer=mybolt.digitalWrite(‘0’,HIGH)
print(buzzer)
#Step 3- Time Delay
time_sleep(30)

I am getting following error-

Traceback (most recent call last):
File “code.py”, line 16, in
price_fetched=get_bitcoin_price()
File “code.py”, line 8, in get_bitcoin_price
response = requests.request(“GET”, URL)
NameError: name ‘requests’ is not defined

Please help me to debug this.
Thank you.

Hii @180020039
I see that in your code you haven’t imported requests, hence the error. Also you have used time.sleep() as time_sleep(30), which is wrong. For this too you would need to import time module. Also import json.

Also please take care not to share your Bolt’s Device Id and API as it can be misused by someone else.

Hope this helps! :smiley:

1 Like

The line for digitalWrite would give an error as HIGH should be written in inverted commas as “HIGH”.

please check the code indentation and do not use tabs for spaces while writing code.
check time.sleep line
check proper updated url

After seeing your code ,you have not imported required libraries like json ,requests ,hence it is giving you the error. Also due to inaccurate indentations after loops is creating a problem.
I am sharing a screenshot of my program you can take a look in that

1 Like