this uses BUILDIN_LED as an output
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Set these to your desired credentials. */
const char *ssid = "ESPap";
const char *password = "thereisnospoon";
ESP8266WebServer server(80);
/* Just a little test message. Go to http://192.168.4.1 in a web browser
connected to this access point to see it.
*/
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1>");
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/on", []() {
digitalWrite(BUILTIN_LED, HIGH);
server.send(200, "text/html", "ON");
});
server.on("/off", []() {
digitalWrite(BUILTIN_LED, LOW);
server.send(200, "text/html", "OFF");
});
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
This uses the pin 3 as an input, pulled up, so just connecting to the GND should do the job
Code: Select all#include <ESP8266WiFi.h>
const char *ssid = "ESPap";
const char *password = "thereisnospoon";
const char* host = "192.168.4.1";
const int inputpin = 3;
bool lastValue = LOW;
void setup() {
Serial.begin(115200);
delay(10);
pinMode(inputpin ,INPUT_PULLUP);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
delay(2000);
bool value = digitalRead(inputpin);
if (value != lastValue) {
Serial.print("connecting to ");
Serial.println(host);
// 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 = "";
//LED is pulled up, so high is not pressed, low is pressed
if (value == HIGH) {
url = "/off";
} else {
url = "/on";
}
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");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
}
Haven't tested it, but it does compile, the input pin one one ESP8266 is controlling the output pin on the other ESP, only when the input is changed it sends a HTTP GET request, and is measures every 2 seconds (this could be faster if necessary)