Показаны различия между двумя версиями страницы.
Следующая версия | Предыдущая версия | ||
products:laboratory_iot_c:exp71 [2024/12/17 18:54] – создано labuser30 | products:laboratory_iot_c:exp71 [2024/12/17 18:59] (текущий) – [Программный код эксперимента] labuser30 | ||
---|---|---|---|
Строка 13: | Строка 13: | ||
==== Программный код эксперимента ==== | ==== Программный код эксперимента ==== | ||
- | < | + | < |
- | from machine import Pin, SPI | + | #include <SPI.h> |
- | from tft import TFT_GREEN | + | #include < |
- | import font | + | #include " |
- | import network | + | #include < |
- | import gc | + | #include < |
- | import time | + | #include < |
- | import socket | + | #include < |
- | import json | + | |
- | _init() | + | #define PIN_CS |
- | gc.collect() | + | #define PIN_DC |
+ | #define PIN_RST 5 | ||
- | wlan_id = "Your_Wi-Fi" | + | #define WIFI_NAME |
- | wlan_pass = "Your_Wi-Fi_Password" | + | #define WIFI_PASSWORD |
+ | |||
+ | Adafruit_ST7735 tft = Adafruit_ST7735(PIN_CS, | ||
+ | LittleFS_ImageReader reader; | ||
- | dc = Pin(4, Pin.OUT) | + | String httpGet(String url) { |
- | cs = Pin(2, Pin.OUT) | + | |
- | rst = Pin(5, Pin.OUT) | + | |
+ | String data = ""; | ||
- | spi = SPI(1, baudrate=40000000, | + | if (http.begin(client, url)) { |
- | tft = TFT_GREEN(128, 160, spi, dc, cs, rst, rotate=0) | + | Serial.println(" |
- | tft.initr(tft.BGR) # tft.initr(tft.RGB) #Если вместо синего цвета отображается красный, | + | int httpCode |
+ | if (httpCode > 0) { | ||
+ | Serial.printf(" | ||
+ | if (httpCode | ||
+ | | ||
+ | } else { | ||
+ | | ||
+ | } | ||
+ | http.end(); | ||
+ | } else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | } | ||
+ | return data; | ||
+ | } | ||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | Serial.println(); | ||
+ | | ||
+ | LittleFS.begin(); | ||
+ | tft.initR(INITR_BLACKTAB); | ||
+ | tft.setRotation(2); | ||
- | FontColor = tft.rgbcolor(33,149, 82) | + | Serial.print(" |
- | FontColor2 | + | WiFi.mode(WIFI_STA); |
+ | WiFi.begin(WIFI_NAME, WIFI_PASSWORD); | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | Serial.print(" | ||
+ | delay(500); | ||
+ | } | ||
+ | Serial.println(); | ||
+ | Serial.println(" | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP()); | ||
- | wlan = network.WLAN(network.STA_IF) | + | String API_key |
- | wlan.active(True) | + | |
- | wlan.connect(wlan_id, | + | |
- | tft.clear(tft.rgbcolor(255,255, 255)) | + | String json_string = httpGet(" |
+ | JsonDocument json_doc; | ||
+ | deserializeJson(json_doc, json_string); | ||
+ | String w_city = json_doc[" | ||
+ | int w_temp = json_doc[" | ||
+ | int w_temp_feels = json_doc[" | ||
+ | int w_humidity = json_doc[" | ||
+ | float w_wind = json_doc[" | ||
+ | String w_pod = json_doc[" | ||
+ | int w_code = int(json_doc[" | ||
- | while wlan.isconnected() == False: | + | String w_pic; |
- | pass | + | if (w_code <= 233) w_pic = " |
+ | else if ((w_code >= 300 && w_code <= 520) || w_code == 522) w_pic = " | ||
+ | else if (w_code >= 521 && w_code <= 600) w_pic = " | ||
+ | else if (w_code >= 601 && w_code <= 622) w_pic = " | ||
+ | else if (w_code >= 623 && w_code <= 751) w_pic = " | ||
+ | else if (w_code == 800) w_pic = " | ||
+ | else if (w_code >= 801 && w_code <=802) w_pic = " | ||
+ | else if (w_code == 803) w_pic = " | ||
+ | else if (w_code == 804) w_pic = " | ||
- | print(' | + | String w_pic_temp; |
+ | if (w_temp > 0) w_pic_temp = " | ||
+ | else w_pic_temp = " | ||
+ | |||
+ | unsigned int font_color = tft.color565(33, 149, 82); | ||
+ | unsigned int font_color2 = tft.color565(8, | ||
- | apiKey = ' | + | tft.fillScreen(ST77XX_WHITE); |
- | cityName = ' | + | |
+ | tft.setTextColor(font_color); | ||
+ | tft.setTextSize(2); | ||
+ | tft.setCursor(5, | ||
+ | tft.print(w_city); | ||
- | def http_get(url, | + | reader.drawBMP("/weather/" |
- | _, _, host, path = url.split('/', 3) | + | |
- | addr = socket.getaddrinfo(host, port)[0][-1] | + | |
- | + | ||
- | s = socket.socket() | + | |
- | s.connect(addr) | + | |
- | s.send(bytes(' | + | |
- | text = '' | + | tft.setTextSize(4); |
+ | tft.setCursor(30, | ||
+ | tft.print(w_temp); | ||
- | while True: | + | tft.setTextColor(font_color2); |
- | data = s.recv(100) | + | tft.setTextSize(1); |
- | if data: | + | |
- | | + | |
- | | + | tft.setCursor(5, 130); |
- | else: | + | |
- | a = text.split(' | + | tft.setCursor(5, 140); |
- | | + | |
- | break | + | } |
+ | void loop() { | ||
+ | } | ||
+ | </ | ||
- | json_data = http_get(' | + | Не забудь заменить в коде название сети Wi-Fi и ее пароль. |
- | + | ||
- | if not json_data: | + | |
- | raise SystemExit | + | |
- | + | ||
- | data = json.loads(json_data) | + | |
- | + | ||
- | wCity = data[' | + | |
- | wTemp = round(data[' | + | |
- | wTempFeels = data[' | + | |
- | wHumidity = data[' | + | |
- | wWind = data[' | + | |
- | + | ||
- | pod = data[' | + | |
- | + | ||
- | wCode = int(data[' | + | |
- | + | ||
- | if wCode <= 233: | + | |
- | wPic = ' | + | |
- | elif (wCode >= 300 and wCode <= 520) or wCode == 522: | + | |
- | wPic = ' | + | |
- | elif wCode in (521, 600): | + | |
- | wPic = ' | + | |
- | elif wCode >= 601 and wCode <= 622: | + | |
- | wPic = ' | + | |
- | elif wCode >= 623 and wCode <= 751: | + | |
- | wPic = ' | + | |
- | elif wCode == 800: | + | |
- | wPic = ' | + | |
- | elif wCode in (801, 802): | + | |
- | wPic = ' | + | |
- | elif wCode == 803: | + | |
- | wPic = ' | + | |
- | elif wCode == 804: | + | |
- | wPic = ' | + | |
- | + | ||
- | if (wTemp > 0): | + | |
- | wTempPic = ' | + | |
- | else: | + | |
- | wTempPic = ' | + | |
- | + | ||
- | tft.text(1, | + | |
- | + | ||
- | tft.draw_bmp(34, | + | |
- | tft.draw_bmp(5, | + | |
- | + | ||
- | tft.text(30, | + | |
- | + | ||
- | tft.text(5, | + | |
- | tft.text(5, | + | |
- | tft.text(5, | + | |
- | </ | + | |
- | Не забудь заменить в коде название сети Wi-Fi и ее пароль | + | < |
- | < | + | #define WIFI_NAME |
- | wlan_id = "Your_Wi-Fi" | + | #define WIFI_PASSWORD |
- | wlan_pass = "Your_Wi-Fi_Password" | + | |
</ | </ | ||
Не забудь заменить в коде ключ доступа к сервису weatherbit.io, | Не забудь заменить в коде ключ доступа к сервису weatherbit.io, | ||
- | < | + | < |
- | apiKey | + | |
</ | </ | ||