implement Outputs
This commit is contained in:
		
							
								
								
									
										10
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,10 @@ | ||||
| { | ||||
|     "idf.flashType": "UART", | ||||
|     "idf.port": "/dev/ttyUSB0", | ||||
|     "idf.adapterTargetName": "esp32", | ||||
|     "files.associations": { | ||||
|         "http_metrics.h": "c", | ||||
|         "outputs.h": "c", | ||||
|         "task.h": "c" | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								README.md
									
									
									
									
									
								
							| @ -25,6 +25,7 @@ Safety <|-- HTTP_Metrics | ||||
|     } | ||||
|  | ||||
|     class Outputs{ | ||||
|         +initOutputs() | ||||
|         +getCirculationPumpState() | ||||
|         +setCirculationPumpState() | ||||
|         +getBurnerState() | ||||
| @ -49,4 +50,12 @@ Safety <|-- HTTP_Metrics | ||||
|         + connect_wifi() | ||||
|         + setMetrics() | ||||
|     } | ||||
| ``` | ||||
| ``` | ||||
|  | ||||
| ### Hardware | ||||
|  | ||||
|  | ||||
| | Function| ESP32 | ES32C14 | | ||||
| |------------------|-----------------|------------------| | ||||
| | CirculationPump   | IO27  | Relay 1 NO1               | | ||||
| | Burner            | IO14  | Relay 1 NC2               | | ||||
|  | ||||
| @ -2,7 +2,7 @@ | ||||
|  | ||||
| static EventGroupHandle_t s_wifi_event_group; | ||||
|  | ||||
| static const char *TAG = "esp32-env-exp-http_metrics"; | ||||
| static const char *TAG = "smart-oil-heater-control-system-http_metrics"; | ||||
|  | ||||
| char caHtmlResponse[HTML_RESPONSE_SIZE]; | ||||
| SemaphoreHandle_t xMutexAccessMetricResponse = NULL; | ||||
|  | ||||
							
								
								
									
										176
									
								
								main/main.c
									
									
									
									
									
								
							
							
						
						
									
										176
									
								
								main/main.c
									
									
									
									
									
								
							| @ -12,31 +12,18 @@ | ||||
| #include "freertos/timers.h" | ||||
|  | ||||
| #include "http_metrics.h" | ||||
| #include "outputs.h" | ||||
|  | ||||
| #define I2C_MASTER_SCL 19 | ||||
| #define I2C_MASTER_SDA 18 | ||||
|  | ||||
| //#define BMP280 | ||||
| //#define DHT11 | ||||
| //#define AHT10 | ||||
| //#define VEML7700 | ||||
|  | ||||
| #ifdef DHT11 | ||||
| #define CONFIG_EXAMPLE_DATA_GPIO 17 | ||||
| #define SENSOR_TYPE DHT_TYPE_DHT11 | ||||
| #endif | ||||
|  | ||||
| #ifdef AHT10 | ||||
| #define AHT_TYPE AHT_TYPE_AHT1x | ||||
| #endif | ||||
|  | ||||
| static const char *TAG = "smart-oil-heater-control-system"; | ||||
|  | ||||
| void app_main(void) | ||||
| { | ||||
|     ESP_LOGI(TAG, "starting ..."); | ||||
|  | ||||
|     //Initialize NVS | ||||
|     // Initialize NVS | ||||
|     esp_err_t ret = nvs_flash_init(); | ||||
|     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) | ||||
|     { | ||||
| @ -45,7 +32,7 @@ void app_main(void) | ||||
|     } | ||||
|     ESP_ERROR_CHECK(ret); | ||||
|  | ||||
|     connect_wifi(); //will return if successful | ||||
|     connect_wifi(); // will return if successful | ||||
|     setup_server(); | ||||
|  | ||||
|     sMetric aMetrics[METRIC_MAX_COUNT]; | ||||
| @ -53,129 +40,68 @@ void app_main(void) | ||||
|     /*Sensor Init*/ | ||||
|     ESP_ERROR_CHECK(i2cdev_init()); | ||||
|  | ||||
| #ifdef BMP280 | ||||
|     bmp280_params_t params; | ||||
|     bmp280_init_default_params(¶ms); | ||||
|     bmp280_t devBMP280; | ||||
|     memset(&devBMP280, 0, sizeof(bmp280_t)); | ||||
|     ESP_ERROR_CHECK(bmp280_init_desc(&devBMP280, BMP280_I2C_ADDRESS_0, 0, I2C_MASTER_SDA, I2C_MASTER_SCL)); | ||||
|     ESP_ERROR_CHECK(bmp280_init(&devBMP280, ¶ms)); | ||||
|     bool bme280p = devBMP280.id == BME280_CHIP_ID; | ||||
|     ESP_LOGI(TAG, "BMP280: found %s\n", bme280p ? "BME280" : "BMP280"); | ||||
| #endif | ||||
|  | ||||
| #ifdef AHT10 | ||||
|     aht_t devAHT10 = {0}; | ||||
|     devAHT10.mode = AHT_MODE_NORMAL; | ||||
|     devAHT10.type = AHT_TYPE; | ||||
|     ESP_ERROR_CHECK(aht_init_desc(&devAHT10, AHT_I2C_ADDRESS_GND, 0, I2C_MASTER_SDA, I2C_MASTER_SCL)); | ||||
|     ESP_ERROR_CHECK(aht_init(&devAHT10)); | ||||
| #endif | ||||
|  | ||||
| #ifdef VEML7700 | ||||
|     i2c_dev_t veml7700_device; | ||||
|     veml7700_config_t veml7700_configuration; | ||||
|      | ||||
|     memset(&veml7700_device, 0, sizeof(i2c_dev_t)); | ||||
|     memset(&veml7700_configuration, 0, sizeof(veml7700_config_t)); | ||||
|  | ||||
|     // initialize the device struct | ||||
|     ESP_ERROR_CHECK(veml7700_init_desc(&veml7700_device, I2C_NUM_0, I2C_MASTER_SDA, I2C_MASTER_SCL)); | ||||
|  | ||||
|     // check if the device is available | ||||
|     ESP_ERROR_CHECK(veml7700_probe(&veml7700_device)); | ||||
|  | ||||
|     /* set configuration parameters | ||||
|      * select gain 1/8 coastest resolution but the sensor will most likely not | ||||
|      * over-saturated | ||||
|      */ | ||||
|     veml7700_configuration.gain = VEML7700_GAIN_DIV_8; | ||||
|  | ||||
|     /* set the time to integrate the light values. more time will lead to a finer | ||||
|      * resolution but will be over-saturated earlier | ||||
|      */ | ||||
|     veml7700_configuration.integration_time = VEML7700_INTEGRATION_TIME_100MS; | ||||
|  | ||||
|     // interrupt is not used | ||||
|     veml7700_configuration.persistence_protect = VEML7700_PERSISTENCE_PROTECTION_4; | ||||
|     veml7700_configuration.interrupt_enable = 1; | ||||
|     veml7700_configuration.shutdown = 0; | ||||
|  | ||||
|     // write the configuration to the device | ||||
|     ESP_ERROR_CHECK(veml7700_set_config(&veml7700_device, &veml7700_configuration)); | ||||
|  | ||||
| #endif | ||||
|     initOutputs(); | ||||
|  | ||||
|     ESP_LOGI(TAG, "running"); | ||||
|  | ||||
|     while (1) | ||||
|     { | ||||
|         ESP_LOGI(TAG,"\n"); | ||||
|         vTaskDelay(pdMS_TO_TICKS(5000)); | ||||
|         uint16_t u16MetricCounter = 0U; | ||||
|  | ||||
|         /*Wifi RSSI*/ | ||||
|         wifi_ap_record_t ap; | ||||
|         esp_wifi_sta_get_ap_info(&ap); | ||||
|         printf("WiFi RSSI: %d\n", ap.rssi); | ||||
|         // printf("WiFi RSSI: %d\n", ap.rssi); | ||||
|         strcpy(aMetrics[u16MetricCounter].caMetricName, "wifi_rssi"); | ||||
|         aMetrics[0].fMetricValue = ap.rssi; | ||||
|         u16MetricCounter++; | ||||
|  | ||||
| #ifdef BMP280 | ||||
|         if ((bmp280_read_float(&devBMP280, &aMetrics[u16MetricCounter].fMetricValue, &aMetrics[u16MetricCounter+1].fMetricValue, 0) == ESP_OK)) | ||||
|         { | ||||
|             strcpy(aMetrics[u16MetricCounter].caMetricName, "bmp280_temperature"); | ||||
|             strcpy(aMetrics[u16MetricCounter+1].caMetricName, "bmp280_pressure"); | ||||
|             printf("(BMP280) Temperature: %.2f C, Pressure: %.2f Pa\n", aMetrics[u16MetricCounter].fMetricValue, aMetrics[u16MetricCounter+1].fMetricValue); | ||||
|             u16MetricCounter++; | ||||
|             u16MetricCounter++; | ||||
|         } else { | ||||
|             printf("(BMP280) Temperature/pressure reading failed\n"); | ||||
|         } | ||||
| #endif | ||||
|  | ||||
| #ifdef AHT10 | ||||
|         if ((aht_get_data(&devAHT10, &aMetrics[u16MetricCounter].fMetricValue, &aMetrics[u16MetricCounter+1].fMetricValue) == ESP_OK)) | ||||
|         { | ||||
|             strcpy(aMetrics[u16MetricCounter].caMetricName, "aht10_temperature"); | ||||
|             strcpy(aMetrics[u16MetricCounter+1].caMetricName, "aht10_humidity"); | ||||
|             printf("(AHT10) Temperature: %.2f C, Humidity: %.2f %% \n", aMetrics[u16MetricCounter].fMetricValue, aMetrics[u16MetricCounter+1].fMetricValue); | ||||
|             u16MetricCounter++; | ||||
|             u16MetricCounter++; | ||||
|         } else { | ||||
|             printf("(AHT10) Temperature/Humidity reading failed\n"); | ||||
|         } | ||||
| #endif | ||||
|  | ||||
| #ifdef DHT11 | ||||
|         if ((dht_read_float_data(SENSOR_TYPE, CONFIG_EXAMPLE_DATA_GPIO, &aMetrics[u16MetricCounter].fMetricValue, &aMetrics[u16MetricCounter+1].fMetricValue) == ESP_OK)) | ||||
|         { | ||||
|             strcpy(aMetrics[u16MetricCounter].caMetricName, "dht11_humidity"); | ||||
|             strcpy(aMetrics[u16MetricCounter+1].caMetricName, "dht11_temperature"); | ||||
|             printf("(DHT11) Humidity: %.2f %%, Temperature: %.2f C\n", aMetrics[u16MetricCounter].fMetricValue, aMetrics[u16MetricCounter+1].fMetricValue); | ||||
|             u16MetricCounter++; | ||||
|             u16MetricCounter++; | ||||
|         } else { | ||||
|             printf("(DHT11) Temperature/pressure reading failed\n"); | ||||
|         } | ||||
| #endif | ||||
|  | ||||
| #ifdef VEML7700 | ||||
|     uint32_t u32AmbientLight = 0U; | ||||
|     uint32_t u32WhiteChannel = 0U; | ||||
|     if ((veml7700_get_ambient_light(&veml7700_device, &veml7700_configuration, &u32AmbientLight) == ESP_OK) && (veml7700_get_white_channel(&veml7700_device, &veml7700_configuration, &u32WhiteChannel) == ESP_OK)) | ||||
|         { | ||||
|             aMetrics[u16MetricCounter].fMetricValue = u32AmbientLight; | ||||
|             aMetrics[u16MetricCounter+1].fMetricValue = u32WhiteChannel; | ||||
|             strcpy(aMetrics[u16MetricCounter].caMetricName, "veml7700_ambient_light"); | ||||
|             strcpy(aMetrics[u16MetricCounter+1].caMetricName, "veml7700_white_channel"); | ||||
|             printf("(VEML7700) Ambient light: %.2f lux, White channel: %.2f lux\n", aMetrics[u16MetricCounter].fMetricValue, aMetrics[u16MetricCounter+1].fMetricValue); | ||||
|             u16MetricCounter++; | ||||
|             u16MetricCounter++; | ||||
|         } else { | ||||
|             printf("(VEML7700)  light reading failed\n"); | ||||
|         } | ||||
| #endif | ||||
|         vSetMetrics(aMetrics, u16MetricCounter); | ||||
|  | ||||
|         setCirculationPumpState(ENABLED); | ||||
|         setBurnerState(ENABLED); | ||||
|  | ||||
|         if (getBurnerState() == ENABLED) | ||||
|         { | ||||
|             ESP_LOGI(TAG, "Burner ENABLED"); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             ESP_LOGI(TAG, "Burner DISABLED"); | ||||
|         } | ||||
|  | ||||
|         if (getCirculationPumpState() == ENABLED) | ||||
|         { | ||||
|             ESP_LOGI(TAG, "CirculationPump ENABLED"); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             ESP_LOGI(TAG, "CirculationPump DISABLED"); | ||||
|         } | ||||
|  | ||||
|         ESP_LOGI(TAG,"\n"); | ||||
|         vTaskDelay(pdMS_TO_TICKS(2000)); | ||||
|         setCirculationPumpState(DISABLED); | ||||
|         setBurnerState(DISABLED); | ||||
|  | ||||
|         if (getBurnerState() == ENABLED) | ||||
|         { | ||||
|             ESP_LOGI(TAG, "Burner ENABLED"); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             ESP_LOGI(TAG, "Burner DISABLED"); | ||||
|         } | ||||
|  | ||||
|         if (getCirculationPumpState() == ENABLED) | ||||
|         { | ||||
|             ESP_LOGI(TAG, "CirculationPump ENABLED"); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             ESP_LOGI(TAG, "CirculationPump DISABLED"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| } | ||||
| @ -0,0 +1,73 @@ | ||||
| #include "freertos/FreeRTOS.h" | ||||
| #include "freertos/task.h" | ||||
| #include "driver/gpio.h" | ||||
| #include "esp_log.h" | ||||
|  | ||||
| #include "outputs.h" | ||||
|  | ||||
| const uint8_t uCirculationPumpGpioPin = 27U; | ||||
| const uint8_t uBurnerGpioPin = 14U; | ||||
|  | ||||
| static eOutput sCirculationPumpState; | ||||
| static eOutput sBurnerState; | ||||
|  | ||||
| void initOutputs(void) | ||||
| { | ||||
|     gpio_config_t ioConfCirculationPump = { | ||||
|         .pin_bit_mask = (1ULL << uCirculationPumpGpioPin), // Pin mask | ||||
|         .mode = GPIO_MODE_OUTPUT,                          // Set as output | ||||
|         .pull_up_en = GPIO_PULLUP_DISABLE,                 // Disable pull-up | ||||
|         .pull_down_en = GPIO_PULLDOWN_DISABLE,             // Disable pull-down | ||||
|         .intr_type = GPIO_INTR_DISABLE                     // Disable interrupts | ||||
|     }; | ||||
|  | ||||
|     gpio_config_t ioConfBurner = { | ||||
|         .pin_bit_mask = (1ULL << uBurnerGpioPin), // Pin mask | ||||
|         .mode = GPIO_MODE_OUTPUT,                 // Set as output | ||||
|         .pull_up_en = GPIO_PULLUP_DISABLE,        // Disable pull-up | ||||
|         .pull_down_en = GPIO_PULLDOWN_DISABLE,    // Disable pull-down | ||||
|         .intr_type = GPIO_INTR_DISABLE            // Disable interrupts | ||||
|     }; | ||||
|     gpio_config(&ioConfCirculationPump); | ||||
|     gpio_config(&ioConfBurner); | ||||
| } | ||||
|  | ||||
| eOutput getCirculationPumpState(void) | ||||
| { | ||||
|     return sCirculationPumpState; | ||||
| } | ||||
|  | ||||
| void setCirculationPumpState(eOutput in) | ||||
| { | ||||
|     sCirculationPumpState = in; | ||||
|     switch (sCirculationPumpState) | ||||
|     { | ||||
|     case ENABLED: | ||||
|         gpio_set_level(uCirculationPumpGpioPin, 1U); // Switch on Circulation Pump | ||||
|         break; | ||||
|     case DISABLED: | ||||
|         gpio_set_level(uCirculationPumpGpioPin, 0U); // Switch off Circulation Pump | ||||
|     default: | ||||
|         break; | ||||
|     } | ||||
| } | ||||
|  | ||||
| eOutput getBurnerState(void) | ||||
| { | ||||
| return sBurnerState; | ||||
| } | ||||
|  | ||||
| void setBurnerState(eOutput in) | ||||
| { | ||||
|     sBurnerState = in; | ||||
|     switch (sBurnerState) | ||||
|     { | ||||
|     case ENABLED: | ||||
|         gpio_set_level(uBurnerGpioPin, 0U); // Switch on Burner | ||||
|         break; | ||||
|     case DISABLED: | ||||
|         gpio_set_level(uBurnerGpioPin, 1U); // Switch off Burner | ||||
|     default: | ||||
|         break; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										12
									
								
								main/outputs.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								main/outputs.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,12 @@ | ||||
| #pragma once | ||||
|  | ||||
| typedef enum _Output{ | ||||
|     ENABLED, | ||||
|     DISABLED | ||||
| } eOutput; | ||||
|  | ||||
| void initOutputs(void); | ||||
| eOutput getCirculationPumpState(void); | ||||
| void setCirculationPumpState(eOutput in); | ||||
| eOutput getBurnerState(void); | ||||
| void setBurnerState(eOutput in); | ||||
		Reference in New Issue
	
	Block a user