Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright Kris Occhipinti December 2, 2021
- * License GPLv3 - https://www.gnu.org/licenses/gpl-3.0.txt
- * https://filmsbykris.com
- *
- */
- #include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- //needed for library
- #include <DNSServer.h>
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
- #include <WiFiClient.h>
- #include <ESP8266HTTPClient.h>
- DHTesp dht;
- char url[]="http://yoururl.com/submit.php";
- //TEMP SENSOR PIN
- int pin=14; //D5 on WeMos
- //amount to wait before loop start
- int wait=10000;
- //amount to wait each loop
- int loop_wait=5000;
- void request();
- void setup() {
- WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial.println();
- String thisBoard= ARDUINO_BOARD;
- Serial.println(thisBoard);
- // Autodetect is not working reliable, don't use the following line
- // dht.setup(17);
- // use this instead:
- dht.setup(pin, DHTesp::DHT11);
- // WiFi.mode(WiFi_STA); // it is a good practice to make sure your code sets wifi mode how you want it.
- //WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
- WiFiManager wm;
- //reset settings - wipe credentials for testing
- //wm.resetSettings();
- // Automatically connect using saved credentials,
- // if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
- // if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
- // then goes into a blocking loop awaiting configuration and will return success result
- bool res;
- // res = wm.autoConnect(); // auto generated AP name from chipid
- res = wm.autoConnect("Wifi_Tempature"); // anonymous ap
- // res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
- if(!res) {
- Serial.println("Failed to connect");
- // ESP.restart();
- }
- else {
- //if you get here you have connected to the WiFi
- Serial.println("connected...yeey :)");
- request(url);
- }
- delay(wait);
- }
- void request(char *URL){
- WiFiClient client;
- HTTPClient http;
- Serial.print("[HTTP] begin...\n");
- if (http.begin(client, URL)) { // HTTP
- Serial.print("[HTTP] GET...\n");
- // start connection and send HTTP header
- int httpCode = http.GET();
- // httpCode will be negative on error
- if (httpCode > 0) {
- // HTTP header has been send and Server response header has been handled
- Serial.printf("[HTTP] GET... code: %d\n", httpCode);
- // file found at server
- if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
- String payload = http.getString();
- Serial.println(payload);
- }
- } else {
- Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- http.end();
- } else {
- Serial.printf("[HTTP} Unable to connect\n");
- }
- }
- void get_temp(){
- delay(dht.getMinimumSamplingPeriod());
- float humidity = dht.getHumidity();
- float temperature = dht.getTemperature();
- float f = dht.toFahrenheit(temperature);
- float index_c = dht.computeHeatIndex(temperature, humidity, false);
- float index_f = dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true);
- Serial.print(humidity, 1);
- Serial.print(" humidity\t\t");
- Serial.print(temperature, 1);
- Serial.print("°C\t\t");
- Serial.print(f, 1);
- Serial.print("°F\t\tHeat Index:");
- Serial.print(index_c, 1);
- Serial.print("°C\t\t");
- Serial.print(index_f, 1);
- Serial.println("°F");
- String h = String(humidity, 1);// using a float and the decimal places
- String c = String(temperature, 1);// using a float and the decimal places
- String ft = String(f, 1);// using a float and the decimal places
- String ic = String(index_c, 1);// using a float and the decimal places
- String iF = String(index_f, 1);// using a float and the decimal places
- String d = h+"|"+c+"|"+ft+"|"+ic+"|"+iF;
- char URL[100];
- strcpy(URL, url);
- strcat(URL, "?data=");
- strcat(URL, d.c_str());
- Serial.println(URL);
- request(URL);
- }
- void loop() {
- get_temp();
- //request(url);
- delay(loop_wait);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement