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

16 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
    2. hi have you solved this problem?
      im having the same problem and its driving me nuts!

      Delete
    3. Anyone solved the problem with the POST?

      Delete
    4. I've spent three days to solve it, but nothing...

      Delete
    5. Try using additional power supply for ESP8266

      maybe helpful

      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
  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
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hi, i tried use wifiesp.h library, and then when my esp send data third times there's command suddenly:
    No Socket Available.
    When i add command after get php file:
    client.stop() or client.flush()
    My esp lost connection with my server.
    It need 7 seconds (for scanning) for my esp can send data. Is there a way to make esp modul keep alive when i use this library?

    Thank you
    Najib

    ReplyDelete
  10. how can i uses this on a wemos mega ?

    ReplyDelete

Note: Only a member of this blog may post a comment.