Advertisement
metalx1000

ESP8266 Temperature Sensor to Server

Jun 9th, 2020
2,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266WiFiMulti.h>
  5.  
  6. #include <ESP8266HTTPClient.h>
  7.  
  8. #include <WiFiClient.h>
  9. #include "DHT.h"
  10.  
  11. #define room "office"
  12.  
  13. #define DHTPIN 2     // Digital pin connected to the DHT sensor
  14. #define DHTTYPE DHT11   // DHT 11
  15. DHT dht(DHTPIN, DHTTYPE);
  16.  
  17.  
  18. ESP8266WiFiMulti WiFiMulti;
  19.  
  20. void setup() {
  21.  
  22.   Serial.begin(115200);
  23.   // Serial.setDebugOutput(true);
  24.  
  25.   Serial.println();
  26.   Serial.println();
  27.   Serial.println();
  28.  
  29.   for (uint8_t t = 4; t > 0; t--) {
  30.     Serial.printf("[SETUP] WAIT %d...\n", t);
  31.     Serial.flush();
  32.     delay(1000);
  33.   }
  34.  
  35.   WiFi.mode(WIFI_STA);
  36.   WiFiMulti.addAP("<SSID>", "<WPA-Phrase>");
  37.  
  38.   dht.begin();
  39. }
  40.  
  41. void loop() {
  42.   // wait for WiFi connection
  43.   if ((WiFiMulti.run() == WL_CONNECTED)) {
  44.  
  45.   ///////////////////////DHT///////////////////////
  46.   // Wait a few seconds between measurements.
  47.   delay(2000);
  48.  
  49.   // Reading temperature or humidity takes about 250 milliseconds!
  50.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  51.   float h = dht.readHumidity();
  52.   String H = String(h);
  53.   // Read temperature as Celsius (the default)
  54.   float t = dht.readTemperature();
  55.   // Read temperature as Fahrenheit (isFahrenheit = true)
  56.   float f = dht.readTemperature(true);
  57.  
  58.   // Check if any reads failed and exit early (to try again).
  59.   if (isnan(h) || isnan(t) || isnan(f)) {
  60.     Serial.println(F("Failed to read from DHT sensor!"));
  61.     return;
  62.   }
  63.  
  64.   // Compute heat index in Fahrenheit (the default)
  65.   float hif = dht.computeHeatIndex(f, h);
  66.   String temp_f = String(f);
  67.  
  68.   // Compute heat index in Celsius (isFahreheit = false)
  69.   float hic = dht.computeHeatIndex(t, h, false);
  70.  
  71.   Serial.print(F("Humidity: "));
  72.   Serial.print(h);
  73.   Serial.print(F("%  Temperature: "));
  74.   Serial.print(t);
  75.   Serial.print(F("°C "));
  76.   Serial.print(f);
  77.   Serial.print(F("°F  Heat index: "));
  78.   Serial.print(hic);
  79.   Serial.print(F("°C "));
  80.   Serial.print(hif);
  81.   Serial.println(F("°F"));
  82.   //////////END DHT///////////////////
  83.  
  84.     WiFiClient client;
  85.  
  86.     HTTPClient http;
  87.  
  88.     Serial.print("[HTTP] begin...\n");
  89.    
  90.     String url = "http://site.com/esp/temp_log/log.php?key=1234&temp=" + temp_f + "&hum=" + h + "&room=" + room;
  91.     Serial.println(url);
  92.     if (http.begin(client, url)) {  // HTTP
  93.  
  94.  
  95.       Serial.print("[HTTP] GET...\n");
  96.       // start connection and send HTTP header
  97.       int httpCode = http.GET();
  98.  
  99.       // httpCode will be negative on error
  100.       if (httpCode > 0) {
  101.         // HTTP header has been send and Server response header has been handled
  102.         Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  103.  
  104.         // file found at server
  105.         if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
  106.           String payload = http.getString();
  107.           Serial.println(payload);
  108.         }
  109.       } else {
  110.         Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  111.       }
  112.  
  113.       http.end();
  114.     } else {
  115.       Serial.printf("[HTTP} Unable to connect\n");
  116.     }
  117.   }
  118.  
  119.   delay(60000);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement