Arduino WiFly - ad hoc网络设置

时间:2012-09-27 18:25:08

标签: arduino

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

我正在使用SparkFun库作为WiFly屏蔽cloned from GitHub以及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();
    }
}

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

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

我知道,我收到来自TelnetArduino的混合消息。由于Ardunio仍然在循环等待评估为true的客户端,所以这些命令都不起作用。我将查看WiFlyServer from the library I imported并查看是否可以解决问题,因为 server.available()方法无法找到新客户端

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


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

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

if (!WiFly.serverConnectionActive) {
    activeClient._port = 0;
}

出于某种原因当我发表评论时,我可以完全连接并重新连接,并且一切正常,。我现在将深入到库中,看看我是否可以解决这个问题,我不确定这是做什么的,但是当服务器连接不活动并且以某种方式阻止后续连接时它会被调用。此解决方案的问题在于,Arduino始终认为已找到客户端,因为clientclient.connected()评估为true,即使其中不存在。当连接终止并找到ghost“client”时,偶数client.available()评估为true,但在第一次运行if语句之后,ghost“client”不再是available()。即使有这个缺陷,它仍然会在它出现时接收一个新的客户端,这就是它工作的原因。

  • 如果不使用此评论黑客,我如何找到此问题的根源?

  • 我可能会遇到任何风险或未来的问题吗?

  • 我首先评论过该区块的目的是什么?

1 个答案:

答案 0 :(得分: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.
    }
}
相关问题