227 lines
5.5 KiB
C++
227 lines
5.5 KiB
C++
#include <Arduino.h>
|
|
#include <ArduinoOTA.h>
|
|
#include <PubSubClient.h>
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
#include <OneButton.h>
|
|
#include <ESP_EEPROM.h>
|
|
|
|
#define LED_WF (16) //Red
|
|
#define LED_WRK (5) //Green
|
|
#define LED_MQ (4) //Blue
|
|
#define ONE_WIRE_BUS (0)
|
|
#define BUTTON (2)
|
|
#define BUZZER (14)
|
|
#define DOOR_HOL (13)
|
|
#define DOOR_MOR (12)
|
|
|
|
#define HOST_NAME "Holodilnik"
|
|
#define MAIN_TOPIC "/home/kuht/"
|
|
|
|
const char* ssid = "wf-home";
|
|
const char* password = "0ndthnrf";
|
|
const char* mqtt_server = "192.168.1.250";
|
|
|
|
|
|
unsigned long cRun = millis();
|
|
bool buzzer_en;
|
|
float max_hol, max_mor;
|
|
unsigned long wrk_ms;
|
|
unsigned long buz_wrk;
|
|
|
|
void connectToWifi();
|
|
void connectToMqtt();
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient mClient(espClient);
|
|
void reconnect();
|
|
|
|
WiFiEventHandler wifiConnectHandler;
|
|
WiFiEventHandler wifiDisconnectHandler;
|
|
void onWifiConnect(const WiFiEventStationModeGotIP& event);
|
|
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event);
|
|
|
|
OneWire oneWire(ONE_WIRE_BUS);
|
|
DallasTemperature tsens(&oneWire);
|
|
DeviceAddress hol = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
|
|
DeviceAddress mor = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
|
|
|
|
OneButton button(BUTTON, true);
|
|
void buttonClick();
|
|
void buttonLongClick();
|
|
void mqttCallback(char* topic, byte* payload, unsigned int length);
|
|
|
|
void setup(){
|
|
Serial.begin(9600);
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.hostname(HOST_NAME);
|
|
Serial.begin(115200);
|
|
|
|
ArduinoOTA.begin();
|
|
|
|
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
|
|
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
|
|
|
|
mClient.setServer("192.168.1.250", 1883);
|
|
|
|
pinMode(LED_WF, OUTPUT);
|
|
pinMode(LED_MQ, OUTPUT);
|
|
pinMode(LED_WRK, OUTPUT);
|
|
pinMode(BUTTON, INPUT_PULLUP);
|
|
pinMode(DOOR_HOL, INPUT_PULLUP);
|
|
pinMode(DOOR_MOR, INPUT_PULLUP);
|
|
digitalWrite(LED_WF, LOW);
|
|
digitalWrite(LED_MQ, HIGH);
|
|
digitalWrite(LED_WRK, HIGH);
|
|
|
|
tsens.begin();
|
|
tsens.setResolution(12);
|
|
tsens.setWaitForConversion(false);
|
|
|
|
button.attachClick(buttonClick);
|
|
button.setPressTicks(3000);
|
|
button.attachLongPressStart(buttonLongClick);
|
|
|
|
buzzer_en = false;
|
|
buz_wrk = 0;
|
|
|
|
EEPROM.begin(sizeof(float) * 2);
|
|
EEPROM.get(0, max_hol);
|
|
EEPROM.get(sizeof(float), max_mor);
|
|
|
|
connectToWifi();
|
|
}
|
|
|
|
void loop(){
|
|
static bool measd = false;
|
|
static uint32_t runSecs = 0;
|
|
char sVal[6];
|
|
float t_hol, t_mor;
|
|
//static bool led_wrk = false;
|
|
if(wrk_ms < millis())
|
|
digitalWrite(LED_WRK, HIGH);
|
|
// if(buz_wrk < millis())
|
|
// noTone(BUZZER);
|
|
ArduinoOTA.handle();
|
|
button.tick();
|
|
if(!mClient.connected()){
|
|
reconnect();
|
|
}
|
|
if(mClient.connected())
|
|
digitalWrite(LED_MQ, HIGH);
|
|
else
|
|
if(!WiFi.isConnected())
|
|
digitalWrite(LED_MQ, LOW);
|
|
mClient.loop();
|
|
if(cRun + 499 < millis()){
|
|
cRun = millis();
|
|
if(!measd){
|
|
tsens.requestTemperatures();
|
|
}
|
|
else{
|
|
if(mClient.connected()){
|
|
t_hol = tsens.getTempC(hol);
|
|
digitalWrite(LED_WRK, LOW);
|
|
wrk_ms = millis() + 300;
|
|
if(t_hol != DEVICE_DISCONNECTED_C)
|
|
sprintf(sVal, "%.1f", t_hol);
|
|
else
|
|
strcpy(sVal, "NaN");
|
|
mClient.publish(MAIN_TOPIC "t_hol", sVal);
|
|
t_mor = tsens.getTempC(mor);
|
|
if(t_mor != DEVICE_DISCONNECTED_C)
|
|
sprintf(sVal, "%.1f", t_mor);
|
|
else
|
|
strcpy(sVal, "NaN");
|
|
mClient.publish(MAIN_TOPIC "t_mor", sVal);
|
|
// if (t_mor > max_mor || t_hol > max_hol){
|
|
// if(buzzer_en)
|
|
// if(buz_wrk+1000 < millis())
|
|
// tone(BUZZER, 1000);
|
|
// }
|
|
// else{
|
|
// buzzer = true;
|
|
// noTone(BUZZER);
|
|
// }
|
|
}
|
|
runSecs++;
|
|
if(runSecs % 60){
|
|
if(mClient.connected()){
|
|
sprintf(sVal, "%d", runSecs / 60);
|
|
mClient.publish(MAIN_TOPIC "hol_mins", sVal);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
if(!WiFi.isConnected()) return;
|
|
for(uint8_t i = 0; i < 3; i++){
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Attempt to connect
|
|
if (mClient.connect(HOST_NAME)) {
|
|
Serial.println("connected");
|
|
mClient.subscribe(MAIN_TOPIC "max_hol");
|
|
mClient.subscribe(MAIN_TOPIC "max_mor");
|
|
break;
|
|
// Once connected, publish an announcement...
|
|
} else {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(mClient.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
// Wait 0.5 seconds before retrying
|
|
delay(1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void mqttCallback(char* topic, byte* payload, unsigned int length) {
|
|
String msg = "";
|
|
for (unsigned int i = 0; i < length; i++) {
|
|
msg += (char)payload[i];
|
|
}
|
|
if(strcmp(topic, MAIN_TOPIC "max_hol") == 0){
|
|
max_hol = msg.toFloat();
|
|
EEPROM.put(0, max_hol);
|
|
EEPROM.commit();
|
|
}
|
|
if(strcmp(topic, MAIN_TOPIC "max_mor") == 0){
|
|
max_mor = msg.toFloat();
|
|
EEPROM.put(sizeof(float), max_hol);
|
|
EEPROM.commit();
|
|
}
|
|
}
|
|
|
|
void connectToWifi() {
|
|
Serial.println("Connecting to Wi-Fi...");
|
|
WiFi.begin(ssid, password);
|
|
while (!WiFi.isConnected()){
|
|
Serial.print(".");
|
|
delay(1000);
|
|
}
|
|
}
|
|
|
|
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
|
|
Serial.println("Connected to Wi-Fi.");
|
|
Serial.print("IP: ");
|
|
Serial.println(WiFi.localIP());
|
|
Serial.println("Connecting to MQTT...");
|
|
//reconnect();
|
|
digitalWrite(LED_WF, LOW);
|
|
}
|
|
|
|
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
|
Serial.println("Disconnected from Wi-Fi.");
|
|
digitalWrite(LED_WF, HIGH);
|
|
digitalWrite(LED_MQ, HIGH);
|
|
}
|
|
|
|
void buttonClick() {
|
|
buzzer_en = false;
|
|
noTone(BUZZER);
|
|
}
|
|
void buttonLongClick() {
|
|
ESP.reset();
|
|
} |