diff --git a/examples/smart_outlet/main/Kconfig.projbuild b/examples/smart_outlet/main/Kconfig.projbuild index 6f3e3fe..d56ea1f 100644 --- a/examples/smart_outlet/main/Kconfig.projbuild +++ b/examples/smart_outlet/main/Kconfig.projbuild @@ -23,4 +23,27 @@ menu "Example Configuration" help Setup id to be used for HomeKot pairing, if hard-coded setup code is enabled. + config OUTLET_GPIO + int "Outlet GPIO number" + range 0 34 + default 33 + help + GPIO number (IOxx) to control relay pin. + + Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used. + + Default is GPIO 33 (Built in Led on ESP32CAM by AI Thinker) + + GPIOs 35-39 are input-only so cannot be used as outputs. + + config OUTLET_IN_USE_GPIO + int "Outlet in use GPIO number" + range 0 34 + default 0 + help + GPIO number (IOxx) to sense outlet in use. + + Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink. + + GPIOs 35-39 are input-only so cannot be used as outputs. endmenu diff --git a/examples/smart_outlet/main/app_main.c b/examples/smart_outlet/main/app_main.c index 782e3fa..4759b35 100644 --- a/examples/smart_outlet/main/app_main.c +++ b/examples/smart_outlet/main/app_main.c @@ -38,6 +38,7 @@ #include #include +#include "outlet.h" static const char *TAG = "HAP outlet"; @@ -45,10 +46,12 @@ static const char *TAG = "HAP outlet"; #define SMART_OUTLET_TASK_STACKSIZE 4 * 1024 #define SMART_OUTLET_TASK_NAME "hap_outlet" -#define OUTLET_IN_USE_GPIO GPIO_NUM_0 +#define OUTLET_IN_USE_GPIO CONFIG_OUTLET_IN_USE_GPIO #define ESP_INTR_FLAG_DEFAULT 0 +#define OUTLET_GPIO CONFIG_OUTLET_GPIO + static xQueueHandle s_esp_evt_queue = NULL; /** * @brief the recover outlet in use gpio interrupt function @@ -117,9 +120,10 @@ static int outlet_write(hap_write_data_t write_data[], int count, write = &write_data[i]; if (!strcmp(hap_char_get_type_uuid(write->hc), HAP_CHAR_UUID_ON)) { ESP_LOGI(TAG, "Received Write. Outlet %s", write->val.b ? "On" : "Off"); - /* TODO: Control Actual Hardware */ + if (outlet_set_on(write->val.b) == 0) { hap_char_update_val(write->hc, &(write->val)); *(write->status) = HAP_STATUS_SUCCESS; + } } else { *(write->status) = HAP_STATUS_RES_ABSENT; } @@ -237,5 +241,8 @@ void app_main() /* Create the application thread */ xTaskCreate(smart_outlet_thread_entry, SMART_OUTLET_TASK_NAME, SMART_OUTLET_TASK_STACKSIZE, NULL, SMART_OUTLET_TASK_PRIORITY, NULL); + gpio_pad_select_gpio(OUTLET_GPIO); + /* Set the GPIO as a push/pull output */ + gpio_set_direction(OUTLET_GPIO, GPIO_MODE_OUTPUT); } diff --git a/examples/smart_outlet/main/outlet.c b/examples/smart_outlet/main/outlet.c new file mode 100644 index 0000000..b958072 --- /dev/null +++ b/examples/smart_outlet/main/outlet.c @@ -0,0 +1,57 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2018 + * + * Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +/* HomeKit Outlet Example Hardware Implementation +*/ + +#include + +#include "driver/gpio.h" +#include "esp_log.h" + + +static bool s_on = false; + +static const char *TAG = "outlet"; +#define OUTLET_GPIO CONFIG_OUTLET_GPIO + + + +/** + * @brief turn on/off the outlet + */ +int outlet_set_on(bool value) +{ + ESP_LOGI(TAG, "outlet_set_on : %s", value == true ? "true" : "false"); + + if (value == true) { + gpio_set_level(OUTLET_GPIO, 0); + } else { + gpio_set_level(OUTLET_GPIO, 1); + } + + return 0; +} + + diff --git a/examples/smart_outlet/main/outlet.h b/examples/smart_outlet/main/outlet.h new file mode 100644 index 0000000..bced210 --- /dev/null +++ b/examples/smart_outlet/main/outlet.h @@ -0,0 +1,43 @@ +/* + * ESPRESSIF MIT License + * + * Copyright (c) 2018 + * + * Permission is hereby granted for use on ESPRESSIF SYSTEMS products only, in which case, + * it is free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +/* HomeKit Outlet Example +*/ + +#ifndef _OUTLET_H_ +#define _OUTLET_H_ + + + +/** + * @brief turn on/off the outlet + * + * @param value The "On" value + * + * @return none + */ +int outlet_set_on(bool value); + + +#endif /* _OUTLET_H_ */