Advertisement
Guest User

huehnertor

a guest
Jul 25th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <Arduino.h>
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266HTTPClient.h>
  5. #include <AccelStepper.h>
  6.  
  7. const int motorPin1 = 2;  // Sonstwas Blue   - In 1
  8. const int motorPin2 = 0;  // Pink   - In 2
  9. const int motorPin3 = 4; // Yellow - In 3
  10. const int motorPin4 = 5; // Orange - In 4
  11.  
  12. AccelStepper stepper(AccelStepper::FULL4WIRE, motorPin1, motorPin3, motorPin2, motorPin4);
  13.  
  14. const char* ssid = "xxx";
  15. const char* password = "yyy";
  16.  
  17. HTTPClient http;
  18. WiFiServer server(80);
  19.  
  20. void setup() {
  21.   Serial.begin(9600);
  22.   pinMode(motorPin1, OUTPUT);
  23.   pinMode(motorPin2, OUTPUT);
  24.   pinMode(motorPin3, OUTPUT);
  25.   pinMode(motorPin4, OUTPUT);
  26.  
  27.   WiFi.mode(WIFI_STA);
  28.   WiFi.begin(ssid, password);
  29.   Serial.print("Connecting");
  30.   while (WiFi.status() != WL_CONNECTED)
  31.   {
  32.     delay(500);
  33.     Serial.print(".");
  34.   }
  35.   Serial.println();
  36.  
  37.   Serial.print("Connected, IP address: ");
  38.   Serial.println(WiFi.localIP());
  39.  
  40.   if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  41.     Serial.printf("WiFi Failed!\n");
  42.     return;
  43.   }
  44.  
  45.   Serial.print("IP Address: ");
  46.   Serial.println(WiFi.localIP());
  47.   Serial.print("Hostname: ");
  48.   Serial.println(WiFi.hostname());
  49.  
  50.   server.begin();
  51.   Serial.println("Server started");
  52.  
  53.  
  54.   stepper.setCurrentPosition(0);
  55.   stepper.setMaxSpeed(800);
  56.   stepper.setSpeed(10);
  57.   stepper.setAcceleration(150);
  58. }
  59.  
  60. void loop() {
  61.  
  62.   if (stepper.distanceToGo() != 0)
  63.   {
  64.       stepper.run();  
  65.   }
  66.  
  67.   WiFiClient client = server.available();
  68.   if (!client) {
  69.     return;
  70.   }
  71.  
  72.   String request = client.readStringUntil('\r');
  73.   client.flush();
  74.  
  75.   if (request.indexOf("/TUER=ZU") != -1) {
  76.     Serial.println("Schliesse Tuer.");
  77.     stepper.stop();
  78.     stepper.move(0);
  79.     stepper.setCurrentPosition(0);
  80.     stepper.moveTo(-40000);
  81.  
  82.   }
  83.   if (request.indexOf("/TUER=AUF") != -1) {
  84.     Serial.println("Oeffne Tuer.");
  85.     stepper.stop();
  86.     stepper.move(0);
  87.     stepper.setCurrentPosition(0);
  88.     stepper.moveTo(40000);
  89.   }
  90.  
  91.   if (request.indexOf("/TUER=STOP") != -1) {
  92.     Serial.println("STOPP Tuer.");
  93.     stepper.stop();
  94.     stepper.move(0);
  95.     stepper.setCurrentPosition(0);
  96.   }
  97.  
  98.  
  99.   // Return the response
  100.   client.println("HTTP/1.1 200 OK");
  101.   client.println("Content-Type: text/html");
  102.   client.println(""); //  do not forget this one
  103.   client.println("<!DOCTYPE HTML>");
  104.   client.println("<html>");
  105.  
  106.   client.println("<br><br>");
  107.   client.println("Click <a href=\"/TUER=ZU\">here</a> Huehnertuer schliessen<br>");
  108.   client.println("Click <a href=\"/TUER=AUF\">here</a> Huehnertur oeffnen<br>");
  109.   client.println("Click <a href=\"/TUER=STOP\">here</a> Huehnertur anhalten<br>");
  110.   client.println("</html>");
  111.  
  112.   Serial.println("Client disconnected");
  113.   delay(1000);
  114.  
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement