Advertisement
metalx1000

ESP8266 Submit DHT11 Temperature to Webserver

Dec 2nd, 2021
1,696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright Kris Occhipinti December 2, 2021
  3.  * License GPLv3 - https://www.gnu.org/licenses/gpl-3.0.txt
  4.  * https://filmsbykris.com
  5.  *
  6. */
  7.  
  8. #include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
  9.  
  10. #include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
  11.  
  12. //needed for library
  13. #include <DNSServer.h>
  14. #include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
  15. #include <WiFiClient.h>
  16. #include <ESP8266HTTPClient.h>
  17.  
  18. DHTesp dht;
  19.  
  20. char url[]="http://yoururl.com/submit.php";
  21.  
  22. //TEMP SENSOR PIN
  23. int pin=14; //D5 on WeMos
  24.  
  25. //amount to wait before loop start
  26. int wait=10000;
  27.  
  28. //amount to wait each loop
  29. int loop_wait=5000;
  30.  
  31. void request();
  32.  
  33. void setup() {
  34.     WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  35.  
  36.     // put your setup code here, to run once:
  37.     Serial.begin(115200);
  38.  
  39.     Serial.println();
  40.     String thisBoard= ARDUINO_BOARD;
  41.     Serial.println(thisBoard);
  42.  
  43.   // Autodetect is not working reliable, don't use the following line
  44.   // dht.setup(17);
  45.   // use this instead:
  46.      dht.setup(pin, DHTesp::DHT11);
  47.      
  48.     // WiFi.mode(WiFi_STA); // it is a good practice to make sure your code sets wifi mode how you want it.
  49.  
  50.     //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
  51.     WiFiManager wm;
  52.  
  53.     //reset settings - wipe credentials for testing
  54.     //wm.resetSettings();
  55.  
  56.     // Automatically connect using saved credentials,
  57.     // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
  58.     // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
  59.     // then goes into a blocking loop awaiting configuration and will return success result
  60.  
  61.     bool res;
  62.     // res = wm.autoConnect(); // auto generated AP name from chipid
  63.     res = wm.autoConnect("Wifi_Tempature"); // anonymous ap
  64.     // res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
  65.  
  66.     if(!res) {
  67.         Serial.println("Failed to connect");
  68.         // ESP.restart();
  69.     }
  70.     else {
  71.         //if you get here you have connected to the WiFi    
  72.         Serial.println("connected...yeey :)");
  73.         request(url);
  74.     }
  75.   delay(wait);
  76. }
  77.  
  78. void request(char *URL){
  79.     WiFiClient client;
  80.  
  81.     HTTPClient http;
  82.  
  83.     Serial.print("[HTTP] begin...\n");
  84.     if (http.begin(client, URL)) {  // HTTP
  85.  
  86.  
  87.       Serial.print("[HTTP] GET...\n");
  88.       // start connection and send HTTP header
  89.       int httpCode = http.GET();
  90.  
  91.       // httpCode will be negative on error
  92.       if (httpCode > 0) {
  93.         // HTTP header has been send and Server response header has been handled
  94.         Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  95.  
  96.         // file found at server
  97.         if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
  98.           String payload = http.getString();
  99.           Serial.println(payload);
  100.         }
  101.       } else {
  102.         Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  103.       }
  104.  
  105.       http.end();
  106.     } else {
  107.       Serial.printf("[HTTP} Unable to connect\n");
  108.     }
  109.  
  110.  
  111.  
  112. }
  113.  
  114. void get_temp(){
  115.   delay(dht.getMinimumSamplingPeriod());
  116.  
  117.   float humidity = dht.getHumidity();
  118.   float temperature = dht.getTemperature();
  119.   float f = dht.toFahrenheit(temperature);
  120.   float index_c = dht.computeHeatIndex(temperature, humidity, false);
  121.   float index_f = dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true);
  122.  
  123.  
  124.   Serial.print(humidity, 1);
  125.   Serial.print(" humidity\t\t");
  126.   Serial.print(temperature, 1);
  127.   Serial.print("°C\t\t");
  128.   Serial.print(f, 1);
  129.   Serial.print("°F\t\tHeat Index:");
  130.   Serial.print(index_c, 1);
  131.   Serial.print("°C\t\t");
  132.   Serial.print(index_f, 1);
  133.   Serial.println("°F");
  134.  
  135.   String h = String(humidity, 1);// using a float and the decimal places
  136.   String c = String(temperature, 1);// using a float and the decimal places
  137.   String ft = String(f, 1);// using a float and the decimal places
  138.   String ic = String(index_c, 1);// using a float and the decimal places
  139.   String iF = String(index_f, 1);// using a float and the decimal places
  140.   String d = h+"|"+c+"|"+ft+"|"+ic+"|"+iF;
  141.  
  142.   char URL[100];
  143.   strcpy(URL, url);
  144.   strcat(URL, "?data=");
  145.   strcat(URL, d.c_str());
  146.   Serial.println(URL);
  147.   request(URL);
  148. }
  149.  
  150. void loop() {
  151.   get_temp();
  152.   //request(url);
  153.   delay(loop_wait);  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement