找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4669|回复: 8
收起左侧

基于arduino的ESP8266天气预报设计资料

  [复制链接]
ID:368227 发表于 2021-5-28 19:10 | 显示全部楼层 |阅读模式
基于arduino的ESP8266天气预报设计
附录:代码、天气相关图片、字样等素材。

单片机源程序如下:
  1. #include<U8g2lib.h>
  2. #include<ESP8266WiFi.h>
  3. #include<avr/pgmspace.h>
  4. #include<SPI.h>
  5. #include<SD.h>
  6. #include<ArduinoJson.h>

  7. /*按键模拟输入的值,按键的误差范围在之后会设置为正负30*/
  8. #define exit_bt 930
  9. #define left_bt 648
  10. #define right_bt  393
  11. #define enter_bt  144
  12. #define null_bt 22      //不按按键时的值(由于会受外界干扰不为0)


  13. #define City_Code_MAX 35              //城市数量(0~35)
  14. #define MAX_CONTENT_SIZE  1000        //接受http响应内容的最大字节数
  15. #define HTTP_TIMEOUT  5000            //最大响应时间

  16. /*几个要用到的类的对象定义*/
  17. File myFile;                          //读取sd卡数据
  18. U8G2_SSD1306_128X64_NONAME_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 5);      //驱动屏幕
  19. WiFiClient client;                    //客户端联接服务器

  20. /*全局变量*/
  21. unsigned char selected[200];          //用于存放取反(黑白倒置)后的图像,做成被选择的效果
  22. unsigned char weather_lb[64];         //存放从sd卡中读取的天气字样
  23. unsigned char weather_pb[512];        //存放从sd卡中读取的天气图像
  24. char response[MAX_CONTENT_SIZE];      //存放http响应的内容
  25. char endOfHeaders[] = "\r\n\r\n";     //http响应头部结束的标志

  26. bool has_net = false;           //判断是否联上网络的标志
  27. bool sd_initial = false;            //判断初始化时是否成功读取sd卡保存的上一次设置的标志
  28. short int s = 0;                      //主界面中选择的状态
  29. short int City_Code = 35;               //当前设置城市对应的编码(开始时默认为佛山)
  30. short int weather_Code = 99;            //http响应内容中天气对应的编码(开始默认为未知)
  31. short int temper;                       //http响应内容中的温度

  32. String City_ID = "WS06YNEMPP18";        //心知天气中城市对应的id,用于url合成
  33. String ssid = "";                       //配网时保存账号
  34. String password = "";                   //配网时保存密码

  35. const char* host = "api.seniverse点com";      //服务器网址
  36. const char* key = "smtq3n0ixdggurox";        //心知天气api的密匙
  37. const char* language = "zh-Hans";            //设置响应的语言-简体中文

  38. //此结构在解析json数据时存放json中的"code",“温度”
  39. struct WeatherData {
  40.   char code[8];         //天气对应的编码
  41.   char temperature[8];  //温度
  42. };


  43. /*函数*/
  44. void Get_initial_data();         //读取上一次设置的城市
  45. void main_Interface();          //主界面
  46. void main_interface_draw();     //主界面绘图函数

  47. void smart_Config();            //主界面中的“网络”,配网连接wifi
  48. void config_draw(short int);    //“网络”绘图函数

  49. void city_Set();                              //主界面中的“工具”,设置城市
  50. void city_set_draw(short int, bool = false);  //“工具”绘图函数
  51. void draw_city_character(short int, short int, short int);  //绘画城市字样函数,三个形参分别是基准点x,y坐标及城市字样对应的数字
  52. void city_set_store();                                      //将设置城市保存到sd卡中

  53. void weather_Report();                                //主界面中的“天气”,发送请求接收响应内容并显示天气
  54. bool SendRequest();                                   //发送请求
  55. bool skipResponseHeaders();                           //跳过响应头部
  56. void readReponseContent(char* );                      //将响应的数据保存到content数组中
  57. bool AnalyseData(char* , struct WeatherData*);        //解析content中的josn数据
  58. void printData(const struct WeatherData* );           //打印解析后的数据(主要用来debug)
  59. void exchange(const struct WeatherData* );            //将解析后存在struct中的code和温度从字符串转换为整数
  60. void weather_Readpic();                               //读取sd卡中对应的天气图片及字样
  61. void weather_report_draw();                           //绘制天气显示
  62. void request_fail();                                  //异常退出函数

  63. /*城市编码和城市ID的键值对数组*/
  64. //sizeof:36X12B
  65. static const char *ID[] = {
  66. "WX4FBXXFKE4F", "WTW3SJ5ZBJUY", "WS0E9D8WN298", "WS10730EM8EV", "WECNV8ZNE40W", "WEBTFUSKS7HB", "WSQQMXBCC2VS", "WWGQDCW6TBW1", "WM7B0X53DZW2",
  67. "YB1UX38K6DY1", "WZC1EXZ0P9HU", "WXRVB9QYXKY8", "TZY33C4YJBP3", "WRR2Q2Z7CXWM", "WQGDF7NXZRZB", "WWC2MYYCM6J5", "WW8P3NH2TPDT", "WQ82K43YVYU8",
  68. "WWE0TGW4PX6N", "WQ3V4QR6VR6G", "WW0V9QP93VS8", "WQJ6YY8MHZP0", "WTSQQYHVQ973", "WTEMH46Z5N09", "WTMKQ069CCJ7", "WT3Q0FW9ZJ3Q", "WM6N2PM3WY2K",
  69. "WJ2B9TWGFBT5", "WT47HJP3HEMP", "WT029G15ETRJ", "WKEZD7MXE04F", "WSSU6EXX52RE", "WK3N92NQV6RQ", "WKJ1F428HH2F", "W7W3YQKE4QDH", "WS06YNEMPP18"
  70. };


  71. /*图像(一般以pb开头)、文字(一般以lb开头)数组*/

  72. //开机时的图像: 88X50px length:550  sizeof:550B
  73. const unsigned char pb_start[] U8X8_PROGMEM= {
  74. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  75. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x03,0x00,
  76. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x04,0x00,0x00,0x00,0xC0,0x07,0x7F,
  77. 0xF0,0x8F,0x81,0x21,0x0B,0x00,0x00,0x00,0xE0,0x0F,0xFF,0xF0,0x8F,0x83,0xA1,0x0A,
  78. 0x00,0x00,0x00,0x70,0x1C,0xC3,0x31,0x80,0x83,0xA1,0x06,0x60,0x00,0x00,0x30,0x18,
  79. 0x83,0x31,0x80,0x87,0x21,0x03,0xF0,0x00,0x00,0x30,0x18,0x83,0x31,0x80,0x8F,0x41,
  80. 0x08,0xC2,0x01,0x00,0x30,0x18,0x83,0x31,0x80,0x8D,0x81,0x87,0x87,0x01,0x00,0x30,
  81. 0x18,0x83,0xF1,0x8F,0x99,0x01,0xC0,0x83,0x03,0x00,0x30,0x18,0xC3,0xF0,0x8F,0x99,
  82. 0x01,0xC0,0x87,0x03,0x00,0x30,0x18,0xFF,0x30,0x80,0xB1,0x01,0x80,0x8C,0x03,0x00,
  83. 0x30,0x18,0x7F,0x30,0x80,0xF1,0x01,0x00,0x98,0x03,0x00,0x30,0x18,0x03,0x30,0x80,
  84. 0xE1,0x01,0x20,0xF0,0x03,0x00,0x30,0x18,0x03,0x30,0x80,0xC1,0x01,0x60,0xE0,0x01,
  85. 0x00,0x30,0x18,0x03,0x30,0x80,0xC1,0x01,0xC0,0xF0,0x00,0x00,0x70,0x1C,0x03,0xF0,
  86. 0x8F,0x81,0x01,0xF0,0xFF,0x01,0x00,0xE0,0x0F,0x03,0xF0,0x8F,0x81,0x01,0x70,0x3F,
  87. 0x03,0x00,0xC0,0x07,0x03,0x00,0x00,0x00,0x00,0x70,0x00,0x06,0x00,0x00,0x00,0x00,
  88. 0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  89. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  90. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  91. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  92. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0xFB,0xF9,0xE3,0xE7,0xC7,0xC3,
  93. 0xE3,0xE3,0xC7,0x0F,0xFC,0xFB,0xF9,0xE7,0xEF,0xE7,0xE7,0xF7,0xE7,0xCF,0x0F,0x60,
  94. 0x18,0xD8,0x66,0x6C,0xE0,0x66,0x36,0x66,0xCC,0x00,0x60,0x18,0xD8,0x66,0x6C,0x60,
  95. 0x60,0x30,0x66,0xCC,0x00,0x60,0x18,0xD8,0x66,0x6C,0x60,0x60,0x30,0x66,0xCC,0x00,
  96. 0x60,0xF8,0xD9,0x66,0xEC,0xE7,0x63,0x30,0x66,0xCC,0x0F,0x60,0xF8,0xD9,0xE6,0xE7,
  97. 0xC7,0x67,0x30,0xE6,0xC7,0x0F,0x60,0x18,0xD8,0xE6,0x63,0x00,0x66,0x30,0xE6,0xC3,
  98. 0x00,0x60,0x18,0xD8,0x66,0x60,0x00,0x66,0x30,0x66,0xC0,0x00,0x60,0x18,0xD8,0x66,
  99. 0x60,0x60,0x66,0x36,0x66,0xC0,0x00,0x60,0xF8,0xD9,0x66,0xE0,0xE7,0xE7,0xF7,0x67,
  100. 0xC0,0x0F,0x60,0xF8,0xD9,0x66,0xE0,0xC7,0xC3,0xE3,0x63,0xC0,0x0F,0x60,0x00,0x00,
  101. 0x60,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  102. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  103. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  104. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  105. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  106. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  107. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  108. 0x00,0x00,0x00,0x00,0x00,0x00
  109. };


  110. /*主界面中的图像字样*/
  111. //主界面中的"工具"图像: 40X40px length:200  sizeof:200B
  112. const unsigned char pb_tool[] U8X8_PROGMEM = {
  113. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  114. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x01,
  115. 0x00,0x7C,0x00,0x40,0x06,0x00,0x63,0x00,0x40,0x0C,0x00,0x31,0x00,0x40,0x30,0x80,
  116. 0x18,0x04,0x80,0x20,0x80,0x08,0x06,0x80,0x60,0x40,0x08,0x05,0x00,0x41,0xC0,0x90,
  117. 0x04,0x00,0x42,0x80,0x60,0x04,0x00,0x9E,0xC0,0x00,0x04,0x00,0x30,0x61,0x00,0x02,
  118. 0x00,0x40,0x32,0x00,0x01,0x00,0x80,0x1C,0xFC,0x00,0x00,0x00,0x09,0x36,0x00,0x00,
  119. ……………………

  120. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
51hei.png
所有资料51hei提供下载:
代码,素材,文档及工具.rar (249.46 KB, 下载次数: 100)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:64089 发表于 2021-5-31 10:09 | 显示全部楼层
不错的资料正在学习中
回复

使用道具 举报

ID:374259 发表于 2021-6-5 23:25 | 显示全部楼层

不错的资料,要是有实物图就更好了
回复

使用道具 举报

ID:925265 发表于 2021-8-28 20:22 | 显示全部楼层
这不是别人免费开源的吗
回复

使用道具 举报

ID:88256 发表于 2021-8-28 21:11 | 显示全部楼层
应该上几张图片看看的
回复

使用道具 举报

ID:963223 发表于 2021-8-31 12:48 | 显示全部楼层
en  想看看,就是没图片啊?
回复

使用道具 举报

ID:291765 发表于 2021-9-4 16:41 | 显示全部楼层
谢谢UP的分享啊,学习了。
回复

使用道具 举报

ID:964490 发表于 2021-9-7 00:09 | 显示全部楼层
这是接的什么显示设备
回复

使用道具 举报

ID:64253 发表于 2021-9-11 20:46 | 显示全部楼层
这个想象在B站有看到过,是某up开源的东西
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表