资讯中心
资讯中心
基于树莓派RP2040游戏机的简易网络气象站
2022-09-06
96
pico esp32s2
tx = Pin(16) --> RXD_PIN (GPIO_NUM_21)
rx = Pin(17) --> RXD_PIN (GPIO_NUM_21)
3V3 --> 3V3
GND --> GND
- http_request/
- CMakeLists.txt
- sdkconfig
- main/ - CMakeLists.txt
- http_main.c esp32主函数,http请求,json解析
- http.h
- uart.c 串口通信部分
- uart.h
- wifi.c wifi连接部分
- wifi.h
# 在此处修改你要连接的wifi名和密码wifi_name = "123"wifi_passwd = "12345678"
/**********wifi初始化函数**************/void wifi_init_sta( char *wifi_ssid , char *wifi_password){
s_wifi_event_group = xEventGroupCreate();
// ESP_ERROR_CHECK(esp_netif_init());
// ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&event_handler,
NULL,
&instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
// .ssid = wifi_ssid ,
// .password = wifi_password ,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
},
};
memcpy(wifi_config.sta.ssid, wifi_ssid, sizeof(wifi_config.sta.ssid));
memcpy(wifi_config.sta.password, wifi_password, sizeof(wifi_config.sta.password));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
wifi_ssid, wifi_password);
http_get_task(); //连接成功,发送http请求
// sendData(TAG,"connectsucess");
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
wifi_ssid, wifi_password);
sendData(TAG,"Connectfail"); //连接失败,发送状态告知pico
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
sendData(TAG,"Connectfail"); //连接失败,发送状态告知pico
}
/* The event will not be processed after unregister */
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupdelete(s_wifi_event_group);}
//HTTP请求函数void http_get(char arg){
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
struct in_addr *addr;
int s, r;
char recv_buf[64];
char mid_buf[1400]; //接受http报文正文部分
memset(mid_buf,0,sizeof(mid_buf));
char WEB_PATH[200] = "GET " ;
// 组合字段构成http请求的发送内容,根据不同的请求进行不同的组合
switch (arg){
//实时天气,例:http://api.seniverse.com/v3/weather/now.json?key=your_api_key&locations=beijing&language=en&unit=c
case WEATHER_CURRENT:
strcat(WEB_PATH,WEB_PATH_CURRENT_1);
strcat(WEB_PATH,reqlocations);
strcat(WEB_PATH,WEB_PATH_CURRENT_2);
strcat(WEB_PATH,REQUEST_ED);
break;
//生活指数,例:http://api.seniverse.com/v3/life/suggestion.json?key=SzOM2PDJp7crLA0Ug&locations=haikou&language=en
case WEATHER_LIFE:
strcat(WEB_PATH,WEB_PATH_LIFE_1);
strcat(WEB_PATH,reqlocations);
strcat(WEB_PATH,WEB_PATH_LIFE_2);
strcat(WEB_PATH,REQUEST_ED);
break;
//天气预报,例:http://api.seniverse.com/v3/weather/daily.json?key=your_api_key&locations=beijing&language=zh-Hans&unit=c&start=0&days=5
case WEATHER_FORECAST:
strcat(WEB_PATH,WEB_PATH_FORECAST_1);
strcat(WEB_PATH,reqlocations);
strcat(WEB_PATH,WEB_PATH_FORECAST_2);
strcat(WEB_PATH,REQUEST_ED);
break;
default:ESP_LOGI(TAG, "wrong");
}
int err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res);
if(err != 0 || res == NULL) {
ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
vTaskDelay(1000 / portTICK_PERIOD_MS);
sendData(TAG,"httprequestfail"); //http初始化失败,告知pico
}else {
/* Code to print the resolved IP.
Note: inet_ntoa is non-reentrant, look at ipadsdr_ntoa_r for "real" code */
addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr));
s = socket(res->ai_family, res->ai_socktype, 0);
if(s < 0) {
ESP_LOGE(TAG, "... Failed to allocate socket.");
freeaddrinfo(res);
vTaskDelay(1000 / portTICK_PERIOD_MS);
sendData(TAG,"httprequestfail"); //http初始化失败,告知pico
}else{
ESP_LOGI(TAG, "... allocated socket");
if(connect(s, res->ai_addr, res->ai_addrlen) != 0) {
ESP_LOGE(TAG, "... socket connect failed errno=%d", errno);
close(s);
freeaddrinfo(res);
vTaskDelay(4000 / portTICK_PERIOD_MS);
sendData(TAG,"httprequestfail"); //http初始化失败,告知pico
}else{
ESP_LOGI(TAG, "... connected");
freeaddrinfo(res);
if (write(s, WEB_PATH, strlen(WEB_PATH)) < 0) {
ESP_LOGE(TAG, "... socket send failed");
close(s);
vTaskDelay(4000 / portTICK_PERIOD_MS);
sendData(TAG,"httprequestfail"); //http初始化失败,告知pico
}else{
ESP_LOGI(TAG, "... socket send success");
struct timevals receiving_timeout;
receiving_timeout.tv_sec = 5;
receiving_timeout.tv_usec = 0;
if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout,
sizeof(receiving_timeout)) < 0) {
ESP_LOGE(TAG, "... failed to set socket receiving timeout");
close(s);
vTaskDelay(4000 / portTICK_PERIOD_MS);
sendData(TAG,"httprequestfail"); //http初始化失败,告知pico
}else{
ESP_LOGI(TAG, "... set socket receiving timeout success");
/* Read HTTP response */
do {
bzero(recv_buf, sizeof(recv_buf));
r = read(s, recv_buf, sizeof(recv_buf)-1);
strcat(mid_buf,recv_buf);
for(int i = 0; i < r; i++) {
putchar(recv_buf[i]);
}
} while(r > 0);
// ESP_LOGI(TAG,"return=%s",mid_buf);
//json格式转化
cjson_to_struct_info(mid_buf,arg);
ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d.", r, errno);
close(s);
}
}
}
}
} }
void http_get_task(void){
memset(send_data_quene,0,sizeof(send_data_quene));
http_get(WEATHER_CURRENT); //天气实况
vTaskDelay(1000 / portTICK_PERIOD_MS); //适当延时
http_get(WEATHER_FORECAST); //天气预报
vTaskDelay(1000 / portTICK_PERIOD_MS);
http_get(WEATHER_LIFE); //生活指数
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGI(TAG,"send_data:%s",send_data_quene);
sendData(TAG,send_data_quene);//整合发送}
实时天气,例:http://api.seniverse.com/v3/weather/now.json?key=your_api_key&locations=beijing&language=en&unit=c
生活指数,例:http://api.seniverse.com/v3/life/suggestion.json?key=SzOM2PDJp7crLA0Ug&locations=haikou&language=en
天气预报,例:http://api.seniverse.com/v3/weather/daily.json?key=your_api_key&locations=beijing&language=zh-Hans&unit=c&start=0&days=5
所以在函数在加入判断到底发送哪一个,相应的请求行要进行不同组合,在http_get()函数中做判断,具体内容在http.h中,如下。
#define WEB_SERVER "api.seniverse.com"#define WEB_PORT "80"#define reqUserKey "SzOM2PDJp7crLA0Ug"// #define reqlocations "Shenzhen"#define reqUnit "c"//天气实况#define WEATHER_CURRENT 'C' #define WEB_PATH_CURRENT_1 "/v3/weather/now.json?key=" reqUserKey "&locations=" #define WEB_PATH_CURRENT_2 "&language=en&unit=" reqUnit//生活指数#define WEATHER_LIFE 'L'#define WEB_PATH_LIFE_1 "/v3/life/suggestion.json?key=" reqUserKey "&locations=" #define WEB_PATH_LIFE_2 "&language=en"//天气预报#define WEATHER_FORECAST 'F'#define WEB_PATH_FORECAST_1 "/v3/weather/daily.json?key=" reqUserKey "&locations=" #define WEB_PATH_FORECAST_2 "&language=en&unit=" reqUnit "&start=0&days=5"//http请求尾static const char *REQUEST_ED = " HTTP/1.0
"
"Host: "WEB_SERVER":"WEB_PORT"
"
"User-Agent: esp-idf/1.0 esp32
"
"
";//城市名char *reqlocations ;
/***********json格式解析************/void cjson_to_struct_info(char *text,char arg){
cJSON *root,*psub;
cJSON *arrayItem;
//截取有效json
char *index=strchr(text,'{');
strcpy(text,index);
root = cJSON_Parse(text);
if(root!=NULL)
{
/*******************天气实况**********/
if(arg == WEATHER_CURRENT){
psub = cJSON_GetObjectItem(root, "results");
arrayItem = cJSON_GetArrayItem(psub,0);
cJSON *locat = cJSON_GetObjectItem(arrayItem, "locations");
cJSON *now = cJSON_GetObjectItem(arrayItem, "now");
if((locat!=NULL)&&(now!=NULL))
{
psub=cJSON_GetObjectItem(locat,"name");
sprintf(weathe.cit,"%s",psub->valuestring);
ESP_LOGI(TAG,"city:%s",weathe.cit);
strcat(send_data_quene,weathe.cit); //拼接发送字符串
strcat(send_data_quene,"+"); //分割符,让pico识别
psub=cJSON_GetObjectItem(now,"text");
sprintf(weathe.weather_text,"%s",psub->valuestring);
ESP_LOGI(TAG,"weather:%s",weathe.weather_text);
strcat(send_data_quene,weathe.weather_text);
strcat(send_data_quene,"+");
psub=cJSON_GetObjectItem(now,"code");
sprintf(weathe.weather_code,"%s",psub->valuestring);
ESP_LOGI(TAG,"%s",weathe.weather_code);
strcat(send_data_quene,weathe.weather_code);
strcat(send_data_quene,"+");
psub=cJSON_GetObjectItem(now,"temperature");
sprintf(weathe.temperatur,"%s",psub->valuestring);
ESP_LOGI(TAG,"temperatur:%s",weathe.temperatur);
strcat(send_data_quene,weathe.temperatur);
strcat(send_data_quene,"+");
}else{
sendData(TAG,"httprequestfail"); //json格式有误。http请求失败
}
}
/*****************天气预报*************************/
if(arg == WEATHER_FORECAST){
psub = cJSON_GetObjectItem(root, "results");
arrayItem = cJSON_GetArrayItem(psub,0);
cJSON *locat = cJSON_GetObjectItem(arrayItem, "locations");
cJSON *daily = cJSON_GetObjectItem(arrayItem, "daily");
if((locat!=NULL)&&(daily!=NULL))
{
for(int i = 0;i<3;i++){
arrayItem = cJSON_GetArrayItem(daily,i);
psub = cJSON_GetObjectItem(arrayItem, "date");
sprintf(weathe.daily_weathe[i].date,"%s",psub->valuestring);
ESP_LOGI(TAG,"date:%s",weathe.daily_weathe[i].date);
strcat(send_data_quene,weathe.daily_weathe[i].date);
strcat(send_data_quene,"+");
psub = cJSON_GetObjectItem(arrayItem, "text_day");
sprintf(weathe.daily_weathe[i].text_day,"%s",psub->valuestring);
ESP_LOGI(TAG,"text_day:%s",weathe.daily_weathe[i].text_day);
strcat(send_data_quene,weathe.daily_weathe[i].text_day);
strcat(send_data_quene,"+");
psub = cJSON_GetObjectItem(arrayItem, "code_day");
sprintf(weathe.daily_weathe[i].code_day,"%s",psub->valuestring);
ESP_LOGI(TAG,"code_day:%s",weathe.daily_weathe[i].code_day);
strcat(send_data_quene,weathe.daily_weathe[i].code_day);
strcat(send_data_quene,"+");
psub = cJSON_GetObjectItem(arrayItem, "text_night");
sprintf(weathe.daily_weathe[i].text_night,"%s",psub->valuestring);
ESP_LOGI(TAG,"text_night:%s",weathe.daily_weathe[i].text_night);
strcat(send_data_quene,weathe.daily_weathe[i].text_night);
strcat(send_data_quene,"+");
psub = cJSON_GetObjectItem(arrayItem, "code_night");
sprintf(weathe.daily_weathe[i].code_night,"%s",psub->valuestring);
ESP_LOGI(TAG,"code_night:%s",weathe.daily_weathe[i].code_night);
strcat(send_data_quene,weathe.daily_weathe[i].code_night);
strcat(send_data_quene,"+");
psub = cJSON_GetObjectItem(arrayItem, "high");
sprintf(weathe.daily_weathe[i].high,"%s",psub->valuestring);
ESP_LOGI(TAG,"high:%s",weathe.daily_weathe[i].high);
strcat(send_data_quene,weathe.daily_weathe[i].high);
strcat(send_data_quene,"+");
psub = cJSON_GetObjectItem(arrayItem, "low");
sprintf(weathe.daily_weathe[i].low,"%s",psub->valuestring);
ESP_LOGI(TAG,"low:%s",weathe.daily_weathe[i].low);
strcat(send_data_quene,weathe.daily_weathe[i].low);
strcat(send_data_quene,"+");
psub = cJSON_GetObjectItem(arrayItem, "precip");
sprintf(weathe.daily_weathe[i].precip,"%s",psub->valuestring);
ESP_LOGI(TAG,"precip:%s",weathe.daily_weathe[i].precip);
strcat(send_data_quene,weathe.daily_weathe[i].precip);
strcat(send_data_quene,"+");
psub = cJSON_GetObjectItem(arrayItem, "humidity");
sprintf(weathe.daily_weathe[i].humidity,"%s",psub->valuestring);
ESP_LOGI(TAG,"humidity:%s",weathe.daily_weathe[i].humidity);
strcat(send_data_quene,weathe.daily_weathe[i].humidity);
strcat(send_data_quene,"+");
}
}
else{
sendData(TAG,"httprequestfail"); //json格式有误。http请求失败
}
}
/**************************生活指数****************************************/
if(arg == WEATHER_LIFE){
psub = cJSON_GetObjectItem(root, "results");
arrayItem = cJSON_GetArrayItem(psub,0);
cJSON *locat = cJSON_GetObjectItem(arrayItem, "locations");
cJSON *suggestion = cJSON_GetObjectItem(arrayItem, "suggestion");
if((locat!=NULL)&&(suggestion!=NULL))
{
cJSON *car_washing=cJSON_GetObjectItem(suggestion,"car_washing");
psub=cJSON_GetObjectItem(car_washing,"brief");
sprintf(weathe.car_washing,"%s",psub->valuestring);
ESP_LOGI(TAG,"car_washing:%s",weathe.car_washing);
strcat(send_data_quene,weathe.car_washing);
strcat(send_data_quene,"+");
cJSON *dressing=cJSON_GetObjectItem(suggestion,"dressing");
psub=cJSON_GetObjectItem(dressing,"brief");
sprintf(weathe.dressing,"%s",psub->valuestring);
ESP_LOGI(TAG,"dressing:%s",weathe.dressing);
strcat(send_data_quene,weathe.dressing);
strcat(send_data_quene,"+");
cJSON *flu=cJSON_GetObjectItem(suggestion,"flu");
psub=cJSON_GetObjectItem(flu,"brief");
sprintf(weathe.flu,"%s",psub->valuestring);
ESP_LOGI(TAG,"flu:%s",weathe.flu);
strcat(send_data_quene,weathe.flu);
strcat(send_data_quene,"+");
cJSON *sport=cJSON_GetObjectItem(suggestion,"sport");
psub=cJSON_GetObjectItem(sport,"brief");
sprintf(weathe.sport,"%s",psub->valuestring);
ESP_LOGI(TAG,"sport:%s",weathe.sport);
strcat(send_data_quene,weathe.sport);
strcat(send_data_quene,"+");
cJSON *travel=cJSON_GetObjectItem(suggestion,"travel");
psub=cJSON_GetObjectItem(travel,"brief");
if (psub->valuestring[0] == '


