AZ3166 WiFi Client

The WiFiClient class is for Arduino Wifi shield.

Assembly

AZ3166WiFiClient.h

Summary

Types
IPAddress
Constructors
WiFiClient - WiFiClient()
Methods
peek - int peek()
connect - int connect(const char *host, unsigned short port)
connect - int connect(IPAddress ip, unsigned short port)
available - int available()
write - unsigned int write(unsigned char b)
write - unsigned int write(const unsigned char *buf, unsigned int size)
read - int read()
read - int read(unsigned char *buf, unsigned int size)
flush - void flush()
stop - void stop()
connected - int connected()

Types

IPAddress

Base class that provides IPAddress

Constructors

WiFiClient

WiFiClient()

Parameters

None.

Methods

peek

int peek()

Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer.

Parameters

None.

Return value

Type Description
int Always 0.

connect

int connect(const char *host, unsigned short port)

Connect to the server with specific host name and port.

Parameters

Type Name Description
char * host The host name of the server.
unsigned short port The port that the client will connect to.

Return value

Type Description
int 1 for success, 0 for fail.

connect

int connect(IPAddress ip, unsigned short port)

Connect to the server with specific IP and port.

Parameters

Type Name Description
IPAddress ip The IP of the server.
unsigned short port The port that the client will connect to.

Return value

Type Description
int 1 for success, 0 for fail.

available

int available()

Returns the number of bytes available for reading.

Parameters

None.

Return value

Type Description
int The number of bytes available.

write

size_t write(unsigned char b)

Write 1 byte data to the connected server.

Parameters

Type Name Description
unsigned char b Data to write.

Return value

Type Description
size_t Size of data written.

write

size_t write(const unsigned char *buf, unsigned int size)

Write size of bytes data to the connected server.

Parameters

Type Name Description
const unsigned char * buf Pointer to data to write.
unsigned int size Size of data to write.

Return value

Type Description
size_t Size of data written.

read

int read()

Read the next byte of data from the connected server.

Parameters

None.

Return value

Type Description
int The next byte, or -1 if none is available.

read

int read(unsigned char *buf, unsigned int size)

Read the next size of bytes of data from the connected server.

Parameters

Type Name Description
unsigned char * buf Pointer to the received data.
unsigned int size Size of data received to read.

Return value

Type Description
int The size of byte read, or -1 if none is available.

flush

void flush()

[Not implemented] Discard any bytes that have been written to the client but not yet read.

Parameters

None.

Return value

void

stop

void stop()

Close the current connection.

Parameters

None.

Return value

void

connected

int connected()

Get current connection state.

Parameters

None.

Return value

Type Description
int 0 for not connected, 1 for connected.

Sample code

#include <AZ3166WiFi.h>
char ssid[] = "{SSID of your access point}";    // your network SSID (name)
char password[] = "{password}";                 // your network password
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[] = "www.httpbin.org";    // 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:
    Serial.begin(115200);
    // check for the presence of the shield:
    if (WiFi.status() == WL_NO_SHIELD) {
        Serial.println("WiFi shield not present");
        // don't continue:
        while (true);
    }
    const char* fv = WiFi.firmwareVersion();
    Serial.printf("Wi-Fi firmware: %s\r\n", fv);
    // 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, password);
        // 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)) {
        Serial.println("connected to server");
        // Make a HTTP request:
        client.println("GET /ip HTTP/1.1");
        client.println("Host: www.httpbin.org");
        client.println("Connection: close\r\n");
        Serial.println("HTTP request sent.");
    }
}
void loop() {
    uint8_t buff[32];
    // if there are incoming bytes available
    // from the server, read them and print them:
    while (true)
    {
        int ret = client.read(buff, sizeof(buff));
        if (ret <= 0)
        break;
        for(int i = 0; i < ret; i++)
        {
            Serial.write((char)buff[i]);
        }
    }
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();
    // do nothing forevermore:
    while (true);
}
void printWifiStatus() {
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());
    // print your WiFi shield's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);
    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
}