先给出两个可以获取天气和时间的网站
https://www.seniverse.com/docs 可以获取天气
http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&HTTP/1.1
可以获取时间
接下来简单说说怎么通过AT指令的方式去获取天气及时间信息
1:连网
首先,ESP8266肯定是需要连上网才能访问这些信息。所以,第一步要做的就是让ESP8266连接上wifi热点,可以是路由器,也可以是手机热点。下面写出重要的步骤
1:使用串口发送指令AT+CWMODE=1设置模块Wi_Fi应用模式为Station模式。
2:发送指令AT+RST使模块重启,重启后等待一段时间
3:发送指令AT+CIPMUX=0 设置模块为单路连接模式
4:发送指令AT+CWJAP="ssid","pwd",连接AP;(ssid就是你要连接WiFi的名字,pwd就是密码)
经过以上四步,ESP8266就可以连接上WIFI了.
完整版代码如下:
u8 atk_8266_wifista_config(void)
{
u8 *p;
p=pvPortMalloc(50);
POINT_COLOR=RED;
while(atk_8266_send_cmd("AT","OK",100)) //Check if the WIFI module is online
{
atk_8266_quit_trans(); //Exit transparent transmission
atk_8266_send_cmd("AT+CIPMODE=0","OK",200);
My_Pri_log_x(1,"ESP8266 does not existrn");
LED0=~LED0;
vTaskDelay(1000);
}
LED0=1;
while(atk_8266_send_cmd("ATE0","OK",20)); //Close echo
atk_8266_send_cmd("AT+CWMODE=1","OK",50); //Set WIFI STA mode
atk_8266_send_cmd("AT+RST","OK",20);
vTaskDelay(3000); //Delay 3S for successful restart
atk_8266_send_cmd("AT+CIPMUX=0","OK",20); //0:Single connection
sprintf((char*)p,"AT+CWJAP="%s","%s"",wifista_ssid,wifista_password);//Set wireless parameters
while(atk_8266_send_cmd(p,"WIFI GOT IP",500));//Connect to the target router
vPortFree(p);
return 0;
}
2:获取天气和时间
以获取天气为例,获取时间就是网站不同罢了。
上面一段话是在心知天气中截屏所得。
其中key= 后面的内容是你在心知天气注册后得到的一个密钥,注册很简单,不麻烦,而且是免费。
location= 后面是你要查询天气的城市,心知的网站上有城市的说明文档;
language= 后面的是语言的选择。官方也有说明文档。
欧克,有了以上可以获取天气等信息的网站后,我们就可以配置ESP8266去访问这个网址,从而得到我们想要的信息。
写出重要的步骤:
1:发送指令AT+CIPSTART= .... 连接到指定网站(就是上面的https://www.seniverse.com/docs )端口信息写80即可
2:发送指令AT+CIPMODE =1 设置为透传模式
3:发送指令AT+CIPSEND 开始透传
4:发送 GET+上面的获取时间的网址
5:稍等片刻,就会将天气等信息回传过来
6:退出透传模式(连续发送3个 + 即可退出透传模式)
7:发送指令AT+CIPCLOSE 来关闭透传
完整版代码如下:
/**
* @brief :Get a real-time weather
* @param 0:Get location, weather, temperature
* @param 1:Get Time
* @retval 0:Successful
* 1:Get failed
*/
u8 get_current_Information(u8 select)
{
u8 *p;
u8 res;
p=pvPortMalloc(40);
if(select == 0)
sprintf((char*)p,"AT+CIPSTART="TCP","%s",%s",WEATHER_SERVERIP,WEATHER_PORTNUM); //Configure the target TCP server
else if(select == 1)
sprintf((char*)p,"AT+CIPSTART="TCP","%s",%s",TIME_SERVERIP,TIME_PORTNUM);
res = atk_8266_send_cmd(p,"OK",200);
if(res==1)
{
vPortFree(p);
return 1;
}
vTaskDelay(300);
atk_8266_send_cmd("AT+CIPMODE=1","OK",200); //The transmission mode is: transparent transmission
USART2_RX_STA=0;
atk_8266_send_cmd("AT+CIPSEND","OK",200); //Start passthrough
My_Pri_log_x(1,"start trans...rn");
if(select == 0)
My_Pri_log_x(2,"GET https://api.seniverse.com/v3/weather/now.json?key=S5b_Q9GDyOKOPWOAS&location=xiaogan&language=en&unit=cnn");
else if(select == 1)
My_Pri_log_x(2,"GET http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&HTTP/1.1nn");
vTaskDelay(20);//Delayed 20ms returns the status of the command sent successfully
//atk_8266_at_response(1);
USART2_RX_STA=0;
vTaskDelay(1000);
atk_8266_at_response(0);
if(USART2_RX_STA&0X8000) //At this time, the data is received again, which is the weather data
USART2_RX_BUF[USART2_RX_STA&0X7FFF]=0;//Add terminator
if(select == 0)
parse_now_weather();
else if(select == 1)
{
parse_now_time();
}
atk_8266_quit_trans();//Exit transparent transmission
atk_8266_send_cmd("AT+CIPCLOSE","OK",50); //Close the connection
vPortFree(p);
return 0;
}
上面的函数通过传入0和1可以达到获取天气或者时间的功能。
3:对获取到的数据进行解析
你获取到的数据其实都是Json格式,我也不懂,但是有Demo对这些数据进行解析。
{"success":"1","result":{"timestamp":"1601813449","datetime_1":"2020-10-04 20:10:49","datetime_2":"2020骞?10鏈?04鏃? 20鏃?10鍒?49绉?","week_1":"0","week_2":"鏄熸湡鏃?","week_3":"鍛ㄦ棩","week_4":"Sunday"}}
上面这段,是获取时间后返回的数据。直接上解析代码,对葫芦画瓢即可
/**
* @brief Analyze the current TIME
*/
u8 parse_now_time(void)
{
cJSON *root;
cJSON *pSub;
cJSON *pItem;
char *utf8str;
root = pvPortMalloc(sizeof(cJSON));
pSub = pvPortMalloc(sizeof(cJSON));
pItem = pvPortMalloc(sizeof(cJSON));
utf8str = pvPortMalloc(50);
memset(utf8str,0,50);
root = cJSON_Parse((const char*)USART2_RX_BUF);
if(root != NULL)
{
pSub = cJSON_GetObjectItem(root,"result");
if(pSub != NULL)
{
pItem = cJSON_GetObjectItem(pSub,"datetime_1");
if(pItem != NULL)
{
utf8str = pItem->valuestring;
memcpy(ESP_TIME,utf8str,20);
My_Pri_log_x(1,"%d--%srn",__LINE__,utf8str);
My_Pri_log_x(1,"%d--%srn",__LINE__,ESP_TIME);
}
pItem = cJSON_GetObjectItem(pSub,"week_1");
if(pItem != NULL)
{
utf8str = pItem->valuestring;
memcpy(ESP_WEEK,utf8str,1);
My_Pri_log_x(1,"%d--%srn",__LINE__,utf8str);
My_Pri_log_x(1,"%d--%srn",__LINE__,ESP_WEEK);
}
}
}
vPortFree(root);
vPortFree(pSub);
vPortFree(pItem);
vPortFree(utf8str);
return 0;
}
上一篇:CAN_总线_STM32 (1)
下一篇:STM32 SPI NSS信号理解和DMA传输遇到的问题
推荐阅读最新更新时间:2024-11-08 11:40