连接图
提示:SCL连线PIN21,SDA连线22
文章简介
提示:这里可以添加技术名词解释
esp32通过HTTPclient,获取腾讯天气的json格式数据,然后提取相应的数据到oled屏幕上。
代码细节
提示:这里可以添加技术细节
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#define SCREEN_WIDTH 128 // OLED宽度(像素)
#define SCREEN_HEIGHT 64 // OLED高度(像素)
#define OLED_ADDR 0x3C // I2C地址(0x3C或0x3D)
int degree = 0;
int humidity = 0;
Adafruit_SSD1306 display(SCREEN_WIDTH,SCREEN_HEIGHT,&Wire,OLED_RESET); //Wire库负责处理I2C通信,定义了OLED显示屏的重置引脚。
//虽然在这个例子中没有使用重置功能,但Adafruit_SSD1306库需要这个参数。
String ssid = "xinxin";
String pswd = "xinxin1102";
void Deserialize(String response);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print("连接wifi");
Serial.println(ssid);
WiFi.begin(ssid,pswd);
display.begin(SSD1306_SWITCHCAPVCC,OLED_ADDR); // 0x3C内存位置
while (WiFi.status()!= WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
if(WiFi.status()== WL_CONNECTED)
{
HTTPClient http;
http.begin("https://wis.qq.com/weather/common?source=pc&weather_type=observe&province=河南&city=新乡&county=牧野区");
int httpCode = http.GET();
if(httpCode>0)
{
String response = http.getString();
Serial.printf("HTTP响应码:%d\n",httpCode);
Serial.print("HTTP响应内容:");
Serial.println(response);
Deserialize(response);
display.setTextColor(WHITE); //开像素点发光
display.clearDisplay(); //清屏
display.setTextSize(2); //字体大小
display.setCursor(15,15); //显示位置
display.println("temp");
display.setCursor(85,15);
display.println(degree);
display.setCursor(15,45); //显示位置
display.println("humi");
display.setCursor(85,45);
display.println(humidity);
display.display(); // 开显示
}
http.end();
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void Deserialize(String response)
{
//{"data":{"observe":{"degree":"6","humidity":"29","precipitation":"0","pressure":"1006","update_time":"202502181100","weather":"晴","weather_bg_pag":"
//","weather_code":"00","weather_color":null,"weather_first":"","weather_pag":"","weather_short":"晴","weather_url":"","wind_direction":"0","wind_direction_name":"微风","wind_power":"4-5"}},"message":"OK","status":200}
DynamicJsonDocument json_msg(1024);
DynamicJsonDocument json_item(1024);
DynamicJsonDocument json_value(1024);
deserializeJson(json_msg,response);
String data = json_msg["data"];
deserializeJson(json_item,data);
String observe = json_item["observe"];
deserializeJson(json_value,observe);
degree = json_value["degree"];
humidity = json_value["humidity"];
String weather_short = json_value["weather_short"];
Serial.printf("温度:%d\n",degree);
Serial.printf("湿度:%d\n",humidity);
Serial.print(weather_short);
}
运行结果
提示:这里可以添加总结
--- Terminal on COM9 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
ets Jul 29 2019 12:21:46
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13232
load:0x40080400,len:3028
entry 0x400805e4
entry 0x400805e4
连接wifixinxin
.HTTP响应码:200
HTTP响应内容:{"data":{"observe":{"degree":"1","humidity":"70","precipitation":"0","pressure":"1028","update_time":"202502220925","weather":"霾","weather_bg_pag":"","weather_code":"53","weather_color":null,"weather_first":"","weather_pag":"","weather_short":"霾","weather_url":"""霾","weather_bg_pag":"","weather_code":"53","weather_color":null,"weather_first":"","weather_pag":"","weather_short":"霾","weather_url":"""霾","weather_bg_pag":"","weather_code":"53","weather_color":null,"weather_first":"","weather_pag":"","weather_short":"霾","weather_url":"","wind_direction":"1","wind_direction_name":"东北风","wind_power":"5-6"}},"message":"OK","status":200}
"霾","weather_bg_pag":"","weather_code":"53","weather_color":null,"weather_first":"","weather_pag":"","weather_short":"霾","weather_url":"""霾","weather_bg_pag":"","weather_code":"53","weather_color":null,"weather_first":"","weather_pag":"","weather_short":"霾","weather_url":"","wind_direction":"1","wind_direction_name":"东北风","wind_power":"5-6"}},"message":"OK","status":200}
温度:1
湿度:70
霾
例如:
提供先进的推理,复杂的指令,更多的创造力。
视频连接
【PlatformIO】基于Arduino的ESP32 获取腾讯天气信息显示到oled屏幕