from machine import Pin, I2C
import ssd1306
import urequests
import ujson
import time
# Initialize I2C and OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# API URL
api_url = 'https://api.thingspeak.com/apps/thinghttp/send_request?api_key=ZWA5W86DBHF4EXZJ'
def fetch_data():
try:
response = urequests.get(api_url)
print("Status Code:", response.status_code)
print("Response Text:", response.text) # Print raw response to help diagnose issues
if response.status_code == 200:
data = ujson.loads(response.text)
# Adjust based on actual data structure
return data.get('price', 'No Data') # Replace 'price' with actual key if different
else:
return f"HTTP Error {response.status_code}"
except Exception as e:
print("Error fetching data:", e)
return "Error"
def display_data(data):
oled.fill(0) # Clear the display
oled.text('NVIDIA Price:', 0, 0)
oled.text(data, 0, 20)
oled.show()
while True:
data = fetch_data()
display_data(data)
time.sleep(60) # Update every minute