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