Advertisement
Corium

Untitled

Dec 4th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid = "XXXXXXXXX";
  4. const char* password = "YYYYYYYYYYY";
  5. int ledPin = 5; // GPIO13
  6. WiFiServer server(80);
  7.  
  8. #include "DHT.h"
  9. #define DHTPIN 14     // what digital pin we're connected to
  10. #define DHTTYPE DHT11   // DHT 11
  11. DHT dht(DHTPIN, DHTTYPE);
  12.  
  13. void setup() {
  14.   Serial.begin(115200);
  15.   delay(10);
  16.   pinMode(ledPin, OUTPUT);
  17.   digitalWrite(ledPin, LOW);
  18.   Serial.println("DHTxx test!");
  19.   dht.begin();
  20.  
  21.   // Connect to WiFi network
  22.   Serial.println();
  23.   Serial.println();
  24.   Serial.print("Connecting to ");
  25.   Serial.println(ssid);
  26.   WiFi.begin(ssid, password);
  27.  
  28.   while (WiFi.status() != WL_CONNECTED) {
  29.     delay(500);
  30.     Serial.print(".");
  31.   }
  32.   Serial.println("");
  33.   Serial.println("WiFi connected");
  34.  
  35.   // Start the server
  36.   server.begin();
  37.   Serial.println("Server started");
  38.  
  39.   // Print the IP address
  40.   Serial.print("Use this URL to connect: ");
  41.   Serial.print("http://");
  42.   Serial.print(WiFi.localIP());
  43.   Serial.println("/");
  44.  
  45. }
  46.  
  47. void loop() {
  48.   // Check if a client has connected
  49.   WiFiClient client = server.available();
  50.   if (!client) {
  51.     return;
  52.   }
  53.  
  54.   // Wait until the client sends some data
  55.   Serial.println("new client");
  56.   while(!client.available()){
  57.     delay(1);
  58.   }
  59.  
  60.   // Read the first line of the request
  61.   String request = client.readStringUntil('\r');
  62.   Serial.println(request);
  63.   client.flush();
  64.  
  65.   // Match the request
  66.  
  67.   int value = LOW;
  68.   if (request.indexOf("/LED=ON") != -1)  {
  69.     digitalWrite(ledPin, HIGH);
  70.     value = HIGH;
  71.   }
  72.   if (request.indexOf("/LED=OFF") != -1)  {
  73.     digitalWrite(ledPin, LOW);
  74.     value = LOW;
  75.   }
  76.  
  77. // Set ledPin according to the request
  78. //digitalWrite(ledPin, value);
  79.  
  80.   // Return the response
  81.   client.println("HTTP/1.1 200 OK");
  82.   client.println("Content-Type: text/html");
  83.   client.println(""); //  do not forget this one
  84.   client.println("<!DOCTYPE HTML>");
  85.   client.println("<html>");
  86.  
  87.   client.print("Led pin is now: ");
  88.  
  89.   if(value == HIGH) {
  90.     client.print("On");
  91.   } else {
  92.     client.print("Off");
  93.   }
  94.   client.println("<br><br>");
  95.   client.println(getMeasurement());
  96.   client.println("<br><br>");
  97.   client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  98.   client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  
  99.   client.println("</html>");
  100.  
  101.   delay(1);
  102.   Serial.println("Client disonnected");
  103.   Serial.println("");
  104.  
  105. }
  106.  
  107.  
  108. String getMeasurement()
  109. {
  110.   delay(100);
  111.   // Reading temperature or humidity takes about 250 milliseconds!
  112.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  113.   float h = dht.readHumidity();
  114.   // Read temperature as Celsius (the default)
  115.   float t = dht.readTemperature();
  116.   // Read temperature as Fahrenheit (isFahrenheit = true)
  117.   float f = dht.readTemperature(true);
  118.  
  119.   // Check if any reads failed and exit early (to try again).
  120.   if (isnan(h) || isnan(t) || isnan(f)) {
  121.     Serial.println("Failed to read from DHT sensor!");
  122.     return "Failed to read from DHT sensor!";
  123.   }
  124.  
  125.   // Compute heat index in Fahrenheit (the default)
  126.   float hif = dht.computeHeatIndex(f, h);
  127.   // Compute heat index in Celsius (isFahreheit = false)
  128.   float hic = dht.computeHeatIndex(t, h, false);
  129.  
  130.   String resultString =
  131.   ("Humidity: ")
  132.   + String(h)
  133.   + (" %\t")
  134.   + ("Temperature: ")
  135.   + String(t)
  136.   + (" *C ")
  137.   + String(f)
  138.   + (" *F\t")
  139.   + ("Heat index: ")
  140.   + String(hic)
  141.   + (" *C ")
  142.   + String(hif)
  143.   + (" *F");
  144.  
  145.   return resultString;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement