Update Benni_rafvirki_5.ino

This commit is contained in:
2023-10-19 17:21:10 +02:00
parent 9dd50d0ebf
commit 98886a0179

View File

@@ -1,276 +1,273 @@
//Codigo a funcionar bem. Temperatura a ser enviada para a base de dados //Codigo a funcionar bem. Temperatura a ser enviada para a base de dados
//http://bennirafvirki.is/sensors/ui/readings //http://bennirafvirki.is/sensors/ui/readings
//Working well for Fífusel 7
//Bennirafvirki.is/sensors/ #ifdef ESP32 // Import required libraries
//Benni +3547738039 #include <WiFi.h>
#ifdef ESP32 // Import required libraries #else
#include <WiFi.h> #include <Arduino.h>
#include <ESP8266WiFi.h>
#else #include <Hash.h>
#include <Arduino.h> #include <ESPAsyncTCP.h>
#include <ESP8266WiFi.h> #include <ESPAsyncWebServer.h>
#include <Hash.h> #endif
#include <ESPAsyncTCP.h> #include <OneWire.h>
#include <ESPAsyncWebServer.h> #include <DallasTemperature.h>
#endif #define ONE_WIRE_BUS 4 // Data wire is connected to GPIO 4 = pin D2
#include <OneWire.h> OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
#include <DallasTemperature.h> DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor
#define ONE_WIRE_BUS 4 // Data wire is connected to GPIO 4 = pin D2
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices //#include <ESP8266WiFi.h>
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor #include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
//#include <ESP8266WiFi.h> //#define SERVER_IP "bennirafvirki.is"
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
//#define SERVER_IP "bennirafvirki.is" // Replaced parameters
// Rikki
// Replaced parameters const char* ssid = "YEP"; //network credentials
const char* password = "rey35";
// Rikki
const char* ssid = "Asus2"; //network credentials /* F7
const char* password = "lukas1234"; //const char* ssid = "Hringdu-2"; //network credentials F7
const char* password = "125gt";
/* F7 */
//const char* ssid = "Hringdu-2.4G-2dfM"; //network credentials F7 const int tmin = -3; // min temperature
const char* password = "8hs9gmmV"; const int tmax = 3; // max temperature
*/ String address = "electropepper";
const int tmin = -3; // min temperature int time_of_delay = 50000; //if 50000 ms, than will measure each minute interval
const int tmax = 3; // max temperature
String address = "electropepper";
int time_of_delay = 50000; //if 50000 ms, than will measure each minute interval // pinout D0=pin16; D1=pin5/GPIO5; D2=pin4/GPIO4; D3=0/Flash; D4=2/Tx1; D5=14; D6=12; D7=13=Rx2; D8=15=Tx2; D9=3=Rx0; D10=1=Tx0;
//const int relay1 = 5; //pin D1 = GPIO5 , relay 1
//const int relay2 = 14; //pin D5 = GPIO14, relay 2
// pinout D0=pin16; D1=pin5/GPIO5; D2=pin4/GPIO4; D3=0/Flash; D4=2/Tx1; D5=14; D6=12; D7=13=Rx2; D8=15=Tx2; D9=3=Rx0; D10=1=Tx0; const int relay1 = 16;
//const int relay1 = 5; //pin D1 = GPIO5 , relay 1 const int relay2 = 5;
//const int relay2 = 14; //pin D5 = GPIO14, relay 2
const int relay1 = 16; /// Serial port for debugging purposes Serial.begin(115200); Serial monitoring
const int relay2 = 5;
AsyncWebServer server(80); // Create AsyncWebServer object on port 80
/// Serial port for debugging purposes Serial.begin(115200); Serial monitoring
AsyncWebServer server(80); // Create AsyncWebServer object on port 80 String readDSTemperatureC() // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
{
sensors.requestTemperatures();
String readDSTemperatureC() // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus float tempC = sensors.getTempCByIndex(0);
{
sensors.requestTemperatures(); if (tempC == -127.00 || tempC == 85.00 )
float tempC = sensors.getTempCByIndex(0); {
Serial.println("Failed to read from DS18B20 sensor");
if (tempC == -127.00 || tempC == 85.00 ) /////////////////////////////////////////////////////////////////////////////////////
{ ////////////////////////////////////////////////////could you created README.MD file in your project folder with those notes?
Serial.println("Failed to read from DS18B20 sensor"); return "--";
///////////////////////////////////////////////////////////////////////////////////// }
////////////////////////////////////////////////////could you created README.MD file in your project folder with those notes? else
return "--"; {
} //Serial.print("Temperature Celsius: ");
else //Serial.print(tempC);
{
//Serial.print("Temperature Celsius: "); }
//Serial.print(tempC); return String(tempC);
}
}
return String(tempC);
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
const char index_html[] PROGMEM = R"rawliteral( <meta name="viewport" content="width=device-width, initial-scale=1">
<!DOCTYPE HTML><html> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<head> <style>
<meta name="viewport" content="width=device-width, initial-scale=1"> html {
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> font-family: Arial;
<style> display: inline-block;
html { margin: 0px auto;
font-family: Arial; text-align: center;
display: inline-block; }
margin: 0px auto; h2 { font-size: 1.0rem; }
text-align: center; h3 { font-size: 1.0rem; }
} p { font-size: 3.0rem; }
h2 { font-size: 1.0rem; } .units { font-size: 1.2rem; }
h3 { font-size: 1.0rem; } .ds-labels{
p { font-size: 3.0rem; } font-size: 1.5rem;
.units { font-size: 1.2rem; } vertical-align:middle;
.ds-labels{ padding-bottom: 15px;
font-size: 1.5rem; }
vertical-align:middle; </style>
padding-bottom: 15px; </head>
} <body>
</style>
</head> <h2> Fifusel, Reykjavik </h2>
<body>
<p>
<h2> Fifusel, Reykjavik </h2> <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="ds-labels">Temperature Celsius</span>
<p> <span id="temperaturec">%TEMPERATUREC%</span>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <sup class="units">&deg;C</sup>
<span class="ds-labels">Temperature Celsius</span> <h3 class="ds-labels">Heaters are ON between -3 and 3 celcius </h3>
<span id="temperaturec">%TEMPERATUREC%</span>
<sup class="units">&deg;C</sup>
<h3 class="ds-labels">Heaters are ON between -3 and 3 celcius </h3> <h2> Benni 7738039 </h2>
<h2> Bennirafvirki.is </h2>
</p>
<h2> Benni 7738039 </h2> </body>
<h2> Bennirafvirki.is </h2> <script>
</p>
</body>
<script> setInterval(function ( )
{
var xhttp = new XMLHttpRequest();
setInterval(function ( ) xhttp.onreadystatechange = function()
{ {
var xhttp = new XMLHttpRequest(); if (this.readyState == 4 && this.status == 200)
xhttp.onreadystatechange = function() {
{ document.getElementById("temperaturec").innerHTML = this.responseText;
if (this.readyState == 4 && this.status == 200) }
{ };
document.getElementById("temperaturec").innerHTML = this.responseText; xhttp.open("GET", "/temperaturec", true);
} xhttp.send();
}; },10000) ; //time 10sec
xhttp.open("GET", "/temperaturec", true);
xhttp.send();
},10000) ; //time 10sec </script>
</html>)rawliteral";
</script>
</html>)rawliteral";
String processor(const String& var) // Replaces placeholder with DHT values
{
//Serial.println(var);
String processor(const String& var) // Replaces placeholder with DHT values if (var == "TEMPERATUREC")
{ {
//Serial.println(var); return readDSTemperatureC();
if (var == "TEMPERATUREC") }
{ return String();
return readDSTemperatureC(); }
}
return String();
} void setup()
{
pinMode(relay1, OUTPUT);
void setup() digitalWrite(relay1, LOW);
{ pinMode(relay2, OUTPUT);
pinMode(relay1, OUTPUT); digitalWrite(relay2, LOW);
digitalWrite(relay1, LOW);
pinMode(relay2, OUTPUT);
digitalWrite(relay2, LOW); Serial.begin(115200); // Serial port for debugging purposes ( baud)
Serial.println();
sensors.begin(); // Start up the DS18B20 library
Serial.begin(115200); // Serial port for debugging purposes ( baud)
Serial.println(); WiFi.begin(ssid, password); // Connect to Wi-Fi
sensors.begin(); // Start up the DS18B20 library Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
WiFi.begin(ssid, password); // Connect to Wi-Fi {
Serial.println("Connecting to WiFi"); delay(500);
while (WiFi.status() != WL_CONNECTED) Serial.print(".");
{ }
delay(500); Serial.println();
Serial.print("."); Serial.println(WiFi.localIP()); // Print ESP Local IP Address 192.168.1.18
}
Serial.println();
Serial.println(WiFi.localIP()); // Print ESP Local IP Address 192.168.1.18
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) // Route for root / web page
{
request->send_P(200, "text/html", index_html, processor);
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) // Route for root / web page });
{ server.on("/temperaturec", HTTP_GET, [](AsyncWebServerRequest * request)
request->send_P(200, "text/html", index_html, processor); {
}); request->send_P(200, "text/plain", readDSTemperatureC().c_str());
server.on("/temperaturec", HTTP_GET, [](AsyncWebServerRequest * request) });
{
request->send_P(200, "text/plain", readDSTemperatureC().c_str()); server.begin(); // Start server
});
}
server.begin(); // Start server
} void loop()
{
void loop() float tempC = sensors.getTempCByIndex(0);
{
if (tempC > tmin && tempC < tmax)
float tempC = sensors.getTempCByIndex(0); {
digitalWrite(relay1, LOW);
if (tempC > tmin && tempC < tmax) digitalWrite(relay2, LOW);
{ Serial.print("Temperature is ");
digitalWrite(relay1, LOW); Serial.print(tempC);
digitalWrite(relay2, LOW); Serial.println(" Celcius and the heaters are ON ");
Serial.print("Temperature is "); }
Serial.print(tempC); else
Serial.println(" Celcius and the heaters are ON "); {
} digitalWrite(relay1, HIGH);
else digitalWrite(relay2, HIGH);
{ Serial.print("Temperature is ");
digitalWrite(relay1, HIGH); Serial.print(tempC) ;
digitalWrite(relay2, HIGH); Serial.println(" Celcius and the heaters are OFF ");
Serial.print("Temperature is "); }
Serial.print(tempC) ; delay(10000);
Serial.println(" Celcius and the heaters are OFF ");
} // wait for WiFi connection
delay(10000); if ((WiFi.status() == WL_CONNECTED))
{
// wait for WiFi connection
if ((WiFi.status() == WL_CONNECTED)) WiFiClient client;
{ HTTPClient http;
WiFiClient client; Serial.print("[HTTP] begin...\n");
HTTPClient http; // configure traged server and url
Serial.print("[HTTP] begin...\n");
// configure traged server and url ///////////////////////////////////////////////////////////////////////////////////////////////////////////
//could you created README.MD file in your project folder with those notes?
//change to: if tempC is between min and max, so it´s: 0 (OFF), 1 (ON), 2 sensor disconnected, 3 wire or resistance fail, 4
/////////////////////////////////////////////////////////////////////////////////////////////////////////// // if tempC = -127, sensor or resistance disconnect, if tempC = 85, sensor
//could you created README.MD file in your project folder with those notes?
//change to: if tempC is between min and max, so it´s: 0 (OFF), 1 (ON), 2 sensor disconnected, 3 wire or resistance fail, 4 int sensor_status = 0;
// if tempC = -127, sensor or resistance disconnect, if tempC = 85, sensor
String link1 = "http://bennirafvirki.is/sensors/add/temperature/";
int sensor_status = 0; String link2 = String(tempC,DEC%4);
String link3 = "/address/";
String link1 = "http://bennirafvirki.is/sensors/add/temperature/"; String link4 = String(address);
String link2 = String(tempC,DEC%4); String link5 = "/state/";
String link3 = "/address/"; String link6 = String(sensor_status,DEC);
String link4 = String(address); String link = link1 + link2 + link3 + link4 + link5 + link6 ;
String link5 = "/state/";
String link6 = String(sensor_status,DEC); http.begin(client,link); //Serial.print(link);
String link = link1 + link2 + link3 + link4 + link5 + link6 ;
http.begin(client,link); //Serial.print(link); http.addHeader("Content-Type", "application/json");
Serial.print("[HTTP] POST...\n");
http.addHeader("Content-Type", "application/json"); // start connection and send HTTP header and body
int httpCode = http.POST("{\"hello\":\"world\"}");
Serial.print("[HTTP] POST...\n");
// start connection and send HTTP header and body // httpCode will be negative on error
int httpCode = http.POST("{\"hello\":\"world\"}"); if (httpCode > 0)
{
// httpCode will be negative on error // HTTP header has been send and Server response header has been handled
if (httpCode > 0) Serial.printf("[HTTP] POST... code: %d\n", httpCode);
{
// HTTP header has been send and Server response header has been handled // file found at server
Serial.printf("[HTTP] POST... code: %d\n", httpCode); if (httpCode == HTTP_CODE_OK)
{
// file found at server const String& payload = http.getString();
if (httpCode == HTTP_CODE_OK) Serial.println("received payload:\n<<");
{ Serial.println(payload);
const String& payload = http.getString(); Serial.println(">>");
Serial.println("received payload:\n<<"); }
Serial.println(payload); }
Serial.println(">>"); else
} {
} Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
else }
{
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); http.end();
} }
http.end(); delay(time_of_delay);
}
delay(time_of_delay); }
}