How to detect new WifiClient?

Hi guys,

I'm trying to use my arduino uno with WifiShield to connect a webserver and to be connected by clients, that is, some clients send some data to arduino's webserver and arduino sends some data to a webserver.

Now, when I tried it for the first time, it worked perfect. Yet then, I realised that when I send the same form from the same device to arduino it gets nothing. It's a simple circuit on which I want to make a LED on if the one who is trying to do this is in my database, namely s/he is eligible to do so. So, if I click to the link to send an ID from a device to arduino, arduino can read (or get) the message but when the same device with same ID or another device with a different ID tries to send their ID, what ardunio gets is nothing. Henceforth, how can I make my arduino to serve more than one request?

ps: since the computer on which I code my programs has no wireless connection or any usb ports I couldn't attach the code, sorry :confused:

I couldn't attach the code

But, you still want us to tell you what is wrong with it.

sorry

Agreed.

I'm asking for you to tell me how.

The WiFi library comes with client and server examples. They show you how.

Now, when I tried it for the first time, it worked perfect. ... Henceforth, how can I make my arduino to serve more than one request?

Sure sounds to me like you want help with some software that you have already written. If not, then forget I said anything.

First, I'm not asking for you to tell what is wrong, I'm asking for you to tell me how. I suppose it's a huge difference. Whatsoever.

Let me write pseudocode-like then.

void setup() {
....
id="";
server.begin();
}
void loop() {
while(1) {
if(LEDon && millis()-lastOn > interval ) {
make led off;
LEDon=false;
}
if(id==""&& !LEDon) {
call respond2httpRequest(); //as server to the device
}

}
if(id!="") {
call makeHttpRequest2Server();
while(server.available()) {
read char c from server;
if(c==1) {
make led on;
LEDon=true;
id="";
}
if(c==0) {
make led off;
LEDon=false;
id="";
}
}
}
}
//I don't think the following function is necessary
makeHttpRequest2Server() {
...
}

respond2httpRequest() {
id="";
WifiClient client = server.available();
if(client){
if(client.available()) {
while(client.connected) {
respond;
set id;
}
}
}
client.stop();
client.flush();
}

If you don't want to know what is wrong and just how to do it, then you apparently already know. Here it is:

  WifiClient client = server.available();
  if(client){

if that's the way then now I'm asking what is wrong. wow how cool you are, congratz!

I can't tell what is wrong with your code if you post pseudo-code. There is a bunch of things wrong with it.

I have an example for the ethernet shield in the playground.
http://playground.arduino.cc/Code/WebServerST

I use the same code changed only slightly for the WiFi shield, and it works. The WiFi server library has some pretty serious bugs, so be forewarned. It uses only one socket, so multiple client requests performed simultaneously can cause the incorrect or corrupted files to be returned by the server.

I'm gonna write the code here. So, forget the pseudo-code please.

It's assumed that there will be no simultaneous requests. The problem is I cannot make the same request more than once or cannot make another request from another device.

The problem with the code I haven't shown you is I cannot make the same request more than once or cannot make another request from another device.

There, I fixed that for you.

I'd choose that way:

PaulS:

The problem with the code I could have shown you, that I couldn't yet is I cannot make the same request more than once or cannot make another request from another device.

I'd choose that way

Fair enough. But, now we are back to reply #1, where I suggested that you are asking for help with code you haven't posted, which you got all snippy about.

Hello again. Here is the code. Sorry for not being able to send it in the original post.

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "";     
char pass[] = "";   
int keyIndex = 0;            

int status = WL_IDLE_STATUS;
int lock = 9;
int count=0;

WiFiClient LServer;
WiFiServer serverG(80);

IPAddress server(192,168,169,101);

unsigned long lastOn = 0;
const unsigned long postingInterval = 16L * 1000L; // delay between updates, in milliseconds
String Username = "";
String password = "";
boolean trying2connect=false;
boolean LEDOn=false;

void setup() {
  pinMode(lock,OUTPUT);
  digitalWrite(lock,HIGH);
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);


    delay(10000);
  }
  printWifiStatus();
  serverG.begin();

}

void loop() {
  while(1) {
    if (LEDOn && (millis() - lastOn > postingInterval)) {
      digitalWrite(9, HIGH);
      Serial.println("LED is closed");
      LEDOn=false;
    }
    if (Username == "" && password == "" && !LEDOn){
      httpResponse();
      break;
    } 
  }
  if (Username != "" && password != ""){ 
    httpRequest();
    while(trying2connect) {
      delay(2000);
      httpRequest();     
    }
    while (LServer.available()) {
      char c = LServer.read();
      if (c == '^'){
        digitalWrite(9, LOW); 
        lastOn=millis();
        LEDOn=true;
        Serial.println("LED is on");
        delay(8000);
        break;
      }
      else if (c == '?'){
        digitalWrite(9, HIGH); //LED off
        Serial.println("Thou shall not pass!");
        break;
      }
    }
  }
}

// this method makes a HTTP connection to the server:
void httpRequest() {
  LServer.stop();
  String serverlink =""; 
  if (LServer.connect(server, 80)) {
    trying2connect=false;
    Serial.println("connecting to LocKEY server...");
    Serial.println(Username);
    
    LServer.print("GET /index.php?Usernamesent=");
    LServer.print(Username);
    LServer.print("&passwordsent=");
    LServer.print(password);
    LServer.println(" HTTP/1.1");
    LServer.println("Host: www.arduino.cc");
    LServer.println("User-Agent: ArduinoWiFi/1.1");
    LServer.println("Connection: close");
    LServer.println();

  }
  else {
    trying2connect=true;
    Serial.println("connection failed");
  }
}

void httpResponse() {
  WiFiClient clientPhone = serverG.available();
  if (clientPhone) {
    Serial.println("new client");           
    String currentLine = "";
    boolean endofuser = false;
    boolean endofpass = false;
    Username = "";
    password = "";
    while (clientPhone.connected()) {           
      if (clientPhone.available()>0) {           
        char cPhone = clientPhone.read();        
        if (endofuser) {
          if (cPhone != '&' ){
            Username += cPhone;
          }
          else {
            endofuser = false;
          }
        }
        if (cPhone == '@'){
          endofuser = true;       
        }
        if (endofpass) {
          if (cPhone != '?' ){
            password += cPhone;
          }
          else {
            endofpass = false;
          }
        }
        if (cPhone == '

){
          endofpass = true;     
        }     
        if (cPhone == '\n') {               
          if (currentLine.length() == 0) { 
            clientPhone.println("HTTP/1.1 200 OK");
            clientPhone.println("Content-type:text/html");
            clientPhone.println("Connection: close");
            clientPhone.println();

clientPhone.println("");
            clientPhone.println("<html xmlns="http://www.w3.org/1999/xhtml\">");

clientPhone.print("Welcome ");
            clientPhone.print(Username);
            clientPhone.println("! Checking your password \n ");
            clientPhone.println(" Click ");
            clientPhone.println("");
            clientPhone.println("");
            clientPhone.println();
            break;       
          }
          else {   
            currentLine = "";
          }
        }   
        else if (cPhone != '\r') {   
          currentLine += cPhone; 
        }

}
    }

Serial.print("Username= ");
    Serial.println(Username);
    Serial.print("password= ");
    Serial.println(password);
    Serial.println(" have been received");
    clientPhone.stop();
    clientPhone.flush();
    Username = "";
    password = "";
    Serial.println("Client disconnected");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

// print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void loop() {
  while(1) {

Whenever I see someone running an infinite loop inside an infinite loop(), I quit reading the code.

Good luck with your problem.

If you have read the rest, you could probably notice that the while-loop is not used instead of loop(). I just wanted to limit loop(). Your attitude is totally offending. You may assume that there exists no other way than yours, yet that is highly probable to end up with being a loser that doesn't even know that s/he is loser.

Whatsoever, any help would be apperiated except PaulS's. And apparently he is one of the big boys here, let's see if there will be any attempt to help.

Am I correct that you are trying to implement a server and a client in that code? Where in the code is it failing? Does it fail (freeze) or just keep going and fails to detect new client requests?

I used this code to troubleshoot my code. It shows the status of the sockets on the wifi shield. Maybe this will help you.

#include <utility/server_drv.h>

void ShowSockStatus() {
  for(int x = 0; x < MAX_SOCK_NUM; x++) {
    Serial.print(WiFi._state[x]);    
    Serial.print("  ");
    Serial.print(WiFi._server_port[x]);    
    Serial.print("  s=");
    Serial.print(serverDrv.getServerState(x));    
    Serial.print("  c=");
    Serial.print(serverDrv.getClientState(x));    
    Serial.print("  d=");
    Serial.println(serverDrv.availData(x));    
 }
}

ps: PaulS is helping you indirectly here. He loaned me his WiFi shield to troubleshoot the firmware. Thanks again, PaulS! :slight_smile:

Yes, you are right, it gets an HTTP request for password from a client as a server and sends an HTTP request for password again to a server as a client. Arduino compares them and then sends a signal according to the result of the comparison. The problem is it keeps going and fails to detect new client. For the first time it works fine, for other requests it prints empty string instead of Username and password on Serial.

PS: Perhaps, I misunderstood him. If so, apologies PaulS. I just felt offended, but if what you intended to do is not being offensive forgive me for being rude .

PaulS can be a bit blunt to put it mildly, but he does know what he is talking about. You are not the first to notice by a long shot.

Add the function I posted earlier, and call it just before you call the server.available() function. See if the server socket is still listening.

s is initially 1, after the first form is submitted, it becomes 196 and after a while becomes 166 and stays so; and d is initially 0, after the first form is submitted becomes 1 and stays the same.

I suppose it stops listening. So, what is the problem, I couldn't get it.

is there any way to make arduino to respond requests on one socket and send requests on another?