Reference.WiFiServerAvailable History

Hide minor edits - Show changes to markup

November 23, 2011, at 01:10 AM by Scott Fitzgerald -
Changed lines 29-30 from:

char ssid[] = "lamaison"; // your network SSID (name) char pass[] = "tenantaccess247"; // your network password

to:

char ssid[] = "Network"; // your network SSID (name) char pass[] = "myPassword"; // your network password

Changed line 52 from:
to:
Deleted line 55:
Changed line 60 from:
to:
Changed line 62 from:
   Serial.println("Connected to client");
to:
      Serial.println("Connected to client");
Changed line 64 from:
to:
Added line 69:
November 23, 2011, at 01:09 AM by Scott Fitzgerald -
Changed lines 61-94 from:
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");       
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
to:
    if (client.connected()) {
   Serial.println("Connected to client");
Changed lines 65-66 from:
    // give the web browser time to receive the data
    delay(1);
to:
November 23, 2011, at 01:05 AM by Scott Fitzgerald -
Added lines 9-10:

available() inherits from the Stream utility class.

Changed lines 25-26 from:

[@

to:
Changed lines 28-35 from:
  1. include <IPAddress.h>

byte mac[6] = { 0 }; IPAddress ip; IPAddress gateway; IPAddress subnet; byte dataBuf[80] = { 0 }; char ssid[32] = { 0 };

to:

char ssid[] = "lamaison"; // your network SSID (name) char pass[] = "tenantaccess247"; // your network password

Changed lines 33-37 from:

Server server(23);

void setup() {

to:

WiFiServer server(80);

void setup() {

  // initialize serial:
Changed lines 38-39 from:
  Serial.println("Setup WiFi...");
  strcpy(ssid, "Vodafone-10289870");
to:
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
Changed lines 41-47 from:
  int status = WiFi.begin(ssid);

  Serial.println("Wifi Connected!");

  Serial.println("Starting server...");
  server.begin();
  delay(1000);  
to:
  status = WiFi.begin(ssid, pass);
  if ( status != WL_CONNECTED) { 
    Serial.println("Couldn't get a wifi connection");
    while(true);
  } 
  else {
    server.begin();
    Serial.print("Connected to wifi. My address:");
    IPAddress myAddress = WiFi.localIP();
    Serial.println(myAddress);

  }
Changed lines 56-69 from:

void loop() {

  static uint8_t count = 0;
  Serial.println("Retry connect...");
  status = WiFi.begin(ssid);
  if (status == WL_CONNECTED)
  {
      byte status = 0;
      Client client = server.available(&status);
      if (client) {
        byte idx = 0;
        while (client.available())
        {
            dataBuf[idx++] = client.read();
to:

void loop() {

  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");       
          }
          client.println("</html>");
          break;
Changed lines 86-90 from:
        if (idx>0) 
        {
          dataBuf[idx]=0; 
          server.write((char*)&dataBuf[0]);
to:
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
Deleted line 93:
        return;
Added lines 95-99:
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
Changed line 102 from:

@]

to:
November 22, 2011, at 03:55 AM by Scott Fitzgerald -
Added lines 1-78:

WiFi : Server class

available()

Description

Gets a client that is connected to the server and has data available for reading. The connection persists when the returned client object goes out of scope; you can close it by calling client.stop().

Syntax

server.available()

Parameters

None

Returns

a Client object; if no Client has data available for reading, this object will evaluate to false in an if-statement

Example

#include <WiFi.h>
#include <IPAddress.h>

byte mac[6] = { 0 };
IPAddress ip;
IPAddress gateway;
IPAddress subnet;
byte dataBuf[80] = { 0 };
char ssid[32] = { 0 };
int status = WL_IDLE_STATUS;

Server server(23);


void setup()
{
  Serial.begin(9600);
  Serial.println("Setup WiFi...");
  strcpy(ssid, "Vodafone-10289870");
  Serial.println(ssid);
  int status = WiFi.begin(ssid);

  Serial.println("Wifi Connected!");

  Serial.println("Starting server...");
  server.begin();
  delay(1000);  
}

void loop()
{
  static uint8_t count = 0;
  Serial.println("Retry connect...");
  status = WiFi.begin(ssid);
  if (status == WL_CONNECTED)
  {
      byte status = 0;
      Client client = server.available(&status);
      if (client) {
        byte idx = 0;
        while (client.available())
        {
            dataBuf[idx++] = client.read();
        }

        if (idx>0) 
        {
          dataBuf[idx]=0; 
          server.write((char*)&dataBuf[0]);
        }
        return;
      }
  }
}

Share