WiFiEsp Example - WebClient

This is one of the examples to the WiFiEsp library.

This example shows you how to make a HTTP request using a WiFi shield. It displays the Arduino logo on your Arduino's serial window.

It is similar to the WiFi Web Client example of the standard Arduino WiFi Shield.


Hardware Required

  • Shield-compatible Arduino board.
  • The ESP WiFi shield is highly recommended but any reliable connection of an ESP8266 module should work.


Code

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "Twim";            // your network SSID (name)
char pass[] = "12345678";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char server[] = "arduino.cc";

// Initialize the Ethernet client object
WiFiEspClient client;

void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

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

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");
  
  printWifiStatus();

  Serial.println();
  Serial.println("Starting 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 /asciilogo.txt HTTP/1.1");
    client.println("Host: arduino.cc");
    client.println("Connection: close");
    client.println();
  }
}

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

  // 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);
  }
}


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");
}

9 comments:

  1. when i run the code it gives this output...

    Starting connection to server...
    [WiFiEsp] Connecting to arduino.cc
    [WiFiEsp] Connecting to arduino.cc
    Connected to server
    [WiFiEsp] >>> TIMEOUT >>>
    [WiFiEsp] Data packet send error (2)
    [WiFiEsp] Failed to write to socket 1
    [WiFiEsp] Disconnecting 1
    [WiFiEsp] >>> TIMEOUT >>>

    ReplyDelete
  2. I am attempting to use this library to POST, and keep getting errors. Can you provide an example of how to POST? Here is what I have:

    if (client.connect(server, 80)) {
    Serial.println("Connected to server");
    // Make a HTTP request
    String content = "Some JSON...";
    client.println("POST /some/uri HTTP/1.1");
    client.println("Host: http://things.ubidots.com");
    client.println("Accept: */*");
    client.println("Content-Length: " + sizeof(content));
    client.println("Content-Type: application/json");
    client.println();
    client.println(content);
    }

    The error I get is this:
    Connected to server
    [WiFiEsp] Data packet send error (2)
    [WiFiEsp] Failed to write to socket 3
    [WiFiEsp] Disconnecting 3

    ReplyDelete
    Replies
    1. If you get the POST problem solved please let me know. I have spent days trig to do a POST.

      paul@kf5wkl/com

      Delete
  3. I have tried to compile this code by selecting 'Generic esp8266 Borad', and it gives the following error. Can anyone please help.

    fatal error: WiFiEsp.h: No such file or directory

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  4. hello, I tried this library with ARDUINO IDE 1.0.5 but I meet 2 pbs to compile :

    - Server.h I have to add the line #include "Print.h" in the original file, so it's ok now !
    - EspDrv.cpp in getNetmask and getGateway, the "fromString" is unknown so I put this line in comment... but how to fix it ??
    the exact error is: 'class IPAddress' has no member named 'fromString'

    ReplyDelete
  5. anny resolutions of this problems?

    ReplyDelete
  6. Thanks for the great examples. Do you know if it is possible to use websocket with the esp shield and a uno?

    ReplyDelete
  7. Hey,

    I've been using this library successfully to connect to receive UDP packets etc. But i want to be able to just send a packet to the PC with a UDP host waiting using this exact library. I tested the NTP Server and that works well. Just can't seem to find an example of just sending a packet which ill send from a sensor.

    Thanks mate.

    ReplyDelete