/
oldprinters
/
Kitchen2450
Обзор
Документация
Войти
/
oldprinters
/
Kitchen2450
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/main.cpp
255 строк
8 KB
oldprinters
что-то древнее
10 дек 2024, 16:42
10 дек 2024, 16:42
6cfcfab
Код
Авторство
О чём код?
//gigaCode #include <Arduino.h> #include <WiFi.h> // library to connect to Wi-Fi network #include "NTPClient.h" #include <WiFiUdp.h> #include <PubSubClient.h> #include <Wire.h> #include <BH1750.h> #include "Timer.h" #include "alarm.h" #include "ObjectM.h" #include "PointLed.h" #include <Adafruit_GFX.h> #include <Adafruit_SH110X.h> #include <Adafruit_AHTX0.h> #include <Fonts/FreeSans9pt7b.h> Adafruit_AHTX0 aht; sensors_event_t humidity, temp; const uint16_t TimerTM{500}; Timer timerTH(TimerTM); float humidityOld{0}; float tempOld{0}; const uint16_t dTimerLight{250}; Timer tRadar(100); Timer timerLight(dTimerLight); ObjectM arrObj[3]; //------------------------------------------------------------------------------------------------------------ //TODO: добавить кнопку для включения максимального света, повторное нажатие отключает максимальный свет. //выход из зоны также отключает максимальный свет. //------------------------------------------------------------------------------------------------------------ const uint8_t ledR{27}; const uint8_t ledG{26}; const uint8_t ledB{25}; //------------------------------------------------------------------------------------------------------- PointLed led1(ledR, 0, arrObj, 10, 1450, 1000); PointLed led2(ledG, 1, arrObj, 1300, 3200, 100); // PointLed led3(ledB, 2, arrObj, 1900, 3200); // PointLed* arrLed[3]{&led1, &led2, &led3}; PointLed* arrLed[]{&led1, &led2}; //--------------------------------------------- // WiFiUDP ntpUDP; // NTPClient timeClient(ntpUDP, "pool.ntp.org", 10800, 3600000); //--------------------------------------------- const char* ssid = "ivanych"; const char* password = "stroykomitet"; WiFiClient espClient; PubSubClient client(espClient); //----------------------------------------------------- const char* mqtt_server = "192.168.1.34"; const char* msgMotion = "kitchen/motion"; const char* msgPresence = "kitchen/presence"; const char* msgLight = "kitchen/light"; const char* msgMaxLightOn = "kitchen/maxLightOn"; const char* msgMaxLightOff = "kitchen/maxLightOff"; const char* msgHSMotion = "hall_small/motion"; const char* msgDT = "kitchen/temp"; const char* msgDH = "kitchen/humidity"; //----------------------------------------------------------- BH1750 lightMeter(0x23); //0x5c 23 float lux{8000}; //яркость света в помещении float luxOld{0}; //предыдущее значение //------------------------------------------------------------ bool statMove{false}; bool statMoveOld{false}; //------------------------------------------------------------ // Alarm ledAlR(ledR); // Alarm ledAlG(ledG); // Alarm ledAlB(ledB); // Alarm* arrAlarm[3]{&ledAlR, &ledAlG, &ledAlB}; //-------------------------------------- void callback(char* topic, byte* payload, unsigned int length) { String str = {}; String strTopic = topic; String sOut{}; for (int i = 0; i < length; i++) { str += (char)payload[i]; } if(strTopic == msgHSMotion){ // light_1.extLightOn(); } else if(strTopic == msgMaxLightOn){ for(auto led: arrLed){ led->setMaxLevelOn(); } } else if(strTopic == msgMaxLightOff){ for(auto led: arrLed){ led->setMaxLevelOff(); } } // } else if(strTopic == lightNightOn){ // } else if(strTopic == lightNightOff){ // } } //----------------------------------- void reconnect_mqtt() { // Loop until we're reconnected if(!client.connected()) { while (!client.connected()) { Serial.println("Attempting MQTT connection..."); String clientId = "Kitchen2450"; // Attempt to connect Serial.println(clientId); if (client.connect(clientId.c_str())) { Serial.println("connected"); client.subscribe(msgMaxLightOff, 0); client.subscribe(msgMaxLightOn, 0); // client.subscribe(msgLightOn, 0); } else { Serial.print("failed, rc= "); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } else { client.loop(); //mqtt } } //------------------------------------------------------------------------------------------------------------ void setup() { Serial.begin(115200); //Feedback over Serial Monitor Serial.println(F("=========================== ptr ==")); Wire.begin(); // Wire.setClock(400000); // use 400 kHz I2C Serial2.begin (256000, SERIAL_8N1, 16, 17); //UART for monitoring the radar delay(500); WiFi.begin(ssid, password); // initialise Wi-Fi and wait while (WiFi.status() != WL_CONNECTED){ Serial.print("."); delay(500); } // timeClient.begin(); // timeClient.update(); client.setServer(mqtt_server, 1883); client.setCallback(callback); reconnect_mqtt(); //--------------------------- Serial.println("Init AHT10 begin."); if (aht.begin(&Wire)) { Serial.println("Found AHT20"); } else { Serial.println("Didn't find AHT20"); } Serial.println("Init AHT10 end."); //--------------------------- if (lightMeter.begin()) {//BH1750::CONTINUOUS_HIGH_RES_MODE)) { Serial.println(F("BH1750 Advanced begin")); } else { Serial.println(F("Error initialising BH1750")); while(1); } } //------------------------------------------------------------------------------------------------------------ void outByte(uint8_t n){ if(n < 10) Serial.print("0"); Serial.print(n, HEX); } //------------------------------------------------------------------------------------------------------------ void outHexArray(uint8_t *byteArray, int size = 26){ for (int i = 0; i < size; i++) { outByte(byteArray[i]); if(i % 2 > 0) Serial.print(" "); } Serial.println(); } //------------------------------------------------------------------------------------------------------------ int getRadar(){ int i = 0; while(Serial2.available()){ if (Serial2.read()==0xAA && Serial2.read()==0xFF) { uint8_t byteArray[28]; int nBytes = Serial2.readBytes(byteArray,28); i = 2; uint16_t m{0}; for(auto& obj: arrObj){ obj.setParam(byteArray, i); // arrAlarm[m++] -> setDuration(obj.setParam(byteArray, i)); } } } return i; } //----------------------------------------------------------------------------------------------------------- //....... измерение освещённости float measurmentLight(){ if (timerLight.getTimer() && lightMeter.measurementReady()) { lux = lightMeter.readLightLevel(); timerLight.setTimer(dTimerLight); uint16_t dLux{1}; if(lux >100)dLux = 5; else if(lux > 50)dLux = 3; else if(lux > 20)dLux = 2; if(abs(lux - luxOld) > dLux){ client.publish(msgLight, String(lux).c_str()); luxOld = lux; } } return lux; } //----------------------------------------------------------------------------------------------------------- //....... измерение освещённости void measurmentTH(){ if (timerTH.getTimer()) { timerTH.setTimer(); aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data from the AHT10 if(abs(humidity.relative_humidity - humidityOld) > 2 || abs(temp.temperature - tempOld) > 0.5){ client.publish(msgDT, String(temp.temperature).c_str()); client.publish(msgDH, String(humidity.relative_humidity).c_str()); humidityOld = humidity.relative_humidity; tempOld = temp.temperature; } // Serial.print(F("Humidity: ")); // Serial.print(humidity.relative_humidity); // Serial.print(F(", Temperature: ")); // Serial.println(temp.temperature); } } //------------------------------------------------------------------------------------------------------------ void loop() { reconnect_mqtt(); if(tRadar.getTimer()){ tRadar.setTimer(); getRadar(); statMove = false; for(auto& aa: arrLed){ statMove |= !aa -> setDist(lux); } // client.publish(msgMotion, String(statMove).c_str()); // Serial.println(statMove); if(statMove != statMoveOld){ client.publish(msgMotion, String(statMove).c_str()); statMoveOld = statMove; } } measurmentTH(); measurmentLight(); for(auto& aa: arrLed){ aa -> cycle(); } }