esp8266/webserver/webserver.ino

66 lines
1.3 KiB
Arduino
Raw Normal View History

2024-03-24 20:45:45 +00:00
#include <ESP8266WebServer.h>
2024-03-25 02:29:53 +00:00
#include <ESP8266WiFi.h>
2024-03-24 20:45:45 +00:00
#ifdef ESP32
2024-03-25 02:29:53 +00:00
#pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY !)
2024-03-24 20:45:45 +00:00
#error Select ESP8266 board.
#endif
ESP8266WebServer server(80); // 80 is the port number
2024-03-25 02:29:53 +00:00
const char *ssid = "Decentrala";
const char *password = "";
2024-03-24 20:45:45 +00:00
2024-03-25 02:29:53 +00:00
String resSwitch1On = "Switch1 is On", resSwitch1Off = "Switch1 is Off",
resSwitch2On = "Switch2 is On", resSwitch2Off = "Switch2 is Off";
2024-03-24 20:45:45 +00:00
2024-03-25 02:29:53 +00:00
void switch1On() {
2024-03-24 20:45:45 +00:00
digitalWrite(4, HIGH);
2024-03-25 02:29:53 +00:00
server.send(200, "text/html", resSwitch1On);
2024-03-24 20:45:45 +00:00
}
2024-03-25 02:29:53 +00:00
void switch1Off() {
2024-03-24 20:45:45 +00:00
digitalWrite(4, LOW);
2024-03-25 02:29:53 +00:00
server.send(200, "text/html", resSwitch1Off);
2024-03-24 20:45:45 +00:00
}
2024-03-25 02:29:53 +00:00
void switch2On() {
2024-03-24 20:45:45 +00:00
digitalWrite(14, HIGH);
2024-03-25 02:29:53 +00:00
server.send(200, "text/html", resSwitch2On);
2024-03-24 20:45:45 +00:00
}
2024-03-25 02:29:53 +00:00
void switch2Off() {
2024-03-24 20:45:45 +00:00
digitalWrite(14, LOW);
2024-03-25 02:29:53 +00:00
server.send(200, "text/html", resSwitch2Off);
2024-03-24 20:45:45 +00:00
}
void setup() {
2024-03-25 02:29:53 +00:00
Serial.begin(74880);
2024-03-24 20:45:45 +00:00
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
2024-03-25 02:29:53 +00:00
while (WiFi.status() != WL_CONNECTED)
delay(500);
2024-03-24 20:45:45 +00:00
Serial.print(WiFi.localIP());
2024-03-25 02:29:53 +00:00
server.on("/switch1on", switch1On);
server.on("/switch1off", switch1Off);
server.on("/switch2on", switch2On);
server.on("/switch2off", switch2Off);
2024-03-24 20:45:45 +00:00
2024-03-25 02:29:53 +00:00
server.enableCORS(true);
2024-03-24 20:45:45 +00:00
server.begin();
2024-03-25 02:29:53 +00:00
pinMode(14, OUTPUT); // D5
pinMode(4, OUTPUT); // D2
2024-03-24 20:45:45 +00:00
digitalWrite(14, LOW);
digitalWrite(4, LOW);
}
2024-03-25 02:29:53 +00:00
void loop() {
2024-03-24 20:45:45 +00:00
server.handleClient();
delay(1);
}