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

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

问题描述

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

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

当上面的链接从局域网内的设备启动时,我可以在字符串变量中获取消息,这里我现在有一个包含 "?A=data1&B=data2" 的消息.

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

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

解决方案

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

#include #include #include #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];开关(分隔){案件 '?':案例&":printf("key=%s\n", tok);休息;案例=":printf("val=%s\n", tok);休息;默认:休息;}tok = strtok(NULL, MESSAGE_TOKENS);}免费(msg_dup);}

对于数据类型,您可以使用ctype.h 头文件的方法(链接).例如,您可以通过遍历字符串的所有字符并验证所有字符是否为数字来验证字符串是否为数字(isnumber() 方法).

I am using Arduino IDE to program my micro-controller which has a built-in Wi-Fi chip (ESP8266 NodeMCU) , it connects to my internet router and then has a specific IP (as like 192.168.1.5).

So I want to send commands (and data) by a message which added to the link, then the link becomes as : 192.168.1.5/?A=data1&B=data2.

When link above is launched from a device within LAN, then I can get the message in a String variable, here I have now a message which contains "?A=data1&B=data2".

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天全站免登陆