Hallo ich brauche für mein Projekt 4 GPIO-Pins und will RX und TX als GPIO verwenden. Leider komme ich nicht mehr weiter.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// wireless network credentials:
const char* ssid = "xxxxx";
const char* password = "xxxxx";
// hostname for this system:
const char* host = "webcontrol";
// define GPIO pins:
const int output1 = 0; //GPIO0
const int output2 = 2; //GPIO2
const int output3 = 1; //TX GPIO1
const int output4 = 3; //RX GPIO3
// output status variables:
boolean output1_state = false;
boolean output2_state = false;
boolean output3_state = false;
boolean output4_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 += "Home Control!
";
if (output1_state)
{
content += "Output1: <a href=\"output1\"><button>ON</button></a>
";
Serial.print("Output1: ON");
Serial.print('\n');
}
else
{
content += "Output1: <a href=\"output1\"><button>OFF</button></a>
";
Serial.print("Output1: OFF");
Serial.print('\n');
}
if (output2_state)
{
content += "Output2: <a href=\"output2\"><button>ON</button></a>
";
Serial.print("Output2: ON");
Serial.print('\n');
}
else
{
content += "Output2: <a href=\"output2\"><button>OFF</button></a>
";
Serial.print("Output2: OFF");
Serial.print('\n');
}
if (output3_state)
{
content += "Output3: <a href=\"output3\"><button>ON</button></a>
";
Serial.print("Output3: ON");
Serial.print('\n');
}
else
{
content += "Output3: <a href=\"output3\"><button>OFF</button></a>
";
Serial.print("Output3: OFF");
Serial.print('\n');
}
if (output4_state)
{
content += "Output4: <a href=\"output4\"><button>ON</button></a>
";
Serial.print("Output4: ON");
Serial.print('\n');
}
else
{
content += "Output4: <a href=\"output4\"><button>OFF</button></a>
";
Serial.print("Output4: OFF");
Serial.print('\n');
}
Serial.print('\n');
content += "</body></html>";
return content;
}
void setup()
{
// configure GPIO 0 and GPIO 2 as outputs:
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
// GPIO1 and GPIO3
pinMode(output3, OUTPUT);
pinMode(output4, OUTPUT);
// set outputs to low:
digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
digitalWrite(output4, LOW);
// initialize serial port for debugging purpose:
Serial.begin(115200);
// connect to the wireless network:
WiFi.begin(ssid, password);
// wait for wireless network connection and print connection settings:
Serial.println("");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("Wireless network connection established.");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// initialize mDNS:
if (MDNS.begin(host))
{
Serial.println("mDNS responder started");
}
MDNS.addService("http", "tcp", 80);
// start HTTP server:
server.begin();
Serial.println("HTTP server started");
// print start page:
server.on("/", [](){
server.send(200, "text/html", getContent());
});
// control output1:
server.on("/output1", [](){
if (output1_state)
{
digitalWrite(output1, LOW);
output1_state = false;
}
else
{
digitalWrite(output1, HIGH);
output1_state = true;
}
server.send(200, "text/html", getContent());
delay(1000);
});
// control output2:
server.on("/output2", [](){
if (output2_state)
{
digitalWrite(output2, LOW);
output2_state = false;
}
else
{
digitalWrite(output2, HIGH);
output2_state = true;
}
server.send(200, "text/html", getContent());
delay(1000);
});
// control output3:
server.on("/output3", [](){
if (output3_state)
{
digitalWrite(output3, LOW);
output3_state = false;
}
else
{
digitalWrite(output3, HIGH);
output3_state = true;
}
server.send(200, "text/html", getContent());
delay(1000);
});
// control output4:
server.on("/output4", [](){
if (output4_state)
{
digitalWrite(output4, LOW);
output4_state = false;
}
else
{
digitalWrite(output4, HIGH);
output4_state = true;
}
server.send(200, "text/html", getContent());
delay(1000);
});
}
void loop()
{
// handle HTTP request:
server.handleClient();
}
Grüße Manuel