Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working Smart_Outlet example #3

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
23 changes: 23 additions & 0 deletions examples/smart_outlet/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions examples/smart_outlet/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@

#include <app_wifi.h>
#include <app_hap_setup_payload.h>
#include "outlet.h"

static const char *TAG = "HAP outlet";

#define SMART_OUTLET_TASK_PRIORITY 1
#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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

57 changes: 57 additions & 0 deletions examples/smart_outlet/main/outlet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* 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 <stdio.h>

#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;
}


43 changes: 43 additions & 0 deletions examples/smart_outlet/main/outlet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* 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_ */