003.0.96‘OLED+合宙ESP32C3+和风天气预报

这篇具有很好参考价值的文章主要介绍了003.0.96‘OLED+合宙ESP32C3+和风天气预报。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

合宙ESP32-C3+OLED天气预报

一、搭建开发框架

003.0.96‘OLED+合宙ESP32C3+和风天气预报

使用VScode platformio开发

1.oled显示

#include <Arduino.h>
#include <SSD1306Wire.h>

const int SDA_PIN = 4;  //引脚连接,ESP32
const int SCL_PIN = 5;  //
SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN);
void setup() {
  display.init();
  display.clear();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);  
}


void loop() {
  display.drawString(0, 24,"This is a 0.96' oled!");
  display.display();
  delay(1000);
  display.clear();
}

2.配置WIFI

#include <Arduino.h>
#include <SSD1306Wire.h>//thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.3.0
#include <WiFi.h>//自带
#include <HTTPClient.h>//自带

const int SDA_PIN = 4;  //引脚连接,ESP32
const int SCL_PIN = 5;  //
SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN);

const char* ssid     = "***"; //填写你的wifi名字
const char* password = "***"; //填写你的wifi密码

void setup() {
  display.init();
  display.clear();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);  

  display.drawString(0, 24,"Connecting...");
  display.display();

  WiFi.begin(ssid, password); //连接wifi
  delay(1000);
  while (WiFi.status()!=WL_CONNECTED)
  {
    delay(500);
  };
  display.clear();
  display.drawString(0, 24,"Connected!");
  display.display();
  delay(1000);  
}


void loop() {
  display.clear();
  display.drawString(0, 24,"This is a 0.96' oled!");
  display.display();
  delay(1000);
  
}

3.得到B站粉丝数

#include <Arduino.h>
#include <SSD1306Wire.h>//thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.3.0
#include <WiFi.h>//自带
#include <HTTPClient.h>//自带

const int SDA_PIN = 4;  //引脚连接,ESP32
const int SCL_PIN = 5;  //
SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN);

const char* ssid     = "***"; //填写你的wifi名字
const char* password = "***"; //填写你的wifi密码




void getfans(){
  HTTPClient http;
  String url = "https://api.bilibili.com/x/relation/stat?vmid=473938416";
  http.begin(url);
  int httpCode=http.GET();
  if(httpCode>0){
    if(httpCode==HTTP_CODE_OK){
      String payload = http.getString();
      Serial.print(payload);
    }
  }
  else{
    Serial.print("[HTTPS] GET... failed");
  }
}

void setup() {
  Serial.begin(115200);
  display.init();
  display.clear();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);  

  display.drawString(0, 24,"Connecting...");
  display.display();

  WiFi.begin(ssid, password); //连接wifi
  delay(1000);
  while (WiFi.status()!=WL_CONNECTED)
  {
    delay(500);
  };
  display.clear();
  display.drawString(0, 24,"Connected!");
  display.display();
  delay(1000);  
}


void loop() {
  getfans();
  delay(2000);
}

4.使用json解析获得的粉丝数,显示到OLED

#include <Arduino.h>
#include <SSD1306Wire.h>//thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.3.0
#include <WiFi.h>//自带
#include <HTTPClient.h>//自带
#include <ArduinoJson.h>

const int SDA_PIN = 4;  //引脚连接,ESP32
const int SCL_PIN = 5;  //
SSD1306Wire display(0x3c, SDA_PIN, SCL_PIN);

const char* ssid     = "***"; //填写你的wifi名字
const char* password = "***"; //填写你的wifi密码




void getfans(){
  HTTPClient http;
  String url = "https://api.bilibili.com/x/relation/stat?vmid=473938416";
  http.begin(url);
  int httpCode=http.GET();
  if(httpCode>0){
    if(httpCode==HTTP_CODE_OK){
      String payload = http.getString();
      Serial.print(payload);

      DynamicJsonDocument  jsonBuffer(2048);
      deserializeJson(jsonBuffer, payload);
      JsonObject root = jsonBuffer.as<JsonObject>();


      String follower = root["data"]["following"];
      display.clear();
      display.drawString(64, 24,follower);
      display.display();
    }
  }
  else{
    Serial.print("[HTTPS] GET... failed");
  }
}

void setup() {
  Serial.begin(115200);
  display.init();
  display.clear();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);  

  display.drawString(0, 24,"Connecting...");
  display.display();

  WiFi.begin(ssid, password); //连接wifi
  delay(1000);
  while (WiFi.status()!=WL_CONNECTED)
  {
    delay(500);
  };
  display.clear();
  display.drawString(0, 24,"Connected!");
  display.display();
  delay(1000);  
}


void loop() {
  getfans();
  delay(2000);
}

二、基本知识

结构体

  1. 定义结构体
typedef struct WeatherData {
    String temp;        /*温度:'19'*/
    String humidity;    /*湿度:'91'*/
}WeatherData;
  1. 实例化结构体
WeatherData todaymsg;//实例化
  1. WeatherData *data可以理解为 int *data,而*data则是指针,指针就是地址
void getweather(WeatherData *data){
    String tmp = root["now"]["temp"];
    data->temp=tmp;
    String hum = root["now"]["humidity"];
    data->humidity=hum;
  1. &todaymsg取出存放的数据,todaymsg相当于地址(指针),数据就存在这个地址中,通过&进行读取

*和&必须成对出现

void loop() {
  getweather(&todaymsg,HEFENG_KEY,HEFENG_LOCATION);
  display.drawString(64, 24,todaymsg.humidity);
}

三、和风天气

1.访问URL

"https://devapi.qweather.com/v7/weather/now?&gzip=n&location="+ location + "&key=" + key;

应当注意&gzip=n,即返回的json数据为非压缩包的形式,否则无法读取。

2.返回JSON数据

{"code":0,"message":"0","ttl":1,"data":{"mid":473938416,"following":11,"whisper":0,"black":0,"follower":0}}

{"code":"200","updateTime":"2022-10-06T10:22+08:00","fxLink":"http://hfx.link/3ef1","now":{"obsTime":"2022-10-06T10:12+08:00","temp":"12","feelsLike":"8","icon":"104","text":"阴","wind360":"315","windDir":"西北风","windScale":"4","windSpeed":"21","humidity":"85","precip":"0.0","pressure":"1022","vis":"21","cloud":"91","dew":"8"},"refer":{"sources":["QWeather","NMC","ECMWF"],"license":["no commercial use"]}}

3.解析JSON,并存放到结构体

  String payload = http.getString();
  
  DynamicJsonDocument  jsonBuffer(2048);
  deserializeJson(jsonBuffer, payload);
  JsonObject root = jsonBuffer.as<JsonObject>();

  String hum = root["now"]["humidity"];
  data->humidity=hum;

4.和风天气结构体

  • 当前天气
typedef struct WeatherData {
    String obsTime;     /*时间:'2022-10-04T20:46+08:00'*/
    String temp;        /*温度:'19'*/
    String feelsLike;   /*体感温度:'19'*/
    String icon;        /*图标:'104'*/
    String text;        /*天气文本:'阴'*/
    String wind360;     /*风向:'8'*/
    String windDir;     /*风向文本:'北风'*/
    String windScale;   /*风力等级:'1'*/
    String windSpeed;   /*风速:'4'*/
    String humidity;    /*湿度:'91'*/
    String precip;      /*降水概率:'0.0'*/
    String pressure;    /*气压:'976'*/
    String meteoconIcon;
}WeatherData;
  • 预报天气
typedef struct WeatherForcastData {
    String fxDate;          /*日期:'2022-10-04'*/
    String sunrise;         /*日出时间:'06:13'*/
    String sunset;          /*日落时间:'17:53'*/
    String moonrise;        /*月出时间:'15:02'*/
    String moonset;         /*月落时间:'00:20'*/
    String moonPhase;       /*月相:'盈凸月'*/
    String moonPhaseIcon;   /*月相图标:'803'*/
    String tempMax;         /*最高温度:'16'*/
    String tempMin;         /*最低温度:'4'*/
    String iconDay;         /*白天天气图标:'100'*/
    String textDay;         /*白天天气文本:'晴'*/
    String iconNight;       /*夜间天气图标:'150'*/
    String textNight;       /*夜间天气文本:'晴'*/
    String wind360Day;      /*白天风向:'353'*/
    String windDirDay;      /*白天风向文本:'北风'*/
    String windScaleDay;    /*白天风力等级:'3-4'*/
    String windSpeedDay;    /*白天风速:'15'*/
    String wind360Night;    /*夜间风向:'270'*/
    String windDirNight;    /*夜间风向文本:'西风'*/
    String windScaleNight;  /*夜间风力等级:'1-2'*/
    String windSpeedNight;  /*夜间风速:'3'*/
    String humidity;        /*湿度:'27'*/
    String precip;          /*降水概率:'0.0'*/
    String pressure;        /*气压:'1024'*/
}WeatherForcastData;

5.和风天气图标

  • 获取String icon

返回值为字符串型的,如"100" / “101”

  • 根据获取的icon值,返回图标在Meteocons_Plain_36字体中对应的值

如"100",则返回"B",具体各返回值是什么,需要进行尝试

String dec2ascii[]  = {//"("代表40,")"代表41...
   "(",")","*","+",",","-",".","/","0","1","2",
   "3","4","5","6","7","8","9",":",";","<","=",
   ">","?","@","A","B","C","D","E","F","G","H",
   "I","J","K","L","M","N","O","P","Q","R","S",
   "T","U","V","W","X","Y","Z",
};
display.setFont(Meteocons_Plain_36);  
for(int i=40;i<90;i++){
  String s=dec2ascii[i-40];
  display.drawString(0, 0,s);
}
  • 根据返回的Meteocons_Plain_36字体中对应的值,进行显示
display.setFont(Meteocons_Plain_36);  
display.drawString(0, 0,todaymsg.meteoconIcon);

003.0.96‘OLED+合宙ESP32C3+和风天气预报文章来源地址https://www.toymoban.com/news/detail-423583.html

到了这里,关于003.0.96‘OLED+合宙ESP32C3+和风天气预报的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 合宙ESP32-C3精简版完全食用指南

    硬件资源 尺寸长宽 21mm*51mm 1路SPI FLASH,板载4MB,支持最高 16MB (dio 模式) 2路UART接口,UART0~UART1,其中下载口为UART0 (精简版为UART和UART1) 5 路 12 比特 ADC,最高采样率 100KSPS 1路低速SPI接口,支持主模式 1路IIC控制器 4路PWM接口,可使用任意GPIO GPIO外部管脚15路,可复用 2路贴片LED指示

    2023年04月19日
    浏览(27)
  • 【Arduino环境下驱动合宙esp32c3单片机基本外设】

    本教程是参加FastBond2活动主题4 - 测量仪器中的【Arduino环境下驱动合宙esp32c3单片机基本外设】。 围绕FastBond2阶段1——基于ESP32C3开发的简易IO调试设备项目需求开发。 设计目标: 多种数字和模拟信号的输入输出:用户可以选择不同的输入输出模式,并通过设备的操作界面进行

    2024年02月04日
    浏览(40)
  • 006.合宙ESP32-C3+蓝牙调试器通过BLE发送接收数据教程

    在平衡小车制作过程中,需要对KP/KD/KSP/KSI等PID系数进行调试,而平衡小车无法通过USB等进行有线调试,而ESP32-C3自带蓝牙+WIFI,使用WIFI比较吃算力,故选择通过蓝牙进行调参,同时能够将Angle/Encoder/PWM等数据回传至手机端进行查看。 前期通过查找资料,发现合宙ESP32-C3自带蓝

    2024年02月03日
    浏览(42)
  • 新上架的简约版合宙ESP32C3使用arduino开发的教程

    经过两个月的缺货下架后,9块9包邮的合宙ESP32C3又重新上架了,真香。这一批都是没有带串口芯片的简约版(9块9要啥自行车)。在下架前,简约版要使用2.0.0版本的ESP32开发板库才能下载,而2.0.0版本有一些丢失arduino自带库的诡异BUG,所以一直没法用于下载。现在由于发布了2

    2024年02月05日
    浏览(68)
  • 物联网开发笔记(87)- 使用Micropython开发ESP32开发板之烧录合宙ESP32C3开发板

    一、目的         这一节我们学习如何使用我们的ESP32开发板来学习合宙ESP32C3开发板,该开发板有两种:一种是带串口通讯的,一种是通过使用USB通讯接口的。  二、环境         ESP32 + 合宙ESP32C3开发板 + USB转type-C线  + Win10 接线方法:         开发板通过USB线插到

    2024年02月14日
    浏览(32)
  • K_A16_003 基于STM32等单片机采集薄膜压力传感器参数串口与OLED0.96双显示

    单片机型号 测试条件 模块名称 代码功能 STM32F103C8T6 晶振8M/系统时钟72M 薄膜压力传感器模块 STM32F103C8T6驱动薄膜压力传感器模块 串口与OLED0.96双显示 其他资料目录 直戳跳转 厚度 :0.4mm 样式 :薄片状,柔性 触发力 :20g,默认电阻值小于 200kΩ 时触发 压力感应范围 :20g~6kg

    2024年02月05日
    浏览(42)
  • 在macOS 上使用 esptool 烧录合宙ESP32C3 开发板 micropython 固件遇到的问题与解决办法

    使用 esptool 烧录遇到报错 A fatal error occurred: Failed to write to target RAM (result was 01070000) 看github 上的讨论,LilyGO 开发板使用的 FTDI 芯片似乎与 mac OS Big Sur UART 驱动程序不兼容;合宙 ESP32C3 开发板同理; 我电脑的 mac OS Monterey 版本同理; 在 mac 上安装这个CH340 驱动程序; 链接: li

    2024年02月09日
    浏览(42)
  • 物联网开发笔记(89)- 使用Micropython开发ESP32开发板之合宙ESP32 C3开发板通过串口SPI控制st7789 TFT液晶屏1.3寸

    一、目的         这一节我们学习如何使用合宙的ESP32 C3开发板控制1.3寸彩色TFT显示屏模块,分辨率240*240,SPI接口,ST7789驱动芯片。 二、环境         ESP32  C3 + Thonny + 1.3寸 st7789液晶屏模块 + 几根杜邦线 + Win10 接线方法:   三、st7789 TFT显示屏驱动 st7789py.py   四、点亮

    2024年02月11日
    浏览(34)
  • ESP8266获取天气预报信息,并使用CJSON解析天气预报数据

    当前文章介绍如何使用ESP8266和STM32微控制器,搭配OLED显示屏,制作一个能够实时显示天气预报的智能设备。将使用心知天气API来获取天气数据,并使用MQTT协议将数据传递给STM32控制器,最终在OLED显示屏上显示。 心知天气是一家专业的气象数据服务提供商,致力于为全球用户

    2024年02月10日
    浏览(38)
  • ESP32 入门笔记04: 0.96寸OLED 显示屏 + u8g2库丝滑显示UI (ESP32 for Arduino IDE)

    先导知识 ESP32 入门笔记01:开发板信息、开发环境搭建以及学资料准备 ESP32 入门笔记02: GPIO参考指南 在本例中,我们使用 I2C 通信协议。ESP32 中最适合 I2C 通信的引脚是通用输入输出接口 22(SCL) 和通用输入输出接口 21(SDA) 。 安装 SSD1306 OLED 库 – ESP32 (也可以用u8g2库,在Ardu

    2024年01月19日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包