019 - ESP32 MicroPython: OpenWeather | RESTful APIs
Introduction
In the previous tutorial, I demonstrate using the RESTful APIs to communicate with Thingspeak server sending DHT sensor data. Now in this aticle, I will demonstrate the opposite which is requesting data from a server. This time, communicating with another server which is the OpenWeather. The receive data will be displayed on the OLED display.
We will use urequests MicroPython library to simplify our code for every HTTP request.
Bill Of Materials
- ESP32 development board or any microcontroller with MicroPython firmware.
- 0.96 OLED display.
Hardware Instruction
- Connect the OLED VCC pin to ESP32 3.3V pin.
- Connect the OLED GND pin to ESP32 GND pin.
- Connect the OLED SCL pin to ESP32 pin 22.
- Connect the OLED SDA pin to ESP32 pin 21.
- Save ssd1306.py to ESP32 MicroPython root directory. Refer to tutorial #10 for more details.
- Copy and paste the source code provided below and save to your computer. Or you might want to save it to ESP32 MicroPython root directory with a name of main.py in order to run it automatically after reset.
- Enjoy.
Video Demonstration
Call To Action
For any concern, write your message in the comment section.
You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.
Thank you and have a good days ahead.
See you,
– George Bantique | tech.to.tinker@gmail.com
Source Code
1# **************************************#
2# Author: George Bantique #
3# TechToTinker Youtube Channel #
4# TechToTinker.blogspot.com #
5# tech.to.tinker@gmail.com #
6# Date: Nov.20, 2020 #
7# Please feel free to modify the code #
8# according to your needs. #
9# **************************************#
10
11# **************************************
12# Load necessary libraries
13import machine
14import ssd1306
15import network
16import urequests
17import ujson as json
18import time
19
20# **************************************
21# Create objects:
22led = machine.Pin(2,machine.Pin.OUT)
23
24scl = machine.Pin(22, machine.Pin.OUT, machine.Pin.PULL_UP)
25sda = machine.Pin(21, machine.Pin.OUT, machine.Pin.PULL_UP)
26i2c = machine.I2C(scl=scl, sda=sda, freq=400000)
27oled = ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
28def print_text(msg, x, y, clr):
29 if clr:
30 oled.fill(0)
31 oled.text(msg, x, y)
32 oled.show()
33
34# **************************************
35# Configure the ESP32 wifi as STAtion
36sta = network.WLAN(network.STA_IF)
37if not sta.isconnected():
38 print('connecting to network...')
39 sta.active(True)
40 #sta.connect('your wifi ssid', 'your wifi password')
41 sta.connect('your wifi ssid', 'your wifi password')
42 while not sta.isconnected():
43 pass
44print('network config:', sta.ifconfig())
45
46# **************************************
47# Global variables and constants:
48#api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
49#https://api.openweathermap.org/data/2.5/weather?q=manila&appid=3c2034495c602ddfa627774642a672b0
50
51BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
52API_KEY = "your api key"
53CITY_NAME = "your city here"
54URL = BASE_URL + "q=" + CITY_NAME + "&appid=" + API_KEY
55
56UPDATE_INTERVAL_ms = 5000 # in ms time unit
57last_update = time.ticks_ms()
58
59# **************************************
60# Main loop:
61while True:
62 if time.ticks_ms() - last_update >= UPDATE_INTERVAL_m
63 # send a weather data request
64 response = urequests.get(URL)
65
66 # check status code of the request
67 if response.status_code == 200:
68 # get the json format of data
69 data = response.json()
70
71 # get the main dict block
72 # it contains the weather data
73 main = data['main']
74
75 # get the temperature and subtract
76 # 273.15 to get temp in Celcius
77 # originally temp is in Kelvin
78 temperature = main['temp'] - 273.15
79
80 # get the humidity in %
81 humidity = main['humidity']
82
83 # get the pressure in hPA
84 pressure = main['pressure']
85
86 # weather report
87 report = data['weather']
88
89 ## OLED display
90 print_text('**OpenWeather**', 3, 5, 1)
91 print_text('City:{}' .format(CITY_NAME), 3, 15, 0)
92 print_text('Temp:{} oC' .format(temperature), 3, 25, 0)
93 print_text('Humi:{} %' .format(humidity), 3, 35, 0)
94 print_text('Pres:{} hPa' .format(pressure), 3, 45, 0)
95 print_text('"{}."' .format(report[0]['description']), 3, 55, 0)
96
97 ## debug messages
98 print('')
99 print('**OpenWeather**')
100 print('City:{}' .format(CITY_NAME))
101 print('Temperature:{} oC' .format(temperature))
102 print('Humidity:{} %' .format(humidity))
103 print('Pressure:{} hPa' .format(pressure))
104 print('Weather Report:{}.' .format(report[0]['description']))
105 else:
106 # show error message
107 print_text('Error in HTTP request.', 3, 20, 1)
108 print('Error in HTTP request.')
109
110 led.value(not led.value())
111 last_update = time.ticks_ms()
Posts in this series
- 026 - ESP32 MicroPython: MFRC522 RFID Module
- 025 - ESP32 MicroPython: ESP32 Bluetooth Low Energy
- 024 - ESP32 MicroPython: How to Use SD Card in MicroPython
- 023 - ESP32 MicroPython: Binary Clock
- 022 - ESP32 MicroPython: MQTT Part 2: Subscribe
- 021 - ESP32 MicroPython: MQTT Part 1: Publish
- 020 - ESP32 MicroPython: RESTful APIs | Demo READ and WRITE
- 018 - ESP32 MicroPython: Thingspeak | RESTful APIs
- 017 - ESP32 MicroPython: DHT Values Auto Updates using AJAX
- 016 - ESP32 MicroPython: Web Server | ESP32 Access Point
- 015 - ESP32 MicroPython: Web Server | ESP32 Station Mode in MicroPython
- 014 - ESP32 MicroPython: SIM800L GSM Module in MicroPython
- 013 - ESP32 MicroPython: UART Serial in MicroPython
- 012 - ESP32 MicroPython: HC-SR04 Ultrasonic Sensor in MicroPython
- 011 - ESP32 MicroPython: DHT11, DHT22 in MicroPython
- 010 - ESP32 MicroPython: 0.96 OLED in MicroPython
- 009 - ESP32 MicroPython: Non-blocking Delays and Multithreading | Multitasking
- 008 - ESP32 MicroPython: Hardware Timer Interrupts
- 007 - ESP32 MicroPython: How to make some sound with MicroPython
- 006 - ESP32 MicroPython: How to control servo motor with MicroPython
- 005 - ESP32 MicroPython: Pulse Width Modulation
- 004 - ESP32 MicroPython: External Interrupts
- 003 - ESP32 MicroPython: General Purpose Input Output | GPIO Pins
- 001 - ESP32 MicroPython: What is MicroPython
- 000 - ESP32 MicroPython: How to Get Started with MicroPython
No comments yet!