Hallo ich habe hier zwei Codes die ich gerne zusammenfügen möchte. Leider kommt immer der Fehler wenn ich diese zusammen füge: conflicting declaration 'WebServer server'.
Hier sind die beiden Codes:
#include <WiFi.h>
#include <Adafruit_NeoPixel.h>
const char* ssid = ""; //Netzwerk Name
const char* password = ""; //Netzwerk Passwort
WiFiServer server(80);
#define Steckdose_PIN 22
#define LED_PIN 23
#define LED_COUNT 300
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
uint32_t red = strip.Color(255, 0, 0);
uint32_t green = strip.Color(0, 255, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t white = strip.Color(255, 255, 255);
uint32_t yellow = strip.Color(227, 235, 7);
uint32_t orange = strip.Color(235, 151, 7);
uint32_t lila = strip.Color(235, 7, 201);
uint32_t cyan = strip.Color(5, 240, 208);
String header;
String outputredState = "Aus";
String outputgreenState = "Aus";
String outputblueState = "Aus";
String outputwhiteState = "Aus";
String outputyellowState = "Aus";
String outputorangeState = "Aus";
String outputlilaState = "Aus";
String outputcyanState = "Aus";
String outputrainbowState = "Aus";
String outputsteckdoseState = "Aus";
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
void setup() {
strip.begin();
strip.show();
Serial.begin(115200);
Serial.print("Verbinden mit "); //Verbinden mit WiFi
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() !=WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(""); //Zeigt die IP Adresse
Serial.println("WiFi verbunden");
Serial.println("IP Adresse: ");
Serial.println(WiFi.localIP());
server.begin();
pinMode(Steckdose_PIN, OUTPUT);
}
void loop() {
WiFiClient client = server.available();
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("Neue Aktion.");
String currentLine = "";
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header +=c;
if (c == '\n') {
if (currentLine.length() ==0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("GET /red/on") >=0) {
Serial.println("GPIO Rot an");
outputredState = "an";
strip.fill(red, 0, 300);
strip.show();
} else if (header.indexOf("GET /red/off") >= 0) {
Serial.println("GPIO Rot aus");
outputredState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
if (header.indexOf("GET /green/on") >=0) {
Serial.println("GPIO Grün an");
outputgreenState = "an";
strip.fill(green, 0, 300);
strip.show();
} else if (header.indexOf("GET /green/off") >= 0) {
Serial.println("GPIO Grün aus");
outputgreenState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
if (header.indexOf("GET /blue/on") >=0) {
Serial.println("GPIO Blau an");
outputblueState = "an";
strip.fill(blue, 0, 300);
strip.show();
} else if (header.indexOf("GET /blue/off") >= 0) {
Serial.println("GPIO Blau aus");
outputblueState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
if (header.indexOf("GET /white/on") >=0) {
Serial.println("GPIO Weiß an");
outputwhiteState = "an";
strip.fill(white, 0, 300);
strip.show();
} else if (header.indexOf("GET /white/off") >= 0) {
Serial.println("GPIO Weiß aus");
outputwhiteState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
if (header.indexOf("GET /yellow/on") >=0) {
Serial.println("GPIO Gelb an");
outputyellowState = "an";
strip.fill(yellow, 0, 300);
strip.show();
} else if (header.indexOf("GET /yellow/off") >= 0) {
Serial.println("GPIO Gelb aus");
outputyellowState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
if (header.indexOf("GET /orange/on") >=0) {
Serial.println("GPIO Orange an");
outputorangeState = "an";
strip.fill(orange, 0, 300);
strip.show();
} else if (header.indexOf("GET /orange/off") >= 0) {
Serial.println("GPIO Orange aus");
outputorangeState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
if (header.indexOf("GET /lila/on") >=0) {
Serial.println("GPIO Lila an");
outputlilaState = "an";
strip.fill(lila, 0, 300);
strip.show();
} else if (header.indexOf("GET /lila/off") >= 0) {
Serial.println("GPIO Lila aus");
outputlilaState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
if (header.indexOf("GET /cyan/on") >=0) {
Serial.println("GPIO Türkis an");
outputcyanState = "an";
strip.fill(cyan, 0, 300);
strip.show();
} else if (header.indexOf("GET /cyan/off") >= 0) {
Serial.println("GPIO Türkis aus");
outputcyanState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
if (header.indexOf("GET /steckdose/on") >=0) {
Serial.println("GPIO Steckdose an");
outputsteckdoseState = "an";
delay(500);
digitalWrite(Steckdose_PIN, HIGH);
} else if (header.indexOf("GET /steckdose/off") >= 0) {
Serial.println("GPIO Steckdose aus");
outputsteckdoseState = "aus";
delay(500);
digitalWrite(Steckdose_PIN, LOW);
}
if (header.indexOf("GET /rainbow/on") >=0) {
Serial.println("GPIO Regenbogen an");
outputrainbowState = "an";
while (true) {
rainbow(30);
delay(10);
}
} else if (header.indexOf("GET /rainbow/off") >= 0) {
Serial.println("GPIO Regenbogen aus");
outputrainbowState = "aus";
strip.fill((0, 0, 0), 0, 300);
strip.show();
}
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<title>Smart Home Control</title>");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: solid; color: black; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer");
client.println(".button2 {background-color: #555555;}</style></head>");
client.println("<body><h1>LED Strip Steuerung</h1>");
client.println("<p>Rot " + outputredState + "</p>");
client.println("<p><a href=\"/red/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/red/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Grün " + outputgreenState + "</p>");
client.println("<p><a href=\"/green/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/green/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Blau " + outputblueState + "</p>");
client.println("<p><a href=\"/blue/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/blue/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Weiß " + outputwhiteState + "</p>");
client.println("<p><a href=\"/white/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/white/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Gelb " + outputyellowState + "</p>");
client.println("<p><a href=\"/yellow/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/yellow/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Orange " + outputorangeState + "</p>");
client.println("<p><a href=\"/orange/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/orange/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Lila " + outputlilaState + "</p>");
client.println("<p><a href=\"/lila/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/lila/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Türkis " + outputcyanState + "</p>");
client.println("<p><a href=\"/cyan/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/cyan/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Steckdose " + outputsteckdoseState + "</p>");
client.println("<p><a href=\"/steckdose/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/steckdose/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("<p>Regenbogen " + outputrainbowState + "</p>");
client.println("<p><a href=\"/rainbow/on\"><button class=\"button\">Einschalten</button></a></p>");
client.println("<p><a href=\"/rainbow/off\"><button class=\"button button2\">Ausschalten</button></a></p>");
client.println("</body></html>");
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
header = "";
client.stop();
Serial.println("Aktion beendet.");
Serial.println("");
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i*1+j) & 255));
}
strip.show();
delay(wait);
}
}
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
zweiter code
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
const char* ssid = "";
const char* password = "";
WebServer server(80);
Adafruit_BMP085 bmp;
float temperatur = 0.0;
float druck = 0.0;
void updateSensorValues() {
double t = bmp.readTemperature();
double p = bmp.readPressure() / 100.0;
temperatur = t;
druck = p;
}
void handleRoot() {
String html = "<html><body>";
html += "<h1>Wählen Sie, welche Werte angezeigt werden sollen</h1>";
html += "<form action='/temperature'><button type='submit'>Temperatur anzeigen</button></form>";
html += "<form action='/pressure'><button type='submit'>Luftdruck anzeigen</button></form>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleTemperature() {
updateSensorValues();
String html = "<html><body>";
html += "<div style='font-size: 4,5em; text-align: center;'>";
if (temperatur <= -10) {
html += "<span style='color: #0099cc;'>" + String(temperatur) + " ℃</span>";
} else if (temperatur > -10 && temperatur <= 0) {
html += "<span style='color: #00cc66;'>" + String(temperatur) + " ℃</span>";
} else if (temperatur > 0 && temperatur <= 10) {
html += "<span style='color: #ffcc00;'>" + String(temperatur) + " ℃</span>";
} else if (temperatur > 10 && temperatur <= 20) {
html += "<span style='color: #ff9900;'>" + String(temperatur) + " ℃</span>";
} else if (temperatur > 20 && temperatur <= 30) {
html += "<span style='color: #ff6600;'>" + String(temperatur) + " ℃</span>";
} else {
html += "<span style='color: #cc0000;'>" + String(temperatur) + " ℃</span>";
}
html += "</div>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handlePressure() {
updateSensorValues();
String html = "<html><body>";
html += "<div style='font-size: 4,5em; text-align: center;'>";
html += "<span style='color: #000000;'>" + String(druck) + " hPa</p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("");
Serial.println("WiFi verbunden");
Serial.println("IP Adresse: ");
Serial.println(WiFi.localIP());
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP180 sensor, check wiring!");
while (1);
}
server.on("/", handleRoot);
server.on("/temperature", handleTemperature);
server.on("/pressure", handlePressure);
server.begin();
Serial.println("Webserver started!");
}
void loop() {
server.handleClient();
delay(100);
updateSensorValues();
}
ich hoffe jemand kann mir helfen