From 07b64425cdc92fdffb277241d2498411b472e049 Mon Sep 17 00:00:00 2001 From: murmur Date: Thu, 4 May 2023 16:35:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0eth=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=20=E6=B7=BB=E5=8A=A0=20getinfo.c=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BE=9D=E8=B5=96=E5=8C=85=20cJSON-latest=20webclient?= =?UTF-8?q?-v2.2.0=20322.54=20KB/32.36=20KB/67636?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- applications/getinfo.c | 268 ++++++++++++++++++++++++++++++++ applications/iwdg_sample.c | 3 +- applications/led_blink_sample.c | 2 +- applications/main.c | 2 +- rtconfig.h | 18 ++- 5 files changed, 289 insertions(+), 4 deletions(-) create mode 100644 applications/getinfo.c diff --git a/applications/getinfo.c b/applications/getinfo.c new file mode 100644 index 0000000..28a31ce --- /dev/null +++ b/applications/getinfo.c @@ -0,0 +1,268 @@ +#include +//#include <../packages/webclient-v2.2.0/inc/webclient.h> +#include + +#define LOG_TAG "getinfo" +#define LOG_LVL LOG_LVL_DBG +#include + +//#define HTTP_GET_URL "http://www.rt-thread.com/service/rt-thread.txt" +#ifndef TT_IP +#define TT_IP "http://192.168.0.232:4005" +#endif +#define TT_SIM TT_IP "/action/webGetSIMState"//SIM +#define TT_XH TT_IP "/action/webGetTDSignal"//信号值 +#define TT_RW TT_IP "/action/webGetTdState"//入网 +#define TT_JH TT_IP "/action/webGetPSState"//激活 +#define TT_DW TT_IP "/action/webGetBDGPS"//定位 +//http://192.168.0.232:4005/action/webGetSIMState webGetBDGPS +#include +#include + +#include +#include //用于strtod函数 +#include + +char *infoH[] = { TT_SIM, TT_XH, TT_RW, TT_JH, TT_DW}; +typedef struct +{ + rt_uint8_t cnt; + rt_uint8_t s; +} CFG; + +typedef struct +{ + rt_uint8_t sim; // SIM + rt_uint8_t xh; // 信号值 + rt_uint8_t rw; // 入网 + rt_uint8_t jh; // 激活 + char *jd; // 经度 + char *wd; // 纬度 + char *ele; // 高度 +} TT; +static TT tmp; +static TT *TTinfo=&tmp; +/** + * @description: 将TT结构体转换为数组 + * @param {TT} *TTinfo TT结构体 + * @param {rt_uint8_t} *buffer 存储转换结果 + * @return {*} buffer大小 + */ +rt_uint8_t info2HEX(TT *TTinfo, rt_uint8_t *buffer) +{ + // 功能:将tt数据转换为9字节HEX数据并返回,存在data + // 第1节为高3位分别为sim、rw和jh,其余位为xh,xh值若大于31则为31(31=b0001 1111) + // 第2-5字节为经度。114.410050= 0xF2 0xD1 0xE4 0x42 + // 第6-9字节为纬度。30.426840= 0x2B 0x6A 0xF3 0x41 + // 第10字节为高度,只保留整数。-31.5= 0x0A + rt_uint8_t tmp = (atoi(TTinfo->sim) << 7) + (atoi(TTinfo->rw) << 6) + (atoi(TTinfo->jh) << 5) + + (atoi(TTinfo->xh) > 31 ? 31 : atoi(TTinfo->xh)); // 位操作 + + char *jd_c = &TTinfo->jd[2]; // 从第二位开始取,去除非数字字符 + char *wd_c = &TTinfo->wd[2]; + float jd_f = strtod(jd_c, NULL); // 字符转浮点 + float wd_f = strtod(wd_c, NULL); + rt_int8_t ele_f = atoi(TTinfo->ele); + +// printf("jd--%f\nwd--%f\nele-- %d \n", jd_f, wd_f, ele_f); + + rt_uint8_t offset = 0; + memcpy(buffer + offset, &tmp, 1); + offset += 1; + memcpy(buffer + offset, &jd_f, sizeof(float)); + offset += sizeof(float); + memcpy(buffer + offset, &wd_f, sizeof(float)); + offset += sizeof(float); + memcpy(buffer + offset, &ele_f, sizeof(rt_int8_t)); + offset += sizeof(rt_int8_t); + + return offset; +} + + +//TT TTinfo; +//TTinfo = (TT*)malloc(sizeof(TT)); + +//TTinfo = &tmp; +/* 数据解析 */ +static void tt_parse(rt_uint8_t *data) +{ + extern TT *TTinfo; + cJSON *root = RT_NULL, *object = RT_NULL, *item = RT_NULL; +// rt_kprintf("%5s%5s%5s%5s%15s%15s%10s\n", TTinfo->sim, TTinfo->xh, TTinfo->rw, +// TTinfo->jh, TTinfo->jd, TTinfo->wd,TTinfo->ele); + + root = cJSON_Parse((const char *) data); + if (!root) + { + rt_kprintf("No memory for cJSON root!\n"); + return; + } + object = cJSON_GetObjectItem(root, "data"); + + item = cJSON_GetObjectItem(object, "tdsignal"); + if (item) + { + TTinfo->xh = atoi(item->valuestring); +// tmp.xh = item->valuestring; +// strcpy(TTinfo->xh,item->valuestring); +// rt_kprintf("\nxh:%s ", item->valuestring); +// rt_kprintf("\nxh:%s ", TTinfo->xh); + cJSON_Delete(root); + return RT_EOK; + } + + item = cJSON_GetObjectItem(object, "tdnetregstate"); + if (item) + { + TTinfo->rw = atoi(item->valuestring); +// rt_kprintf("\nrw:%s ", TTinfo.rw); + cJSON_Delete(root); + return RT_EOK; + } + + item = cJSON_GetObjectItem(object, "tdpsstate"); + if (item) + { + TTinfo->jh = atoi(item->valuestring); +// rt_kprintf("\njh:%s ", TTinfo.jh); + cJSON_Delete(root); + return RT_EOK; + } + + item = cJSON_GetObjectItem(object, "latitude"); + if (item) + { + TTinfo->wd = item->valuestring; + + item = cJSON_GetObjectItem(object, "longitude"); + TTinfo->jd = item->valuestring; + + item = cJSON_GetObjectItem(object, "elevation"); + TTinfo->ele = item->valuestring; +// rt_kprintf("%5s%5s%5s%5s%15s%15s%10s\n", TTinfo->sim, TTinfo->xh, TTinfo->rw, +// TTinfo->jh, TTinfo->jd, TTinfo->wd,TTinfo->ele); + + cJSON_Delete(root); + return RT_EOK; + } + + item = cJSON_GetObjectItem(object, "tdsimstate"); + if (item) + { + TTinfo->sim = atoi(item->valuestring); +// tmp.sim = item->valuestring; +// strcpy(TTinfo.sim,item->valuestring) +// rt_kprintf("\nSIM:%s \n", TTinfo->sim); + cJSON_Delete(root); + return RT_EOK; + } + + if (root != RT_NULL) + cJSON_Delete(root); +} + +/* HTTP client download data by GET request */ +static int webclient_get_data(const char *url) +{ + unsigned char *buffer = RT_NULL; + size_t length = 0; + + if (webclient_request(url, RT_NULL, RT_NULL, 0, (void **) &buffer, &length) < 0) + { + LOG_E("TT server is not ready."); + return -RT_ERROR; + } + + // LOG_D("webclient GET request response data :"); + // LOG_D("%s", buffer); + + tt_parse(buffer); + if (buffer) + { + web_free(buffer); + } + + return RT_EOK; +} + +void getTTinfo_thread_entry(void* parameter) +{ + + CFG* cfg = RT_NULL; + cfg = (CFG*) parameter; +// TT TTinfo; + + rt_uint8_t var = 0, isize = 0, i = 0; + isize = sizeof(infoH) / sizeof(infoH[0]); + rt_kprintf("\n%10s%s\n", " ", "↓---------getTT START.--------↓"); + rt_kprintf("%5s%5s%5s%5s%15s%15s%10s\n", "SIM", "xh", "rw", "jh", "N", "E", "ele"); + for (i = 0; i < cfg->cnt; ++i)//按指定次数轮询 + { + for (var = 0; var < isize; var++)//轮询每个参数 + { + char *url = RT_NULL; + url = web_strdup(*(infoH + var)); + if (url == RT_NULL) + { + rt_kprintf("no memory for create getTT url buffer.\n"); + return -RT_ENOMEM; + } + webclient_get_data(url); + web_free(url); + } +// rt_kprintf("%5d++%5d --%5d\n",TTinfo->xh,tmp.xh,TTinfo->sim); +// rt_kprintf("%5d%5d%5d%5d%15s%15s%10s\n", TTinfo.sim, TTinfo.xh, TTinfo.rw, TTinfo.jh, TTinfo.jd, TTinfo.wd,TTinfo.ele); + rt_kprintf("%5d%5d%5d%5d%15s%15s%10s\n", TTinfo->sim, TTinfo->xh, TTinfo->rw, + TTinfo->jh, TTinfo->jd, TTinfo->wd,TTinfo->ele); +if (i != cfg->cnt - 1)//最后一次采集不延时 + { + rt_thread_mdelay(cfg->s * 1000); + } + } + rt_kprintf("%10s%s\n", " ", "↑---------getTT DONE.---------↑"); +} + +void getTT(int argc, char **argv) +{ +// size_t cnt = 0, s=3;//次数、间隔 + static CFG cfg; + memset(&cfg, 0, sizeof(CFG)); + if (argc == 1) + { + cfg.cnt = 1; + cfg.s = 3; + } + else if (argc == 2) + { + cfg.cnt = atoi(argv[1]); + cfg.s = 3; + } + else if (argc == 3) + { + cfg.cnt = atoi(argv[1]); + cfg.s = atoi(argv[2]); + } + +// LOG_D("cnt-%d",cfg.cnt); +// LOG_D("s-%d",cfg.s); + + /* 创建 serial 线程 */ + rt_thread_t thread = rt_thread_create("getTT", getTTinfo_thread_entry, (void *) &cfg, 1024 * 3, 25, 10); + /* 创建成功则启动线程 */ + if (thread != RT_NULL) + { + rt_thread_startup(thread); +// rt_kprintf("done"); + } + else + { + LOG_E("thread 'getTT' create failure."); + return RT_ERROR; + } + +} + +/* 导出到自动初始化 */ +MSH_CMD_EXPORT(getTT, 获取天通信息,支持参数。 "getTT 3 1" means try 3 times with 1 second interval.); +//MSH_CMD_EXPORT_ALIAS(webclient_get_data, gTTinfo, GET info of TT server.); diff --git a/applications/iwdg_sample.c b/applications/iwdg_sample.c index bee2158..e92292d 100644 --- a/applications/iwdg_sample.c +++ b/applications/iwdg_sample.c @@ -78,5 +78,6 @@ static int iwdg_sample(int argc, char *argv[]) return ret; } /* 导出到 msh 命令列表中 */ -MSH_CMD_EXPORT(iwdg_sample, iwdg sample); +//MSH_CMD_EXPORT(iwdg_sample, iwdg sample); +/* 导出到自动初始化 */ INIT_DEVICE_EXPORT(iwdg_sample); diff --git a/applications/led_blink_sample.c b/applications/led_blink_sample.c index 2d57bff..483b6c5 100644 --- a/applications/led_blink_sample.c +++ b/applications/led_blink_sample.c @@ -81,7 +81,7 @@ int led_sample(int argc, char *argv[]) return ret; } /* 导出到 msh 命令列表中 */ -MSH_CMD_EXPORT(led_sample, led sample); +//MSH_CMD_EXPORT(led_sample, led sample); /* 导出到自动初始化 */ //INIT_COMPONENT_EXPORT(led_sample); diff --git a/applications/main.c b/applications/main.c index f8be1ef..def418c 100644 --- a/applications/main.c +++ b/applications/main.c @@ -36,7 +36,7 @@ int main(void) rt_pin_mode(LED_HEART, PIN_MODE_OUTPUT); rt_pin_mode(ETH_RESET_PIN, PIN_MODE_OUTPUT); - rt_pin_write(ETH_RESET_PIN, PIN_LOW);//关闭ETH +// rt_pin_write(ETH_RESET_PIN, PIN_LOW);//关闭ETH while (1) diff --git a/rtconfig.h b/rtconfig.h index be07d05..3ccaf80 100644 --- a/rtconfig.h +++ b/rtconfig.h @@ -71,7 +71,7 @@ #define FINSH_THREAD_PRIORITY 20 #define FINSH_THREAD_STACK_SIZE 4096 #define FINSH_USING_HISTORY -#define FINSH_HISTORY_LINES 5 +#define FINSH_HISTORY_LINES 10 #define FINSH_USING_SYMTAB #define FINSH_CMD_SIZE 80 #define MSH_USING_BUILT_IN_COMMANDS @@ -141,10 +141,17 @@ /* Network */ +#define RT_USING_NETDEV +#define NETDEV_USING_IFCONFIG +#define NETDEV_USING_PING +#define NETDEV_IPV4 1 +#define NETDEV_IPV6 0 #define RT_USING_LWIP #define RT_USING_LWIP212 #define RT_USING_LWIP_VER_NUM 0x20102 #define RT_LWIP_MEM_ALIGNMENT 4 +#define RT_LWIP_ICMP +#define RT_LWIP_DNS /* Static IPv4 Address */ @@ -152,7 +159,9 @@ #define RT_LWIP_GWADDR "192.168.0.1" #define RT_LWIP_MSKADDR "255.255.255.0" /* end of Static IPv4 Address */ +#define RT_LWIP_UDP #define RT_LWIP_TCP +#define RT_LWIP_RAW #define RT_MEMP_NUM_NETCONN 4 #define RT_LWIP_PBUF_NUM 4 #define RT_LWIP_RAW_PCB_NUM 4 @@ -175,6 +184,7 @@ #define LWIP_SO_RCVBUF 1 #define LWIP_SO_LINGER 0 #define LWIP_NETIF_LOOPBACK 0 +#define RT_LWIP_USING_PING /* end of Network */ /* Utilities */ @@ -203,6 +213,10 @@ /* IoT - internet of things */ +#define PKG_USING_WEBCLIENT +#define WEBCLIENT_NOT_USE_TLS +#define PKG_USING_WEBCLIENT_V220 +#define PKG_WEBCLIENT_VER_NUM 0x20200 /* Wi-Fi */ @@ -228,6 +242,8 @@ /* JSON: JavaScript Object Notation, a lightweight data-interchange format */ +#define PKG_USING_CJSON +#define PKG_USING_CJSON_LATEST_VERSION /* end of JSON: JavaScript Object Notation, a lightweight data-interchange format */ /* XML: Extensible Markup Language */