Skip to content

Commit d02f028

Browse files
LiKunZhmessigogogo
authored andcommitted
[BSP][nxp]增加wifi_weather_sample.c (RT-Thread#9118)
* [BSP][nxp]增加wifi_weather_sample.c * [BSP][nsp]增加wifi_weather_sample.c
1 parent 1d16a29 commit d02f028

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed

bsp/nxp/mcx/mcxn/frdm-mcxn947/board/SConscript

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ if GetDepend(['BSP_USING_RW007']):
1818
if GetDepend(['RT_USING_SFUD']):
1919
src += Glob('ports/drv_filesystem_spi_flash.c')
2020

21+
if GetDepend(['PKG_USING_CJSON']) and GetDepend(['PKG_USING_WEBCLIENT']):
22+
src += Glob('ports/wifi_weather_sample.c')
23+
2124

2225
CPPPATH = [cwd, cwd + '/MCUX_Config/board']
2326
CPPDEFINES = ['DEBUG', 'CPU_MCXN947VDF_cm33_core0']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
/*
2+
* Copyright (c) 2006-2023, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024-06-27 Zhang the first version
9+
*/
10+
11+
#include <rtdevice.h>
12+
#include <rtthread.h>
13+
#include "spi_wifi_rw007.h"
14+
#include "webclient.h"
15+
#include "cJSON.h"
16+
#include "stdio.h"
17+
18+
#ifdef PKG_USING_CJSON
19+
#ifdef PKG_USING_WEBCLIENT
20+
21+
#define city_id 54523
22+
#define MAX_RESPONSE_SIZE 1024
23+
#define INITIAL_RESPONSE_SIZE 128
24+
25+
static void parse_weather_data(const char *buf);
26+
static void get_weather_forecast(void);
27+
28+
static void parse_weather_data(const char *buf)
29+
{
30+
31+
cJSON *root = cJSON_Parse(buf);
32+
if (root == NULL) {
33+
rt_kprintf("Error parsing JSON data\n");
34+
return;
35+
}
36+
37+
cJSON *msg = cJSON_GetObjectItem(root, "msg");
38+
if (msg == NULL) {
39+
rt_kprintf("Error getting msg object\n");
40+
cJSON_Delete(root);
41+
return;
42+
}
43+
else {
44+
rt_kprintf("msg: %s\n", msg->valuestring);
45+
}
46+
47+
cJSON *code = cJSON_GetObjectItem(root, "code");
48+
if (code == NULL) {
49+
rt_kprintf("Error getting code object\n");
50+
cJSON_Delete(root);
51+
return;
52+
}
53+
else {
54+
rt_kprintf("code: %d\n", code->valueint);
55+
}
56+
57+
cJSON *data = cJSON_GetObjectItem(root, "data");
58+
if (data == NULL) {
59+
rt_kprintf("Error getting data object\n");
60+
cJSON_Delete(root);
61+
return;
62+
}
63+
else {
64+
rt_kprintf("data: \n");
65+
}
66+
67+
cJSON *location = cJSON_GetObjectItem(data, "location");
68+
if (location == NULL) {
69+
rt_kprintf("Error getting location object\n");
70+
cJSON_Delete(root);
71+
return;
72+
}
73+
else {
74+
rt_kprintf(" location: \n");
75+
}
76+
77+
cJSON *id = cJSON_GetObjectItem(location, "id");
78+
cJSON *name = cJSON_GetObjectItem(location, "name");
79+
cJSON *path = cJSON_GetObjectItem(location, "path");
80+
81+
if (id != NULL && name != NULL && path != NULL) {
82+
rt_kprintf(" id: %d\n", id->valueint);
83+
rt_kprintf(" name: %s\n", name->valuestring);
84+
rt_kprintf(" path: %s\n", path->valuestring);
85+
} else {
86+
rt_kprintf("Error getting fields\n");
87+
}
88+
89+
cJSON *now = cJSON_GetObjectItem(data, "now");
90+
if (now == NULL) {
91+
rt_kprintf("Error getting now object\n");
92+
cJSON_Delete(root);
93+
return;
94+
}
95+
else {
96+
rt_kprintf(" now: \n");
97+
}
98+
99+
cJSON *precipitation = cJSON_GetObjectItem(now, "precipitation");
100+
cJSON *temperature = cJSON_GetObjectItem(now, "temperature");
101+
cJSON *pressure = cJSON_GetObjectItem(now, "pressure");
102+
cJSON *humidity = cJSON_GetObjectItem(now, "humidity");
103+
cJSON *windDirection = cJSON_GetObjectItem(now, "windDirection");
104+
cJSON *windDirectionDegree = cJSON_GetObjectItem(now, "windDirectionDegree");
105+
cJSON *windSpeed = cJSON_GetObjectItem(now, "windSpeed");
106+
cJSON *windScale = cJSON_GetObjectItem(now, "windScale");
107+
108+
if (precipitation != NULL && temperature != NULL && pressure != NULL && humidity != NULL &&
109+
windDirection != NULL && windDirectionDegree != NULL && windSpeed != NULL && windScale != NULL) {
110+
rt_kprintf(" precipitation: %d\n", precipitation->valueint);
111+
rt_kprintf(" temperature: %.1f\n", temperature->valuedouble);
112+
rt_kprintf(" pressure: %d\n", pressure->valueint);
113+
rt_kprintf(" humidity: %d\n", humidity->valueint);
114+
rt_kprintf(" windDirection: %s\n", windDirection->valuestring);
115+
rt_kprintf(" windDirectionDegree: %d\n", windDirectionDegree->valueint);
116+
rt_kprintf(" windSpeed: %.1f\n", windSpeed->valuedouble);
117+
rt_kprintf(" windScale: %s\n", windScale->valuestring);
118+
} else {
119+
rt_kprintf("Error getting fields\n");
120+
}
121+
122+
cJSON *alarm = cJSON_GetObjectItem(data, "alarm");
123+
if (alarm == NULL) {
124+
rt_kprintf("Error getting alarm object\n");
125+
cJSON_Delete(root);
126+
return;
127+
}
128+
else {
129+
rt_kprintf(" alarm: %s\n", cJSON_Print(alarm));
130+
}
131+
132+
cJSON *lastUpdate = cJSON_GetObjectItem(data, "lastUpdate");
133+
if (lastUpdate == NULL) {
134+
rt_kprintf("Error getting lastUpdate object\n");
135+
cJSON_Delete(root);
136+
return;
137+
}
138+
else {
139+
rt_kprintf(" lastUpdate: %s\n", cJSON_Print(lastUpdate));
140+
}
141+
142+
cJSON_Delete(root);
143+
}
144+
145+
static void get_weather_forecast(void)
146+
{
147+
char url[256];
148+
snprintf(url, sizeof(url), "http://weather.cma.cn/api/now/%d", city_id);
149+
150+
struct webclient_session* session = webclient_session_create(1024);
151+
if(session == NULL) return;
152+
153+
int rc = webclient_get(session, url);
154+
if(rc != 200) goto free_session_exit;
155+
156+
char *response = rt_malloc(INITIAL_RESPONSE_SIZE);
157+
if(response == NULL) goto free_session_exit;
158+
159+
int data_ptr = 0;
160+
int buf_size = INITIAL_RESPONSE_SIZE;
161+
do {
162+
int bytes_read = webclient_read(session, &response[data_ptr], INITIAL_RESPONSE_SIZE);
163+
if(bytes_read <= 0) {
164+
break;
165+
}
166+
167+
data_ptr += bytes_read;
168+
if(data_ptr > MAX_RESPONSE_SIZE) {
169+
break;
170+
}
171+
172+
if((data_ptr + INITIAL_RESPONSE_SIZE) > buf_size) {
173+
response = rt_realloc(response, data_ptr + INITIAL_RESPONSE_SIZE);
174+
buf_size += INITIAL_RESPONSE_SIZE;
175+
}
176+
} while(1);
177+
178+
/* response is alive */
179+
parse_weather_data(response);
180+
181+
rt_free(response);
182+
183+
free_session_exit:
184+
webclient_close(session);
185+
}
186+
187+
static void wifi_weather_sample(void)
188+
{
189+
/* wifi join */;
190+
get_weather_forecast();
191+
rt_kprintf("\r\n");
192+
rt_kprintf("wifi_weather_sample complete\r\n");
193+
}
194+
195+
MSH_CMD_EXPORT(wifi_weather_sample, wifi weather sample);
196+
197+
#endif
198+
199+
#endif

0 commit comments

Comments
 (0)