from machine import Pin, SPI from tft import TFT_GREEN import font import network import gc import time import socket import json _init() gc.collect() wlan_id = "Your_Wi-Fi" wlan_pass = "Your_Wi-Fi_Password" dc = Pin(4, Pin.OUT) cs = Pin(2, Pin.OUT) rst = Pin(5, Pin.OUT) spi = SPI(1, baudrate=40000000, polarity=0, phase=0) tft = TFT_GREEN(128, 160, spi, dc, cs, rst, rotate=0) tft.initr(tft.BGR) # tft.initr(tft.RGB) #Если вместо синего цвета отображается красный, а вместо красного синий FontColor = tft.rgbcolor(33,149, 82) FontColor2 = tft.rgbcolor(8,85, 41) wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(wlan_id, wlan_pass) tft.clear(tft.rgbcolor(255,255, 255)) while wlan.isconnected() == False: pass print('Device IP:', wlan.ifconfig()[0]) apiKey = 'Your_Key' cityName = 'Moscow,RU' def http_get(url, port=80): _, _, host, path = url.split('/', 3) addr = socket.getaddrinfo(host, port)[0][-1] s = socket.socket() s.connect(addr) s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8')) text = '' while True: data = s.recv(100) if data: text += str(data, 'utf8') pass else: a = text.split('\r\n\r\n', 2)[1] return a break json_data = http_get('http://api.weatherbit.io/v2.0/current?city=' + cityName + '&key=' + apiKey) if not json_data: raise SystemExit data = json.loads(json_data) wCity = data['data'][0]['city_name'] wTemp = round(data['data'][0]['temp']) wTempFeels = data['data'][0]['app_temp'] wHumidity = data['data'][0]['rh'] wWind = data['data'][0]['wind_spd'] pod = data['data'][0]['pod'] wCode = int(data['data'][0]['weather']['code']) if wCode <= 233: wPic = '11' elif (wCode >= 300 and wCode <= 520) or wCode == 522: wPic = '09' elif wCode in (521, 600): wPic = '10' elif wCode >= 601 and wCode <= 622: wPic = '13' elif wCode >= 623 and wCode <= 751: wPic = '50' elif wCode == 800: wPic = '01' elif wCode in (801, 802): wPic = '02' elif wCode == 803: wPic = '03' elif wCode == 804: wPic = '04' if (wTemp > 0): wTempPic = 'tp' else: wTempPic = 'tn' tft.text(1,1,wCity, font.terminalfont, FontColor, 2) tft.draw_bmp(34,16,'/weather/' + wPic + pod + '.bmp') tft.draw_bmp(5,70,'/weather/' + wTempPic + '.bmp') tft.text(30,75, str(wTemp), font.terminalfont, FontColor, 4) tft.text(5,120,"Feels: " + str(wTempFeels), font.terminalfont, FontColor2, 1) tft.text(5,128,"Humidity: " + str(wHumidity) + "%", font.terminalfont, FontColor2, 1) tft.text(5,136,"Wind: " + str(wWind) + "m/s", font.terminalfont, FontColor2, 1)