Go Down

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

Ahmed-Mansour

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() {

  Serial.begin(19200);
  Serial2.begin(9600);   
  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:
    delay(10000);
  }
  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();
    //Serial.setTimeout(.001);
  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");
    //Serial.println();
  }
 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();

    //Serial.print(c);
    ////Serial.println(b,DEC);
  }

  //Serial.println();
  //Serial.println("disconnecting.");
  //Serial.println("==================");
  //Serial.println();
 client_IP.flush();
 client_IP.stop();
 
 
  }
   
   
 }
  client_IP.flush();
  WiFiClient client = server.available();   // listen for incoming clients
  test_flag=test_flag+1;
  ////Serial.println(test_flag,DEC);
   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");
             client.println("Content-type:text/html");
            client.println();

            // 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:
            client.println();
     
            // break out of the while loop:
            break;         
          }
          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")) {


         Serial2.write("S");
         delay(400);
         Serial2.end();
         Serial2.begin(9600);
           
         
        }

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


         Serial2.write("C");
         delay(400);
         Serial2.end();
         Serial2.begin(9600);
         
        }

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


         Serial2.write("A");
         delay(400);
         Serial2.end();
         Serial2.begin(9600);
         
                 
        }

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

         Serial2.write("B");
         delay(400);
         Serial2.end();
         Serial2.begin(9600);
     
        }

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

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");
  // print where to go in a browser:
  //Serial.print("To see this page in action, open a browser to http://");
 Serial.println(ip);
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy