WiFi Server Available Issues - Any experts?

Hey everyone,

I am in the process of building a home automation project for my final semester at my university. The last piece I am working on is a server on my Arduino Mega using my WiFi shield that will receive commands coming from a windows application I have developed. So far I have managed to successfully connect via TCP and send a message when I did an example project. When I put that code into my main project it fails to read the message but successfully connects.

Below is the example code.

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

//WiFiUDP Udp;
IPAddress ip(***, ***, ***, ***); //Arduino IP address set to be static
IPAddress dns(***, ***, ***, ***);
IPAddress gateway(***, ***, ***, ***);
IPAddress subnet(***, ***, ***, ***);

//Wifi variable declarations
char ssid[] = "network";     //  your network SSID (name)
char pass[] = "password";  // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

WiFiServer server(31000);

void setup() {
  Serial.begin(9600);      // initialize serial communication

  // 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);
  }
  Serial.println("Successfully connected...");
  server.begin();                           // start the web server on port 80
}


void loop() {
  WiFiClient tcpClient = server.available();   // listen for incoming clients
  char receiveBuffer[25];
  int charIndex = 0;
  //Serial.println("Checking for messages");
  if (tcpClient) {                             // if you get a client
    while (tcpClient.available()) {             // if there's bytes to read from the client,
      char c = tcpClient.read();             // read a byte, then
      if (int(c) >= 32 && charIndex != 24)
      {
        //Serial.write(c);                    // print it out the serial monitor
        receiveBuffer[charIndex] = c;
        charIndex++;
      }
    }
    if (charIndex > 0)
    {
      Serial.println(String(receiveBuffer));
    }
    charIndex = 0;

    // close the connection:
    tcpClient.stop();
    Serial.println("Client disconnected");
  }
}

As you can see the code is the same I just added it to a custom function and I call that every iteration.

void setup() {
  //Setup and initialize RTC
  Wire.begin();
  RTC.begin();

  //Initialize serial and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  //Initialize SD card
  if (!SD.begin(4)) {
    Serial.println("Initialization failed!");
    return;
  }
  Serial.println("Initialization done.");

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

  WiFi.config(ip, dns, gateway, subnet);

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.println("You're connected to the network");

  //Determine current hour;
  DateTime now = RTC.now();
  lastHour = now.hour();
  currentHour = 0;

  //Set the flags for first iteration and update successful
  firstIteration = true;
  updatedFlag = false;

  //Serial.println(currentHour);

  //Udp.begin(localUdpPort);
  server.begin();
  pinMode(53, OUTPUT);
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  delay(2000);
}

void loop()
{
  //Find currentHour every iteration of loop
  DateTime now = RTC.now();
  int currentHour = now.hour();
  //Serial.println(currentHour);

  if ((lastHour != currentHour) || (firstIteration) || (!updatedFlag))
  {
    Serial.println("Current hour: " + String(currentHour));
    Serial.println("Last Hour: " + String(lastHour));

    if (wifiClient.connect(php_server, 80))
    {
      Serial.println("Connection Successful: Processing Data");
      //processData();

      //if ((lastHour != currentHour) || (firstIteration))
      //{
      //  Serial.println("Getting current reading...");
      //  String currentDataReading = getCurrentDateTimeReading();
      //  currentDataReading += ';';
      //  uploadData(currentDataReading);
      //  lastHour = currentHour;
      //  firstIteration = false;
      //}
    }
    else if (updatedFlag)
    {
      Serial.println("Connection unsuccessful!");
      //String currentDataReading = getCurrentDateTimeReading();
      //dataLogger(currentDataReading);
    }

    if (wifiClient.connected())
    {
      Serial.println("Client stopped");
      wifiClient.stop();
    }
  }
  receiveTcp();
}

void receiveTcp()
{
  //Serial.println("Checking for tcp message");
  WiFiClient tcpClient = server.available();
  char receiveBuffer[25];
  int charIndex = 0;
  //Serial.print("Status: ");
  //Serial.println(tcpClient);

  if (tcpClient) {                           
    //Serial.println("Connected");
    while (tcpClient.available()) {             
      char c = tcpClient.read();             
      if (int(c) >= 32 && charIndex != 24)
      {
        //Serial.write(c);                    
        receiveBuffer[charIndex] = c;
        charIndex++;
      }
    }
    if (charIndex > 0)
    {
      Serial.println(String(receiveBuffer));
      tcpClient.stop();
    }
    charIndex = 0;
  }
    // close the connection:
    tcpClient.stop();
    Serial.println("Client disconnected");
  }
}

Any idea why it isn’t working? I read in some cases the arduino isn’t collecting the data because it notices the connection attempt but the data hasn’t actually been sent yet. Any ideas? Thanks!

The example doesn't include wifi config for some reason. I'm surprise it works.

The problem is likely the WiFi shield. Read this or load the simple wifi server code that comes pre loaded, once you call the server once the arduino will run for 6 - 7 minutes then it will freeze and become unresponsive. The freeRam says there is plenty of memory, and there is no reason to suspect that heap fragmentation has anything to do with the problem. As you can see from responses from others, the wifi shield has a lot of issues.

it notices the connection attempt but the data hasn't actually been sent yet. Any ideas?

if something else connects to the same port if (wifiClient.connect(php_server, 80)), it will read it as a connection to the device, even if it isn't connected to the php_sever address. I don't know why, but i do know for a fact it happens.