Skip to content

Commit

Permalink
moved to used shared_functions and add wifi_reset characteristic
Browse files Browse the repository at this point in the history
  • Loading branch information
maccoylton committed Nov 17, 2019
1 parent 5d205ff commit 255903e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 111 deletions.
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@
[submodule "components/esp-homekit-common-functions"]
path = components/esp-homekit-common-functions
url = https://github.com/maccoylton/esp-homekit-common-functions.git
[submodule "components/UDPlogger"]
path = components/UDPlogger
url = https://github.com/HomeACcessoryKid/UDPlogger.git
[submodule "components/esp-wifi-config"]
path = components/esp-wifi-config
url = https://github.com/maximkulkin/esp-wifi-config
[submodule "components/esp-adv-button"]
path = components/esp-adv-button
url = https://github.com/RavenSystem/esp-adv-button.git
25 changes: 22 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@ EXTRA_COMPONENTS = \
extras/dht \
extras/http-parser \
extras/rboot-ota \
extras/dhcpserver \
$(abspath ../components/esp-wolfssl) \
$(abspath ../components/esp-cjson) \
$(abspath ../components/esp-homekit)\
$(abspath ../components/esp-homekit-common-functions/button)\
$(abspath ../components/esp-wifi-config)\
$(abspath ../components/UDPlogger)\
$(abspath ../components/esp-adv-button)\
$(abspath ../components/esp-homekit-common-functions/led_codes)\
$(abspath ../components/esp-homekit-common-functions/ota)
$(abspath ../components/esp-homekit-common-functions/ota)\
$(abspath ../components/esp-homekit-common-functions/custom_characteristics)\
$(abspath ../components/esp-homekit-common-functions/shared_functions)



FLASH_SIZE ?= 8
FLASH_MODE ?= dout
FLASH_SPEED ?= 40

HOMEKIT_MAX_CLIENTS = 16
HOMEKIT_SMALL = 0

HOMEKIT_SPI_FLASH_BASE_ADDR ?= 0x8c000

EXTRA_CFLAGS += -I../.. -DHOMEKIT_SHORT_APPLE_UUIDS -DHOMEKIT_DEBUG
EXTRA_CFLAGS += -I../.. -DHOMEKIT_SHORT_APPLE_UUIDS
EXTRA_CFLAGS += -DHOMEKIT_DEBUG
EXTRA_CFLAGS += -DHOMEKIT_OVERCLOCK_PAIR_VERIFY
EXTRA_CFLAGS += -DHOMEKIT_OVERCLOCK_PAIR_SETUP
EXTRA_CFLAGS += -DUDPLOG_PRINTF_TO_UDP
EXTRA_CFLAGS += -DUDPLOG_PRINTF_ALSO_SERIAL


include $(SDK_PATH)/common.mk

Expand Down
150 changes: 42 additions & 108 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,32 @@

#include <homekit/homekit.h>
#include <homekit/characteristics.h>
//#include <wifi_config.h>

#include <button.h>
#include <adv_button.h>
#include <led_codes.h>
#include <udplogger.h>
#include <custom_characteristics.h>
#include <shared_functions.h>

// add this section to make your device OTA capable
// create the extra characteristic &ota_trigger, at the end of the primary service (before the NULL)
// it can be used in Eve, which will show it, where Home does not
// and apply the four other parameters in the accessories_information section

#include <ota-api.h>

void switch_on_callback(homekit_characteristic_t *_ch, homekit_value_t on, void *context);

homekit_characteristic_t wifi_reset = HOMEKIT_CHARACTERISTIC_(CUSTOM_WIFI_RESET, false, .setter=wifi_reset_set);
homekit_characteristic_t ota_trigger = API_OTA_TRIGGER;
homekit_characteristic_t name = HOMEKIT_CHARACTERISTIC_(NAME, DEVICE_NAME);
homekit_characteristic_t manufacturer = HOMEKIT_CHARACTERISTIC_(MANUFACTURER, DEVICE_MANUFACTURER);
homekit_characteristic_t serial = HOMEKIT_CHARACTERISTIC_(SERIAL_NUMBER, DEVICE_SERIAL);
homekit_characteristic_t model = HOMEKIT_CHARACTERISTIC_(MODEL, DEVICE_MODEL);
homekit_characteristic_t revision = HOMEKIT_CHARACTERISTIC_(FIRMWARE_REVISION, FW_VERSION);
homekit_characteristic_t switch_on = HOMEKIT_CHARACTERISTIC_(
ON, false, .callback=HOMEKIT_CHARACTERISTIC_CALLBACK(switch_on_callback)
);


// The GPIO pin that is connected to the relay and the blue LED on the Sonoff S20.
Expand All @@ -62,87 +71,39 @@ const int LED_GPIO = 13;
// The GPIO pin that is oconnected to the button on the Sonoff S20.
const int button_gpio = 0;

void switch_on_callback(homekit_characteristic_t *_ch, homekit_value_t on, void *context);
void button_callback(uint8_t gpio, button_event_t event);
int led_off_value=1; /* global varibale to support LEDs set to 0 where the LED is connected to GND, 1 where +3.3v */
const int status_led_gpio = 13; /*set the gloabl variable for the led to be sued for showing status */

void relay_write(bool on) {
gpio_write(relay_gpio, on ? 1 : 0);
}

void led_write(bool on) {
gpio_write(LED_GPIO, on ? 0 : 1);
}

void reset_configuration_task() {
//Flash the LED first before we start the reset
led_code (LED_GPIO, WIFI_CONFIG_RESET);

printf("Resetting Wifi Config\n");

// wifi_config_reset();

vTaskDelay(1000 / portTICK_PERIOD_MS);

printf("Resetting HomeKit Config\n");

homekit_server_reset();

vTaskDelay(1000 / portTICK_PERIOD_MS);

printf("Restarting\n");
void button_single_press_callback(uint8_t gpio, void* args, const uint8_t param) {

sdk_system_restart();
printf("button_single_press_callback:Toggling relay\n");
switch_on.value.bool_value = !switch_on.value.bool_value;
relay_write(switch_on.value.bool_value, gpio);

vTaskDelete(NULL);
}

void reset_configuration() {
printf("Resetting Sonoff configuration\n");
xTaskCreate(reset_configuration_task, "Reset configuration", 256, NULL, 2, NULL);
}

homekit_characteristic_t switch_on = HOMEKIT_CHARACTERISTIC_(
ON, false, .callback=HOMEKIT_CHARACTERISTIC_CALLBACK(switch_on_callback)
);

void gpio_init() {

adv_button_set_evaluate_delay(10);
printf("Initialising buttons\n");
adv_button_create(button_gpio, true, false);
adv_button_register_callback_fn(button_gpio, button_single_press_callback, SINGLEPRESS_TYPE, NULL, 0);
adv_button_register_callback_fn(button_gpio, reset_button_callback, VERYLONGPRESS_TYPE, NULL, 0);

gpio_enable(LED_GPIO, GPIO_OUTPUT);
led_write(false);
led_write(false, LED_GPIO);
gpio_enable(relay_gpio, GPIO_OUTPUT);
relay_write(switch_on.value.bool_value);
relay_write(switch_on.value.bool_value, relay_gpio);
}

void switch_on_callback(homekit_characteristic_t *_ch, homekit_value_t on, void *context) {
relay_write(switch_on.value.bool_value);
}

void button_callback(uint8_t gpio, button_event_t event) {
switch (event) {
case button_event_single_press:
printf("Toggling relay\n");
switch_on.value.bool_value = !switch_on.value.bool_value;
relay_write(switch_on.value.bool_value);
homekit_characteristic_notify(&switch_on, switch_on.value);
break;
case button_event_long_press:
reset_configuration();
break;
default:
printf("Unknown button event: %d\n", event);
}
}

void switch_identify_task(void *_args) {
// We identify the Sonoff by Flashing it's LED.

led_code( LED_GPIO, IDENTIFY_ACCESSORY);
vTaskDelete(NULL);
printf("switch_on_callback: Homekit etting Relay to %s\n", switch_on.value.bool_value ? "true" : "false");
relay_write(switch_on.value.bool_value, relay_gpio);
}

void switch_identify(homekit_value_t _value) {
printf("Switch identify\n");
xTaskCreate(switch_identify_task, "Switch identify", 128, NULL, 2, NULL);
}


homekit_accessory_t *accessories[] = {
Expand All @@ -153,67 +114,40 @@ homekit_accessory_t *accessories[] = {
&serial,
&model,
&revision,
HOMEKIT_CHARACTERISTIC(IDENTIFY, switch_identify),
HOMEKIT_CHARACTERISTIC(IDENTIFY, identify),
NULL
}),
HOMEKIT_SERVICE(SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]){
HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
&switch_on,
&ota_trigger,
&wifi_reset,
NULL
}),
NULL
}),
NULL
};

void accessory_init (void ){
/* initalise anything you don't want started until wifi and pairing is confirmed */

}


homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111"
};

void create_accessory_name() {

int serialLength = snprintf(NULL, 0, "%d", sdk_system_get_chip_id());

char *serialNumberValue = malloc(serialLength + 1);

snprintf(serialNumberValue, serialLength + 1, "%d", sdk_system_get_chip_id());

int name_len = snprintf(NULL, 0, "%s-%s-%s",
DEVICE_NAME,
DEVICE_MODEL,
serialNumberValue);

if (name_len > 63) {
name_len = 63;
}

char *name_value = malloc(name_len + 1);

snprintf(name_value, name_len + 1, "%s-%s-%s",
DEVICE_NAME, DEVICE_MODEL, serialNumberValue);


name.value = HOMEKIT_STRING(name_value);
serial.value = name.value;
}

void user_init(void) {
uart_set_baud(0, 115200);

gpio_init();

create_accessory_name();

if (button_create(button_gpio, 0, 4000, button_callback)) {
printf("Failed to initialize button\n");
}

int c_hash=ota_read_sysparam(&manufacturer.value.string_value,&serial.value.string_value,
&model.value.string_value,&revision.value.string_value);
if (c_hash==0) c_hash=1;
config.accessories[0]->config_number=c_hash;
standard_init (&name, &manufacturer, &model, &serial, &revision);

gpio_init();

wifi_config_init("SonoffMini", NULL, on_wifi_ready);

homekit_server_init(&config);

}

0 comments on commit 255903e

Please sign in to comment.