Pages: [1]   Go Down
Author Topic: Client / Server connection  (Read 783 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

I'm having trouble maintaining a server I'm trying to build over WiFi; here is the synopsis:

By the way I'm using:

Arduino Wifi Shield
Arduino Uno

- Turn on Arduino; WiFi boots up automatically and connect's to my home (LAN).  I setup a server on port 8080 listening for incoming clients
- I then make a call (using WiFi client, which I'm not entirely sure is correct) to GET the current time (EST).
- I'm able to get the current time with this call, but after this I'm unable to handle any clients incoming to the server (even though there is currently none)
- I try connecting via (http://192.xxx.xxx.xxx:8080), but it just hangs; If I remove the GET call - which retrieves the time, I'm then able to consistently connect.

Here is my full code:

Code:
#include <SPI.h>
#include <WiFi.h>
#include <SerialLCD.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <EasyTransferI2C.h>

EasyTransferI2C master;

struct DATA_STRUCTURE {
  float t; // temperature
  float h; // humidity
  boolean connection; // connection to wifi
  boolean overridePumps;
  boolean fan1;
  long time;
  boolean isAM;
  long hr;
  long mins;
  long secs;
};

DATA_STRUCTURE data;

int PORT = 8080;
char ssid[] = "tynet";
char pass[] = "xxxxxxxx";

boolean toggleBacklightButton = false;
boolean toggleBacklight = true;

boolean timeReceived = false;

char toggleBox;
char buffer[500];

#define I2C_SLAVE_ADDRESS 9
#define I2C_SLAVE_M_ADDRESS 10

int status = WL_IDLE_STATUS;

WiFiServer server(PORT);
WiFiServer puller(80);

char timeapi[] = "www.timeapi.org";

// IMPORTANT NOTE: [4,7,10,11,12,13] pins are used by the WIFI adapter
SerialLCD slcd(8,9);

void setup() {

  data.connection = false;

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  slcd.begin();
  slcd.backlight();

  wifiConnect(true);

}

void wifiConnect(boolean serverBegin) {

  if(WiFi.status() == WL_NO_SHIELD) {

    slcd.clear();
    slcd.setCursor(0, 0);
    delay(250);
    slcd.print("No WIFI!");
    while(true);

  }

  while(status != WL_CONNECTED) {

    slcd.setCursor(0, 0);
    slcd.print("Connecting to: ");
    slcd.setCursor(0, 1);
    slcd.print(ssid);

    status = WiFi.begin(ssid, pass);
    delay(5000);

  }

  if(serverBegin) {
    server.begin();
  }

  wifiSuccess();

}

void loop() {

  /*toggleBox = digitalRead(2);
   
   if(toggleBox == LOW && toggleBacklightButton == true) {
   
   toggleBacklightButton = false;
   toggleBacklight = !toggleBacklight;
   
   if(toggleBacklight) {
   
   slcd.backlight();
   slcd.display();
   
   } else {
   
   slcd.noBacklight();
   slcd.noDisplay();
   
   }
   
   } else if(toggleBox == HIGH) {
   
   toggleBacklightButton = true;
   // the button was just toggled; once we release
   // the button will update.
   
   }*/

  WiFiClient client = server.available();

  if(client) {

      int bufferIndex = 0;
      boolean isBlank = true;

      Serial.println("Client connected.");

      while(client.connected()) {
        while(client.available()) {

          char c = client.read();
          delay(1);
         
          //if(!timeReceived) {

          //} else {
           
            if(c == '\n' && isBlank) {
 
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println("Connnection: close");
              client.println();
              client.println("<!DOCTYPE HTML>");
              client.println("<html>");
              client.println("</html>");
 
              break;
 
            }
 
            if(c == '\n') {
 
              isBlank = true;
 
            }
            else if(c != '\r') {
 
              buffer[bufferIndex++] = c;
              isBlank = false;
 
            }
           
          //}
         
          Serial.write(c);
         
        }
      }

      /*if(strstr(buffer, "!TIME")) {
       
       Serial.println("FOUND time.");
       String time = strrchr(buffer, '!TIME');
       time = time.substring(1, -1);
       data.time = time.toInt();
       
       master.sendData(I2C_SLAVE_ADDRESS);
       
       }// else {
       
      /*if(strstr(buffer, "GET /DATA")) {
       
       }*/

      //}

      // give the web browser time to receive the data
      delay(1);
      // close the connection:
      client.flush();
      client.stop();
      Serial.println("client disonnected");

  }

  //if(master.receiveData()) {}

}

void wifiSuccess() {

  Wire.begin(I2C_SLAVE_M_ADDRESS);
  master.begin(details(data), &Wire);
  Wire.onReceive(receive);

  data.connection = true; // tell the slave we are connected to the internet

  master.sendData(I2C_SLAVE_ADDRESS);

  IPAddress ip = WiFi.localIP();
  char lcdMessage[] = "Connected: ";
  char ipMessage[] = "IP: .";
  char portColon[] = ":8080";
  char buffer[5];

  slcd.clear();
  slcd.setCursor(0, 0);

  strcat(lcdMessage, ssid);
  slcd.print(lcdMessage);

  delay(50);

  slcd.setCursor(0, 1);
  slcd.print(ipMessage);

  for(int i = 0; i < 4; i++) {
    sprintf (buffer, "%d", ip[i]);
  }

  slcd.print(buffer);
  slcd.print(portColon);

  delay(250);

  /*WiFiClient curlRequest;

  if(curlRequest.connect(timeapi, 80)) {
    curlRequest.println("GET /est/now?format=!TIME\\s HTTP/1.1");
    curlRequest.println("Host:www.timeapi.org");
    curlRequest.println("Connection: close");
    curlRequest.println();
  }*/
 
  timeReceived = true;

}

void receive(int numBytes) {}



I'm unsure exactly how to set this up correctly; some guidance would be greatly appreciated!

Thanks all,
Tyler B
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49027
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Once you make the client request, I don't see you properly closing the client, using client.stop(). I don't see you reading the data that the server returns, either.
Logged

Chicago
Offline Offline
Full Member
***
Karma: 0
Posts: 113
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Paul
 Does client.stop() provide a graceful disconnection from the server it was connected to? I've been using the Twitter library and a function to send and email out. I notice that neither of these use client.stop(). I added it to the email function. I haven't tried to add it to Twitter Library wanted to ask you about it's use. When I'm done communicating with the smtp server I have the command client.println("QUIT"); So I'm telling the server I'm done should I just let the server drop the connection to the client? I appreciate your input here.
Thanks
Don

Code:
bool Twitter::post(const char *msg)
{
#if defined(ARDUINO) && ARDUINO < 100 //if earliar than Ardunio 1.0
DNSError err = EthernetDNS.resolveHostName(LIB_DOMAIN, server);
if (err != DNSSuccess) {
return false;
}
#endif
parseStatus = 0;
statusCode = 0;
#if defined(ARDUINO) && ARDUINO < 100 //if eariler than Ardunio 1.0
if (client.connect()) {
#else
if (client.connect(LIB_DOMAIN, 80)) {
#endif
client.println("POST http://" LIB_DOMAIN "/update HTTP/1.0");
client.print("Content-Length: ");
client.println(strlen(msg)+strlen(token)+14);
client.println();
client.print("token=");
client.print(token);
client.print("&status=");
client.println(msg);
                            client.stop() //? should I use it?? *****************
} else {
return false;
}
return true;
}


byte postEmail()
{
  digitalWrite(ledPost,ON);
  EthernetClient client;
  if (client.connect("smtp.mail.wowway.com", 25))
    {
       delay(500);
       client.println("HELO MYSERVER");
       client.println("MAIL FROM:<user@wowway.com>");
       client.println("RCPT TO:<personat@gmail.com>");
       client.println("DATA");
       client.println("From: User <user@wowway.com>");
       client.println("To: Person at <personat@gmail.com>");
              
       client.println("SUBJECT: my subject here");
       client.println();
       client.println("Body of message");
       client.println();
       client.println(".");  //tell server we are done
       delay(500);
       client.println("QUIT");
       client.stop();  // seems to work fine should I use it?? *********************
       delay(2000);
       digitalWrite(ledPost,OFF);
       return true;
    }
  else
    {
       digitalWrite(ledError,ON);
       return false;
    }  
}

Logged

Pages: [1]   Go Up
Jump to: