Reference.WiFiServerWrite History

Show minor edits - Show changes to markup

November 23, 2011, at 01:26 AM by Scott Fitzgerald -
Changed lines 19-20 from:

None

to:

byte : the number of bytes written. It is not necessary to read this.

Changed lines 23-24 from:

[@

to:
Changed lines 26-33 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[] = "yourNetwork"; char pass[] = "yourPassword";

Changed lines 31-35 from:

Server server(23);

void setup() {

to:

WiFiServer server(80);

void setup() {

  // initialize serial:
Changed lines 36-37 from:
  Serial.println("Setup WiFi...");
  strcpy(ssid, "Vodafone-10289870");
to:
  Serial.println("Attempting to connect to WPA network...");
  Serial.print("SSID: ");
Changed lines 39-45 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();
  }
Changed lines 50-72 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();
        }

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

void loop() {

  // listen for incoming clients
  WiFiClient client = server.available();
  if (client == true) {
       // read bytes from the incoming client and write them back
    // to any clients connected to the server:
    server.write(client.read());
Changed line 59 from:

@]

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

WiFi : Server class

write()

Description

Write data to all the clients connected to a server.

Syntax

server.write(data)

Parameters

data: the value to write (byte or char)

Returns

None

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