//Load "https://www.sitepoint.com/introduction-chart-js-2-0-six-examples/" to Set chart.js into your browser cache! //And then connect to newly created AP #include #include "RTClib.h" #include #include #include #include #include #include "DHT.h" #include "TM1637.h" #include tm1637 display(D0, D5); Ticker timer; bool tock = false; //Dns Server const byte DNS_PORT = 53; IPAddress apIP(192, 168, 1, 1); DNSServer dnsServer; //WEB SERVER ESP8266WebServer server(80); RTC_DS3231 rtc; //SET PIN OF YOUR DHT #define DHTPIN D6 // IO 12 for esp12 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); unsigned long timer1 = 0; String sdata; unsigned long timer2 = 0; int pdate = 0; //Serial Output #define DEBUG 1 //10 Minutes Interval for Logging #define INTERVAL 600 //Output Files on SPIFFS #define TFILE "tem.txt" #define HFILE "hum.txt" #define TIMEFILE "time.txt" int br = 4; int bl = 0; //##################################################### void setup() { if (DEBUG == 1)Serial.begin(115200); if (DEBUG == 1)Serial.println("init..."); if (! rtc.begin()) { if (DEBUG == 1)Serial.println("Couldn't find RTC"); while (1); } WiFi.mode(WIFI_AP); WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0)); WiFi.softAP("Temperature"); dnsServer.setTTL(300); // set which return code will be used for all other domains (e.g. sending // ServerFailure instead of NonExistentDomain will reduce number of queries // sent by clients) // default is DNSReplyCode::NonExistentDomain dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure); // start DNS server for all domain names dnsServer.start(DNS_PORT, "*", apIP); //Handle Webpage server.onNotFound([]() { htmldata(); }); server.begin(); Serial.println("HTTP server started"); //Read all data of file // sdata = ""; //spiff("data.txt", 1, ""); // if(DEBUG==1)Serial.println(sdata); //DHT init dht.begin(); if (rtc.lostPower()) { rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //rtc.adjust(DateTime(2016, 12, 24, 11,9, 0)); } DateTime now = rtc.now(); if (DEBUG == 1)Serial.print(now.year(), DEC); if (DEBUG == 1)Serial.print('/'); if (DEBUG == 1)Serial.print(now.month(), DEC); if (DEBUG == 1)Serial.print('/'); if (DEBUG == 1)Serial.print(now.day(), DEC); if (DEBUG == 1)Serial.print(' '); if (DEBUG == 1)Serial.print(now.hour(), DEC); if (DEBUG == 1)Serial.print(':'); if (DEBUG == 1)Serial.print(now.minute(), DEC); if (DEBUG == 1)Serial.print(':'); if (DEBUG == 1)Serial.print(now.second(), DEC); if (DEBUG == 1)Serial.println(); display.setBrightness(br); timer.attach(1, tick); } void tick(void) { tock = true; } void loop() { dnsServer.processNextRequest(); server.handleClient(); if (tock == true) { tock = false; DateTime now = rtc.now(); int sec = now.second(); int hr = now.hour(); int mi = now.minute(); if (hr > 12)hr = hr - 12; if (now.hour() > 22) { if (!br == 1) { br = 1; display.setBrightness(br); } } else if (now.hour() > 6) { if (!br == 4) { br = 4; display.setBrightness(br); } } if (bl == 1)bl = 0; else bl = 1; if (sec == 1) { //Read Temperature int t, h; int ok = 0; int tries=0; while (ok == 0) { tries++; if(tries>50)break; h = dht.readHumidity(); t = dht.readTemperature(); if (isnan(h) || isnan(t) ) { ok = 0; delay(100); } else if (t > 100) { ok = 0; delay(100); } else { ok = 1; } } display.clear(); delay(500); display.writeTime(now.day(), now.month(), 0); delay(5000); display.clear(); delay(500); display.writeTime(t,h , 0); delay(2000); display.clear(); delay(500); display.writeTime(hr, mi, bl); } else { display.writeTime(hr, mi, bl); } } if (((millis() - timer1) / 1000) > 600) { timer1 = millis(); } if (((millis() - timer2) / 1000) > INTERVAL) { DateTime now = rtc.now(); String date1; date1 += ",\""; date1 += now.day(); date1 += "/"; date1 += now.month(); date1 += "/"; date1 += now.year(); date1 += "\""; String time1; time1 += ",\""; time1 += now.hour(); time1 += ":"; time1 += now.minute(); time1 += "\""; //next day if (now.day() != pdate) { pdate = now.day(); time1 = date1; } int h = dht.readHumidity(); int t = dht.readTemperature(); if (isnan(h) || isnan(t) ) { // if(DEBUG==1)Serial.println("Sensor Err"); return; } if (t > 100 || h > 100)return; String t1; String h1; String t2; // d = time1; // d += " T:" ; // d += t; // d += " H:" ; // d += h; // d += "\r\n"; t1 = ","; t1 += t; h1 = ","; h1 += h; if (DEBUG == 1)Serial.print("Saving = "); if (DEBUG == 1)Serial.print(t); if (DEBUG == 1)Serial.print(" , "); if (DEBUG == 1)Serial.println(h); spiff(TFILE, 0, t1); spiff(HFILE, 0, h1); spiff(TIMEFILE, 0, time1); timer2 = millis(); } } //##################################################### void htmldata() { if (!SPIFFS.begin()) { if (DEBUG == 1)Serial.println("Failed to mount file system"); //return (0); } /* String path = "/"; Serial.println("handleFileList: " + path); Dir dir = SPIFFS.openDir(path); path = String(); String output = "["; while(dir.next()){ File entry = dir.openFile("r"); if (output != "[") output += ','; bool isDir = false; output += "{\"type\":\""; output += "{\"size\":\""; output+=entry.size(); output+="\""; output += (isDir)?"dir":"file"; output += "\",\"name\":\""; output += String(entry.name()); output += "\"}"; entry.close(); } output += "]"; //server.send(200, "text/json", output); */ // Prepare the response String s = "\r\nTemperature Logger By Mani (9915652222)

Temperature Logger By Mani (9915652222)

"; s += ""; s += ""; s += "\n"; server.send(200, "text/html", s); } //##################################################### // Usage: spiff(FILE_NAME,READ/WRITE,STRING DATA) //Write String Data: spiff("data.txt", 0, d); //Read String Data in "sdata" Variable spiff("data.txt", 1, d); //##################################################### long spiff(String file, int read, String value) { if (!SPIFFS.begin()) { if (DEBUG == 1)Serial.println("Failed to mount file system"); return (0); } file = "/" + file; FSInfo fs_info; SPIFFS.info(fs_info); if ((fs_info.totalBytes - fs_info.usedBytes) < 1000) { if (DEBUG == 1)Serial.println("Memory Full"); return (0); } File f; //Init the Spiff FS (check if our INIT file exists or not f = SPIFFS.open("init.txt", "r+"); if (!f) { if (DEBUG == 1)Serial.println("Formatting Spiff... Please Wait."); delay(200); //Format SPIFFS.format(); //Make a Init File File f1 = SPIFFS.open("init.txt", "w+"); f1.println("init"); f1.close(); if (DEBUG == 1)Serial.println("Done"); delay(200); } //###################################################### //If Reading Check file if (read == 1) { if (!SPIFFS.exists(file)) { if (DEBUG == 1) { Serial.print("File not exists: "); Serial.println(file); } return (0); } } //If Reading Open file R or A if (read == 1) { f = SPIFFS.open(file, "r"); } else { f = SPIFFS.open(file, "a"); } int s = 0; //if File Exists if (f) { s = f.size(); // if(DEBUG==1)Serial.printf("File Opened , Size=%d\r\n", s); //Read if (read == 1) { sdata = f.readString(); } else { //Write f.print(value); } f.close(); //Return the size of file return (s); } else { if (DEBUG == 1)Serial.print("Unable to open file "); if (DEBUG == 1)Serial.println(file); } }