ESP as webserver and client simultaneously [SOLVED:kind of ]
Posted: Mon Feb 15, 2016 3:02 am
Hi to all! (and a thanks to this community, I've learned a lot reading here and still learning)
I wonder if it possible to use the ESP as server and client simultaneously for example a GET request.
(I'm using a ESP-12E on devkit1)
What I need is while working as a server and responding to requests to GET a page every i.e. 1 hour
the working code of a server is (generic)
and it's OK, works as it should..
now the following code is obviously WRONG (while delaying in the loop section the server doesn't respond)
is what I'm seeking doable?
I wonder if it possible to use the ESP as server and client simultaneously for example a GET request.
(I'm using a ESP-12E on devkit1)
What I need is while working as a server and responding to requests to GET a page every i.e. 1 hour
the working code of a server is (generic)
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Set these to your desired credentials. */
const char *ssid = "XXXXXXXX";
const char *password = "XXXXXXXXXXXXXXXXXX";
// Update these with values suitable for your network.
IPAddress ip(192, 168, 1, 10); //Node static IP
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
ESP8266WebServer server(80);
void setup() {
delay(1000);
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("done");
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", []() {
delay(5000);
server.send(200, "text/html", webPage1 + pinstate + webPage2);
});
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
and it's OK, works as it should..
now the following code is obviously WRONG (while delaying in the loop section the server doesn't respond)
Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char *host = "www.example.com";
/* Set these to your desired credentials. */
const char *ssid = "XXXXXXXX";
const char *password = "XXXXXXXXXXXXXXXXXX";
// Update these with values suitable for your network.
IPAddress ip(192, 168, 1, 10); //Node static IP
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
ESP8266WebServer server(80);
void setup() {
delay(1000);
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("done");
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", []() {
delay(5000);
server.send(200, "text/html", webPage1 + pinstate + webPage2);
});
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
client.flush();
client.stop();
delay(3600000);
}
is what I'm seeking doable?