Go Down

Topic: Help , Wi-Fi Shield - Sending data Randomly ! (Read 1 time) previous topic - next topic


I have problem with my system , i'm working on a Home Automation system, I'm using arduino wi-fi shield as a receiver that receive orders from any mobile device (smart phone) , and i put a X-Bee as a sender that send orders to other receiving circuits around the home.

the code is working very good with one smart phone , but the problem is when i send two orders from two different phones in the same moment , the arduino is coming confused and send data randomly

for example ;

i'm using one button to send code (S)
and another button to send code (A)

if i pressed them in the same moment , the one should send (A) now is sending (S) and  the one should send (S) now is sending (A).

so, is there any method to clear that register that storing data inside.
or any way to avoid that happening

please help :(  :(  :(

this is my code ;

#include <SPI.h>
#include <WiFi.h>
#define SEND_NON_BLOCKING 1       
 //   #include <SoftwareSerial.h>
//    SoftwareSerial xbee(2,3 ); // RX, TX

//IPAddress ip(192, 168, 0, 177); 

char ssid[] = "My Home";      //  your network SSID (name)
char pass[] = "123456789";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
char server_IP[] = "www.google.com";
unsigned long test_flag=0;
int i=0;
int status = WL_IDLE_STATUS;
WiFiClient client_IP;
WiFiServer server(80);

void setup() {

  pinMode(8, OUTPUT);     

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    //Serial.println("WiFi shield not present");
    while(true);        // don't continue

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    ////Serial.print("Attempting to connect to Network named: ");
    //Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:   
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status

void loop() {
  if(Serial.available() > 0){
    byte inChar;
   inChar = Serial.read();
  if(inChar == 'a'){
    //Serial.println("\nStarting connection to dnsdynamic...");
  // if you get a connection, report back via serial:
if (client_IP.connect(server_IP, 80)) {
    //Serial.println("connected to server");
  //  client_IP.println("GET / HTTP/1.1");
   // client_IP.println("Host:www.google.com");
   // client_IP.println("Connection: close");
   // client_IP.println();
 else {
    //Serial.println("connection failed");
 int b = client_IP.available();
    while(client_IP.connected() && !client_IP.available()) delay(1); //waits for data
  while (client_IP.connected() || client_IP.available()) { //connected or data available
    char c = client_IP.read();


  WiFiClient client = server.available();   // listen for incoming clients
   if (client) {                             // if you get a client,
    //Serial.println("new client");           // print a message out the serial port
   String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,

        char c = client.read();             // read a byte, then
        //Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) { 
             //HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:   
            client.println("HTTP/1.1 200 OK");

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/Smart\">here</a> to chose the Smart control<br>");
            client.print("Click <a href=\"/Classic\">here</a> to chose the Classic control<br>");
            client.print("Click <a href=\"/ON\">here</a> to turn Light ON<br>");
            client.print("Click <a href=\"/OFF\">here</a> to turn Light OFF<br>");

            // The HTTP response ends with another blank line:
            // break out of the while loop:
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";

        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        // Circuit No.1  - Living Room
        if (currentLine.endsWith("GET /Smart")) {


        if (currentLine.endsWith("GET /Classic")) {


        if (currentLine.endsWith("GET /ON")) {


        if (currentLine.endsWith("GET /OFF")) {


    // close the connection:
    //Serial.println("client disonnected");

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

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

  // print the received signal strength:
 // long rssi = WiFi.RSSI();
  //Serial.print("signal strength (RSSI):");
  //Serial.println(" dBm");
  // print where to go in a browser:
  //Serial.print("To see this page in action, open a browser to http://");

Go Up