Arduino WiFly - 临时网络设置

met*_*Man 5 arduino

我目前正在与 Arduino 合作,尝试构建一个临时网络,设备可以连接到该网络并向其发送 Web 请求。我目前遇到的问题是我只能建立一个连接,然后当该连接终止时(使用client.stop()),服务器不会接收所有后续连接,即使cURL命令只是坐在那里旋转。我在重置服务器时启动的第一个连接工作正常,并且我能够与服务器通信;但在那之后,Arduino 无法再找到新客户(即使它正在尝试使用给定的库)。

我正在将 SparkFun 库用于从 GitHub 克隆的 WiFly 盾牌,以及一个Arduino Uno

我当前的代码基于他们的默认示例“WiFly_AdHoc_Example”,但我必须删除一些内容才能启动网络,这可能是导致此问题的原因。

这是我正在运行的.ino文件。

#include <SPI.h>
#include <WiFly.h>
//#include <SoftwareSerial.h>

//SoftwareSerial mySerial( 5, 4); //Part from example not used (see below)

WiFlyServer server(80); //Use telnet port instead, if debugging with telnet

void setup() 
{
    Serial.begin(9600);

    //The code below is from the example, but when I run it the WiFly will hang
    // on Wifly.begin(). Without it, the WiFly starts up fine.
    //mySerial.begin(9600);
    //WiFly.setUart(&mySerial); // Tell the WiFly library that we are not 
                                // using the SPIUart

    Serial.println("**************Starting WiFly**************");
    // Enable Adhoc mod
    WiFly.begin(true);
    Serial.println("WiFly started, creating network.");

    if (!WiFly.createAdHocNetwork("wifly")) 
    {
        Serial.print("Failed to create ad hoc network.");
        while (1) 
        {
            // Hang on failure.
        }
    }
    Serial.println("Network created");
    Serial.print("IP: ");
    Serial.println(WiFly.ip());

    Serial.println("Starting Server...");
    server.begin();
    Serial.print("Server started, waiting for client.");
}

void loop() 
{
    delay(200);
    WiFlyClient client = server.available();
    if (client) 
    {
        Serial.println("Client Found.");

        // A string to store received commands
        String current_command = "";

        while (client.connected()) 
        {
            if (client.available()) 
            {
                //Gets a character from the sent request.
                char c = client.read();
                if (c=='#' || c=='\n') //End of extraneous output
                {
                    current_command = "";
                }
                else if(c!= '\n')
                {
                    current_command+=c;
                }

                if (current_command== "get") 
                {
                    // output the value of each analog input pin
                    for (int i = 0; i < 6; i++) 
                    {
                        client.print("analog input ");
                        client.print(i);
                        client.print(" is ");
                        client.print(analogRead(i));
                        client.println("<br />");
                    }     
                }
                else if(current_command== "hello")
                {
                    client.println("Hello there, I'm still here.");
                }
                else if (current_command== "quit")
                {
                    client.println("Goodbye...");
                    client.stop();
                    current_command == "";
                    break;
                }
                else if (current_command == "*OPEN*")
                {
                    current_command == "";
                }
            }
        }
        // Give the web browser time to receive the data
        delay(200);
        // close the connection
        client.stop();
    }
}
Run Code Online (Sandbox Code Playgroud)

这个脚本只是我设置用来测试的一个迷你协议。与 wifly 模块连接后,您可以发送诸如“get”、“hello”或“quit”之类的文本,而 wifly 模块应该会回复。

使用Telnet我可以成功连接(第一次)并向Arduino发送命令,包括“退出”以终止连接(调用client.stop()方法)。但是当我尝试通过Telnet重新连接时,它说连接成功,但在Arduino 上它仍然循环认为客户端仍然是错误的。什么??

我知道对,我收到了来自TelnetArduino 的混合消息。这些命令都没有明显的作用,因为Ardunio仍在循环等待评估为 true 的客户端。我将从我导入的库中查看WiFlyServer,看看我是否可以挖掘问题,因为不知何故server.available() 方法没有找到新的客户端

我注意到库代码中有很多 TODO ......


于是我找到了问题的原因。它位于SparkFun 库WiFlyServer.cpp文件中。导致重新连接问题的代码实际上是方法。在方法的顶部,有一个检查:server.availible()

// TODO: Ensure no active non-server client connection.

if (!WiFly.serverConnectionActive) {
    activeClient._port = 0;
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当我对此进行评论时,我可以完美地连接和重新连接,并且一切正常。我现在将深入库,看看我是否可以解决这个问题,我不确定这是做什么的,但是当服务器连接不活动并且以某种方式阻止后续连接时它会被调用。这个解决方案的问题是Arduino总是认为它已经找到了一个客户clientclient.connected()即使一个客户不存在也评估为真。client.available()当连接终止并找到幽灵“客户端”时,甚至评估为 true,但在第一次运行 if 语句之后,幽灵“客户端”不再是available(). 即使有这个缺陷,当它出现时它仍然会选择一个新客户,这就是它起作用的原因。

  • 如果不使用这个评论技巧,我如何才能找到这个问题的根源?

  • 他们这样做有什么风险或未来的问题吗?

  • 我首先注释掉的块的目的是什么?

fuz*_*uzz 0

那么,当您调用时,client.stop();Arduino 如何知道客户端是否必须重新启动?

记住setup()只执行一次。

您是否尝试过在循环中包含以下代码来告诉 Arduino 再次创建 WiFly AdHoc 网络?这可能有效,也可能无效。我自己没有,也没有玩过 Wifly 盾牌,但值得一试。

请记住,每次需要再次连接时只执行一次代码,因为它位于始终运行的循环内。

WiFly.begin(true);
Serial.println("WiFly started, creating network.");

if (!WiFly.createAdHocNetwork("wifly")) 
{
    Serial.print("Failed to create ad hoc network.");
    while (1) 
    {
        // Hang on failure.
    }
}
Run Code Online (Sandbox Code Playgroud)