#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
boolean trigger = 1;
const char* host = "192.168.4.5";
String requested_data = "None";
void handleRoot() {
server.send(200, "text/html", "<h1>Requested data from ESP_AP: </h1>" + requested_data);
}
void setup() {
delay(1000);
pinMode(2, INPUT);
WiFi.softAP("ESPap", "thereisnospoon");
server.on("/", handleRoot);
server.begin();
}
void send_request() {
WiFiClient client;
if (!client.connect(host, 80)) {
return;
}
String url = "/station_test/";
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
while (client.available()) {
requested_data = client.readStringUntil('\r');
}
}
void loop() {
server.handleClient();
if (digitalRead(2) != HIGH and trigger == 1) {
send_request();
trigger = 0;
} else {
trigger = 1;
}
}
And here's the ESP_STA code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
int request_number = 0;
void handleRoot(){
server.send(200, "text/html", "<h1>Number of requests: </h1>"+String(request_number));
}
void handleRequestData(){
server.send(200, "text/html", "<h2>I'm the requested data :)</h2>");
request_number+=1;
}
void setup() {
Serial.begin(115200);
delay(10);
WiFi.begin("ESPap", "thereisnospoon");
//WiFi.config(IPAddress(192,168,4,5), IPAddress(192,168,4,1), IPAddress(255,255,255,0));
pinMode(2,OUTPUT);
digitalWrite(2,LOW);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/station_test/", handleRequestData);
server.begin();
digitalWrite(2,HIGH);
}
void loop() {
server.handleClient();
}
For now I'm trying to figure out how to connect the station to the access point, so I haven't yet tested the "request" code.