Code
Code
//Input Pins
#include <WiFi.h> const uint8_t powerPin = 36;
#include <EasyButton.h> const uint8_t modePin = 39;
#include <DHTesp.h> const uint8_t tempDownPin = 34;
#include <AsyncMqttClient.h> const uint8_t tempUpPin = 35;
#include <U8g2lib.h> const uint8_t DHTPin = 32;
#include <WiFiClient.h>
#include <WebServer.h> //Output Pins
#include <ESPmDNS.h> const uint8_t buzzerPin=26;
#include <Update.h> const uint8_t compressorPin = 33;
const uint8_t fanPin = 25;
//Network Parameters
#define WIFI_SSID "IoTnet" //DHT Sensor
#define WIFI_PASSWORD "1l4E1g0R9@2n9D" DHTesp dht;
#define MQTT_HOST IPAddress(10, 26, 178, 10) int dhtPin = 32;
#define MQTT_PORT 1883 //const uint8_t tempSensor = A4;
String mqttTopic = "ac_GuestRoom";
const char* host = "esp32"; //System Variables
WebServer server(80); float sensorTemperature;
String sensorTempOld;
//OLED Display Driver float setTemperature = 21.00;
U8G2_SH1106_128X64_NONAME_1_HW_I2C float setTempOld = 0;
u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); long readTempTimer = 0;
int acPower = 0;
//External Timers int acMode = 1;
extern "C" { char ip[32];
#include "freertos/FreeRTOS.h" char acPowerChar[15];
#include "freertos/timers.h" char acModeChar[15];
} char acSetTempChar[15];
TimerHandle_t mqttReconnectTimer; char acTempSensor[15];
TimerHandle_t wifiReconnectTimer; String IPaddress;
void ota(){
/*use mdns for host name resolution*/
if (!MDNS.begin(host)) { //http://esp32.local
Serial.println("Error setting up MDNS responder!");
while (1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
/*return index page which is stored in serverIndex */
server.on("/", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", loginIndex);
});
server.on("/serverIndex", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});
/*handling uploading firmware file */
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ?
"FAIL" : "OK");
ESP.restart();
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\n",
upload.filename.c_str());
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_8x13B_tf);
u8g2.drawStr(2,20,"Firmware Update...");
} while ( u8g2.nextPage() );
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
//start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
/* flashing firmware to ESP*/
if (Update.write(upload.buf, upload.currentSize) !=
upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the
current progress
Serial.printf("Update Success:
%u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
}
});
server.begin();