Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Pins im Programm entsprechen auf dem Board...
- *
- D0 = 16;
- D1 = 5;
- D2 = 4;
- D3 = 0;
- D4 = 2;
- D5 = 14;
- D6 = 12;
- D7 = 13;
- D8 = 15;
- D9 = 3;
- D10 = 1;
- */
- #include <ESP8266WiFi.h>
- // WLAN-Daten
- const char* ssid = "SSID";
- const char* password = "Passwort";
- //Ein- und Ausgänge
- int Relais = 4;
- int Reed_Schalter_offen = 0;
- int Reed_Schalter_geschlossen = 5;
- // Create an instance of the server
- // specify the port to listen on as an argument
- WiFiServer server(80);
- // SETUP
- void setup() {
- Serial.begin(115200);
- // delay(10);
- pinMode(Reed_Schalter_offen, INPUT);
- pinMode(Reed_Schalter_geschlossen, INPUT);
- pinMode(Relais,OUTPUT);
- digitalWrite(Relais, 1);
- // Connect to WiFi network
- // Serial.println();
- // Serial.println();
- // Serial.print("Connecting to ");
- // Serial.println(ssid);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- // Serial.print(".");
- }
- // Serial.println("");
- // Serial.println("WiFi connected");
- // Start the server
- server.begin();
- // Serial.println("Server started");
- // Print the IP address
- // Serial.println(WiFi.localIP());
- }
- void loop() {
- //Serial.println("1");
- int Status_Reedschalter_offen = digitalRead(Reed_Schalter_offen);
- int Status_Reedschalter_geschlossen = digitalRead(Reed_Schalter_geschlossen);
- // Serial.print(Status_Reedschalter_offen);
- // Serial.print("\t");
- // Serial.println(Status_Reedschalter_geschlossen);
- String Status_Garagentor;
- //Serial.println("2");
- if (Status_Reedschalter_offen == 1 && Status_Reedschalter_geschlossen == 0){
- Status_Garagentor = "offen";
- }
- else if (Status_Reedschalter_offen == 0 && Status_Reedschalter_geschlossen == 1){
- Status_Garagentor = "geschlossen";
- }
- else if (Status_Reedschalter_offen == 0 && Status_Reedschalter_geschlossen == 0){
- Status_Garagentor = "dazwischen";
- }
- //Serial.println("3");
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Serial.println("4");
- // Wait until the client sends some data
- // Serial.println("new client");
- while(!client.available()){
- // delay(1);
- }
- // Read the first line of the request
- String req = client.readStringUntil('\r');
- Serial.println(req);
- client.flush();
- // Serial.println("5");
- // Match the request
- int val;
- // Serial.println("6");
- if (req.indexOf("/Output") != -1){
- val = 0;
- digitalWrite(Relais, val);
- delay(200);
- val = 1;
- digitalWrite(Relais, val);
- // Serial.println("7");
- }
- else if (req.indexOf("/start") != -1){
- //Prepare the response
- String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
- s += Status_Garagentor;
- // Send the response to the client
- client.print(s);
- client.flush();
- // Serial.println("8");
- }
- else {
- // Serial.println("invalid request");
- // client.stop();
- return;
- }
- client.flush();
- // Serial.println("9");
- // //Prepare the response
- // String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
- //
- // s += Status_Garagentor;
- // //s += (val)?"low":"high";
- //// s += "</html>\n";
- // Send the response to the client
- // client.print(s);
- // delay(1);
- // Serial.println("Client disonnected");
- // The client will actually be disconnected
- // when the function returns and 'client' object is detroyed
- }
Advertisement
Add Comment
Please, Sign In to add comment