Hardware/ESP32

LILYGO T5 - ESP32 MicroPython - REST API CALL

로아_ 2023. 10. 10. 11:24
728x90

이전 포스팅에서 LILYGO T5 - ESP32 MicroPython을 설치 후 샘플을 구동해 보았다.

 

이제 MicroPython으로 REST API를 호출해보자

 

#테스트용 rest api 발급

https://api.openweathermap.org

 

Weather API - OpenWeatherMap

Please, sign up to use our fast and easy-to-work weather APIs. As a start to use OpenWeather products, we recommend our One Call API 3.0. For more functionality, please consider our products, which are included in professional collections.

openweathermap.org

 

#REST API 호출 코드

LILYGO MicroPytho에는 urequests라이브러리가 없으므로 설치

import network
import urequests

def connect_to_wifi(wlan, ssid, password):
    if not wlan.isconnected():
        print("Connecting to network...")
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass

connect_to_wifi(wlan, "WIFI-SSID", "WIFI-PASS")

#urequests 라이브러리 패키지 설치
import upip
upip.install('urequests')

#REST API 호출 후 JSON 화면에 출력

import network
import urequests
import epd
from time import sleep

def connect_to_wifi(wlan, ssid, password):
    if not wlan.isconnected():
        print("Connecting to network...")
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
connect_to_wifi(wlan, "WIFI-SSID", "WIFI-PASS")

response = urequests.get('https://api.openweathermap.org/data/2.5/weather?q=Minneapolis&units=imperial&appid=APP_ID')



print(response.text)
weather_data = response.json()

epd47 = epd.EPD47()

epd47.power(True)
epd47.clear()

epd47.text(response.text, 100, 100, font_size=9)

REST API로 Response JSON 결과 값이 화면에 출력이 되는것을 확인할 수 있다.

728x90