Guest User

Untitled

a guest
Apr 23rd, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1.  
  2.  
  3.  
  4. /* Pins im Programm entsprechen auf dem Board...
  5. *
  6. D0 = 16;
  7. D1 = 5;
  8. D2 = 4;
  9. D3 = 0;
  10. D4 = 2;
  11. D5 = 14;
  12. D6 = 12;
  13. D7 = 13;
  14. D8 = 15;
  15. D9 = 3;
  16. D10 = 1;
  17. */
  18.  
  19.  
  20.  
  21. #include <ESP8266WiFi.h>
  22.  
  23. // WLAN-Daten
  24. const char* ssid = "SSID";
  25. const char* password = "Passwort";
  26.  
  27. //Ein- und Ausgänge
  28.  
  29.  
  30. int Relais = 4;
  31.  
  32. int Reed_Schalter_offen = 0;
  33. int Reed_Schalter_geschlossen = 5;
  34.  
  35.  
  36. // Create an instance of the server
  37. // specify the port to listen on as an argument
  38. WiFiServer server(80);
  39.  
  40.  
  41.  
  42. // SETUP
  43. void setup() {
  44. Serial.begin(115200);
  45. // delay(10);
  46.  
  47. pinMode(Reed_Schalter_offen, INPUT);
  48. pinMode(Reed_Schalter_geschlossen, INPUT);
  49. pinMode(Relais,OUTPUT);
  50.  
  51. digitalWrite(Relais, 1);
  52.  
  53.  
  54. // Connect to WiFi network
  55. // Serial.println();
  56. // Serial.println();
  57. // Serial.print("Connecting to ");
  58. // Serial.println(ssid);
  59.  
  60. WiFi.mode(WIFI_STA);
  61. WiFi.begin(ssid, password);
  62.  
  63. while (WiFi.status() != WL_CONNECTED) {
  64.  
  65. delay(500);
  66. // Serial.print(".");
  67. }
  68. // Serial.println("");
  69. // Serial.println("WiFi connected");
  70.  
  71. // Start the server
  72. server.begin();
  73. // Serial.println("Server started");
  74.  
  75. // Print the IP address
  76. // Serial.println(WiFi.localIP());
  77.  
  78. }
  79.  
  80. void loop() {
  81.  
  82. //Serial.println("1");
  83.  
  84. int Status_Reedschalter_offen = digitalRead(Reed_Schalter_offen);
  85. int Status_Reedschalter_geschlossen = digitalRead(Reed_Schalter_geschlossen);
  86.  
  87. // Serial.print(Status_Reedschalter_offen);
  88. // Serial.print("\t");
  89. // Serial.println(Status_Reedschalter_geschlossen);
  90.  
  91. String Status_Garagentor;
  92. //Serial.println("2");
  93.  
  94.  
  95. if (Status_Reedschalter_offen == 1 && Status_Reedschalter_geschlossen == 0){
  96.  
  97. Status_Garagentor = "offen";
  98.  
  99. }
  100.  
  101. else if (Status_Reedschalter_offen == 0 && Status_Reedschalter_geschlossen == 1){
  102.  
  103. Status_Garagentor = "geschlossen";
  104.  
  105. }
  106.  
  107. else if (Status_Reedschalter_offen == 0 && Status_Reedschalter_geschlossen == 0){
  108.  
  109. Status_Garagentor = "dazwischen";
  110.  
  111. }
  112. //Serial.println("3");
  113.  
  114.  
  115. // Check if a client has connected
  116. WiFiClient client = server.available();
  117. if (!client) {
  118. return;
  119. }
  120. // Serial.println("4");
  121.  
  122. // Wait until the client sends some data
  123. // Serial.println("new client");
  124. while(!client.available()){
  125. // delay(1);
  126. }
  127.  
  128. // Read the first line of the request
  129. String req = client.readStringUntil('\r');
  130. Serial.println(req);
  131. client.flush();
  132. // Serial.println("5");
  133.  
  134.  
  135. // Match the request
  136. int val;
  137. // Serial.println("6");
  138.  
  139.  
  140. if (req.indexOf("/Output") != -1){
  141. val = 0;
  142. digitalWrite(Relais, val);
  143. delay(200);
  144. val = 1;
  145. digitalWrite(Relais, val);
  146. // Serial.println("7");
  147. }
  148.  
  149.  
  150.  
  151.  
  152. else if (req.indexOf("/start") != -1){
  153.  
  154. //Prepare the response
  155. String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
  156. s += Status_Garagentor;
  157.  
  158. // Send the response to the client
  159. client.print(s);
  160. client.flush();
  161. // Serial.println("8");
  162.  
  163. }
  164.  
  165.  
  166. else {
  167. // Serial.println("invalid request");
  168. // client.stop();
  169. return;
  170. }
  171.  
  172.  
  173.  
  174. client.flush();
  175. // Serial.println("9");
  176.  
  177. // //Prepare the response
  178. // String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
  179. //
  180. // s += Status_Garagentor;
  181. // //s += (val)?"low":"high";
  182. //// s += "</html>\n";
  183.  
  184.  
  185.  
  186. // Send the response to the client
  187. // client.print(s);
  188. // delay(1);
  189. // Serial.println("Client disonnected");
  190.  
  191. // The client will actually be disconnected
  192. // when the function returns and 'client' object is detroyed
  193. }
Advertisement
Add Comment
Please, Sign In to add comment