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"); }
when i run the code it gives this output...
ReplyDeleteStarting 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 >>>
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:
ReplyDeleteif (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
If you get the POST problem solved please let me know. I have spent days trig to do a POST.
Deletepaul@kf5wkl/com
hi have you solved this problem?
Deleteim having the same problem and its driving me nuts!
Anyone solved the problem with the POST?
DeleteI've spent three days to solve it, but nothing...
DeleteTry using additional power supply for ESP8266
Deletemaybe helpful
I have tried to compile this code by selecting 'Generic esp8266 Borad', and it gives the following error. Can anyone please help.
ReplyDeletefatal error: WiFiEsp.h: No such file or directory
This comment has been removed by the author.
Deletehello, I tried this library with ARDUINO IDE 1.0.5 but I meet 2 pbs to compile :
ReplyDelete- 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'
anny resolutions of this problems?
ReplyDeleteThanks for the great examples. Do you know if it is possible to use websocket with the esp shield and a uno?
ReplyDeleteHey,
ReplyDeleteI'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.
This comment has been removed by the author.
ReplyDeleteHi, i tried use wifiesp.h library, and then when my esp send data third times there's command suddenly:
ReplyDeleteNo 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
how can i uses this on a wemos mega ?
ReplyDelete