diff --git a/50.bin b/50.bin new file mode 100644 index 0000000..a1ec879 Binary files /dev/null and b/50.bin differ diff --git a/51.bin b/51.bin new file mode 100644 index 0000000..ba131d9 Binary files /dev/null and b/51.bin differ diff --git a/52.bin b/52.bin new file mode 100644 index 0000000..5e6c844 Binary files /dev/null and b/52.bin differ diff --git a/M5Atom_airqa.ino b/M5Atom_airqa.ino index b32ed06..00f1e1e 100644 --- a/M5Atom_airqa.ino +++ b/M5Atom_airqa.ino @@ -17,13 +17,11 @@ 24A160474D14 24A160542B80 - Белая 2B80 Шумная 38CC У вас 23AC Офис 5450 -Кухня 4D41 - +Кухня 4D14 ***************************************************************************/ #include "esp_sleep.h" @@ -56,7 +54,7 @@ #define CCS811_ADDR 0x5B //Default I2C Address //#define CCS811_ADDR 0x5A //Alternate I2C Address -unsigned int VersionSW = 48; //65536 Версия прошивки +unsigned int VersionSW = 54; //65536 Версия прошивки //15 - добавлено то, се, забыл вообще дописать что добавлено Serial //19 - вывод в консоль всех действий, ошибки с обновлнеием - починил, прияногое мигание светодиодом, тест для поиска metrics. @@ -83,8 +81,11 @@ unsigned int VersionSW = 48; //65536 Версия прошивки //43 - Больше задержек для чтения i2c //44 - Новая либа твок "maarten-pennings" Спаркфан и Адафрут суки - с ошибками изначально пишут на всех форумах. //46 - Смена локального сервера времени, хрони на одроиде. Установка базовых линий по новой. -//48 - алгоритм фильтрации - +//48 - алгоритм фильтрации +//50 - нахуй хост менеджер, смена качества +//52 - убрал базовые линии ЕСО2. +//53 - базовые линии вернулись на место. Обновление по старту css811 7 измерений вместо 10 + WiFiManager wm; // обьект менеджера WiFiManagerParameter custom_field; Preferences OTApreferences; //Обьект хранения настроек хеша прошивки @@ -94,7 +95,7 @@ Preferences OTApreferences; //Обьект хранения настроек х CCS811 ccs811(23); ClosedCube_HDC1080 hdc1080; -StaticJsonDocument<200> doc; +StaticJsonDocument<200> doc; WiFiClient espClient; PubSubClient MqttClient(espClient); @@ -104,8 +105,10 @@ PubSubClient MqttClient(espClient); WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "192.168.89.210", 0, 20000); //Собственно сервер времени смещение и частоат запроса, но он вручную const PROGMEM char *willmess = "{\"conn\":\"err\"}"; -const char *mqttHostName = "metrics"; //Хостнейм брокера metrics.local cctv.automation.art //192.168.89.210 -unsigned int mqttPort = 1883; //Порт брокера 1883 8889 + //Хостнейм брокера metrics.local cctv.automation.art //192.168.89.210 + +IPAddress ipaddrBro(192, 168, 89, 210); +unsigned int mqttPort = 1883; //Порт брокера 1883 8889 const char *prefArea = "ota-config"; @@ -118,7 +121,6 @@ const PROGMEM char *mqttLogin = "AA_Lab", *mqttPass = "automation.art$"; char bufTopic[140]; char bufWillTopic[150]; - const char *mqttIPHost; RTC_DATA_ATTR byte countOta = 0; @@ -133,15 +135,6 @@ unsigned int cntWhile = 4; bool mqttSendFlag = false; bool flagblink = true; - -float k = 0.1; -float expRunningAverage(float newVal) { -RTC_DATA_ATTR static float filVal = 0; - filVal += (newVal - filVal) * k; - return filVal; -} - - void ledset(byte color = 0, bool blink = false) { M5.dis.setBrightness(30); //Половина яркости @@ -165,109 +158,27 @@ void ledset(byte color = 0, bool blink = false) } } -IPAddress str2IP(String str) -{ - IPAddress ret(getIpBlock(0, str), getIpBlock(1, str), getIpBlock(2, str), getIpBlock(3, str)); - return ret; -} - -int getIpBlock(int index, String str) -{ - char separator = '.'; - int found = 0; - int strIndex[] = {0, -1}; - int maxIndex = str.length() - 1; - - for (int i = 0; i <= maxIndex && found <= index; i++) - { - if (str.charAt(i) == separator || i == maxIndex) - { - found++; - strIndex[0] = strIndex[1] + 1; - strIndex[1] = (i == maxIndex) ? i + 1 : i; - } - } - - return found > index ? str.substring(strIndex[0], strIndex[1]).toInt() : 0; -} - -int setMqttServer(const char *mqttHostNameF, unsigned int mqttPortF) -{ - mdns_init(); - - IPAddress ipaddr; - OTApreferences.begin("ota-config"); - - String keyIpMqtt = OTApreferences.getString("mqttip", "0"); - //Если нет IP в preferences - Serial.print("keyIpMqtt: "); - Serial.println(keyIpMqtt); - - if (keyIpMqtt == "0") - { - //Запрос по хостнейму .local - ipaddr = MDNS.queryHost(mqttHostNameF, 4000); - //Таймаут 4 секунды - //Если не получили по хостнейму МДНС - Serial.print("mdns: "); - Serial.println(ipaddr); - - if (ipaddr.toString() == "0.0.0.0") - { - //Запрос к ДНС по адресу сайта - int err = WiFi.hostByName(mqttHostNameF, ipaddr); - ////real internal timeout in lwip library is 14[s] - //Если получили по хостнейму - if (err == 1) - { - Serial.print("hostByName: "); - Serial.println(ipaddr); - MqttClient.setServer(ipaddr, mqttPortF); - OTApreferences.putString("mqttip", ipaddr.toString()); - } - else - { - Serial.print("All method not FOUND IP"); - } - } - else - { //Если все хорошо - OTApreferences.putString("mqttip", ipaddr.toString()); - MqttClient.setServer(ipaddr, mqttPortF); - } - } - else - { - MqttClient.setServer(str2IP(keyIpMqtt), mqttPortF); - } - - OTApreferences.end(); - return 0; -} - //Настройки void setup() { - pinMode(GPIO_WAK, OUTPUT); //Пин датчика для работы - digitalWrite(GPIO_WAK, LOW); - delay(10); + digitalWrite(GPIO_WAK, LOW); + delay(10); Serial.begin(115200); Serial.setDebugOutput(true); - + M5.begin(true, false, true); Wire.begin(25, 21); //Пины для I2c на ATOM - pinMode(TRIGGER_PIN, INPUT); - hdc1080.begin(0x40); hdc1080.setResolution(HDC1080_RESOLUTION_11BIT, HDC1080_RESOLUTION_11BIT); + startCcs811(macc); - startCcs811(macc); - - WiFi.mode(WIFI_STA); + pinMode(TRIGGER_PIN, INPUT); + + WiFi.mode(WIFI_STA); topicTemp.toCharArray(bufTopic, topicTemp.length() + 1); willTopicTemp.toCharArray(bufWillTopic, willTopicTemp.length() + 1); @@ -292,8 +203,11 @@ void setup() ESP.restart(); } + + + //Запрос IP сервера MQTT и установка сервера - setMqttServer(mqttHostName, mqttPort); + // MqttClient.setServer(ipaddrBro, mqttPort); if (reqNtpTime() == 1) { @@ -352,7 +266,6 @@ void OTAUpdate() if (httpResponseCode > 0) { - Serial.println(httpResponseCode); payload = http.getString(); @@ -398,7 +311,7 @@ void OTAUpdate() if (flagOTA == true) { // flagOTA = false; - disconnectMQTT(); + disconnectMQTT(); ESPhttpUpdate.rebootOnUpdate(true); @@ -441,27 +354,28 @@ String getMacAddress() int startCcs811(String MacThat) { - - unsigned int coefBaseline = 0; - - ccs811.set_i2cdelay(50); - bool ok = ccs811.begin(); + unsigned int coefBaseline = 0; + ccs811.set_i2cdelay(50); + bool ok = ccs811.begin(); + delay(100); if (!ok) { Serial.println("setup: CCS811 begin FAILED"); + httpSerial("CCS811_failed_begin"); return 1; } - Serial.print("setup: hardware version: "); - Serial.println(ccs811.hardware_version(), HEX); - Serial.print("setup: bootloader version: "); - Serial.println(ccs811.bootloader_version(), HEX); - Serial.print("setup: application version: "); - Serial.println(ccs811.application_version(), HEX); + // Serial.print("setup: hardware version: "); + // Serial.println(ccs811.hardware_version(), HEX); + // Serial.print("setup: bootloader version: "); + // Serial.println(ccs811.bootloader_version(), HEX); + // Serial.print("setup: application version: "); + // Serial.println(ccs811.application_version(), HEX); ok = ccs811.start(CCS811_MODE_1SEC); if (!ok) { Serial.println("setup: CCS811 start FAILED"); + httpSerial("CCS811_failed_start"); return 2; } @@ -471,7 +385,7 @@ int startCcs811(String MacThat) } if (MacThat == "5002918A38CC") - { + { coefBaseline = 29814; } @@ -492,11 +406,11 @@ int startCcs811(String MacThat) if (coefBaseline != 0) { - ccs811.set_baseline(coefBaseline); + ccs811.set_baseline(coefBaseline); } return 0; -} +} int SetCallibrationCoeff(String MacThat) { @@ -562,50 +476,50 @@ void reqSensorData() while (ccsflag) { - delay(1100); + ccs811.read(&eco2N, &etvocN, &errstat, &raw); ccs811.get_baseline(&baseline); // Print measurement results based on status - if (errstat == CCS811_ERRSTAT_OK) - { - Serial.print("CCS811: "); - Serial.print("eco2="); - Serial.print(eco2N); - Serial.print(" ppm "); - Serial.print("etvoc="); - Serial.print(etvocN); - Serial.print(" ppb "); + // if (errstat == CCS811_ERRSTAT_OK) + // { + // // Serial.print("CCS811: "); + // // Serial.print("eco2="); + // // Serial.print(eco2N); + // // Serial.print(" ppm "); + // // Serial.print("etvoc="); + // // Serial.print(etvocN); + // // Serial.print(" ppb "); + // // Serial.print(" baseline= "); + // // Serial.print(baseline); + // // Serial.println(); + // } - Serial.print(" baseline= "); - Serial.print(baseline); - - //Serial.print("raw6="); Serial.print(raw/1024); Serial.print(" uA "); - //Serial.print("raw10="); Serial.print(raw%1024); Serial.print(" ADC "); - //Serial.print("R="); Serial.print((1650*1000L/1023)*(raw%1024)/(raw/1024)); Serial.print(" ohm"); - Serial.println(); - } - else if (errstat == CCS811_ERRSTAT_OK_NODATA) + // if (errstat == CCS811_ERRSTAT_OK_NODATA) + // { + // // Serial.println("CCS811: waiting for (new) data"); + // } + // else + + if (errstat & CCS811_ERRSTAT_I2CFAIL) { - Serial.println("CCS811: waiting for (new) data"); + httpSerial("CCS811_i2c_error"); } - else if (errstat & CCS811_ERRSTAT_I2CFAIL) + else { - Serial.println("CCS811: I2C error"); - } - else - { - Serial.print("CCS811: errstat="); - Serial.print(errstat, HEX); - Serial.print("="); - Serial.println(ccs811.errstat_str(errstat)); + // Serial.print("CCS811: errstat="); + // Serial.print(errstat, HEX); + // Serial.print("="); + // Serial.println(); + httpSerial("errstat="+String(ccs811.errstat_str(errstat))); } counAvail++; - if (counAvail >= 10) + if (counAvail >= 8) { ccsflag = false; } + delay(1200); } ledset(2, false); @@ -613,7 +527,6 @@ void reqSensorData() Eco2Av = eco2N; TvocAv = etvocN; BaselineTvoc = baseline; - rssi = WiFi.RSSI(); } @@ -630,7 +543,7 @@ int SendMqttReq(bool sendVal = true, bool sendStatus = true, byte statusConn = 1 if (sendVal == true) { - doc["t"] = expRunningAverage(TempAv); + doc["t"] = TempAv; doc["h"] = HumAv; doc["eco"] = (int)Eco2Av; doc["tvoc"] = (int)TvocAv; @@ -715,26 +628,26 @@ void reconnectMqtt() while (!MqttClient.connected()) { circle++; - if (circle == 7) + if (circle == 4) { break; } - String maccrandom = macc + String(random(0xffff), HEX); - const char *clientId = maccrandom.c_str(); + MqttClient.setServer(ipaddrBro, mqttPort); + const char *clientId = macc.c_str(); if (MqttClient.connect(clientId, mqttLogin, mqttPass, bufWillTopic, 2, true, willmess)) - { + { Serial.println("Mqttconnect - OK"); - - mqttSendFlag = true; - + mqttSendFlag = true; SendMqttReq(false, true, 1); } else { Serial.print("Mqtt.state() = "); Serial.println(MqttClient.state()); + httpSerial("MqttState:" + String(MqttClient.state())); + /* -4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time -3 : MQTT_CONNECTION_LOST - the network connection was broken @@ -753,6 +666,31 @@ void reconnectMqtt() } } +int httpSerial(String serialmess) +{ + + OTApreferences.begin("ota-config"); + if (WiFi.status() == WL_CONNECTED) + { + HTTPClient http; + String serverPath = "http://meteosence.s-host.net/serial/log.php?mac=" + macc + "&logmess=" + serialmess; + http.begin(serverPath.c_str()); + int httpResponseCode = http.GET(); + + if (httpResponseCode > 0) + { + // Serial.print("HTTP Response code: "); + // Serial.println(httpResponseCode); + String payload = http.getString(); + if (payload == "false") + { + Serial.println("Failed write to Http.Serial"); + } + } + } + return 0; +} + void loop() { //Проверка старта сервера @@ -767,20 +705,22 @@ void loop() MqttClient.loop(); reqSensorData(); - SetCallibrationCoeff(macc); + + SetCallibrationCoeff(macc); + SendMqttReq(true, true, 3); if (M5.Btn.pressedFor(1000)) { + httpSerial("Reset_in_button"); ledset(4, true); checkButton(); } - + if (countOta >= 10) { // reqNtpTime(); countOta = 0; - SendMqttReq(false, true, 4); OTAUpdate(); SendMqttReq(false, true, 1); @@ -793,6 +733,7 @@ void loop() int disconnectMQTT() { + MqttClient.disconnect(); return 0; } diff --git a/M5Atom_airqa.ino.m5stack_atom.bin b/M5Atom_airqa.ino.m5stack_atom.bin new file mode 100644 index 0000000..ca766dc Binary files /dev/null and b/M5Atom_airqa.ino.m5stack_atom.bin differ diff --git a/Сеть - Ярлык.lnk b/Сеть - Ярлык.lnk new file mode 100644 index 0000000..df36592 Binary files /dev/null and b/Сеть - Ярлык.lnk differ