Voor wie ook een temperatuurmeter wil maken met een ESP32 en DS18820.
hieronder de code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
const char *ssid = "DeBraveHendrik";
const char *password = "Geheim";
const char *labels[] = {" C"," F"};
const int relayport = 17;
const int maxnumberx=1008;
const int nrsamplesbytes=6;
// Static IP configuration
IPAddress staticIP(192, 168, 8, 2); // ESP32 static IP
IPAddress gateway(192, 168, 8, 1); // IP Address of your network gateway (router)
IPAddress subnet(255, 255, 255, 0); // Subnet mask
IPAddress primaryDNS(192, 168, 8, 1); // Primary DNS (optional)
IPAddress secondaryDNS(0, 0, 0, 0); // Secondary DNS (optional)
int usegoogle=0;
int glevel=95; // trigger temperatuur
int n=1;
int av[nrsamplesbytes];
char a[maxnumberx*nrsamplesbytes];
int currentnumber=0;
int waitsecnr=1;
float temperatureC=0;
int failedcnt=0;
int seclast=0;
int secnew=0;
int count=0;
char temp[900];
WebServer server(80);
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Function to calculate NMEA 0183 checksum
char calculateChecksum(const char *nmea) {
char checksum = 0;
// Skip the initial '$' character
nmea++;
// Calculate the checksum by XORing all characters until '*'
while (*nmea != '*' && *nmea != '\0') {
checksum ^= *nmea;
nmea++;
}
return checksum;
}
void nmeacode() {
char buff[30];
snprintf(buff, 900,"$YDXDR,C,%d,C,MOTOR*",av[0]);
// Calculate checksum
char checksum = calculateChecksum(buff);
snprintf(temp, 900,"%s%02x",buff, checksum);
server.send(200, "text/html", temp);
}
void handleRoot() {
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
Serial.println("showroot");
for (uint8_t i = 0; i < server.args(); i++) {
if(server.argName(i)=="waitsecnr")
{
waitsecnr=string2int(server.arg(i));
}
if(server.argName(i)=="glevel")
{
glevel=string2int(server.arg(i));
}
}
snprintf(temp, 900,
"<html>\
<head>\
<title>ESP32 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>De Brave Hendrik motor temperatuur</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
<table>\
<tr><td>%d</td><td>%s</td></tr>\
</table>\
<form action='/setvalues' method='post'> \
<input type='submit' value='Configure'> \
</form>\
<form action='/showgraph' method='post'> \
<input type='submit' value='Graph'> \
</form>\
</body>\
</html>",
hr, min % 60, sec % 60,
av[0],labels[0]
);
server.send(200, "text/html", temp);
}
void showgraph() {
snprintf(temp, 900,
"<html>\
<head>\
<title>ESP32 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<script>\
const myTimeout = setTimeout(reloadfunc, 15000);\
function reloadfunc() { location.reload(); }\
</script>\
<h1>De Brave Hendrik Motor Temperatuur</h1>\
<h1>Current Temp %2.1f C Signal Temp %d C</h1>\
<img src=\"/test.svg\" />\
<form action='/' method='post'> \
<input type='submit' value='Start'> \
</form>\
</body>\
</html>", temperatureC,glevel);
server.send(200, "text/html", temp);
}
void storeval(int val,int r)
{
a[currentnumber*6+2*r]=(char)((val & 0xFF00)>>8);
a[currentnumber*6+2*r+1]=(char)(val & 0xFF);
}
void tenminpast()
{
storeval((int)round(av[0]),0);
storeval((int)round(av[0]),1);
storeval((int)round(av[0]),2);
currentnumber++;
if(currentnumber>maxnumberx)
{
currentnumber=0;
}
}
int string2int(String strpoint)
{
int inumb=0;
for( char c: strpoint)
{
if(c>='0' && c<='9') {
inumb = inumb*10 + (c - '0');
}
}
return inumb;
}
void setvalues() {
snprintf(temp, 900,
"<html>\
<head>\
<title>ESP32 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<form action='/' method='post'> \
<label for='waitsecnr'>Interval value (in sec):</label> \
<input type='text' id='waitsecnr' name='waitsecnr' value='%d'><br><br> \
<label for='glevel'>Temperature Treshold:</label> \
<input type='text' id='glevel' name='glevel' value='%d'><br><br> \
<input type='submit' value='Submit'> \
</form>\
</body>\
</html>",waitsecnr,glevel,usegoogle );
server.send(200, "text/html", temp);
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void drawGraph() {
String out = "";
int maxval=110;
int day;
int hour;
int minutes;
int seconds;
int n;
for (int x = 0; x < maxnumberx; x++) {
if((a[4+x*6]*256+a[5+x*6])>maxval)
{
maxval=a[4+x*6]*256+a[5+x*6];
}
}
out += "<svg xmlns=\"
www.w3.org/2000/svg\" version=\"1.1\" width=\"1025\" height=\"288\">\n";
out += "<rect width=\"1025\" height=\"288\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"black\">\n";
out += "<text x='1' y='269' fill='red'>0</text>\n";
for(int yt=0;yt<8;yt++)
{
out += "<text x='1' y='";
out += yt*32+15;
out += "' fill='red'>";
out += (int)(maxval-(maxval*yt)/8);
out += "</text>\n";
out += "<line x1='1' y1='";
out += yt*32+20;
out += "' x2='";
out += maxnumberx;
out += "' y2='";
out += yt*32+20;
out += "' stroke-width='1' />\n";
out += "<text x='";
out += maxnumberx-(yt*132+80);
out += "' y='285' fill='red'>-(";
n= (int)(((waitsecnr*maxnumberx)*yt)/8);
day = n / (24 * 3600);
n = n % (24 * 3600);
hour = n / 3600;
n %= 3600;
minutes = n / 60 ;
n %= 60;
seconds = n;
out += day;
out += "d";
out += hour;
out += "h";
out += minutes;
out += "m";
out += seconds;
out += "s";
out += ")</text>\n";
}
int y = -1;
int ya = -1;
int yb = -1;
for (int x = 1; x < maxnumberx; x++) {
int posx;
int y2 = (int)(((float)a[4+x*6]*256+(float)a[5+x*6])*255/(float)maxval);
int y1 = (int)(((float)a[2+x*6]*256+(float)a[3+x*6])*255/(float)maxval);
int y0 = (int)(((float)a[x*6]*256+(float)a[1+x*6])*255/(float)maxval);
if(y == -1)
{
y = y2;
yb = y1;
ya = y0;
}
if(x<currentnumber)
{
posx=maxnumberx+x-currentnumber;
}
else
{
posx=x-currentnumber;
}
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" stroke=\"red\" />\n", posx, 270 - y, posx + 6 , 270 - y2);
out += temp;
//sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" stroke=\"blue\" />\n", posx, 270 - yb, posx + 1, 270 - y1);
//out += temp;
//sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", posx, 270 - ya, posx + 1, 270 - y0);
//out += temp;
y = y2;
yb = y1;
ya = y0;
x = x + 5;
}
out += "</g>\n</svg>\n";
server.send(200, "image/svg+xml", out);
}
void setup() {
// Start the Serial Monitor
Serial.begin(115200);
// Start the DS18B20 sensor
sensors.begin();
pinMode(relayport, OUTPUT);
digitalWrite(relayport, LOW);
digitalWrite(relayport, HIGH);
delay(500);
digitalWrite(relayport, LOW);
delay(120000);
delay(500);
digitalWrite(relayport, HIGH);
delay(500);
digitalWrite(relayport, LOW);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
while ((WiFi.status() != WL_CONNECTED) && (count<50)) {
delay(500);
count++;
Serial.print(".");
}
if(count<50)
{
// Configuring static IP
if(!WiFi.config(staticIP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("Failed to configure Static IP");
} else {
Serial.println("Static IP configured!");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", nmeacode);
server.on("/root", handleRoot);
server.on("/test.svg", drawGraph);
server.on("/showgraph",showgraph);
server.on("/setvalues", setvalues);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
delay(1000);
seclast = millis() / 1000;
}
void loop() {
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
av[0]=int(temperatureC);
//float temperatureF = sensors.getTempFByIndex(0);
//Serial.print(temperatureC);
//Serial.println("ºC");
//Serial.print(temperatureF);
//Serial.println(WiFi.status());
//delay(1000);
if(av[0]>glevel)
{
digitalWrite(relayport, HIGH);
}
else
{
digitalWrite(relayport, LOW);
}
if(count<50)
{
server.handleClient();
}
secnew = millis() / 1000;
if((secnew-seclast)>waitsecnr)
{
tenminpast();
seclast=secnew;
n=1;
}
if(WiFi.status()==1)
{
count=40;
//WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
while ((WiFi.status() != WL_CONNECTED) && (count<50)) {
delay(500);
count++;
Serial.print(".");
}
}
delay(500);
}