Facebook
From Crimson Bison, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 32
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <WiFiClient.h>
  4. #include <Wire.h>
  5. #include <OneWire.h>
  6. #include <DallasTemperature.h>
  7.  
  8. #define ONE_WIRE_BUS_1 14
  9. #define ONE_WIRE_BUS_2 13
  10. const char* ssid = "świnkofon";
  11. const char* password = "ruchanie123";
  12.  
  13. float tempC, tempA ;
  14.  
  15.  
  16. WiFiServer server(80);
  17.    
  18. void handleRoot() {
  19. String  content = "<html> <head><title>Weather</title></head>";
  20. content +="<body style=\"background-color: #333; min-height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column\">";
  21. content +="<button style=\"cursor: pointer; margin: 20px; border: 5px solid #c31432; border-radius: 25px;\">><a style=\"display: flex; justify-content: center; align-items: center; text-decoration: none; color: black; padding: 10px 40px;\" href=\"handle_OnConnect\"><h1>handle_OnConnect</h1></a></button>";
  22. content +="<button style=\"cursor: pointer; margin: 20px; border: 5px solid #c31432; border-radius: 25px;\">><a style=\"display: flex; justify-content: center; align-items: center; text-decoration: none; color: black; padding: 10px 40px;\" href=\"handle_OnConnect1\"><h1>handle_OnConnect1</h1></a></button>";
  23. content +="<div>";
  24. content +="</body>";
  25. server.send(200, "text/html", content);
  26. }
  27.  
  28.  
  29. OneWire oneWire_in(ONE_WIRE_BUS_1);
  30. OneWire oneWire_out(ONE_WIRE_BUS_2);
  31.  
  32. // Pass our oneWire reference to Dallas Temperature sensor
  33.  
  34. DallasTemperature sensor_inhouse(&oneWire_in);
  35. DallasTemperature sensor_outhouse(&oneWire_out);          
  36.  
  37. void setup() {
  38.   WiFi.begin(ssid, password);
  39.  
  40.   while (WiFi.status() != WL_CONNECTED) {
  41.     delay(500);
  42.     Serial.print(".");
  43.   }
  44.   Serial.println("");
  45.   Serial.println("WiFi connected");
  46.  
  47.   // Start the server
  48.   server.begin();
  49.   Serial.println("Server started");
  50.  
  51.   // Print the IP address
  52.   Serial.println(WiFi.localIP());
  53.   server.begin();
  54.   server.on("/", handleRoot);
  55.   server.on("/handle_OnConnect", handle_OnConnect);
  56.   server.on("/handle_OnConnect1", handle_OnConnect1);
  57.  
  58.  
  59.   Serial.println("HTTP server started");
  60.  
  61. }
  62. void loop() {
  63.   server.handleClient();
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70. void handle_OnConnect() {
  71.   sensor_outhouse.requestTemperatures();
  72.   tempA = sensor_outhouse.getTempCByIndex(0);
  73.   server.send(200, "text/html", SendHTML(tempA));
  74. }
  75. void handle_OnConnect1() {
  76.  sensor_inhouse.requestTemperatures();
  77.   tempC = sensor_inhouse.getTempCByIndex(0);
  78.   server.send(200, "text/html", SendHTML1(tempC));
  79. }
  80.  
  81. String SendHTML(float tempA){
  82.   String ptr = "<!DOCTYPE html> <html>\n";
  83.   ptr +="<body>\n";
  84.   ptr +="<div id=\"webpage\">\n";
  85.   ptr +="<p>Temperature: ";
  86.   ptr +=tempA;
  87.   ptr +="&deg;C</p>";
  88.   ptr +="</div>\n";
  89.   ptr +="</body>\n";
  90.   ptr +="</html>\n";
  91.   return ptr;
  92. }
  93. String SendHTML1(float tempC){
  94.   String ptr = "<!DOCTYPE html> <html>\n";
  95.   ptr +="<body>\n";
  96.   ptr +="<div id=\"webpage\">\n";
  97.   ptr +="<p>Temperature: ";
  98.   ptr +=tempC;
  99.   ptr +="&deg;C</p>";
  100.   ptr +="</div>\n";
  101.   ptr +="</body>\n";
  102.   ptr +="</html>\n";
  103.   return ptr;
  104. }