-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
87 lines (73 loc) · 2.32 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import network
import usocket
from time import sleep
import urequests
from machine import Pin, I2C
import ssd1306
i2c = I2C(-1, scl=Pin(22), sda=Pin(21)) #For ESP32: pin initializing
#i2c = I2C(-1, scl=Pin(5), sda=Pin(4)) #For ESP8266: pin initializing
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
ssid = "WIFISSID"
password = "WIFIPASSWORD"
server_port = 80
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print("Connecting to WiFi...")
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print("WiFi Connected!")
print("IP Address:", wlan.ifconfig()[0])
btc_price = get_btc_price()
OLEDdisplay(btc_price)
def start_server():
server = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
server.bind(('0.0.0.0', server_port))
server.listen(5)
print("Server Started!")
return server
def handle_request(client):
print("NEW Client")
currentline = ""
while True:
try:
data = client.recv(1024)
if data:
request = data.decode('utf-8')
print(request, end='')
if "\r\n\r\n" in request:
client.sendall(b"HTTP/1.1 200 OK\r\n")
client.sendall(b"Content-type:text/html\r\n")
client.sendall(b"\r\n")
btc_price = get_btc_price()
html_code = f"<html><head></head><body>BTC PRICE: {btc_price}</body></html>"
client.sendall(html_code.encode('utf-8'))
break
except OSError:
break
client.close()
print("Client Disconnected")
def OLEDdisplay(price):
oled.text(f"BTC: ", 32, 16)
oled.text(f"{price}", 32, 32)
oled.show()
def get_btc_price():
url = "https://api.nobitex.ir/v2/orderbook/BTCUSDT"
response = urequests.get(url)
data = response.json()
last_trade_price = data["lastTradePrice"]
return last_trade_price
def main():
connect_wifi()
#server = start_server()
while True:
client, addr = server.accept()
if client:
handle_request(client)
sleep(0.1)
if __name__ == '__main__':
main()