/* Smart Thermostat by Roque Campos Sketch to read temperatures with an ESP32 board and a DHTxx sensor Complete Project Details https://www.smart-thermostat.eu */ #include #include #include #include "esp_system.h" #include "soc/rtc.h" #include "rom/uart.h" int timezone = 3; // Replace with your network credentials const char* ssid = "SSID"; const char* password = "PASSWORD"; const String nodeName = "NODENAME"; //Don't touch anything below this point unless you know what you are doing. const String sketchVersion = "2"; const String nodeType = "TH"; String thError = ""; WiFiServer server(80); //Onboard led const int ONBOARD_LED_PIN = 2; // DHT Sensor const int DHTPin = 18; // Initialize DHT sensor. DHTesp dht; // Client variables char linebuf[80]; int charcount = 0; void setup() { Serial.begin(115200); pinMode(ONBOARD_LED_PIN, OUTPUT); // Uncomment your sensor type, from DHTesp.h line 102 dht.setup(DHTPin, DHTesp::AUTO_DETECT); // dht.setup(DHTPin, DHTesp::DHT11); // dht.setup(DHTPin, DHTesp::DHT22); // dht.setup(DHTPin, DHTesp::AM2302); // dht.setup(DHTPin, DHTesp::RHT03); setUpWifi(); configTime(7200, -3600, "pool.ntp.org", "time.nist.gov"); rtc_clk_cpu_freq_set(RTC_CPU_FREQ_160M); uart_tx_wait_idle(0); int clockspeed= rtc_clk_cpu_freq_get(); char* clockSpeeds[5]={"XTAL","80Mhz","160Mhz","240Mhz","2Mhz"}; Serial.print("CPU freq : "); Serial.print(clockSpeeds[clockspeed]); Serial.print("\n"); } void setUpWifi() { Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); // attempt to connect to Wifi network: int counter = 0; while (WiFi.status() != WL_CONNECTED) { WiFi.disconnect(); WiFi.begin(ssid, password); int counter = 0; while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); counter++; if (counter > 10) { break; } } } Serial.println(""); Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); Serial.print("MAC: "); Serial.println(WiFi.macAddress()); server.begin(); } double celsius2Fahrenheit(double celsius) { return celsius * 9 / 5 + 32; } void loop() { if (WiFi.status() != WL_CONNECTED) { setUpWifi(); } // listen for incoming clients WiFiClient client = server.available(); if (client) { digitalWrite(ONBOARD_LED_PIN, HIGH); Serial.println("New client"); memset(linebuf, 0, sizeof(linebuf)); charcount = 0; // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); //read char by char HTTP request linebuf[charcount] = c; if (charcount < sizeof(linebuf) - 1) charcount++; // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { int chk = OK; double celsiusTemp = -10000; double fahrenheitTemp = -10000; float temperature = dht.getTemperature(); float humidity = dht.getHumidity(); if (dht.getStatus() !=0) { // Print the error on a new line to be clear Serial.println("DHT Error status: " + String(dht.getStatusString())); } time_t rawtime; struct tm * timeinfo; time (&rawtime); timeinfo = localtime (&rawtime); char date [80]; strftime(date,80,"%d/%m/%Y %T ",timeinfo); Serial.print( String(date) + " DHT " + String(dht.getStatusString())); Serial.print(" TempCelcius= "); Serial.print((float)temperature); Serial.print(" C°\t"); Serial.print("Humidity= "); Serial.print((float)humidity); Serial.print("\t"); celsiusTemp = (float)temperature; fahrenheitTemp = celsius2Fahrenheit(celsiusTemp); // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: application/json"); client.println(); client.println("{"); client.println(); client.println("\"nodeType\":\"" + nodeType + "\","); client.println("\"name\":\"" + nodeName + "\","); client.println("\"version\":\"" + sketchVersion + "\","); client.println("\"thError\":\"" + thError + "\","); client.println("\"mac\":\"" + WiFi.macAddress() + "\","); String cc = String(celsiusTemp); cc.trim(); client.print("\"celsius\":\"" + cc); client.println("\","); String ff = String(fahrenheitTemp); ff.trim(); client.print("\"fahrenheit\":\"" + ff); client.println("\","); String hh = String(humidity); hh.trim(); client.print("\"humidity\":\"" + hh); client.println("\""); client.println(); client.println("}"); client.println(); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; memset(linebuf, 0, sizeof(linebuf)); charcount = 0; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(250); // close the connection: client.stop(); Serial.println("client disconnected"); digitalWrite(ONBOARD_LED_PIN, LOW); } }