Initial commit
This commit is contained in:
123
ESPtermostat.ino
Normal file
123
ESPtermostat.ino
Normal file
@@ -0,0 +1,123 @@
|
||||
//========== Preprocesor ==========
|
||||
|
||||
//---- Required ----
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ArduinoOTA.h>
|
||||
//------------------
|
||||
#include <ESPAsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
#include "Tasker.h"
|
||||
#include "OneWire.h"
|
||||
#include "DallasTemperature.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
//========== Create objects ==========
|
||||
|
||||
AsyncWebServer server(80);
|
||||
Tasker tasker;
|
||||
OneWire oneWire(DS18B20_PIN);
|
||||
DallasTemperature sensor(&oneWire);
|
||||
|
||||
//========== Init variables ==========
|
||||
|
||||
float temperature;
|
||||
String metrics;
|
||||
|
||||
//=================================
|
||||
|
||||
|
||||
void setup() {
|
||||
//========== Wi-Fi setup ==========
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(STASSID, STAPSK);
|
||||
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
|
||||
//Connection Failed! Rebooting...
|
||||
delay(5000);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
//========== OTA setup ==========
|
||||
|
||||
// Port defaults to 8266
|
||||
// ArduinoOTA.setPort(8266);
|
||||
|
||||
// Hostname defaults to esp8266-[ChipID]
|
||||
// ArduinoOTA.setHostname("myesp8266");
|
||||
|
||||
// No authentication by default
|
||||
// ArduinoOTA.setPassword("admin");
|
||||
|
||||
// Password can be set with it's md5 value as well
|
||||
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
|
||||
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
|
||||
ArduinoOTA.begin();
|
||||
|
||||
//========== Pin setup ==========
|
||||
|
||||
pinMode(RELAY_PIN, OUTPUT);
|
||||
|
||||
//========== Sensor setup ==========
|
||||
|
||||
sensor.begin();
|
||||
// do not block during temperature conversion
|
||||
sensor.setWaitForConversion(false);
|
||||
|
||||
//========== Tasks init ==========
|
||||
|
||||
startConversion(); // First temp read
|
||||
tasker.setInterval(startConversion, 15000); // read temperature every 15 seconds
|
||||
//tasker.setTimeout(relayLoop, 5000);
|
||||
tasker.setInterval(otaHandle, 1000);
|
||||
|
||||
//========== Web server setup ==========
|
||||
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
request->send(200, "text/plain", "ESP termostat, tady se bude posílat aplikace z flash paměti");
|
||||
});
|
||||
if(METRICS_EXPORT){
|
||||
server.on("/metrics", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
request->send(200, "text/plain; charset=utf-8", metrics);
|
||||
});
|
||||
}
|
||||
server.onNotFound(notFound);
|
||||
server.begin();
|
||||
|
||||
//=================================
|
||||
}
|
||||
|
||||
void loop() {
|
||||
tasker.loop();
|
||||
}
|
||||
|
||||
void otaHandle(){
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
|
||||
void notFound(AsyncWebServerRequest *request) {
|
||||
request->send(404, "text/plain", "Not found");
|
||||
}
|
||||
|
||||
//void relayLoop(){
|
||||
// byte pin = RELAY_PIN;
|
||||
// bool led = !digitalRead(pin);
|
||||
// digitalWrite(pin, led);
|
||||
// tasker.setTimeout(relayLoop, led ? 3000 : 7000);
|
||||
//}
|
||||
|
||||
void readSensor() {
|
||||
// read the actual temperature after it's been converted
|
||||
temperature = sensor.getTempCByIndex(0);
|
||||
// do what you need with the temperature here
|
||||
metrics = "temp ";
|
||||
metrics += temperature;
|
||||
}
|
||||
|
||||
void startConversion() {
|
||||
// start temperature conversion (does not block)
|
||||
sensor.requestTemperatures();
|
||||
// schedule reading the actual temperature in 750 milliseconds
|
||||
tasker.setTimeout(readSensor, 750);
|
||||
}
|
||||
Reference in New Issue
Block a user