diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 99e6027..11f015e 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -82,8 +82,9 @@ #include "include/CanGrow_Core.h" #include "include/CanGrow_Wifi.h" #include "include/CanGrow_LittleFS.h" -#include "include/CanGrow_Webserver.h" #include "include/CanGrow_Sensor.h" +#include "include/CanGrow_Webserver.h" + void setup() { @@ -142,16 +143,16 @@ void setup() { Serial.println(":: [SETUP] Sensor drivers"); - for(byte i = 0; i < SensorIndex_length; i++) { + for(byte i = 1; i <= SensorIndex_length; i++) { Serial.print(":: [SETUP] Sensor Index "); Serial.print(i); Serial.print(", Name '"); Serial.print(SensorIndex[i].name); - Serial.println("'"); - Serial.println(":: [SETUP] Readings: "); + Serial.println("', Readings"); + for(byte j = 0; j < SENSOR_MAX_READING; j++) { if(SensorIndex[i].reading[j] > 0 ) { - Serial.print(":: [SETUP] - "); + Serial.print(":: [SETUP] "); Serial.print(j); Serial.print(": "); Serial.print(Sensor_Reading_descr[SensorIndex[i].reading[j]]); diff --git a/Arduino/CanGrow/include/CanGrow.h b/Arduino/CanGrow/include/CanGrow.h index 8dacb05..53b0dc1 100644 --- a/Arduino/CanGrow/include/CanGrow.h +++ b/Arduino/CanGrow/include/CanGrow.h @@ -147,8 +147,6 @@ struct GPIO_Index { const byte note; }; -const byte Max_Outputs = 16; - /* * @@ -156,6 +154,11 @@ const byte Max_Outputs = 16; * */ +// define Max limits for outputs and sensors +const byte Max_Outputs = 16; +const byte Max_Sensors = 16; + + /* * Config WiFi */ @@ -218,31 +221,14 @@ struct Config_System_Output { struct Config_System_Sensor { /* * Config System Sensor - * - * - Sensor Name - * - Sensors can offer following types - * - temperature - * - humidity - * - moisture - * - raw - * - * - * - type: sensor type like I2C, OneWire, ... - * 1 - builtin ADC - * 2 - I2C - * 3 - Webcall - * - * What are sensors? - * - Analog builtin - * - I2C ADC - * - I2C Soil Moisture - * - I2C often multisensor offering mutliple values for temp, humidity, light , whatever - * - + * - type: Index ID of SensorIndex, which Sensor to use (ADC, BME280, Chirp, ...) + * - name: nice name + * - gpio: gpio to use for RPM reading, builtin ADC, OneWire */ - byte type[Max_Outputs]; - byte device[Max_Outputs]; - char name[Max_Outputs][32]; + byte type[Max_Sensors]; + char name[Max_Sensors][32]; + byte gpio[Max_Sensors]; }; /* main System struct */ diff --git a/Arduino/CanGrow/include/CanGrow_Core.h b/Arduino/CanGrow/include/CanGrow_Core.h index 7a0782f..75c64c2 100644 --- a/Arduino/CanGrow/include/CanGrow_Core.h +++ b/Arduino/CanGrow/include/CanGrow_Core.h @@ -73,6 +73,23 @@ byte Give_Free_OutputId() { return outputId_free; } +byte Give_Free_SensorId() { + byte sensorId_free; + for(byte i=0; i < Max_Sensors; i++) { + if(config.system.sensor.type[i] > 0) { + // here i define that 255 stands for "no more free outputs" + sensorId_free = 255; + } else { + sensorId_free = i; + break; + } + } + #ifndef DEBUG + Serial.printf("DB [Core:Give_Free_SensorId] next free output id: %d\n", sensorId_free); + #endif + return sensorId_free; +} + //bool Check_OutputId_Used(byte outputId) { //} diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index f4bdcaf..6e72f33 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -205,9 +205,9 @@ bool LoadConfig() { config.system.httpLogSerial = objSystem["httpLogSerial"]; config.system.schedulerInterval = objSystem["schedulerInterval"]; - JsonObject objSystemOutput = objSystem["output"][0]; /* System Outputs */ + JsonObject objSystemOutput = objSystem["output"][0]; for(byte i=0; i < Max_Outputs; i++) { if(objSystemOutput["type"][i] > 0) { config.system.output.type[i] = objSystemOutput["type"][i]; @@ -226,6 +226,20 @@ bool LoadConfig() { strlcpy(config.system.output.webcall_path_off[i], objSystemOutput["webcall_path_off"][i], sizeof(config.system.output.webcall_path_off[i])); } } + + + /* System Sensors */ + JsonObject objSystemSensor = objSystem["sensor"][0]; + for(byte i=0; i < Max_Sensors; i++) { + if(objSystemSensor["type"][i] > 0) { + config.system.sensor.type[i] = objSystemSensor["type"][i]; + strlcpy(config.system.sensor.name[i], objSystemSensor["name"][i], sizeof(config.system.sensor.name[i])); + // gpio + config.system.sensor.gpio[i] = objSystemSensor["gpio"][i]; + + } + } + /* Grow */ JsonObject objGrow = doc["grow"][0]; @@ -308,6 +322,17 @@ bool SaveConfig(bool writeToSerial = false) { } } + /* System Sensors */ + JsonObject objSystemSensor = objSystem["sensor"].add(); + for(byte i=0; i < Max_Sensors; i++) { + if(config.system.sensor.type[i] > 0) { + objSystemSensor["type"][i] = config.system.sensor.type[i]; + objSystemSensor["name"][i] = config.system.sensor.name[i]; + objSystemSensor["gpio"][i] = config.system.sensor.gpio[i]; + + } + } + /* Grow */ JsonObject objGrow = doc["grow"].add(); diff --git a/Arduino/CanGrow/include/CanGrow_Sensor.h b/Arduino/CanGrow/include/CanGrow_Sensor.h index 34bc34d..d4a9178 100644 --- a/Arduino/CanGrow/include/CanGrow_Sensor.h +++ b/Arduino/CanGrow/include/CanGrow_Sensor.h @@ -78,38 +78,43 @@ struct Sensor_Index { * */ const char name[32]; - byte reading[SENSOR_MAX_READING]; + const byte reading[SENSOR_MAX_READING]; }; const byte SensorIndex_length = 4; Sensor_Index SensorIndex[] { - // 0 - first sensor - { "internal ADC", { - SENSOR_READING_RAW, + // 0 is for unset in config + { "unset", { + {}, }}, - // 1 + // 1 - internal ADC + { SENSOR_00_NAME, { + SENSOR_READING_RAW, + }}, + + // 2 - BME280 { "BME280", { SENSOR_READING_TEMP, SENSOR_READING_HUMIDITY, SENSOR_READING_PRESSURE, }}, - // 2 + // 3 { "Chirp", { SENSOR_READING_MOISTURE, SENSOR_READING_TEMP, SENSOR_READING_RAW, }}, - // 3 - { "AS1115", { + // 4 + { "ADS1115", { SENSOR_READING_RAW, SENSOR_READING_RAW, SENSOR_READING_RAW, SENSOR_READING_RAW }}, - // 4 + // 5 }; diff --git a/Arduino/CanGrow/include/Sensor/00_ADC_builtin.h b/Arduino/CanGrow/include/Sensor/00_ADC_builtin.h index 61a9dbe..0a631a6 100644 --- a/Arduino/CanGrow/include/Sensor/00_ADC_builtin.h +++ b/Arduino/CanGrow/include/Sensor/00_ADC_builtin.h @@ -26,3 +26,5 @@ * THE SOFTWARE. * */ + +#define SENSOR_00_NAME "ADC builtin" diff --git a/Arduino/CanGrow/include/Webserver/Page_system.h b/Arduino/CanGrow/include/Webserver/Page_system.h index f5803c8..5963d51 100644 --- a/Arduino/CanGrow/include/Webserver/Page_system.h +++ b/Arduino/CanGrow/include/Webserver/Page_system.h @@ -794,25 +794,21 @@ String Proc_WebPage_system_sensor(const String& var) { // build table body // i dont know a better way at the moment. if you do, please tell me! String output_tr_td; - for(byte i=0; i < Max_Outputs; i++) { - if(config.system.output.type[i] > 0) { + for(byte i=0; i < Max_Sensors; i++) { + if(config.system.sensor.type[i] > 0) { #ifndef DEBUG - Serial.printf("DB [Webserver:system:output(Proc)] OutputID %d Type %d\n", i, config.system.output.type[i]); + Serial.printf("DB [Webserver:system:sensor(Proc)] sensorId %d Type %d\n", i, config.system.sensor.type[i]); #endif output_tr_td += ""; output_tr_td += i; output_tr_td += ""; - output_tr_td += config.system.output.name[i]; + output_tr_td += config.system.sensor.name[i]; output_tr_td += ""; - output_tr_td += Output_Type_descr[config.system.output.type[i]]; - output_tr_td += ""; - output_tr_td += Output_Device_descr[config.system.output.device[i]]; - output_tr_td += ""; - output_tr_td += config.system.output.enabled[i]; + output_tr_td += SensorIndex[config.system.sensor.type[i]].name; output_tr_td += ""; // edit button - output_tr_td += "
"; + output_tr_td += ""; output_tr_td += ""; @@ -820,12 +816,12 @@ String Proc_WebPage_system_sensor(const String& var) { // delete button - output_tr_td += ""; - output_tr_td += ""; + output_tr_td += ""; output_tr_td += "
"; output_tr_td += ""; @@ -842,42 +838,32 @@ String Proc_WebPage_system_sensor_POST(const String& var) { if(var == "SAVE_MSG") { return String(Common_HTML_SAVE_MSG); } else { - return Proc_WebPage_system_output(var); + return Proc_WebPage_system_sensor(var); } } void WebPage_system_sensor(AsyncWebServerRequest *request) { if(request->method() == HTTP_POST) { - if(request->hasParam("delete_output", true)) { - byte outputId; + if(request->hasParam("delete_sensor", true)) { + byte sensorId; - const AsyncWebParameter* p_delete_output = request->getParam("delete_output", true); - Serial.printf(":: [Webserver:system:output] POST[%s]: %s\n", p_delete_output->name().c_str(), p_delete_output->value().c_str()); + const AsyncWebParameter* p_delete_sensor = request->getParam("delete_sensor", true); + Serial.printf(":: [Webserver:system:sensor] POST[%s]: %s\n", p_delete_sensor->name().c_str(), p_delete_sensor->value().c_str()); - outputId = p_delete_output->value().toInt(); + sensorId = p_delete_sensor->value().toInt(); - Serial.printf(":: [Webserver:system:output] Deleting output: %d\n", outputId); + Serial.printf(":: [Webserver:system:output] Deleting output: %d\n", sensorId); // we ensure that every field is empty - config.system.output.type[outputId] = 0; - config.system.output.device[outputId] = 0; - // set every field of char array to 0x00 with memset - memset(config.system.output.name[outputId], '\0', sizeof config.system.output.name[outputId]); - config.system.output.enabled[outputId] = 0; - config.system.output.gpio[outputId] = 0; - config.system.output.gpio_pwm[outputId] = 0; - config.system.output.gpio_invert[outputId] = 0; - memset(config.system.output.i2c[outputId], '\0', sizeof config.system.output.i2c[outputId]); - memset(config.system.output.webcall_host[outputId], '\0', sizeof config.system.output.webcall_host[outputId]); - memset(config.system.output.webcall_path_on[outputId], '\0', sizeof config.system.output.webcall_path_on[outputId]); - memset(config.system.output.webcall_path_off[outputId], '\0', sizeof config.system.output.webcall_path_off[outputId]); + config.system.sensor.type[sensorId] = 0; + memset(config.system.sensor.name[sensorId], '\0', sizeof config.system.sensor.name[sensorId]); + config.system.output.gpio[sensorId] = 0; SaveConfig(); } - request->send_P(200, "text/html", Page_system_sensor_HTML, Proc_WebPage_system_sensor_POST); - Serial.println(":: [Webserver:system:output] [POST] hello"); + Serial.println(":: [Webserver:system:sensor] [POST] hello"); } else { @@ -897,20 +883,20 @@ void WebPage_system_sensor(AsyncWebServerRequest *request) { /* returns select