将API链接消息解析为C中的服务器(Arduino IDE) [英] parse an API link message as a server in C (Arduino IDE)

查看:121
本文介绍了将API链接消息解析为C中的服务器(Arduino IDE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Arduino IDE对具有内置Wi-Fi芯片(ESP8266 NodeMCU)的微控制器进行编程,它连接到我的互联网路由器,然后具有特定的IP(如192.168.1.5)./p>

所以我想通过添加到链接的消息发送命令(和数据),然后链接变为:192.168.1.5/?A=data1&B=data2.

从LAN内的设备启动上面的链接时,我可以在String变量中获取消息,这里有一条包含"?A=data1&B=data2"的消息.

所以问题是:如何在单独的变量中获取AB内容?

第二个更简单的问题:如何将内容转换为布尔值,intfloat变量?

解决方案

算法看起来像这样.此示例仅打印令牌,但是您应该能够对其进行修改以处理键,值和异常情况.

#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define MESSAGE_TOKENS ("=&?")

int main()
{
    char *msg = "?A=data1&B=data2";
    char *msg_dup = strdup(msg);
    char *tok = strtok(msg_dup, MESSAGE_TOKENS);

    while (tok != NULL)
    {
        char delim = msg[tok - msg_dup - 1];

        switch(delim)
        {
            case '?':
            case '&':
                printf("key=%s\n", tok);
                break;
            case '=':
                printf("val=%s\n", tok);
                break;
            default:
                break;
        }

        tok = strtok(NULL, MESSAGE_TOKENS);
    }

    free(msg_dup);
}

对于数据类型,可以使用ctype.h头文件的方法(.

So the question is: How I can obtain A and B contents at separate variables?

Second easier question: How to convert contents to a Boolean, int or float variables?

解决方案

The algorithm would look like this. This example only print the tokens, but you should be able to modify it to handle the keys, the values and the exception cases.

#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define MESSAGE_TOKENS ("=&?")

int main()
{
    char *msg = "?A=data1&B=data2";
    char *msg_dup = strdup(msg);
    char *tok = strtok(msg_dup, MESSAGE_TOKENS);

    while (tok != NULL)
    {
        char delim = msg[tok - msg_dup - 1];

        switch(delim)
        {
            case '?':
            case '&':
                printf("key=%s\n", tok);
                break;
            case '=':
                printf("val=%s\n", tok);
                break;
            default:
                break;
        }

        tok = strtok(NULL, MESSAGE_TOKENS);
    }

    free(msg_dup);
}

As for data types, you can use methods of the ctype.h header file (link). For example, you can verify if a string is a number by iterating through all characters of the string and verifying that all chars are numbers (the isnumber() method).

这篇关于将API链接消息解析为C中的服务器(Arduino IDE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆