当前位置: 首页 > java >正文

桌面小屏幕实战课程:DesktopScreen 16 HTTP

飞书文档http://https://x509p6c8to.feishu.cn/docx/doxcnrxBs55qGn6xoysTcJpqwRf

/home/kemp/work/esp/esp-idf/examples/protocols/http_request

源码下载方式参考:

源码下载方式

心知天气

注册账号,申请产品,获取密钥

产品 天气数据 HyperData 天气监控机器人 HyperBot 天气数据可视化分析平台 HyperInsights 气象灾害监控与预警系统 HyperAlert 行业 新能源 Energy 电力

公钥
PwIymwzzHNcTxxKkL
私钥
SmazqPcltzTft-X3v

API接口说明:

查看/修改你的API密钥 · 心知科技

API测试:

https://api.seniverse.com/v3/weather/now.json?key=SmazqPcltzTft-X3v&location=guangzhou&language=zh-Hans&unit=c

时间

时间https请求接口方式弃用。

时间接口可以参考工程ds_sntp.c文件实现,不建议使用http请求实现,外部第三方公司接口容易变动。

B站粉丝数

B站API更新后,只支持https方式请求,可以参考https章节代码实现。http方式弃用

https://api.bilibili.com/x/relation/stat?vmid=383943678&jsonp=jsonp

/* HTTP GET Example using plain POSIX socketsThis example code is in the Public Domain (or CC0 licensed, at your option.)Unless required by applicable law or agreed to in writing, thissoftware is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES ORCONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"#include "esp_netif.h"
#include "esp_tls.h"
#include "esp_http_client.h"
#include "cJSON.h"#include "ds_http_request.h"static const char *TAG = "HTTP_CLIENT";int fans_type = 0;/*
{"sysTime2":"2022-07-10 10:12:43","sysTime1":"20220710101243"
}
*/
static void cjson_time_info(char *text)
{cJSON *root,*psub;char time[20];//截取有效jsonchar *index=strchr(text,'{');strcpy(text,index);root = cJSON_Parse(text);if(root!=NULL){psub = cJSON_GetObjectItem(root, "sysTime1");sprintf(time,"%s",psub->valuestring);ESP_LOGI(TAG,"sysTime:%s",time);}cJSON_Delete(root);int len = strlen(time);if(len < 11){return;}// uint8_t hour;// uint8_t minute;// hour = (time[8] - '0')*10+time[9] - '0';// minute = (time[10] - '0')*10+time[11] - '0';
}//天气解析结构体
typedef struct
{char city[20];char weather_text[20];char weather_code[2];char temperatur[3];
}weather_info;weather_info weathe;
/*
{"results":[{"location":{"id":"WS0E9D8WN298","name":"广州","country":"CN","path":"广州,广州,广东,中国","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"多云","code":"4","temperature":"31"},"last_update":"2022-07-10T11:00:02+08:00"}]
}
*/
void cjson_weather_info(char *text)
{cJSON *root,*psub;cJSON *arrayItem;//截取有效jsonchar *index=strchr(text,'{');strcpy(text,index);root = cJSON_Parse(text);if(root!=NULL){psub = cJSON_GetObjectItem(root, "results");arrayItem = cJSON_GetArrayItem(psub,0);cJSON *locat = cJSON_GetObjectItem(arrayItem, "location");cJSON *now = cJSON_GetObjectItem(arrayItem, "now");if((locat!=NULL)&&(now!=NULL)){psub=cJSON_GetObjectItem(locat,"name");sprintf(weathe.city,"%s",psub->valuestring);ESP_LOGI(TAG,"city:%s",weathe.city);psub=cJSON_GetObjectItem(now,"text");sprintf(weathe.weather_text,"%s",psub->valuestring);ESP_LOGI(TAG,"weather:%s",weathe.weather_text);psub=cJSON_GetObjectItem(now,"code");sprintf(weathe.weather_code,"%s",psub->valuestring);ESP_LOGI(TAG,"%s",weathe.weather_code);psub=cJSON_GetObjectItem(now,"temperature");sprintf(weathe.temperatur,"%s",psub->valuestring);ESP_LOGI(TAG,"temperatur:%s",weathe.temperatur);}}cJSON_Delete(root);
}/*
{"code":0,"message":"0","ttl":1,"data":{"mid":383943678,"following":13,"whisper":0,"black":0,"follower":14233}
}
*/
void cjson_fans_info(char *text)
{cJSON *root,*psub,*ppsub;int fans = 0;//截取有效jsonchar *index=strchr(text,'{');strcpy(text,index);root = cJSON_Parse(text);if(root!=NULL){psub = cJSON_GetObjectItem(root, "data");if(psub!=NULL && psub->type == cJSON_Object){ppsub = cJSON_GetObjectItem(psub, "follower");if(ppsub != NULL && ppsub->type == cJSON_Number){fans = ppsub->valueint;ESP_LOGI(TAG,"fans:%d",fans);}}}fans_type++;cJSON_Delete(root);
}/*
{"cip":"121.32.92.51","cid":"440106","cname":"广东省广州市天河区"
}
*/
static void cjson_city_info(char *text)
{cJSON *root,*psub;char name[20];char cid[20];//截取有效jsonchar *index=strchr(text,'{');strcpy(text,index);root = cJSON_Parse(text);if(root!=NULL){psub = cJSON_GetObjectItem(root, "cname");sprintf(name,"%s",psub->valuestring);ESP_LOGI(TAG,"name:%s",name);psub = cJSON_GetObjectItem(root, "cid");sprintf(cid,"%s",psub->valuestring);ESP_LOGI(TAG,"cid:%s",cid);}cJSON_Delete(root);
}//事件回调
static esp_err_t _http_time_event_handle(esp_http_client_event_t *evt)
{switch(evt->event_id) {case HTTP_EVENT_ON_DATA://接收数据事件ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);if (!esp_http_client_is_chunked_response(evt->client)) {printf("%.*s\n", evt->data_len, (char*)evt->data);if(evt->data_len < 100)cjson_time_info((char*)evt->data);}break;case HTTP_EVENT_ERROR:break;case HTTP_EVENT_ON_CONNECTED:break;case HTTP_EVENT_HEADERS_SENT:break;case HTTP_EVENT_ON_HEADER:break;case HTTP_EVENT_DISCONNECTED:break;case HTTP_EVENT_ON_FINISH:break;}return ESP_OK;
}static esp_err_t _http_weather_event_handle(esp_http_client_event_t *evt)
{switch(evt->event_id) {case HTTP_EVENT_ON_DATA://接收数据事件ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);if (!esp_http_client_is_chunked_response(evt->client)) {printf("%.*s\n", evt->data_len, (char*)evt->data);cjson_weather_info((char*)evt->data);}break;case HTTP_EVENT_ERROR:break;case HTTP_EVENT_ON_CONNECTED:break;case HTTP_EVENT_HEADERS_SENT:break;case HTTP_EVENT_ON_HEADER:break;case HTTP_EVENT_DISCONNECTED:break;case HTTP_EVENT_ON_FINISH:break;}return ESP_OK;
}static esp_err_t _http_fans_event_handle(esp_http_client_event_t *evt)
{switch(evt->event_id) {case HTTP_EVENT_ON_DATA://接收数据事件ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);if (!esp_http_client_is_chunked_response(evt->client)) {printf("%.*s\n", evt->data_len, (char*)evt->data);cjson_fans_info((char*)evt->data);}break;case HTTP_EVENT_ERROR:break;case HTTP_EVENT_ON_CONNECTED:break;case HTTP_EVENT_HEADERS_SENT:break;case HTTP_EVENT_ON_HEADER:break;case HTTP_EVENT_DISCONNECTED:break;case HTTP_EVENT_ON_FINISH:break;}return ESP_OK;
}static esp_err_t _http_city_event_handle(esp_http_client_event_t *evt)
{switch(evt->event_id) {case HTTP_EVENT_ON_DATA://接收数据事件ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);if (!esp_http_client_is_chunked_response(evt->client)) {printf("%.*s\n", evt->data_len, (char*)evt->data);cjson_city_info((char*)evt->data);}break;case HTTP_EVENT_ERROR:break;case HTTP_EVENT_ON_CONNECTED:break;case HTTP_EVENT_HEADERS_SENT:break;case HTTP_EVENT_ON_HEADER:break;case HTTP_EVENT_DISCONNECTED:break;case HTTP_EVENT_ON_FINISH:break;}return ESP_OK;
}void http_time_get(){//http client配置esp_http_client_config_t config = {.method = HTTP_METHOD_GET, //get请求.url = "http://quan.suning.com/getSysTime.do",.event_handler = _http_time_event_handle,//注册时间回调.skip_cert_common_name_check = true,};esp_http_client_handle_t time_client = esp_http_client_init(&config);//初始化配置esp_err_t err = esp_http_client_perform(time_client);//执行请求if(err == ESP_OK){ESP_LOGI(TAG, "Status = %d, content_length = %d",esp_http_client_get_status_code(time_client),//状态码esp_http_client_get_content_length(time_client));//数据长度}esp_http_client_cleanup(time_client);//断开并释放资源
}void http_weather_get(){//http client配置esp_http_client_config_t config = {.method = HTTP_METHOD_GET, //get请求.url = "https://api.seniverse.com/v3/weather/now.json?key=SmazqPcltzTft-X3v&location=guangzhou&language=zh-Hans&unit=c",.event_handler = _http_weather_event_handle,//注册时间回调.skip_cert_common_name_check = true,};esp_http_client_handle_t weather_client = esp_http_client_init(&config);//初始化配置esp_err_t err = esp_http_client_perform(weather_client);//执行请求if(err == ESP_OK){ESP_LOGI(TAG, "Status = %d, content_length = %d",esp_http_client_get_status_code(weather_client),//状态码esp_http_client_get_content_length(weather_client));//数据长度}esp_http_client_cleanup(weather_client);//断开并释放资源
}void http_fans_get(){char url[100];if(fans_type == 0){//小智strcpy(url,"https://api.bilibili.com/x/relation/stat?vmid=383943678&jsonp=jsonp");}else{//阿奇strcpy(url,"https://api.bilibili.com/x/relation/stat?vmid=257459324&jsonp=jsonp");}//http client配置esp_http_client_config_t config = {.method = HTTP_METHOD_GET, //get请求.url = url,.event_handler = _http_fans_event_handle,//注册时间回调.skip_cert_common_name_check = true,};esp_http_client_handle_t fans_client = esp_http_client_init(&config);//初始化配置esp_err_t err = esp_http_client_perform(fans_client);//执行请求if(err == ESP_OK){ESP_LOGI(TAG, "Status = %d, content_length = %d",esp_http_client_get_status_code(fans_client),//状态码esp_http_client_get_content_length(fans_client));//数据长度}esp_http_client_cleanup(fans_client);//断开并释放资源
}void http_city_get(){//http client配置esp_http_client_config_t config = {.method = HTTP_METHOD_GET, //get请求.url = "http://pv.sohu.com/cityjson?ie=utf-8", //请求url.event_handler = _http_city_event_handle,//注册时间回调.skip_cert_common_name_check = true,};esp_http_client_handle_t city_client = esp_http_client_init(&config);//初始化配置esp_err_t err = esp_http_client_perform(city_client);//执行请求if(err == ESP_OK){ESP_LOGI(TAG, "Status = %d, content_length = %d",esp_http_client_get_status_code(city_client),//状态码esp_http_client_get_content_length(city_client));//数据长度}esp_http_client_cleanup(city_client);//断开并释放资源
}void ds_http_post(void)
{// //http client配置// esp_http_client_config_t config = {//     .method = HTTP_METHOD_GET, //get请求//     .url = "http://quan.suning.com/getSysTime.do",//     .event_handler = _http_event_handle,//注册时间回调//     .skip_cert_common_name_check = true,// };// // // POST// // const char *post_data = "field1=value1&field2=value2";// // esp_http_client_set_url(client, "http://httpbin.org/post");// // esp_http_client_set_method(client, HTTP_METHOD_POST);// // esp_http_client_set_post_field(client, post_data, strlen(post_data));// // int err = esp_http_client_perform(client);// // if (err == ESP_OK) {// //     ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",// //             esp_http_client_get_status_code(client),// //             esp_http_client_get_content_length(client));// //             int len =  esp_http_client_get_content_length(client);// //             int read_len = 0;// //             char buf[1024] = {0};// //             read_len = esp_http_client_read(client, buf, len);// //             printf("\nrecv data len:%d %d %s\n",read_len,len,buf);// // } else {// //     ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));// // }// esp_http_client_cleanup(client);
}xQueueHandle http_get_event_queue;void ds_http_set_type(HTTP_GET_TYPE_E type){HTTP_GET_EVENT_T evt;evt.type = type;xQueueSend(http_get_event_queue, &evt, 10);
}static void http_get_task(void *pvParameters)
{while(1) {HTTP_GET_EVENT_T evt;xQueueReceive(http_get_event_queue, &evt, portMAX_DELAY);ESP_LOGI(TAG, "http_get_task %d",evt.type);vTaskDelay(10000 / portTICK_PERIOD_MS);//根据接收到的消息,请求不同的接口if(evt.type == HTTP_GET_TIME){//时间获取http_time_get();}else if(evt.type == HTTP_GET_WEATHER){//天气获取http_weather_get();}else if(evt.type == HTTP_GET_FANS){//粉丝获取http_fans_get();}else if(evt.type == HTTP_GET_CITY){//城市获取http_city_get();}}
}void ds_http_request_init(void)
{http_get_event_queue = xQueueCreate(10, sizeof(HTTP_GET_EVENT_T));xTaskCreate(&http_get_task, "http_get_task", 4096, NULL, 5, NULL);
}
http://www.xdnf.cn/news/14682.html

相关文章:

  • Java锁机制知识点
  • 《Go语言高级编程》RPC 入门
  • python -日期与天数的转换
  • 量化面试绿皮书:56. 多项式求和
  • web3 docs
  • Linux进程关系
  • Flutter 网络请求指南, 从 iOS 到 Flutter 的 Dio + Retrofit 组合
  • 飞算科技依托 JavaAI 核心技术,打造企业级智能开发全场景方案
  • 数据应该如何组织,才能让Excel“读懂”?
  • Django ORM 1. 创建模型(Model)
  • 【2024 CVPR-Backbone】RepViT: Revisiting Mobile CNN From ViT Perspective
  • 什么是上证50etf期权波动率?
  • CPT204-Advanced OO Programming: Lists, Stacks, Queues, and Priority Queues
  • 工作台-02.代码开发
  • HTTP协议中Connection: Keep-Alive和Keep-Alive: timeout=60, max=100的作用
  • 什么是国际期货?期货交易平台搭建
  • [ linux-系统 ] 磁盘与文件系统
  • 【大模型实战 | BERT 量化分析(2)】
  • 从萌芽到领航:广州华锐互动的 AR 奋进之路​
  • 【github】从本地更新仓库里的文件笔记
  • MCP-安全(代码实例)
  • oracle基础审计管理
  • 【Linux指南】压缩、网络传输与系统工具
  • 2025.6.26总结
  • Kotlin环境搭建与基础语法入门
  • springcloud 尚硅谷 看到9开头
  • linux cp与mv那个更可靠
  • MySQL5.7和8.0 破解root密码
  • mysql之timestamp字段自动更新问题
  • ISP Pipeline(5): Auto White Balance Gain Control (AWB) 自动白平衡