185 lines
5.1 KiB
C++
185 lines
5.1 KiB
C++
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
|
|
* WiFi Thermometer *
|
|
* (C)05.12.2019 Paolo Iocco *
|
|
* adapted 24.11.2020 to #DS18B20 *
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
|
|
*
|
|
* DHT11 temperature sensor on D4
|
|
* DS18B20 temperatuer sensor on D5
|
|
*
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
|
|
#include <ESP8266WiFi.h>
|
|
#include <Ticker.h>
|
|
#include <DHT.h>
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
#include "FS.h"
|
|
|
|
#define DAY_SECONDS 86400 //
|
|
#define MAX_SAMPLES 240 // "j" in Setup() depens on that
|
|
#define DHTTYPE DHT11 // DHT 11
|
|
#define DHTPIN D4 // PIN DHT11, 10k pull up
|
|
#define DS_PIN D5 // PIN DS18B20 OneWire, 10k pull up
|
|
|
|
float kelvin = 0, celsius = 0, humidity = 0, tempC = 0;
|
|
int i = 0, j = 0;
|
|
float array_t[MAX_SAMPLES];
|
|
float array_ds[MAX_SAMPLES];
|
|
char buffer [(MAX_SAMPLES * 16)]; // buffer for sprintf in HTML Server. Oversized?
|
|
const unsigned long periodo = (DAY_SECONDS / MAX_SAMPLES);
|
|
const char* ssid = "OnDe";
|
|
const char* password = "44444444444444444444444444";
|
|
time_t tnow;
|
|
|
|
WiFiServer server(80);
|
|
WiFiClient client;
|
|
File file;
|
|
Ticker blinker;
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
OneWire ds(DS_PIN);
|
|
DallasTemperature sensors(&ds);
|
|
|
|
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
void ClockDisplay() {
|
|
tnow = time(nullptr);
|
|
Serial.println(ctime(&tnow));
|
|
}
|
|
|
|
void setup() {
|
|
struct tm * timeinfo;
|
|
|
|
// Pins and variables initialize
|
|
pinMode(A0, INPUT);
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
|
|
// clear array
|
|
for (i=0;i<MAX_SAMPLES; i++){
|
|
array_t[i]=0;
|
|
array_ds[i]=0;
|
|
}
|
|
// Mount filesystem
|
|
Serial.begin(57600);
|
|
if(!SPIFFS.begin()){
|
|
Serial.println("An Error has occurred while mounting SPIFFS");
|
|
return;
|
|
}
|
|
Serial.println("\nFilesystem mounted");
|
|
|
|
// Sensor start
|
|
sensors.begin();
|
|
dht.begin();
|
|
|
|
// WiFi Connect
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("\nWiFi is connected");
|
|
server.begin();
|
|
Serial.println("Server started");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
// NTP Synchronize
|
|
configTime(0, 0, "0.se.pool.ntp.org");
|
|
//setenv("TZ", "UTC+7", 1);
|
|
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 3); // this sets TZ to Brussels/Paris/Vienna
|
|
tzset();
|
|
ClockDisplay(); // output current time on serial
|
|
|
|
// set j as day time index
|
|
tnow = time(nullptr);
|
|
// time(&tnow);
|
|
timeinfo = localtime(&tnow);
|
|
j= timeinfo->tm_hour * 10 + (timeinfo->tm_min / 6); // 240 ticks
|
|
|
|
// Set Ticker
|
|
blinker.attach(periodo, Memorizza);
|
|
}
|
|
|
|
void Memorizza() {
|
|
j=(j+1) % MAX_SAMPLES;
|
|
array_t[j]=celsius;
|
|
array_ds[j]=tempC;
|
|
// blink
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
delay(10);
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
|
}
|
|
|
|
void loop() {
|
|
celsius = dht.readTemperature();
|
|
humidity = dht.readHumidity();
|
|
sensors.requestTemperatures();
|
|
tempC = sensors.getTempCByIndex(0);
|
|
kelvin = ((3.3 * analogRead(A0) / 1024)*100)-273.15;
|
|
tnow = time(nullptr);
|
|
Serial.print("Umidità: ");
|
|
Serial.print(humidity);
|
|
Serial.print("% , Temp DHT:");
|
|
Serial.print(celsius);
|
|
Serial.print(" °C , Temp DS:");
|
|
Serial.print(tempC);
|
|
Serial.print(" °C , Temp AN:");
|
|
Serial.print(kelvin);
|
|
Serial.println(" °C");
|
|
|
|
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *
|
|
* Compose and output Web-Page *
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
|
|
client = server.available();
|
|
file = SPIFFS.open("/index.html", "r");
|
|
if(!file) {
|
|
Serial.println("Failed to open file for reading");
|
|
return;
|
|
}
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-Type: text/html");
|
|
client.println("Connection: close");
|
|
client.println("Refresh: 60");
|
|
client.println();
|
|
client.println("<!DOCTYPE html>");
|
|
client.println("<html>");
|
|
client.println("<head>");
|
|
// Output temperature vector array
|
|
client.print("<script> var array = [");
|
|
for (i=0;i<MAX_SAMPLES; i++) {
|
|
sprintf(buffer,"[%2.2f,%2.2f]", (float)(i)/(float)(MAX_SAMPLES/24), array_t[i]);
|
|
client.print(buffer);
|
|
if (i<(MAX_SAMPLES-1)){
|
|
client.print(",");
|
|
}
|
|
}
|
|
client.println("]");
|
|
client.print("var array_ds = [");
|
|
for (i=0;i<MAX_SAMPLES; i++) {
|
|
sprintf(buffer,"[%2.2f,%2.2f]", (float)(i)/(float)(MAX_SAMPLES/24), array_ds[i]);
|
|
client.print(buffer);
|
|
if (i<(MAX_SAMPLES-1)){
|
|
client.print(",");
|
|
}
|
|
}
|
|
client.println("] </script>");
|
|
// Output HTML page and JavaScript
|
|
while(file.available()){
|
|
client.write(file.read());
|
|
}
|
|
file.close();
|
|
// Ourput HTML-END
|
|
client.println(celsius);
|
|
client.print("°C </p>");
|
|
client.print("<p style='text-align: left; font-family:arial'><span style='color: #0082ff;'>Temperatura DS18B20 = ");
|
|
client.println(tempC);
|
|
client.print("°C </p>");
|
|
client.print("<p style='text-align: left; font-family:arial'><span style='color: #0082ff;'>Umidità = ");
|
|
client.println(humidity);
|
|
client.print("% </p>");
|
|
client.print("<p>Time: ");
|
|
client.println(ctime(&tnow));
|
|
client.print(" </p>");
|
|
client.println("</body>");
|
|
client.println("</html>");
|
|
}
|