- /***********************************************************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] config.h
- * [url=home.php?mod=space&uid=187600]@author[/url] King
- * [url=home.php?mod=space&uid=895143]@version[/url] V1.00
- * [url=home.php?mod=space&uid=212281]@date[/url] 18-Jan-2023
- * [url=home.php?mod=space&uid=247401]@brief[/url] ......
- *********************************************************************************************************************/
- /* Define to prevent recursive inclusion -----------------------------------------------------------------------------*/
- #ifndef __CONFIG_H__
- #define __CONFIG_H__
- /* Includes ----------------------------------------------------------------------------------------------------------*/
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /* Includes ----------------------------------------------------------------------------------------------------------*/
- #include "rd8_conf.h"
- /* Includes ----------------------------------------------------------------------------------------------------------*/
- #include "TimeSlice.h"
- /* Includes ----------------------------------------------------------------------------------------------------------*/
- #include "ESP12.h"
- #include "LED.h"
- #include "MCU.h"
- #include "OLED.h"
- #include "RGB.h"
- #include "json.h"
- #define SSID "OpenBSD"
- #define PASS "********"
- #define WIFI_CONN_STR "AT+CWJAP="" SSID "",""PASS"""
- #define API_KEY "S**************g"
- #define API_CITY "beijing"
- #define API_URL "https://api.seniverse.com/v3/weather/now.json?key=" API_KEY "&location=" API_CITY "&language=zh-Hans&unit=c"
- #define API_REQUEST_STR "GET " API_URL "\r\n"
- #endif
- /*****************************************************END OF FILE******************************************************/
对应的WiFi连接部分,则修改位置在ESP12.c文件中:
- /* 连接WiFi */
- /* AT+CWJAP="SSID","PASS" */
- if (ESP12_AT_SendCommand(WIFI_CONN_STR, "OK", 8000) == 0)
- {
- if (ESP12_AT_SendCommand(NULL, "OK", 8000) == 0)
- {
- OLED_Clear();
- OLED_ShowString(0, 0, "WiFi Connect Error!", 16);
- return;
- }
- }
对应心知天气数据获取的部分,同样修改该文件:
- void ESP12_TxHandler(void)
- {
- if (1 == ESP12_InitFlag)
- {
- /* 每间隔5秒获取一次天气信息, 服务器限制每分钟最多20次!!! */
- ESP12_SendString(API_REQUEST_STR);
- }
- }
上述API Key的获取,可以访问心知天气(https://www.seniverse.com/),注册后开通免费服务,在从如下界面获取:
由于心知天气返回的是json字符串:
- {"results":[{"location":{"id":"****************","name":"北京","country":"CN","path":"北京,北京,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"晴","code":"1","temperature":"6"},"last_update":"2023-03-01T00:10:13+08:00"}]}
C51资源有限,所以原实例简单起见,直接显示从temperature往后的所有字符如。
经过摸索,找到了一个非常简化的可用于C51的json解析代码,虽然功能极其有限且不完善,但是用于此处的解析足够了:
- /* Define to prevent recursive inclusion -----------------------------------------------------------------------------*/
- #ifndef __JSON_H__
- #define __JSON_H__
- /***************************************
- name: JSON C函数库 头文件
- ***************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /***************************************
- name: 函数声明
- input:
- output:
- description:
- ***************************************/
- char json_check(char *str); // JSON 校验函数
- char json_get_value(char *json, char *json_key, char *json_value); // JSON获取键值
- char json_check_value(char *str1, char *str2); // JSON 键值对比函数
- #endif
- /*****************************************************END OF FILE******************************************************/
这个库只能做最基础的处理,json_get_value()用于获取对应key的值。就获取第一个找到的,如果有多个重复的,抱歉。
在这篇分享提供的代码中,已经提供了这两个库,可以直接调用即可。
然后,将显示的部分,进行如下的处理:
- if (!bStatus) {
- bStatus = 1;
- OLED_Clear();
- OLED_ShowString(0, 0, "Date Time:", 16);
- OLED_ShowString(0, 2, "D: ", 16);
- OLED_ShowString(0, 4, "T: ", 16);
- OLED_ShowString(0, 6, "Temp:", 16);
- }
- OLED_ShowString(50, 6, " ", 16);
- if(json_get_value((char *)ESP12_Buffer, "temperature", json_value))
- {
- OLED_ShowString(50, 6, json_value, 16);
- }
-
- if(json_get_value((char *)ESP12_Buffer, "last_update", json_value))
- {
- strncpy(date, json_value, 10);
- strncpy(time, json_value+11, 8);
- OLED_ShowString(18, 2, date, 16);
- OLED_ShowString(18, 4, time, 16);
- }
这个地方,本来想把这个时间作为一个基础,来按秒递增,从而实现互联网时钟的功能。
但是实际调用后发现,这个时间,不是当前的时间,而是当前天气数据更新的时间。所以就不能用作时钟的更新显示了。
后续将会定时调用 https://f.m.suning.com/api/ct.do,来获取最新的时间戳,并结合定时器,来实现按秒走动的时钟。
目前效果如下:
再次感谢 xld0932 提供的实例。
后续完善:
1. 通过在线api获取最新的时间戳,然后用于时钟的显示
2. 研究如何给板载的ESP-12F刷MQTT固件,实现进一步的数据上报和远程控制。