I am currently working with Arduino trying to create an ad hoc network to which the device can connect and send web requests. The problem I'm currently facing is that I can only establish one connection, and then when this connection is completed (with client.stop() ), all subsequent connections will not be received by the server, even cURL is just sitting there. The first connection that I start when I reset the server is working fine, and I can talk to the server; but after that Arduino can no longer find new clients (even if it tries with the provided library).
I use the SparkFun library for a WiFly screen cloned from GitHub , as well as Arduino Uno .
My current code is based on the default example “WiFly_AdHoc_Example”, but I had to delete a few things in order to start the network, which could be the cause of this problem.
Here is the file . ino , which I run.
#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(); } }
This script is just a mini-protocol that I installed for testing. After connecting to the willy module, you can send text, for example, "get", "hello" or "quit", and the module should come back.
Using Telnet I can successfully connect (the first time) and send Arduino commands, including "quit" to terminate the connection (calls the client.stop() method). But when I try to connect again, Telnet , it says that the connection was successful, but on Arduino it still loops, thinking that the client is still false. What??
I know correctly, I get mixed messages from Telnet vs Arduino . None of the commands work, as Ardunio continues the loop, expecting the client to evaluate to true. I'm going to take a look at WiFlyServer from the library that I imported and see if I can solve the problem, because somehow this server.available () method does not find new clients .
I notice a lot of TODO in library code ....
So, I found the cause of the problem. It was in the WiFlyServer.cpp file from the SparkFun library . The code causing the reconnection problem was actually the server.availible() method. At the top of the method there is a check:
For some reason , when I comment on this, I can connect and reconnect completely fine, and everything works as it should . Now I will dive into the library and see if I can fix it, I'm not quite sure what it does, but it is called when the connection to the server is inactive and somehow blocks subsequent connections. The problem with this solution is that Arduino always believes that it found a client, because client and client.connected() evaluated as true, even if one does not exist. Even client.available() evaluates to true when the connection ends and the ghost client appears, but after that the if-statement is first executed, the client client is no longer available() . Even with this flaw, he still raises a new customer when he arrives, and why he works.
How can I get to the root of this problem without using this commenting hack?
Do they have any risks or future problems that I may have encountered in this?
What is the purpose of the block that I commented on first?