• 基于VScode的platformio软件实现ESP32的WIFI模块获取实时天气数据并利用u8g2库通过i2c协议进行OLED显示


    步骤一:硬/软件准备

    硬件部分:

    1.ESP-32开发板        1块

    2.SSD1306的0.96寸OLED屏幕(4,7孔均可)   1块

    3.杜邦线  若干

    软件部分:

    1.Visual Studio Code  Platformio

    2.任意串口调试助手

    步骤二:ESP-32 WiFi模块代码编写

    方法很简单,直接调用Arduino库函数文件即可

    1. #include
    2. //预设WiFi热点账号与密码
    3. const char* ssid = "预设WiFi账号";
    4. const char* password = "预设WiFi密码";
    5. //预设接口地址 城市 API接口密钥
    6. String url = "(不唯一)具体参考聚合数据网页版的天气预报模块";
    7. String city = "(城市名称)";
    8. String key = "个人API接口密钥";
    9. void setup() {
    10. //串口初始化
    11. Serial.begin(9600);
    12. // 连接 WiFi
    13. WiFi.begin(ssid, password);
    14. Serial.print("正在连接 Wi-Fi");
    15. // 检测是否连接成功
    16. while (WiFi.status() != WL_CONNECTED) {
    17. delay(500);
    18. Serial.print(".");
    19. }
    20. Serial.println("连接成功");
    21. Serial.print("IP 地址:");
    22. Serial.println(WiFi.localIP());
    23. }

    具体API接口获取,下面给出一种方式,可根据个人需求修改

    直接进入网站寻找接口地址 即可 这里是 http://apis.juhe.cn/simpleWeather/query

    密钥获取:直接查看个人API的key即可;

    这样我们运行程序,正确连接WiFi,通过串口打印结果即可(波特率预设9600)

    正确结果为:

    正在连接 Wi-Fi.连接成功
    IP 地址:192.168.118.229

    步骤三:对得到的JSON格式的天气数据进行提取

    利用API接口提取的数据为json格式并不能直接进行使用,需要进行提取

    我们需要另外两个库函数 

    #include
    #include  

    其中 在plantfromio的home里面的library里面进行搜索导入即可

    (如果导入比较慢,则需要上魔法)

    接着进行数据读取和解析提取 温度(temp) 天气(info) 空气指数(aqi)

    1. // 创建 HTTPClient 对象
    2. HTTPClient http;
    3. // 发送GET请求
    4. http.begin(url+"?city="+city+"&key="+key);
    5. int httpCode = http.GET();
    6. // 获取响应状态码
    7. Serial.printf("HTTP 状态码: %d\n", httpCode);
    8. // 获取响应正文
    9. String response = http.getString();
    10. //Serial.println("响应数据");
    11. //Serial.println(response);
    12. http.end();
    13. // 创建 DynamicJsonDocument 对象
    14. DynamicJsonDocument doc(1024);
    15. // 解析 JSON 数据
    16. deserializeJson(doc, response);
    17. // 从解析后的 JSON 文档中获取值
    18. unsigned int temp = doc["result"]["realtime"]["temperature"].as<unsigned int>();
    19. String info = doc["result"]["realtime"]["info"].as();
    20. int aqi = doc["result"]["realtime"]["aqi"].as<int>();
    21. Serial.println("徐州");
    22. Serial.printf("温度: %d\n", temp);
    23. Serial.printf("天气: %s\n", info);
    24. Serial.printf("空气指数: %d\n", aqi);
    这样就得到了相关天气数据

    HTTP 状态码: 200
    [  1266][E][WiFiClient.cpp:517] flush(): fail on fd 48, errno: 11, "No more processes"
    徐州
    温度: 30
    天气: 晴
    空气指数: 109

    步骤四:ESP-32 基于u8g2库的OLED驱动代码编写

    获取了相关天气数据后我们最后进行OLED驱动设置即可

    u8g2库需要我们在plantfromio的home里面的library里面进行搜索;

    之后进行下载,导入工程文件即可

    下面给出示例代码(仅用作参考,后面会进行部分修正)

    基于i2c协议 

    Vcc   3.3v

    Gnd   vee 

    D0    OLED_SCK 

    D1    OLED_SCA

    1. #include
    2. #include
    3. #define WIDTH 128
    4. #define HEIGHT 64
    5. #define OLED_SCA 13
    6. #define OLED_SCK 18
    7. #define OLED_REST 15
    8. //基于u8g2库i2c协议构造结构体u8g2
    9. U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0,OLED_REST,OLED_SCK,OLED_SCA);
    10. void setup() {
    11. // Serial.begin(115200);
    12. // u8g2.setBusClock(800000);
    13. //u8g2(OLED)初始化
    14. u8g2.begin();
    15. //u8g2库字体使能
    16. u8g2.enableUTF8Print();
    17. }
    18. void loop() {
    19. // static unsigned int start = millis();
    20. //清除显示
    21. u8g2.clearBuffer();
    22. //设置英文字符字体
    23. u8g2.setFont(u8g2_font_ncenB12_tf);
    24. //设置光标位置并绘制字体
    25. u8g2.drawStr(0,13,"Hello World!");
    26. //切换中文GB字体
    27. u8g2.setFont(u8g2_font_wqy14_t_gb2312);
    28. //设置光标位置
    29. u8g2.setCursor(0,30);
    30. //绘制中文
    31. u8g2.print("你好");
    32. 设置光标位置并绘制中文
    33. u8g2.drawUTF8(0,50,"嘿嘿");
    34. //选择字体大小并绘制图案
    35. u8g2.setFont(u8g2_font_open_iconic_weather_4x_t);
    36. u8g2.drawGlyph(90,60,0x0045);
    37. //显示绘制内容
    38. u8g2.sendBuffer();
    39. // static unsigned int end = millis();
    40. // Serial.println(end-start);
    41. }

    u8g2库是一种很强的库,基本涵盖了OLED所有的开与显示可能。

    步骤五:整合上述板块,并根据实际进行优化调试

    最后,把得到的数据利用u8g2.print();进行显示即可,下面是完整整合代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #define WIDTH 128
    7. #define HEIGHT 64
    8. #define OLED_SCA 13
    9. #define OLED_SCK 18
    10. #define OLED_REST 15
    11. //基于u8g2库i2c协议构造结构体u8g2
    12. U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0,OLED_REST,OLED_SCK,OLED_SCA);
    13. //WIFI账号密码设置
    14. const char* ssid = "预设WiFi账号";
    15. const char* password = "预设WiFi密码";
    16. //预设接口地址 城市 API接口密钥
    17. String url = "(不唯一)具体参考聚合数据网页版的天气预报模块";
    18. String city = "(城市名称)";
    19. String key = "个人API接口密钥";
    20. unsigned int temp;
    21. String info;
    22. int aqi;
    23. void setup() {
    24. //u8g2(OLED)初始化
    25. u8g2.begin();
    26. //u8g2库字体使能
    27. u8g2.enableUTF8Print();
    28. //初始化串口
    29. Serial.begin(9600);
    30. // 连接 WiFi
    31. WiFi.begin(ssid, password);
    32. Serial.print("正在连接 Wi-Fi");
    33. // 检测是否连接成功
    34. while (WiFi.status() != WL_CONNECTED) {
    35. delay(500);
    36. Serial.print(".");
    37. }
    38. Serial.println("连接成功");
    39. Serial.print("IP 地址:");
    40. Serial.println(WiFi.localIP());
    41. // 创建 HTTPClient 对象
    42. HTTPClient http;
    43. // 发送GET请求
    44. http.begin(url+"?city="+city+"&key="+key);
    45. int httpCode = http.GET();
    46. // 获取响应状态码
    47. Serial.printf("HTTP 状态码: %d\n", httpCode);
    48. // 获取响应正文
    49. String response = http.getString();
    50. //Serial.println("响应数据");
    51. //Serial.println(response);
    52. http.end();
    53. // 创建 DynamicJsonDocument 对象
    54. DynamicJsonDocument doc(1024);
    55. // 解析 JSON 数据
    56. deserializeJson(doc, response);
    57. // 从解析后的 JSON 文档中获取值
    58. temp = doc["result"]["realtime"]["temperature"].as<unsigned int>();
    59. info = doc["result"]["realtime"]["info"].as();
    60. aqi = doc["result"]["realtime"]["aqi"].as<int>();
    61. Serial.println("徐州");
    62. Serial.printf("温度: %d\n", temp);
    63. Serial.printf("天气: %s\n", info);
    64. Serial.printf("空气指数: %d\n", aqi);
    65. }
    66. void loop() {
    67. //等待数据延时
    68. delay(3000);
    69. //清除显示
    70. u8g2.clearBuffer();
    71. //切换中文GB字体
    72. u8g2.setFont(u8g2_font_wqy14_t_gb2312);
    73. u8g2.setFontDirection(0);
    74. //设置光标位置并显示数据
    75. u8g2.setCursor(0,15);
    76. u8g2.print("徐州 ");
    77. u8g2.print("温度:");
    78. u8g2.print(temp);
    79. u8g2.print("℃");
    80. u8g2.setCursor(0,35);
    81. u8g2.print("天气:");
    82. u8g2.print(info);
    83. u8g2.setCursor(0,55);
    84. u8g2.print("空气指数:");
    85. u8g2.print(aqi);
    86. //选择字体大小并绘制图案
    87. u8g2.setFont(u8g2_font_open_iconic_weather_4x_t);
    88. u8g2.drawGlyph(90,60,0x0045);
    89. //显示绘制内容
    90. u8g2.sendBuffer();
    91. }

    最后,显示结果为;

    步骤六:特别鸣谢

    1.极客侠Geekman

    2.b站up小鱼创意   

  • 相关阅读:
    将文件移动到ubuntu/linux/centos虚拟机上
    【python】待君有余暇,看春赏樱花,这不得来一场浪漫的樱花旅~
    python LeetCode 刷题记录 94
    sgx支持数据库环境配置,编译,debug
    基于云存储技术的仓储管理系统
    持续集成/持续部署(3)Jenkins(2)
    珈创生物上市再次失败:先后折戟科创板、创业板,郑从义为董事长
    中国电影票房排行数据爬取及分析可视化
    数据挖掘学习——KNN(k-近邻)
    【代码随想录day23】不同路径
  • 原文地址:https://blog.csdn.net/qq_61576269/article/details/132640352