diff --git a/include/Consts.h b/include/Consts.h index 1efaa87f..d017003f 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -2,7 +2,7 @@ //===========Firmware============================================================================================================================================= #define FIRMWARE_VERSION 271 -//#define FLASH_SIZE_1MB +//#define FLASH_SIZE_1MB true #ifdef ESP8266 #define FIRMWARE_NAME "esp8266-iotm" #endif @@ -121,4 +121,10 @@ enum ConfigType_t { //Flash: [===== ] 54.5% (used 569296 bytes from 1044464 bytes) //RAM: [===== ] 45.6% (used 37336 bytes from 81920 bytes) -//Flash: [====== ] 55.3% (used 577396 bytes from 1044464 bytes) \ No newline at end of file +//Flash: [====== ] 55.3% (used 577396 bytes from 1044464 bytes) + + +//eventBuf - буфер событий которые проверяются в сценариях, +//и если событие удовлетворяет какому нибудь условию то выполняются указанные команды + +//orderBuf - буфер команд которые выполняются сейчас же \ No newline at end of file diff --git a/include/MqttClient.h b/include/MqttClient.h index f2d7b82c..88a166d7 100644 --- a/include/MqttClient.h +++ b/include/MqttClient.h @@ -17,6 +17,7 @@ boolean publishChart(const String& topic, const String& data); boolean publishControl(String id, String topic, String state); boolean publishChart_test(const String& topic, const String& data); boolean publishStatus(const String& topic, const String& data); +boolean publishEvent(const String& topic, const String& data); boolean publishInfo(const String& topic, const String& data); void publishWidgets(); diff --git a/include/items/SensorModbusClass.h b/include/items/SensorModbusClass.h index a93063c8..494540ea 100644 --- a/include/items/SensorModbusClass.h +++ b/include/items/SensorModbusClass.h @@ -1,83 +1,85 @@ -//#pragma once -//#include -//#include -//#include -// -//#include "Class/LineParsing.h" -//#include "Global.h" -//#include "items/SensorConvertingClass.h" -// -//ModbusMaster modbus1; -//SoftwareSerial uart(13, 12); // RX, TX -// -//class SensorModbusClass : public SensorConvertingClass { -// public: -// SensorModbusClass() : SensorConvertingClass(){}; -// -// void SensorModbusInit() { -// uart.begin(9600); -// jsonWriteStr(configOptionJson, _key + "_map", _map); -// jsonWriteStr(configOptionJson, _key + "_с", _c); -// sensorReadingMap10sec += _key + " " + _addr + " " + _reg + ","; -// Serial.println(sensorReadingMap10sec); -// } -// -// void SensorModbusRead(String key, uint8_t slaveAddress, uint16_t regAddress) { -// int value; -// -// modbus1.begin(slaveAddress, uart); -// uint16_t reqisterValue = modbus1.readHoldingRegisters(regAddress, 1); -// if (getResultMsg(&modbus1, reqisterValue)) { -// reqisterValue = modbus1.getResponseBuffer(0); -// value = reqisterValue; -// } else { -// value = NULL; -// } -// -// int valueFl = this->correction(key, value); -// eventGen2(key, String(valueFl)); -// jsonWriteStr(configLiveJson, key, String(valueFl)); -// publishStatus(key, String(valueFl)); -// SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl) + ", Slave dev addr: " + String(slaveAddress) + ", Register: " + String(regAddress)); -// } -// -// bool getResultMsg(ModbusMaster* modbus1, uint16_t result) { -// String tmpstr; -// switch (result) { -// case modbus1->ku8MBSuccess: -// return true; -// tmpstr += "Ok"; -// break; -// case modbus1->ku8MBIllegalFunction: -// tmpstr += "Illegal Function"; -// break; -// case modbus1->ku8MBIllegalDataAddress: -// tmpstr += "Illegal Data Address"; -// break; -// case modbus1->ku8MBIllegalDataValue: -// tmpstr += "Illegal Data Value"; -// break; -// case modbus1->ku8MBSlaveDeviceFailure: -// tmpstr += "Slave Device Failure"; -// break; -// case modbus1->ku8MBInvalidSlaveID: -// tmpstr += "Invalid Slave ID"; -// break; -// case modbus1->ku8MBInvalidFunction: -// tmpstr += "Invalid Function"; -// break; -// case modbus1->ku8MBResponseTimedOut: -// tmpstr += "Response Timed Out"; -// break; -// case modbus1->ku8MBInvalidCRC: -// tmpstr += "Invalid CRC"; -// break; -// default: -// tmpstr += "Unknown error: " + String(result); -// break; -// } -// SerialPrint("I", "Modbus", tmpstr); -// return false; -// } -//}; -//extern SensorModbusClass mySensorModbus; \ No newline at end of file +#ifdef modbus +#pragma once +#include +#include +#include + +#include "Class/LineParsing.h" +#include "Global.h" +#include "items/SensorConvertingClass.h" + +ModbusMaster modbus1; +SoftwareSerial uart(13, 12); // RX, TX + +class SensorModbusClass : public SensorConvertingClass { + public: + SensorModbusClass() : SensorConvertingClass(){}; + + void SensorModbusInit() { + uart.begin(9600); + jsonWriteStr(configOptionJson, _key + "_map", _map); + jsonWriteStr(configOptionJson, _key + "_с", _c); + sensorReadingMap10sec += _key + " " + _addr + " " + _reg + ","; + Serial.println(sensorReadingMap10sec); + } + + void SensorModbusRead(String key, uint8_t slaveAddress, uint16_t regAddress) { + int value; + + modbus1.begin(slaveAddress, uart); + uint16_t reqisterValue = modbus1.readHoldingRegisters(regAddress, 1); + if (getResultMsg(&modbus1, reqisterValue)) { + reqisterValue = modbus1.getResponseBuffer(0); + value = reqisterValue; + } else { + value = NULL; + } + + int valueFl = this->correction(key, value); + eventGen2(key, String(valueFl)); + jsonWriteStr(configLiveJson, key, String(valueFl)); + publishStatus(key, String(valueFl)); + SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl) + ", Slave dev addr: " + String(slaveAddress) + ", Register: " + String(regAddress)); + } + + bool getResultMsg(ModbusMaster* modbus1, uint16_t result) { + String tmpstr; + switch (result) { + case modbus1->ku8MBSuccess: + return true; + tmpstr += "Ok"; + break; + case modbus1->ku8MBIllegalFunction: + tmpstr += "Illegal Function"; + break; + case modbus1->ku8MBIllegalDataAddress: + tmpstr += "Illegal Data Address"; + break; + case modbus1->ku8MBIllegalDataValue: + tmpstr += "Illegal Data Value"; + break; + case modbus1->ku8MBSlaveDeviceFailure: + tmpstr += "Slave Device Failure"; + break; + case modbus1->ku8MBInvalidSlaveID: + tmpstr += "Invalid Slave ID"; + break; + case modbus1->ku8MBInvalidFunction: + tmpstr += "Invalid Function"; + break; + case modbus1->ku8MBResponseTimedOut: + tmpstr += "Response Timed Out"; + break; + case modbus1->ku8MBInvalidCRC: + tmpstr += "Invalid CRC"; + break; + default: + tmpstr += "Unknown error: " + String(result); + break; + } + SerialPrint("I", "Modbus", tmpstr); + return false; + } +}; +extern SensorModbusClass mySensorModbus; +#endif \ No newline at end of file diff --git a/include/items/SensorUltrasonicClass.h b/include/items/SensorUltrasonicClass.h deleted file mode 100644 index 96d6883c..00000000 --- a/include/items/SensorUltrasonicClass.h +++ /dev/null @@ -1,49 +0,0 @@ -//#pragma once -//#include -// -//#include "Class/LineParsing.h" -//#include "Global.h" -//#include "items/SensorConvertingClass.h" -//#include "GyverFilters.h" -// -//GMedian<6, int> testFilter; -// -//class SensorUltrasonic : public SensorConvertingClass { -// public: -// SensorUltrasonic() : SensorConvertingClass(){}; -// void init() { -// sensorReadingMap10sec += _key + ","; -// String trig = selectFromMarkerToMarker(_pin, ",", 0); -// String echo = selectFromMarkerToMarker(_pin, ",", 1); -// pinMode(trig.toInt(), OUTPUT); -// pinMode(echo.toInt(), INPUT); -// jsonWriteStr(configOptionJson, _key + "_trig", trig); -// jsonWriteStr(configOptionJson, _key + "_echo", echo); -// jsonWriteStr(configOptionJson, _key + "_map", _map); -// jsonWriteStr(configOptionJson, _key + "_с", _c); -// } -// -// void SensorUltrasonicRead(String key) { -// int trig = jsonReadStr(configOptionJson, key + "_trig").toInt(); -// int echo = jsonReadStr(configOptionJson, key + "_echo").toInt(); -// int value; -// -// digitalWrite(trig, LOW); -// delayMicroseconds(2); -// digitalWrite(trig, HIGH); -// delayMicroseconds(10); -// digitalWrite(trig, LOW); -// long duration_ = pulseIn(echo, HIGH, 30000); // 3000 µs = 50cm // 30000 µs = 5 m -// value = duration_ / 29 / 2; -// -// value = testFilter.filtered(value); -// -// value = this->mapping(key, value); -// float valueFl = this->correction(key, value); -// eventGen2(key, String(valueFl)); -// jsonWriteStr(configLiveJson, key, String(valueFl)); -// publishStatus(key, String(valueFl)); -// SerialPrint("I", "Sensor", "'" + key + "' data: " + String(valueFl)); -// } -//}; -//extern SensorUltrasonic mySensorUltrasonic; \ No newline at end of file diff --git a/include/items/vSensorUltrasonic.h b/include/items/vSensorUltrasonic.h index a0c6311c..368af649 100644 --- a/include/items/vSensorUltrasonic.h +++ b/include/items/vSensorUltrasonic.h @@ -8,8 +8,6 @@ class SensorUltrasonic; typedef std::vector MySensorUltrasonicVector; - - class SensorUltrasonic : public SensorConvertingClass { public: diff --git a/platformio.ini b/platformio.ini index db994f90..7b6ac912 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,7 +37,6 @@ extra_scripts = ./tools/littlefsbuilder.py framework = arduino ;board = esp01_1m board = nodemcuv2 -;board_build.ldscript = eagle.flash.1m512.ld board_build.ldscript = eagle.flash.1m256.ld platform = https://github.com/platformio/platform-espressif8266.git lib_deps = diff --git a/src/Class/ScenarioClass3.cpp b/src/Class/ScenarioClass3.cpp index 085f8784..ec2f4efb 100644 --- a/src/Class/ScenarioClass3.cpp +++ b/src/Class/ScenarioClass3.cpp @@ -10,7 +10,8 @@ void eventGen2(String eventName, String eventValue) { String event = eventName + " " + eventValue + ","; eventBuf += event; - streamEventUDP(event); + if (jsonReadBool(configSetupJson, "snaMqtt")) publishEvent(eventName, eventValue); + //streamEventUDP(event); } void streamEventUDP(String event) { diff --git a/src/ItemsList.cpp b/src/ItemsList.cpp index d80c11c0..acab5352 100644 --- a/src/ItemsList.cpp +++ b/src/ItemsList.cpp @@ -41,7 +41,6 @@ void addItem(String name) { String url = serverIP + F("/projects/iotmanager/config/items/") + name + ".txt"; String item = getURL(url); Serial.println(url); - Serial.println(item); if (item == "error") return; #endif #ifndef FLASH_SIZE_1MB @@ -75,30 +74,31 @@ void addItem(String name) { item.replace("\r\n", ""); item.replace("\r", ""); item.replace("\n", ""); + addFile(DEVICE_CONFIG_FILE, "\n" + item); + Serial.println(item); } void addPreset(String name) { #ifdef FLASH_SIZE_1MB - String url2 = serverIP + F("/projects/iotmanager/config/presets/") + name + ".txt"; - String preset = getURL(url2); - Serial.println(url2); - Serial.println(preset); + String url = serverIP + F("/projects/iotmanager/config/presets/") + name + ".txt"; + String preset = getURL(url); + Serial.println(url); if (preset == "error") return; #endif #ifndef FLASH_SIZE_1MB String preset = readFile("presets/" + name + ".txt", 4048); #endif - + addFile(DEVICE_CONFIG_FILE, "\n" + preset); + Serial.println(preset); name.replace(".c", ".s"); #ifdef FLASH_SIZE_1MB - String url = serverIP + F("/projects/iotmanager/config/presets/") + name + ".txt"; + url = serverIP + F("/projects/iotmanager/config/presets/") + name + ".txt"; String scenario = getURL(url); Serial.println(url); - Serial.println(scenario); if (scenario == "error") return; #endif #ifndef FLASH_SIZE_1MB @@ -106,8 +106,11 @@ void addPreset(String name) { #endif removeFile(DEVICE_SCENARIO_FILE); + + addFile(DEVICE_SCENARIO_FILE, scenario); loadScenario(); + Serial.println(scenario); } void delAllItems() { diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index 3486745d..2d31e71b 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -72,7 +72,7 @@ void mqttSubscribe() { mqtt.subscribe((mqttRootDevice + "/update").c_str()); if (jsonReadBool(configSetupJson, "snaMqtt")) { - mqtt.subscribe((mqttPrefix + "/+/+/status").c_str()); + mqtt.subscribe((mqttPrefix + "/+/+/event").c_str()); mqtt.subscribe((mqttPrefix + "/+/+/info").c_str()); } } @@ -142,26 +142,24 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) { SerialPrint("I", "=>MQTT", "Msg from iotmanager app: " + key + " " + payloadStr); } - else if (topicStr.indexOf("status") != -1) { + else if (topicStr.indexOf("event") != -1) { if (!jsonReadBool(configSetupJson, "snaMqtt")) { return; } if (topicStr.indexOf(chipId) == -1) { String devId = selectFromMarkerToMarker(topicStr, "/", 2); String key = selectFromMarkerToMarker(topicStr, "/", 3); - String value = jsonReadStr(payloadStr, "status"); - - SerialPrint("I", "=>MQTT", "Msg from other device: '" + devId + "' " + key + " " + value); - - eventGen2(key, value); + SerialPrint("I", "=>MQTT", "Received event from other device: '" + devId + "' " + key + " " + payloadStr); + String event = key + " " + payloadStr + ","; + eventBuf += event; } } else if (topicStr.indexOf("info") != -1) { if (topicStr.indexOf("scen") != -1) { writeFile(String(DEVICE_SCENARIO_FILE), payloadStr); - loadScenario(); - SerialPrint("I", "=>MQTT", "Scenario received"); + loadScenario(); + SerialPrint("I", "=>MQTT", "Scenario received"); } } @@ -215,6 +213,11 @@ boolean publishStatus(const String& topic, const String& data) { return mqtt.publish(path.c_str(), json.c_str(), false); } +boolean publishEvent(const String& topic, const String& data) { + String path = mqttRootDevice + "/" + topic + "/event"; + return mqtt.publish(path.c_str(), data.c_str(), false); +} + boolean publishInfo(const String& topic, const String& data) { String path = mqttRootDevice + "/" + topic + "/info"; return mqtt.publish(path.c_str(), data.c_str(), false); diff --git a/src/Web.cpp b/src/Web.cpp index 8e4b6eb9..5990488b 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -323,6 +323,10 @@ void web_init() { SerialPrint("I", "Update", "firmware version: " + String(lastVersion)); String msg = ""; + //if (FLASH_SIZE_1MB) { + // msg = F("Обновление невозможно, память устройства 1 мб"); + //} + //else { if (lastVersion == FIRMWARE_VERSION) { msg = F("Актуальная версия прошивки уже установлена."); } @@ -338,6 +342,7 @@ void web_init() { else if (lastVersion < FIRMWARE_VERSION) { msg = F("Ошибка версии. Попробуйте повторить позже..."); } + //} // else if (lastVersion == "") { //msg = F("Нажмите на кнопку \"обновить прошивку\" повторно..."); @@ -361,7 +366,7 @@ void web_init() { request->send(200, "text/html"); }); - SerialPrint("I", F("Web"), F("WebAdmin Init")); + SerialPrint("I", F("Web"), F("WebAdmin Init")); } void setConfigParam(const char* param, const String& value) { diff --git a/src/items/SensorUltrasonicClass.cpp b/src/items/SensorUltrasonicClass.cpp deleted file mode 100644 index 1d2fb965..00000000 --- a/src/items/SensorUltrasonicClass.cpp +++ /dev/null @@ -1,20 +0,0 @@ -//#include "BufferExecute.h" -//#include "items/SensorUltrasonicClass.h" -////#ifdef SensorUltrasonicEnabled -////=========================================Модуль ультрозвукового дальномера================================================================== -////ultrasonic-cm;id;anydata;Сенсоры;Расстояние;order;pin[12,13];map[1,100,1,100];c[1] -////========================================================================================================================================= -//SensorUltrasonic mySensorUltrasonic; -//void ultrasonicCm() { -// mySensorUltrasonic.update(); -// String key = mySensorUltrasonic.gkey(); -// sCmd.addCommand(key.c_str(), ultrasonicReading); -// mySensorUltrasonic.init(); -// mySensorUltrasonic.clear(); -//} -// -//void ultrasonicReading() { -// String key = sCmd.order(); -// mySensorUltrasonic.SensorUltrasonicRead(key); -//} -////#endif \ No newline at end of file