Advertisement
Guest User

ESP8266 - Thermostat - Cooling

a guest
Dec 27th, 2019
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 20.25 KB | None | 0 0
  1. /* ESP8266 Programmable Thermostat IoT
  2.    Version 1.0  1/7/2017  Version 1.0   Damon Borgnino
  3.    Revision by: Martin Chlebovec (martinius96)
  4.    Web: https://arduino.php5.sk?lang=en
  5. */
  6. #include <ESP8266WiFi.h>
  7. #include <WiFiClient.h>
  8. #include <ESP8266WebServer.h>
  9. #include <FS.h> // FOR SPIFFS
  10. #include <ctype.h> // for isNumber check
  11. #include <OneWire.h>
  12. #include <DallasTemperature.h>
  13. #define ONE_WIRE_BUS 2  //D4
  14. OneWire oneWire(ONE_WIRE_BUS);
  15. DallasTemperature sensors(&oneWire);
  16. #define RELAYPIN 4 // D2
  17. const char* ssid     = "WiFI_NAME";
  18. const char* password = "WIFI_PASSWORD";
  19.  
  20. int CoolingOn = 73;
  21. int CoolingOff = 69;
  22. String relayState = "OFF";
  23. const static String fName = "prop.txt";
  24. const static String dfName = "data.txt";
  25. int dataLines = 0;
  26. int maxFileData = 20;
  27.  
  28.  
  29. ESP8266WebServer server(80);
  30.  
  31. // Initialize DHT sensor
  32.  
  33. // This is for the ESP8266 processor on ESP-01
  34. //DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
  35.  
  36. float temp_f;
  37. float temp_f2;  // Values read from sensor
  38. String webString = "";   // String to display
  39. String webMessage = "";
  40. // Generally, you should use "unsigned long" for variables that hold time to handle rollover
  41. unsigned long previousMillis = 0;        // will store last temp was read
  42. unsigned long interval = 20000;              // interval at which to read sensor
  43.  
  44.  
  45. // reads the temp and Temperature 2 from the DHT sensor and sets the global variables for both
  46. void gettemperature() {
  47.   sensors.requestTemperatures();
  48.   delay(1000); //wait for datas
  49.   temp_f = sensors.getTempCByIndex(0);
  50.   temp_f2 = sensors.getTempCByIndex(1);
  51.   if (temp_f >= CoolingOn)
  52.   {
  53.     digitalWrite(RELAYPIN, HIGH);
  54.     relayState = "ON";
  55.   }
  56.   else if (temp_f <= CoolingOff)
  57.   {
  58.     digitalWrite(RELAYPIN, LOW);
  59.     relayState = "OFF";
  60.   }
  61. }
  62.  
  63. /////////////////////////////////////////////////
  64.  
  65. void clearDataFile() // deletes all the stored data
  66. {
  67.   File f = SPIFFS.open(dfName, "w");
  68.   if (!f) {
  69.     Serial.println("data file open to clear failed");
  70.   }
  71.   else
  72.   {
  73.     Serial.println("-- Data file cleared =========");
  74.     f.close();
  75.   }
  76. }
  77.  
  78. ///////////////////////////////////////////////////////////////////////////////////
  79. // removes the first line of a file by writing all data except the first line
  80. // to a new file. The old file is deleted and new file is renamed.
  81. ///////////////////////////////////////////////////////////////////////////////////
  82.  
  83. void removeFileLine(String fi)
  84. {
  85.   File original = SPIFFS.open(fi, "r");
  86.   if (!original) {
  87.  
  88.     Serial.println("original data file open failed");
  89.   }
  90.  
  91.   File temp = SPIFFS.open("tempfile.txt", "w");
  92.   if (!temp) {
  93.  
  94.     Serial.println("temp data file open failed");
  95.   }
  96.   Serial.println("------ Removing Data Lines ------");
  97.   //Lets read line by line from the file
  98.   for (int i = 0; i < maxFileData; i++) {
  99.     String str = original.readStringUntil('\n'); // read a line
  100.     if (i > 0) { // skip writing first line to the temp file
  101.       temp.println(str);
  102.     }
  103.   }
  104.  
  105.   int origSize = original.size();
  106.   int tempSize = temp.size();
  107.   temp.close();
  108.   original.close();
  109.  
  110.   Serial.print("size orig: "); Serial.print(origSize); Serial.println(" bytes");
  111.   Serial.print("size temp: "); Serial.print(tempSize); Serial.println(" bytes");
  112.  
  113.   if (! SPIFFS.remove(dfName))
  114.     Serial.println("Remove file failed");
  115.  
  116.  
  117.   if (! SPIFFS.rename("tempfile.txt", dfName))
  118.     Serial.println("Rename file failed");
  119.   // dataLines--;
  120. }
  121.  
  122. //////////////////////////////////////////////////////////////////////
  123. // writes the most recent variable values to the data file          //
  124. //////////////////////////////////////////////////////////////////////
  125.  
  126. void updateDataFile()
  127. {
  128.   Serial.println("dataLines: ");
  129.   Serial.println(dataLines);
  130.   if (dataLines >= maxFileData)
  131.   {
  132.     removeFileLine(dfName);
  133.   }
  134.   ///////
  135.   File f = SPIFFS.open(dfName, "a");
  136.   if (!f) {
  137.  
  138.     Serial.println("data file open failed");
  139.   }
  140.   else
  141.   {
  142.     Serial.println("====== Writing to data file =========");
  143.  
  144.     f.print(relayState); f.print(":");
  145.     f.print(temp_f); f.print( ","); f.println(temp_f2);
  146.  
  147.     Serial.println("Data file updated");
  148.     f.close();
  149.   }
  150.  
  151.   Serial.print("millis: ");
  152.   Serial.println(millis());
  153.  
  154. }
  155.  
  156. //////////////////////////////////////////////////////////////////////////////
  157. // reads the data and formats it so that it can be used by google charts    //
  158. //////////////////////////////////////////////////////////////////////////////
  159.  
  160. String readDataFile()
  161. {
  162.   String returnStr = "";
  163.   File f = SPIFFS.open(dfName, "r");
  164.  
  165.   if (!f)
  166.   {
  167.     Serial.println("Data File Open for read failed.");
  168.  
  169.   }
  170.   else
  171.   {
  172.     Serial.println("----Reading Data File-----");
  173.     dataLines = 0;
  174.  
  175.     while (f.available()) {
  176.  
  177.       //Lets read line by line from the file
  178.       dataLines++;
  179.       String str = f.readStringUntil('\n');
  180.       String switchState =  str.substring(0, str.indexOf(":")  );
  181.       /*
  182.           String tempF = str.substring(str.indexOf(":") + 1, str.indexOf(",")  );
  183.         //  String humid = str.substring(str.indexOf(",") + 1 );
  184.         //    String milliSecs = str.substring(str.indexOf("~") + 1 , str.indexOf("~"));
  185.         //   Serial.println(tempF);
  186.         //   Serial.println(humid);
  187.         //  Serial.println(str);
  188.       */
  189.  
  190.       returnStr += ",['" + switchState + "'," + str.substring(str.indexOf(":") + 1) + "]";
  191.     }
  192.     f.close();
  193.   }
  194.  
  195.   return returnStr;
  196.  
  197. }
  198.  
  199. ////////////////////////////////////////////////////////////////////////////////////
  200. //      creates the HTML string to be sent to the client                          //
  201. ////////////////////////////////////////////////////////////////////////////////////
  202.  
  203. void setHTML()
  204. {
  205.   webString = "<html><head>\n";
  206.   webString += "<meta http-equiv=\"refresh\" content=\"60;url=http://" + WiFi.localIP().toString() + "\"> \n";
  207.  
  208.   ///////////// google charts script
  209.   webString += "<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script> \n";
  210.   webString += "   <script type=\"text/javascript\"> \n";
  211.   webString += "    google.charts.load('current', {'packages':['corechart','gauge']}); \n";
  212.   webString += "    google.charts.setOnLoadCallback(drawTempChart); \n";
  213.   webString += "    google.charts.setOnLoadCallback(drawHumidChart); \n";
  214.   webString += "    google.charts.setOnLoadCallback(drawChart); \n ";
  215.  
  216.   // draw temp guage
  217.   webString += "   function drawTempChart() { \n";
  218.   webString += "      var data = google.visualization.arrayToDataTable([ \n";
  219.   webString += "        ['Label', 'Value'], ";
  220.   webString += "        ['Temp\xB0',  ";
  221.   webString += temp_f;
  222.   webString += " ], ";
  223.   webString += "       ]); \n";
  224.   // setup the google chart options here
  225.   webString += "    var options = {";
  226.   webString += "      width: 250, height: 150,";
  227.   webString += "      min: -10, max: 120,";
  228.   webString += "      greenFrom: -10, greenTo: " + String(CoolingOff) + ",";
  229.   webString += "      yellowFrom: " + String(CoolingOff) + ", yellowTo: " + String(CoolingOn) + ",";
  230.   webString += "      redFrom: " + String(CoolingOn) + ", redTo: 120,";
  231.   webString += "       minorTicks: 5";
  232.   webString += "    }; \n";
  233.   webString += "   var chart = new google.visualization.Gauge(document.getElementById('chart_divTemp')); \n";
  234.   webString += "  chart.draw(data, options); \n";
  235.   webString += "  } \n";
  236.  
  237.   // draw Temperature 2 guage
  238.   webString += "   function drawHumidChart() { \n";
  239.   webString += "      var data = google.visualization.arrayToDataTable([ \n";
  240.   webString += "        ['Label', 'Value'], ";
  241.   webString += "        ['Temprature 2',  ";
  242.   webString += temp_f2;
  243.   webString += " ], ";
  244.   webString += "       ]); \n";
  245.   // setup the google chart options here
  246.   webString += "    var options = {";
  247.   webString += "      width: 250, height: 150,";
  248.   webString += "      min: -10, max: 120,";
  249.   webString += "      greenFrom: -10, greenTo: " + String(CoolingOff) + ",";
  250.   webString += "      yellowFrom: " + String(CoolingOff) + ", yellowTo: " + String(CoolingOn) + ",";
  251.   webString += "      redFrom: " + String(CoolingOn) + ", redTo: 120,";
  252.   webString += "       minorTicks: 5";
  253.   webString += "    }; \n";
  254.   webString += "   var chart = new google.visualization.Gauge(document.getElementById('chart_divHumid')); \n";
  255.   webString += "  chart.draw(data, options); \n";
  256.   webString += "  } \n";
  257.  
  258.  
  259.   // draw main graph
  260.   webString += "    function drawChart() { \n";
  261.  
  262.   webString += "     var data = google.visualization.arrayToDataTable([ \n";
  263.   webString += "       ['Hit', 'Temp F', 'Temperature 2'] ";
  264.  
  265.  
  266.   webString += readDataFile();
  267.   // open file for reading
  268.  
  269.   webString += "     ]); \n";
  270.   webString += "     var options = { \n";
  271.   webString += "        title: 'Temp/Temperature 2 Activity', ";
  272.   webString += "        curveType: 'function', \n";
  273.  
  274.   webString += "  series: { \n";
  275.   webString += "         0: {targetAxisIndex: 0}, \n";
  276.   webString += "         1: {targetAxisIndex: 1} \n";
  277.   webString += "       }, \n";
  278.  
  279.   webString += "  vAxes: { \n";
  280.   webString += "         // Adds titles to each axis. \n";
  281.   webString += "         0: {title: 'Temp Fahrenheit'}, \n";
  282.   webString += "         1: {title: 'Temperature 2 %'} \n";
  283.   webString += "       }, \n";
  284.  
  285.   webString += "  hAxes: { \n";
  286.   webString += "         // Adds titles to each axis. \n";
  287.   webString += "         0: {title: 'Cooling On/Off'}, \n";
  288.   webString += "         1: {title: ''} \n";
  289.   webString += "       }, \n";
  290.  
  291.   webString += "         legend: { position: 'bottom' } \n";
  292.   webString += "       }; \n";
  293.  
  294.   webString += "       var chart = new google.visualization.LineChart(document.getElementById('curve_chart')); \n";
  295.  
  296.   webString += "       chart.draw(data, options); \n";
  297.   webString += "      } \n";
  298.  
  299.   ////////////
  300.   webString += "    </script> \n";
  301.   webString += "</head><body>\n";
  302.   webString += "<form action=\"http://" + WiFi.localIP().toString() + "/submit\" method=\"POST\">";
  303.   webString += "<h1>ESP8266-12E DS18B20 Thermostat IoT</h1>\n";
  304.   webString += "<div style=\"color:red\">" + webMessage + "</div>\n";
  305.  
  306.   webString += "<table style=\"width:800px;\"><tr><td>";
  307.   webString += "<div>Cooling On Setting: &ge; " + String((int)CoolingOn) + "&deg; C</div>\n";
  308.   webString += "<div>Cooling Off Setting: &le; " + String((int)CoolingOff) + "&deg; C</div>\n";
  309.   webString += "</td><td style=\"text-align:right\">";
  310.   webString += "Cooling On: &ge; <input type='text' value='" + String((int)CoolingOn) + "' name='CoolingOff' maxlength='3' size='2'><br>\n";
  311.   webString += "Cooling Off: &le; <input type='text' value='" + String((int)CoolingOff) + "' name='CoolingOn' maxlength='3' size='2'><br>\n";
  312.   webString += "</td><td style=\"text-align:right\">";
  313.   webString += "Sample Rate (seconds): <input type='text' value='" + String((long)interval / 1000) + "' name='sRate' maxlength='3' size='2'><br>\n";
  314.   webString += "Maximum Chart Data: <input type='text' value='" + String((long)maxFileData) + "' name='maxData' maxlength='3' size='2'><br>\n";
  315.  
  316.   webString += "</td><td>";
  317.   webString += "<input type='submit' value='Submit' >";
  318.   webString += "</td></tr></table>\n";
  319.  
  320.   webString += "<table style=\"width:800px;\"><tr><td>";
  321.   webString += "<div style=\"width:300px\"><b>Last Readings</b></div>\n";
  322.   webString += "<div>Temperature: " + String(temp_f, 2) + "&deg; C</div>\n";
  323.   webString += "<div>Tempreature2: " + String(temp_f2, 2) + "&deg; C</div>\n";
  324.  
  325.   if (digitalRead(RELAYPIN) == LOW)
  326.     webString += "<div>Cooling is OFF</div>";
  327.   else
  328.     webString += "<div>Cooling is ON</div>";
  329.  
  330.   webString += "<div>Data Lines: " + String((int)dataLines) + "</div> \n";
  331.   //  webString += "<div>Total On Time: " + String( ((onCount * interval) / 1000) / 60 ) + " minutes</div> \n";
  332.   webString += "<div>Sample Rate: " + String(interval / 1000) + " seconds</div> \n";
  333.  
  334.   webString += "<div><a href=\"http://" + WiFi.localIP().toString() + "\">Refresh</a></div>\n";
  335.   webString += "</td><td>";
  336.   webString += "<div id=\"chart_divTemp\" style=\"width: 250px;\"></div>\n";
  337.   webString += "</td><td>";
  338.   webString += "<div id=\"chart_divHumid\" style=\"width: 250px;\"></div>\n";
  339.   webString += "</td></tr></table>\n";
  340.   webString += "<div id=\"curve_chart\" style=\"width: 1000px; height: 500px\"></div> \n";
  341.   webString += "<div><a href=\"/clear\">Clear Data</a></div> \n";
  342.  
  343.   webString += "</body></html>\n";
  344. }
  345.  
  346. //////////////////////////////////
  347. ///  used for error checking   ///
  348. //////////////////////////////////
  349.  
  350. boolean isValidNumber(String str) {
  351.   for (byte i = 0; i < str.length(); i++)
  352.   {
  353.     if (isDigit(str.charAt(i))) return true;
  354.   }
  355.   return false;
  356. }
  357.  
  358. ////////////////////////////////////////////////////////////////////
  359. // handler for web server request: http://IpAddress/submit        //
  360. ////////////////////////////////////////////////////////////////////
  361.  
  362. void handle_submit() {
  363.  
  364.   webMessage = "";
  365.   int tempON = 0;
  366.   int tempOFF = 0;
  367.   long sRate = 0;
  368.   int maxData = 0;
  369.  
  370.   if (server.args() > 0 ) {
  371.     for ( uint8_t i = 0; i < server.args(); i++ ) {
  372.  
  373.       if (server.argName(i) == "CoolingOff") {
  374.  
  375.         if (server.arg(i) != "")
  376.         {
  377.           if (isValidNumber(server.arg(i)) )
  378.             tempON = server.arg(i).toInt();
  379.           else
  380.             webMessage += "Cooling Off must be a number<br>";
  381.         }
  382.         else
  383.           webMessage += "Cooling Off is required<br>";
  384.       }
  385.  
  386.       if (server.argName(i) == "CoolingOn") {
  387.  
  388.         if (server.arg(i) != "")
  389.         {
  390.           if (isValidNumber(server.arg(i)) )
  391.             tempOFF = server.arg(i).toInt();
  392.           else
  393.             webMessage += "Cooling On must be a number<br>";
  394.  
  395.         }
  396.         else
  397.           webMessage += "Cooling On is required<br>";
  398.       }
  399.  
  400.  
  401.       if (server.argName(i) == "sRate") {
  402.  
  403.         if (server.arg(i) != "")
  404.         {
  405.           if (isValidNumber(server.arg(i)) )
  406.             sRate = server.arg(i).toInt();
  407.           else
  408.             webMessage += "Sample Rate must be a number<br>";
  409.         }
  410.         else
  411.           webMessage += "Sample Rate is required<br>";
  412.       }
  413.  
  414.       if (server.argName(i) == "maxData") {
  415.  
  416.         if (server.arg(i) != "")
  417.         {
  418.           if (isValidNumber(server.arg(i)) )
  419.             maxData = server.arg(i).toInt();
  420.           else
  421.             webMessage += "Max Chart Data must be a number<br>";
  422.         }
  423.         else
  424.           webMessage += "Max Chart Data is required<br>";
  425.       }
  426.  
  427.     }
  428.  
  429.     if (tempON <= tempOFF)
  430.       webMessage += "Cooling On must be lower than Cooling Off<br>";
  431.  
  432.     if (sRate < 10)
  433.       webMessage += "Sample Rate must be greater than or equal to 10<br>";
  434.  
  435.     if (maxData < 10 || maxData > 300)
  436.       webMessage += "Max Chart Data must be between 10 and 300<br>";
  437.  
  438.     if (webMessage == "")
  439.     {
  440.       CoolingOn = tempON;
  441.       CoolingOff = tempOFF;
  442.       interval = sRate * 1000;
  443.       maxFileData = maxData;
  444.       ///////
  445.       File f = SPIFFS.open(fName, "w");
  446.       if (!f) {
  447.  
  448.         Serial.println("file open for properties failed");
  449.       }
  450.       else
  451.       {
  452.         Serial.println("====== Writing to config file =========");
  453.  
  454.         f.print(CoolingOn); f.print( ","); f.print(CoolingOff);
  455.         f.print("~"); f.print(sRate);
  456.         f.print(":"); f.println(maxData);
  457.         Serial.println("Properties file updated");
  458.         f.close();
  459.       }
  460.  
  461.     }
  462.   }
  463.  
  464.  
  465.   if (webMessage == "") {
  466.     webMessage = "Settings Updated";
  467.   }
  468.  
  469.  
  470.   gettemperature();
  471.  
  472.   setHTML();
  473.   server.send(200, "text/html", webString);            // send to someones browser when asked
  474.  
  475.   delay(100);
  476. }
  477.  
  478.  
  479. ////////////////////////////////////////////////////////////
  480. // handler for web server request: http://IpAddress/      //
  481. ////////////////////////////////////////////////////////////
  482.  
  483. void handle_root() {
  484.  
  485.   gettemperature();       // read sensor
  486.   webMessage = "";
  487.   setHTML();
  488.   server.send(200, "text/html", webString);            // send to someones browser when asked
  489.  
  490.   delay(100);
  491. }
  492.  
  493. ///////////////////////////////////////
  494. // first funtion to run at power up //
  495. //////////////////////////////////////
  496.  
  497. void setup(void) {
  498.   sensors.begin();
  499.   // You can open the Arduino IDE Serial Monitor window to see what the code is doing
  500.   Serial.begin(115200);  // Serial connection from ESP-01 via 3.3v console cable
  501.  
  502.   pinMode(RELAYPIN, OUTPUT);
  503.   digitalWrite(RELAYPIN, LOW);
  504.   // Connect to WiFi network
  505.   WiFi.begin(ssid, password);
  506.   WiFi.config(IPAddress(192, 168, 1, 201), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));
  507.  
  508.   Serial.print("\n\r \n\rWorking to connect");
  509.  
  510.   // Wait for connection
  511.   while (WiFi.status() != WL_CONNECTED) {
  512.     delay(500);
  513.     Serial.print(".");
  514.   }
  515.   Serial.println("");
  516.   Serial.println("DS18B20 server");
  517.   Serial.print("Connected to ");
  518.   Serial.println(ssid);
  519.   Serial.print("IP address: ");
  520.   Serial.println(WiFi.localIP());
  521.  
  522.   //  dht.begin();           // initialize temperature sensor
  523.   delay(10);
  524.  
  525.  
  526.   SPIFFS.begin();
  527.   delay(10);
  528.   ///////////////////
  529.   // SPIFFS.format(); // uncomment to completely clear data
  530.  
  531.  
  532.   File f = SPIFFS.open(fName, "r");
  533.  
  534.   if (!f) {
  535.     // no file exists so lets format and create a properties file
  536.     Serial.println("Please wait 30 secs for SPIFFS to be formatted");
  537.  
  538.     SPIFFS.format();
  539.  
  540.     Serial.println("Spiffs formatted");
  541.  
  542.     f = SPIFFS.open(fName, "w");
  543.     if (!f) {
  544.  
  545.       Serial.println("properties file open failed");
  546.     }
  547.     else
  548.     {
  549.       // write the defaults to the properties file
  550.       Serial.println("====== Writing to properties file =========");
  551.  
  552.       f.print(CoolingOn); f.print( ","); f.print(CoolingOff);
  553.       f.print("~"); f.print(interval / 1000);
  554.       f.print(":"); f.println(maxFileData);
  555.       Serial.println("Properties file created");
  556.       dataLines = 1;
  557.       f.close();
  558.     }
  559.  
  560.   }
  561.   else
  562.   {
  563.     // if the properties file exists on startup,  read it and set the defaults
  564.     Serial.println("Properties file exists. Reading.");
  565.  
  566.     while (f.available()) {
  567.  
  568.       //Lets read line by line from the file
  569.       String str = f.readStringUntil('\n');
  570.  
  571.       String loSet = str.substring(0, str.indexOf(",")  );
  572.       String hiSet = str.substring(str.indexOf(",") + 1, str.indexOf("~") );
  573.       String sampleRate = str.substring(str.indexOf("~") + 1, str.indexOf(":") );
  574.       String maxData = str.substring(str.indexOf(":") + 1 );
  575.  
  576.       Serial.println(loSet);
  577.       Serial.println(hiSet);
  578.       Serial.println(sampleRate);
  579.       Serial.println(maxData);
  580.  
  581.       CoolingOn = loSet.toInt();
  582.       CoolingOff = hiSet.toInt();
  583.       interval = sampleRate.toInt() * 1000;
  584.       maxFileData = maxData.toInt();
  585.     }
  586.  
  587.     f.close();
  588.   }
  589.  
  590.   // now lets read the data file mainly to set the number of lines
  591.   readDataFile();
  592.   // now read the DHT and set the temp and Temperature 2 variables
  593.   gettemperature();
  594.   // update the datafile to start a new session
  595.   updateDataFile();
  596.  
  597.   // web client handlers
  598.   server.on("/", handle_root);
  599.  
  600.   server.on("/submit", handle_submit);
  601.  
  602.   server.on("/clear", []() {
  603.     // handler for http://iPaddress/clear
  604.  
  605.     // deletes all the stored data for temp and Temperature 2
  606.     clearDataFile();
  607.  
  608.     webMessage = "Data Cleared";
  609.  
  610.     // read the DHT and use new values to start new file data
  611.     gettemperature();
  612.     updateDataFile();
  613.     setHTML(); // this will set the webString varialbe with HTML
  614.     server.send(200, "text/html", webString);            // send to someones browser when asked
  615.     delay(100);
  616.  
  617.   });
  618.  
  619.   // start the web server
  620.   server.begin();
  621.   Serial.println("HTTP server started");
  622.  
  623. }
  624.  
  625. ////////////////////////////////////////////////////////////////////////////////
  626.  
  627. void loop(void)
  628. {
  629.  
  630.   // check timer to see if it's time to update the data file with another DHT reading
  631.   unsigned long currentMillis = millis();
  632.  
  633.   // cast to unsigned long to handle rollover
  634.   if ((unsigned long)(currentMillis - previousMillis) >= interval )
  635.   {
  636.     // save the last time you read the sensor
  637.     previousMillis = currentMillis;
  638.  
  639.     gettemperature();
  640.  
  641.     Serial.print("Temp: ");
  642.     Serial.println(temp_f);
  643.     Serial.print("Temp 2: ");
  644.     Serial.println(temp_f2);
  645.  
  646.     updateDataFile();
  647.     readDataFile();
  648.   }
  649.  
  650.   server.handleClient();
  651.  
  652. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement