Move ESP12 to Kuhnya

This commit is contained in:
2020-10-31 14:24:37 +03:00
parent 92f523e37c
commit 25b71bc371
10 changed files with 805 additions and 4 deletions

525
Kuhnya/src/main.cpp Normal file
View File

@@ -0,0 +1,525 @@
#include "main.h"
OneWire oneWire(ONE_WIRE);
DallasTemperature sensors(&oneWire);
BME280I2C bme;
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
LiquidCrystal_PCF8574 lcd(0x3F); // set the LCD address to 0x27 for a 16 chars and 2 line display
//Adafruit_BME280 bme280; // use I2C interface
//Adafruit_Sensor *bme_temp = bme280.getTemperatureSensor();
//Adafruit_Sensor *bme_pressure = bme280.getPressureSensor();
//Adafruit_Sensor *bme_humidity = bme280.getHumiditySensor();
WiFiClient espClient;
//PubSubClient client(espClient);
Bounce butt = Bounce();
float tempC;
char f[4];
void setup() {
//Serial.begin(9600);
//Serial.println("Booting"); // "Загрузка"
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(LED_OUT, OUTPUT);
digitalWrite(LED_OUT, LOW);
pinMode(MOVE_S, INPUT);
pinMode(BUTTON, INPUT_PULLUP);
butt.attach(BUTTON);
butt.interval(5); // interval in ms
Wire.begin();
Wire.beginTransmission(0x3F);
int error = Wire.endTransmission();
//Serial.print("Error: ");
//Serial.print(error);
if (error == 0) {
//Serial.println(": LCD found.");
lcd.begin(16, 2); // initialize the lcd
lcd.clear();
lcd.setBacklight(255);
} else {
//Serial.println(": LCD not found.");
} // if
lcd.setCursor(0, 0);
lcd.print("Booting");
WiFi.mode(WIFI_STA);
WiFi.hostname("ESP-Kuh");
//-----------------------------------------------------------------------
// WiFi.begin(ssid, password);
// while (WiFi.waitForConnectResult() != WL_CONNECTED) {
// //Serial.println("Connection Failed! Rebooting...");
// // "Соединиться не удалось! Перезагрузка..."
// delay(5000);
// ESP.restart();
// }
// lcd.setCursor(0, 0);
// lcd.print("Connected");
//-----------------------------------------------------------------------
ArduinoOTA.onStart([]() { /*Serial.println("Start");*/}); // "Начало OTA-апдейта"
ArduinoOTA.onEnd([]() { /*Serial.println("\nEnd");*/}); // "Завершение OTA-апдейта"
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { /*Serial.printf("Progress: %u%%\r", (progress / (total / 100)));*/ });
ArduinoOTA.onError([](ota_error_t error) {
/*Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); // "Ошибка при аутентификации"
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); // "Ошибка при начале OTA-апдейта"
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); // "Ошибка при подключении"
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); // "Ошибка при получении данных"
else if (error == OTA_END_ERROR) Serial.println("End Failed"); // "Ошибка при завершении OTA-апдейта"*/
});
ArduinoOTA.begin();
//Serial.println("Ready"); // "Готово"
//Serial.print("IP address: "); // "IP-адрес: "
//Serial.println(WiFi.localIP());
bme.begin();
sensors.begin();
sensors.setResolution(12);
sensors.requestTemperatures();
lcd.setCursor(0, 0);
lcd.print("Measure temps...");
delay(1000);
lcd.setCursor(0, 0);
lcd.print("Measure done... ");
//tempOut = sensors.getTempCByIndex(0);
tempOut = sensors.getTempC(da[0]);
if (tempOut == DEVICE_DISCONNECTED_C){
tempOut = NAN;
stat[0] = 0;
}
else stat[0] = 1;
//delay(100);
//tempHol = sensors.getTempCByIndex(1);
tempHol = sensors.getTempC(da[1]);
if (tempHol == DEVICE_DISCONNECTED_C){
tempHol = NAN;
stat[1] = 0;
}
else stat[1] = 1;
//delay(100);
//tempHoM = sensors.getTempCByIndex(2);
tempHoM = sensors.getTempC(da[2]);
if (tempHoM == DEVICE_DISCONNECTED_C){
tempHoM = NAN;
stat[2] = 0;
}
else stat[2] = 1;
//delay(100);
//tempMor = sensors.getTempCByIndex(3);
tempMor = sensors.getTempC(da[3]);
if (tempMor == DEVICE_DISCONNECTED_C){
tempMor = NAN;
stat[3] = 0;
}
else stat[3] = 1;
//client.setServer(mqtt_server, 1883);
//client.setCallback(callback);
EEPROM.begin(64);
EEPROM.get(0, lightData);
lightSP = lightData.SP;
lightDB = lightData.DB;
/*lightData.DB = 20;
lightData.SP = 100;
EEPROM.put(0, lightData);
EEPROM.commit();*/
minCount = 0;
LCDpage = 0;
lightOn = false;
mv = prMV = digitalRead(MOVE_S);
//-----------------------------------------------------------------------
// if (!client.connected()) {
// reconnect();
// }
// if (client.connected())
// client.publish("/home/kuh/move", String(mv).c_str());
//-----------------------------------------------------------------------
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onMessage(onMqttMessage);
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setClientId("ESPKuh");
connectToWifi();
//-----------------------------------------------------------------------
crun = millis();
// bmeread = true;
}
void loop() {
float tempT(NAN), humT(NAN), pressT(NAN);
ArduinoOTA.handle();
butt.update();
if (butt.rose()){
LCDpage++;
if(LCDpage > 3) LCDpage = 0;
showLCD(LCDpage, mv);
}
if((crun + 199) < millis()){
crun = millis();
adc = analogRead(A0);
mv = digitalRead(MOVE_S);
if(mv != prMV){
if(mv){
//mv = true;
lcd.setBacklight(255);
mqttClient.publish("/home/kuh/move", 1, false, "1");
//-----------------------------------------------------------------------
// if (client.connected())
// client.publish("/home/kuh/move", String(mv).c_str());
//-----------------------------------------------------------------------
}
else{
//mv = false;
lcd.setBacklight(0);
mqttClient.publish("/home/kuh/move", 1, false, "0");
//-----------------------------------------------------------------------
// if (client.connected())
// client.publish("/home/kuh/move", String(mv).c_str());
//-----------------------------------------------------------------------
}
prMV = mv;
}
if((adc > (lightSP + lightDB)) || (mv == false)){
digitalWrite(LED_OUT, LOW);
lightOn = false;
}
if((mv == true) && (adc < lightSP)){
digitalWrite(LED_OUT, HIGH);
lightOn = true;
}
//if(((minCount+5) % 10) == 0){ //Once in Second
bme.read(pressT, tempT, humT, tempUnit, presUnit);
if(!isnan(pressT)){
if(!isnan(press)) press += (pressT / 133.322f - press) / 600.0f;
else press = pressT / 133.322f;
}
if(!isnan(tempT)){
if(!isnan(tempIn)) tempIn += (tempT - tempIn) / 300.0f;
else tempIn = tempT;
}
if(!isnan(humT)){
if(!isnan(hum)) hum += (humT - hum) / 300.0f;
else hum = humT;
}
//}
minCount++;
if (minCount == 300){ //Once in Minute
lcd.begin(16, 2);
publishMin();
minCount = 0;
}
if((minCount % 5) == 0){ //Once in Second
getTemp();
publishSec();
showLCD(LCDpage, mv);
}
}
}
void showLCD(int page, bool l_on)
{
//char s[5];
char outS[20];
//String s1, s2;
/*if(l_on) lcd.setBacklight(255);
else lcd.setBacklight(0);*/
switch (page)
{
case 0:
//s1 = String(tempOut, 1);
//s2 = String(tempIn, 1);
lcd.setCursor(0, 0);
//snprintf(outS, 17, "O:%5sC I:%4sC ", s1.c_str(), s2.c_str());
lcd.printf("O:%5.1fC I:%4.1fC ", tempOut, tempIn);
lcd.setCursor(0, 1);
//s1 = String(hum, 1);
//s2 = String(press, 0);
//snprintf(outS, 17, "H:%4s%% Pr:%3smm", s1.c_str(), s2.c_str());
lcd.printf("H:%4.1f%% Pr:%3.0fmm", hum, press);
break;
case 1:
//s1 = String(tempHol, 1);
//s2 = String(tempHoM, 1);
lcd.setCursor(0, 0);
//snprintf(outS, 17, "H:%4sC M:%5sC ", s1.c_str(), s2.c_str());
lcd.printf("H:%4.1fC M:%5.1fC ", tempHol, tempHoM);
//s1 = String(tempMor, 1);
lcd.setCursor(0, 1);
//snprintf(outS, 17, "Mor:%5sC ", s1.c_str());
lcd.printf("Mor:%5.1fC ", tempMor);
break;
case 2:
lcd.setCursor(0, 0);
//snprintf(outS, 17, "L: %3d SP: %3d ", adc, lightSP);
lcd.printf("L: %3d SP: %3d ", adc, lightSP);
lcd.print(outS);
lcd.setCursor(0, 1);
//snprintf(outS, 17, "DB: %2d MS: %1d L:%1d", lightDB, mv, lightOn);
lcd.printf("DB: %2u MS: %1u L:%1d", lightDB, mv, lightOn);
break;
case 3:
lcd.setCursor(0, 0);
lcd.printf("T1:%d T2:%d %d", stat[0], stat[1], nSens);
lcd.setCursor(0, 1);
lcd.printf("T3:%d T4:%d %3d", stat[2], stat[3], WiFi.RSSI());
break;
default:
break;
}
}
// void callback(char* topic, byte* message, unsigned int length) {
// bool w = false;
// //Serial.print("Message arrived on topic: ");
// // "Сообщение прибыло в топик: "
// //Serial.print(topic);
// //Serial.print(". Message: "); // ". Сообщение: "
// String messageTemp;
// for (unsigned int i = 0; i < length; i++) {
// //Serial.print((char)message[i]);
// messageTemp += (char)message[i];
// }
// if(strcmp(topic, "/home/kuh/light_sp_set") == 0){
// lightSP = messageTemp.toInt();
// w = true;
// }
// if(strcmp(topic, "/home/kuh/light_db_set") == 0){
// lightDB = messageTemp.toInt();
// w = true;
// }
// if(w){
// lightData.SP = lightSP;
// lightData.DB = lightDB;
// EEPROM.put(0, lightData);
// EEPROM.commit();
// }
// Serial.println();
// }
void publishSec()
{
// if (!client.connected()) {
// reconnect();
// }
// if(!client.loop())
// client.connect("ESPKuh");
// if (client.connected()) {
digitalWrite(GREEN, HIGH);
dtostrf(tempOut, 6, 1, strFVal);
mqttClient.publish("/hometest/kuh1s/temp_out", 1, false, strFVal);
dtostrf(tempIn, 6, 1, strFVal);
mqttClient.publish("/hometest/kuh1s/temp_in", 1, false, strFVal);
dtostrf(hum, 6, 1, strFVal);
mqttClient.publish("/hometest/kuh1s/humidity", 1, false, strFVal);
dtostrf(press, 6, 1, strFVal);
mqttClient.publish("/hometest/kuh1s/pressure", 1, false, strFVal);
itoa(adc, strFVal, 10);
mqttClient.publish("/hometest/kuh1s/light_cur", 1, false, strFVal);
dtostrf(tempHol, 6, 1, strFVal);
mqttClient.publish("/hometest/kuh1s/hol_top", 1, false, strFVal);
dtostrf(tempHoM, 6, 1, strFVal);
mqttClient.publish("/hometest/kuh1s/hol_down", 1, false, strFVal);
dtostrf(tempMor, 6, 1, strFVal);
mqttClient.publish("/hometest/kuh1s/moroz", 1, false, strFVal);
itoa(crun, strFVal, 10);
mqttClient.publish("/hometest/kuh1s/millis", 1, false, strFVal);
digitalWrite(GREEN, LOW);
//}
}
void publishMin()
{
// if (!client.connected()) {
// reconnect();
// }
// if(!client.loop())
// client.connect("ESPKuh");
// if (client.connected()) {
digitalWrite(BLUE, HIGH);
if(!isnan(tempOut)){
dtostrf(tempOut, 6, 1, strFVal);
mqttClient.publish("/home/kuh/temp_out", 1, false, strFVal);
}
if(!isnan(tempIn)){
dtostrf(tempIn, 6, 1, strFVal);
mqttClient.publish("/home/kuh/temp_in", 1, false, strFVal);
}
if(!isnan(hum)){
dtostrf(hum, 6, 1, strFVal);
mqttClient.publish("/home/kuh/humidity", 1, false, strFVal);
}
if(!isnan(press)){
dtostrf(press, 6, 1, strFVal);
mqttClient.publish("/home/kuh/pressure", 1, false, strFVal);
}
itoa(lightSP, strFVal, 10);
mqttClient.publish("/home/kuh/light_sp", 1, false, strFVal);
itoa(lightDB, strFVal, 10);
mqttClient.publish("/home/kuh/light_db", 1, false, strFVal);
itoa(adc, strFVal, 10);
mqttClient.publish("/home/kuh/light_cur", 1, false, strFVal);
if(!isnan(tempHol)){
dtostrf(tempHol, 6, 1, strFVal);
mqttClient.publish("/home/kuh/hol_top", 1, false, strFVal);
}
if(!isnan(tempHoM)){
dtostrf(tempHoM, 6, 1, strFVal);
mqttClient.publish("/home/kuh/hol_down", 1, false, strFVal);
}
if(!isnan(tempMor)){
dtostrf(tempMor, 6, 1, strFVal);
mqttClient.publish("/home/kuh/moroz", 1, false, strFVal);
}
ultoa(crun, strFVal, 10);
mqttClient.publish("/home/kuh/millis", 1, false, strFVal);
// digitalWrite(BLUE, LOW);
// }
}
// void reconnect() {
// digitalWrite(RED, HIGH);
// //Serial.print("Attempting MQTT connection...");
// // "Попытка подключиться к MQTT-брокеру... "
// // Пытаемся подключиться:
// if (client.connect("ESPKuh")) {
// //Serial.println("connected"); // "подключен"
// // подписываемся или переподписываемся на топик;
// // можно подписаться не только на один, а на несколько топиков
// client.subscribe("/home/kuh/light_sp_set");
// client.subscribe("/home/kuh/light_db_set");
// client.publish("/home/kuh/light_db_set", String(lightData.DB).c_str());
// client.publish("/home/kuh/light_sb_set", String(lightData.SP).c_str());
// } else {
// //Serial.print("failed, rc="); // "подключение не удалось"
// //Serial.print(client.state());
// //Serial.println(" try again in 5 seconds");
// }
// delay(100);
// digitalWrite(RED, LOW);
// }
void getTemp()
{
static bool readTemp = false;
if (readTemp){
sensors.setWaitForConversion(false);
sensors.requestTemperatures();
readTemp = !readTemp;
}
else{
float t = sensors.getTempC(da[nSens]);//ByIndex(nSens);
//float t = sensors.getTempCByIndex(nSens);
//Serial.print(nSens);
//Serial.print(" Temp readed=");
//Serial.println(t);
if ((t > -127) && (t < 85)){
stat[nSens] = 1;
switch(nSens){
case 0:
if (!isnan(tempOut)) tempOut += (t - tempOut) / 240.0f;
else tempOut = t;
break;
case 1:
if (!isnan(tempHol)) tempHol += (t - tempHol) * 0.05f;
else tempHol = t;
break;
case 2:
if (!isnan(tempHoM)) tempHoM += (t - tempHoM) * 0.05f;
else tempHoM = t;
break;
case 3:
if (!isnan(tempMor)) tempMor += (t - tempMor) * 0.05f;
else tempMor = t;
break;
}
}
else stat[nSens] = 0;
if (++nSens > 3){
nSens = 0;
readTemp = !readTemp;
}
}
//n++;
}
void connectToWifi() {
WiFi.begin(ssid, password);
}
void connectToMqtt() {
mqttClient.connect();
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
connectToMqtt();
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
wifiReconnectTimer.once(2, connectToWifi);
}
void onMqttConnect(bool sessionPresent) {
char v[6];
itoa(lightData.SP, v, 10);
mqttClient.publish("/home/kuh/light_sp_set", 1, false, v);
itoa(lightData.DB, v, 10);
mqttClient.publish("/home/kuh/light_db_set", 1, false, v);
mqttClient.publish("/home/kuh/move", 1, false, mv == 1 ? "1" : "0");
mqttClient.subscribe("/home/kuh/light_sp_set", 1);
mqttClient.subscribe("/home/kuh/light_db_set", 1);
digitalWrite(BLUE, LOW);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
if (WiFi.isConnected()) {
mqttReconnectTimer.once(2, connectToMqtt);
}
digitalWrite(BLUE, HIGH);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
bool w = false;
if(strcmp(topic, "/home/kuh/light_sp_set") == 0){
lightSP = atoi(payload);
w = true;
}
if(strcmp(topic, "/home/kuh/light_db_set") == 0){
lightDB = atoi(payload);
w = true;
}
if(w){
lightData.SP = lightSP;
lightData.DB = lightDB;
EEPROM.put(0, lightData);
EEPROM.commit();
}
}