Files
NodeMCU_Sensors/Raspjavik_1.ino
2023-10-19 17:21:44 +02:00

272 lines
8.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// http://bennirafvirki.is/sensors/ui/readings
// Working well as a draft, the code need to be clean.
#ifdef ESP32 // Import required libraries
#include <WiFi.h>
#else
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#endif
#include <OneWire.h>
#include <DallasTemperature.h>
#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
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor
//#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
//#define SERVER_IP "bennirafvirki.is"
// Replaced parameters
// Rikki
const char* ssid = "As"; //network credentials
const char* password = "hgj65";
/*
// F7
const char* ssid = "Hringdu-2"; //network credentials F7
const char* password = "45jy";
*/
const int tmin = -3; // min temperature
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
const int relay1 = 16;
const int relay2 = 5;
/// 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();
float tempC = sensors.getTempCByIndex(0);
if (tempC == -127.00 || tempC == 85.00 )
{
Serial.println("Failed to read from DS18B20 sensor");
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////could you created README.MD file in your project folder with those notes?
return "--";
}
else
{
//Serial.print("Temperature Celsius: ");
//Serial.print(tempC);
}
return String(tempC);
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 1.0rem; }
h3 { font-size: 1.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
.ds-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2> Fifusel, Reykjavik </h2>
<p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="ds-labels">Temperature Celsius</span>
<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> Design by tech company Raspjavik.is </h2>
<h2> Produced by Electropepper.is </h2>
<h2> Instalation by Bennirafvirki.is </h2>
</p>
</body>
<script>
setInterval(function ( )
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("temperaturec").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperaturec", true);
xhttp.send();
},10000) ; //time 10sec
</script>
</html>)rawliteral";
String processor(const String& var) // Replaces placeholder with DHT values
{
//Serial.println(var);
if (var == "TEMPERATUREC")
{
return readDSTemperatureC();
}
return String();
}
void setup()
{
pinMode(relay1, OUTPUT);
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
WiFi.begin(ssid, password); // Connect to Wi-Fi
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
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("/temperaturec", HTTP_GET, [](AsyncWebServerRequest * request)
{
request->send_P(200, "text/plain", readDSTemperatureC().c_str());
});
server.begin(); // Start server
}
void loop()
{
float tempC = sensors.getTempCByIndex(0);
if (tempC > tmin && tempC < tmax)
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
Serial.print("Temperature is ");
Serial.print(tempC);
Serial.println(" Celcius and the heaters are ON ");
}
else
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
Serial.print("Temperature is ");
Serial.print(tempC) ;
Serial.println(" Celcius and the heaters are OFF ");
}
delay(10000);
// wait for WiFi connection
if ((WiFi.status() == WL_CONNECTED))
{
WiFiClient client;
HTTPClient http;
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
int sensor_status = 0;
String link1 = "http://bennirafvirki.is/sensors/add/temperature/";
String link2 = String(tempC,DEC%4);
String link3 = "/address/";
String link4 = String(address);
String link5 = "/state/";
String link6 = String(sensor_status,DEC);
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");
// start connection and send HTTP header and body
int httpCode = http.POST("{\"hello\":\"world\"}");
// httpCode will be negative on error
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK)
{
const String& payload = http.getString();
Serial.println("received payload:\n<<");
Serial.println(payload);
Serial.println(">>");
}
}
else
{
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(time_of_delay);
}