ESP8266 ESP-01: GPIO-Ausgänge über Webinterface steuern (Version 3)
Im Gegensatz zu den ersten zwei Versionen von WebControl, sind bei der folgende dritten Version die Ausgängszustände invertiert (On = Low; Off = High).
/*
* Creation: 02.11.2016
* Last Update: 20.12.2016
*
* Copyright (c) 2016 by Georg Kainzbauer <http://www.gtkdb.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// wireless network credentials:
const char* ssid = "<your_ssid>";
const char* password = "<your_wireless_password>";
// hostname for this system:
const char* host = "webcontrol";
// define GPIO pins:
const int output1 = 0;
const int output2 = 2;
// output status variables:
boolean output1_state = false;
boolean output2_state = false;
// web server on port 80:
ESP8266WebServer server(80);
String getContent()
{
// create content for the website:
String content = "<html><head><title>ESP8266 WebControl</title></head><body>";
content += "This page shows the current state of the ESP8266 outputs and can be used to control these.<br /><br />";
if (output1_state)
{
content += "Output1: <a href=\"output1\"><button>ON</button></a><br />";
}
else
{
content += "Output1: <a href=\"output1\"><button>OFF</button></a><br />";
}
if (output2_state)
{
content += "Output2: <a href=\"output2\"><button>ON</button></a><br />";
}
else
{
content += "Output2: <a href=\"output2\"><button>OFF</button></a><br />";
}
content += "</body></html>";
return content;
}
void setup()
{
// configure GPIO 0 and GPIO 2 as outputs:
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
// set outputs to low:
digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);
// connect to the wireless network:
WiFi.begin(ssid, password);
// wait for wireless network connection and print connection settings:
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
// initialize mDNS:
MDNS.begin(host);
MDNS.addService("http", "tcp", 80);
// start HTTP server:
server.begin();
// print start page:
server.on("/", [](){
server.send(200, "text/html", getContent());
});
// control output1:
server.on("/output1", [](){
if (output1_state)
{
digitalWrite(output1, HIGH);
output1_state = false;
}
else
{
digitalWrite(output1, LOW);
output1_state = true;
}
server.send(200, "text/html", getContent());
delay(1000);
});
// control output2:
server.on("/output2", [](){
if (output2_state)
{
digitalWrite(output2, HIGH);
output2_state = false;
}
else
{
digitalWrite(output2, LOW);
output2_state = true;
}
server.send(200, "text/html", getContent());
delay(1000);
});
}
void loop()
{
// handle HTTP request:
server.handleClient();
}
* Creation: 02.11.2016
* Last Update: 20.12.2016
*
* Copyright (c) 2016 by Georg Kainzbauer <http://www.gtkdb.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// wireless network credentials:
const char* ssid = "<your_ssid>";
const char* password = "<your_wireless_password>";
// hostname for this system:
const char* host = "webcontrol";
// define GPIO pins:
const int output1 = 0;
const int output2 = 2;
// output status variables:
boolean output1_state = false;
boolean output2_state = false;
// web server on port 80:
ESP8266WebServer server(80);
String getContent()
{
// create content for the website:
String content = "<html><head><title>ESP8266 WebControl</title></head><body>";
content += "This page shows the current state of the ESP8266 outputs and can be used to control these.<br /><br />";
if (output1_state)
{
content += "Output1: <a href=\"output1\"><button>ON</button></a><br />";
}
else
{
content += "Output1: <a href=\"output1\"><button>OFF</button></a><br />";
}
if (output2_state)
{
content += "Output2: <a href=\"output2\"><button>ON</button></a><br />";
}
else
{
content += "Output2: <a href=\"output2\"><button>OFF</button></a><br />";
}
content += "</body></html>";
return content;
}
void setup()
{
// configure GPIO 0 and GPIO 2 as outputs:
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
// set outputs to low:
digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);
// connect to the wireless network:
WiFi.begin(ssid, password);
// wait for wireless network connection and print connection settings:
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
// initialize mDNS:
MDNS.begin(host);
MDNS.addService("http", "tcp", 80);
// start HTTP server:
server.begin();
// print start page:
server.on("/", [](){
server.send(200, "text/html", getContent());
});
// control output1:
server.on("/output1", [](){
if (output1_state)
{
digitalWrite(output1, HIGH);
output1_state = false;
}
else
{
digitalWrite(output1, LOW);
output1_state = true;
}
server.send(200, "text/html", getContent());
delay(1000);
});
// control output2:
server.on("/output2", [](){
if (output2_state)
{
digitalWrite(output2, HIGH);
output2_state = false;
}
else
{
digitalWrite(output2, LOW);
output2_state = true;
}
server.send(200, "text/html", getContent());
delay(1000);
});
}
void loop()
{
// handle HTTP request:
server.handleClient();
}
Dieser Eintrag wurde am 05.01.2017 erstellt.
Direkter Link zu dieser Seite: http://www.gtkdb.de/index_18_2884.html
[ Zur Startseite ] [ Zur Kategorie ]
© 2004-2021 by Georg Kainzbauer