I am just doing my second project with the ESP8266. I want to make a temperature logger usinf a DS18b20 sensor, that measures the temp every 5 seconds and them refreshes the website to show the last measurement. I am also using a base64 encoded pixel to visualize the last ten measurements.
But somehow it keeps getting out of sync or shifting more than one value to the left of the array.
I guess it has to do with the Meta refresh (5 seconds in the html code) and the two delay commands at the end of the script. I tried different variations but noithing seems to get it quite right
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
const char* ssid = "xxx";
const char* password = "xxx";
WiFiServer server(80);
OneWire oneWire(4);
DallasTemperature sensors(&oneWire);
float temp = 0;
int temps[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void setup()
{
Serial.begin(115200);
while (!Serial){;}
sensors.begin();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Starting the web server
server.begin();
Serial.println("Web server running. Waiting for the ESP IP...");
delay(10000);
// Printing the ESP IP address
Serial.println(WiFi.localIP());
Serial.println(F("Temperaturlogger"));
}
void loop()
{
WiFiClient client = server.available();
//get temps
sensors.requestTemperatures();
temp = sensors.getTempCByIndex(0);
//display webpage
if (client) {
Serial.println("New client");
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && blank_line) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><META HTTP-EQUIV=\"refresh\" CONTENT=\"5\"></head>");
client.println("<body><h1>Temperaturlogger</h1>");
//shift last ten values within the array
for (int i=0; i<9; i++){
temps[i] = temps[i+1];
}
temps[9] = temp;
Serial.println(temp);
client.print("<table border=\"0\"><tr>");
//display vertical bar graph of last ten temps
for (int i=0; i<=9; i++){
client.print("<td valign=\"bottom\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=\" height=\"");
client.print(temps[i]*4);
client.print("\" width=\"14\">");
}
client.print("</tr><tr>");
//dispaly values of last ten temp below bar graph
for (int i=0; i<=9; i++){
client.print("<td>");
client.print(temps[i]);
client.print("</td>");
}
//display recent temperature
client.print("</tr></table><br>");
client.print("Aktuelle Temperatur: ");
client.print(temp);
client.print(" °C <br><br><br>");
client.println("</body></html>");
delay(1);
break;
}
if (c == '\n') {
// when starts reading a new line
blank_line = true;
}
else if (c != '\r') {
// when finds a character on the current line
blank_line = false;
}
}
}
// closing the client connection
delay(2000);
client.stop();
Serial.println("Client disconnected.");
}
}