jeudi 21 janvier 2016

Arduino disconnecting from a webserver

My project is to connect the Arduino to web-server in order to light up an LED through the web, everything work fine with me, the Arduino is fully connected to the server and the LED light when a request from the website recieved. The only problem I'm facing is the connection to the server is disconnected immediately.

This is my code:

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

char ssid[] = "HomeBroadband"; //  your network SSID (name)
char pass[] = "h12345678";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "daffostore.com";    // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  pinMode(8,OUTPUT);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

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

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

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(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("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)  == 1) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /ardd/ledstatus.txt HTTP/1.1");
    client.println("Host: daffostore.com");
    client.println("Connection: keep-alive");
    //client.println("Content-Length: 1845");
    client.println("Keep-Alive: timeout=10, max=20");
    client.println();
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()  && status == WL_CONNECTED) {
    char c = client.read();
    Serial.write(c);

    if(c=='1'){
      digitalWrite(8, HIGH);
      }
      else{
        digitalWrite(8, LOW);
        }


  }



  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

and this is the serial monitor output:

Attempting to connect to SSID: HomeBroadband Connected to wifi SSID: HomeBroadband IP Address: 192.168.1.12 signal strength (RSSI):-54 dBm

Starting connection to server... connected to server HTTP/1.1 200 OK Date: Thu, 21 Jan 2016 13:30:21 GMT Server: Apache Last-Modified: Thu, 21 Jan 2016 13:25:36 GMT Accept-Ranges: bytes Content-Length: 1 Cache-Control: max-age=5 Expires: Thu, 21 Jan 2016 13:30:26 GMT Vary: Accept-Encoding Keep-Alive: timeout=2, max=32 Connection: Keep-Alive Content-Type: text/plain

disconnecting from server.




Aucun commentaire:

Enregistrer un commentaire