From d74b06e72c62e8e9d737c87e4da20c2197829470 Mon Sep 17 00:00:00 2001 From: maccoylton Date: Thu, 7 Feb 2019 20:11:48 +0000 Subject: [PATCH] added led codes and sysparams --- components/esp-homekit | 2 +- {src => libs/button}/button.c | 0 {src => libs/button}/button.h | 0 libs/button/component.mk | 8 + libs/check_wifi/check_wifi.c | 43 ++++++ libs/check_wifi/check_wifi.h | 3 + libs/check_wifi/component.mk | 8 + libs/custom_characteristics/component.mk | 10 ++ .../custom_characteristics.c | 140 ++++++++++++++++++ .../custom_characteristics.h | 60 ++++++++ libs/http_post/component.mk | 8 + libs/http_post/http_post.c | 130 ++++++++++++++++ libs/http_post/http_post.h | 4 + libs/led_codes/component.mk | 8 + libs/led_codes/led_codes.c | 70 +++++++++ libs/led_codes/led_codes.h | 38 +++++ libs/ota/component.mk | 8 + {src => libs/ota}/ota-api.c | 0 {src => libs/ota}/ota-api.h | 0 src/Makefile | 6 +- src/wifi_thermostat.c | 123 +++++++++++++-- 21 files changed, 651 insertions(+), 18 deletions(-) rename {src => libs/button}/button.c (100%) rename {src => libs/button}/button.h (100%) create mode 100644 libs/button/component.mk create mode 100644 libs/check_wifi/check_wifi.c create mode 100644 libs/check_wifi/check_wifi.h create mode 100644 libs/check_wifi/component.mk create mode 100644 libs/custom_characteristics/component.mk create mode 100644 libs/custom_characteristics/custom_characteristics.c create mode 100644 libs/custom_characteristics/custom_characteristics.h create mode 100644 libs/http_post/component.mk create mode 100644 libs/http_post/http_post.c create mode 100644 libs/http_post/http_post.h create mode 100644 libs/led_codes/component.mk create mode 100644 libs/led_codes/led_codes.c create mode 100644 libs/led_codes/led_codes.h create mode 100644 libs/ota/component.mk rename {src => libs/ota}/ota-api.c (100%) rename {src => libs/ota}/ota-api.h (100%) diff --git a/components/esp-homekit b/components/esp-homekit index c2e67c4..09e2ed7 160000 --- a/components/esp-homekit +++ b/components/esp-homekit @@ -1 +1 @@ -Subproject commit c2e67c468890b376343bf867309b31390af0dad9 +Subproject commit 09e2ed75193eec63bf9c467a0c71bbc4b4eb4140 diff --git a/src/button.c b/libs/button/button.c similarity index 100% rename from src/button.c rename to libs/button/button.c diff --git a/src/button.h b/libs/button/button.h similarity index 100% rename from src/button.h rename to libs/button/button.h diff --git a/libs/button/component.mk b/libs/button/component.mk new file mode 100644 index 0000000..c2daabc --- /dev/null +++ b/libs/button/component.mk @@ -0,0 +1,8 @@ +# Component makefile for button + +INC_DIRS += $(button_ROOT) + +button_INC_DIR = $(button_ROOT) +button_SRC_DIR = $(button_ROOT) + +$(eval $(call component_compile_rules,button)) diff --git a/libs/check_wifi/check_wifi.c b/libs/check_wifi/check_wifi.c new file mode 100644 index 0000000..920cd5b --- /dev/null +++ b/libs/check_wifi/check_wifi.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "check_wifi.h" + +bool wifi_connected; + +void checkWifiTask(void *pvParameters) +{ + uint8_t status ; + + wifi_connected = false; + + while (1) + { + status = sdk_wifi_station_get_connect_status(); + switch (status) + { + case STATION_WRONG_PASSWORD: + printf("WiFi: wrong password\n\r"); + break; + case STATION_NO_AP_FOUND: + printf("WiFi: AP not found\n\r"); + break; + case STATION_CONNECT_FAIL: + printf("WiFi: connection failed\r\n"); + break; + case STATION_GOT_IP: + wifi_connected = true; + break; + default: + printf("%s: status = %d\n\r", __func__, status); + break; + } + vTaskDelay(CHECK_INTERVAL / portTICK_PERIOD_MS); + } +} diff --git a/libs/check_wifi/check_wifi.h b/libs/check_wifi/check_wifi.h new file mode 100644 index 0000000..18c4164 --- /dev/null +++ b/libs/check_wifi/check_wifi.h @@ -0,0 +1,3 @@ +#define CHECK_INTERVAL 36000000 + +void checkWifiTask(void *pvParameters); diff --git a/libs/check_wifi/component.mk b/libs/check_wifi/component.mk new file mode 100644 index 0000000..6c2303e --- /dev/null +++ b/libs/check_wifi/component.mk @@ -0,0 +1,8 @@ +# Component makefile for check_wifi + +INC_DIRS += $(check_wifi_ROOT) + +check_wifi_INC_DIR = $(check_wifi_ROOT) +check_wifi_SRC_DIR = $(check_wifi_ROOT) + +$(eval $(call component_compile_rules,check_wifi)) diff --git a/libs/custom_characteristics/component.mk b/libs/custom_characteristics/component.mk new file mode 100644 index 0000000..6f5cd62 --- /dev/null +++ b/libs/custom_characteristics/component.mk @@ -0,0 +1,10 @@ +# Component makefile for custom_characteristics + + +INC_DIRS += $(custom_characteristics_ROOT) + + +custom_characteristics_INC_DIR = $(custom_characteristics_ROOT) +custom_characteristics_SRC_DIR = $(custom_characteristics_ROOT) + +$(eval $(call component_compile_rules,custom_characteristics)) diff --git a/libs/custom_characteristics/custom_characteristics.c b/libs/custom_characteristics/custom_characteristics.c new file mode 100644 index 0000000..ccf063d --- /dev/null +++ b/libs/custom_characteristics/custom_characteristics.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include +#include + +void save_characteristic_to_flash(homekit_characteristic_t *ch, homekit_value_t value){ + + sysparam_status_t status = SYSPARAM_OK; + bool bool_value; + int8_t int8_value; + int32_t int32_value; + float float_value; + char *string_value=NULL; + + printf ("Save characteristic to flash\n"); + switch (ch->format) { + case homekit_format_bool: + printf ("writing bool value to flash\n"); + status = sysparam_get_bool(ch->description, &bool_value); + if (status == SYSPARAM_OK && bool_value != ch->value.bool_value) { + status = sysparam_set_bool(ch->description, ch->value.bool_value); + } else if (status == SYSPARAM_NOTFOUND) { + status = sysparam_set_bool(ch->description, ch->value.bool_value); + } + break; + case homekit_format_uint8: + printf ("writing int8 value to flash\n"); + status = sysparam_get_int8(ch->description, &int8_value); + if (status == SYSPARAM_OK && int8_value != ch->value.int_value) { + status = sysparam_set_int8(ch->description, ch->value.int_value); + } else if (status == SYSPARAM_NOTFOUND) { + status = sysparam_set_int8(ch->description, ch->value.int_value); + } + break; + case homekit_format_uint16: + case homekit_format_uint32: + printf ("writing int32 value to flash\n"); + status = sysparam_get_int32(ch->description, &int32_value); + if (status == SYSPARAM_OK && int32_value != ch->value.int_value) { + status = sysparam_set_int32(ch->description, ch->value.int_value); + } else if (status == SYSPARAM_NOTFOUND) { + status = sysparam_set_int32(ch->description, ch->value.int_value); + } + break; + case homekit_format_string: + printf ("writing string value to flash\n"); + status = sysparam_get_string(ch->description, &string_value); + if (status == SYSPARAM_OK && !strcmp (string_value, ch->value.string_value)) { + status = sysparam_set_string(ch->description, ch->value.string_value); + } else if (status == SYSPARAM_NOTFOUND) { + status = sysparam_set_string(ch->description, ch->value.string_value); + } + free(string_value); + break; + case homekit_format_float: + printf ("writing float value to flash\n"); + status = sysparam_get_int32(ch->description, &int32_value); + float_value = int32_value * 1.00f / 100; + if (status == SYSPARAM_OK && float_value != ch->value.float_value) { + int32_value = (int)ch->value.float_value*100; + status = sysparam_set_int32(ch->description, int32_value); + } else if (status == SYSPARAM_NOTFOUND) { + int32_value = (int)ch->value.float_value*100; + status = sysparam_set_int32(ch->description, int32_value); + } + break; + case homekit_format_uint64: + case homekit_format_int: + case homekit_format_tlv: + default: + printf ("Unknown characteristic format in save_charactersitics_to_flash\n"); + } + if (status != SYSPARAM_OK){ + printf ("Error in sysparams error:%i writing characteristic\n", status); + } + +} + +void load_characteristic_from_flash (homekit_characteristic_t *ch){ + + + sysparam_status_t status = SYSPARAM_OK; + bool bool_value; + int8_t int8_value; + int32_t int32_value; + char *string_value = NULL; + printf ("Loading sysparam %s\n",ch->description); + switch (ch->format){ + case homekit_format_bool: + printf("Loading bool\n"); + status = sysparam_get_bool(ch->description, &bool_value); + if (status == SYSPARAM_OK ) { + ch->value.bool_value = bool_value; + } + break; + case homekit_format_uint8: + printf("Loading int8\n"); + status = sysparam_get_int8(ch->description, &int8_value); + if (status == SYSPARAM_OK) { + ch->value.int_value = int8_value; + } + break; + case homekit_format_uint16: + case homekit_format_uint32: + printf("Loading in32\n"); + status = sysparam_get_int32(ch->description, &int32_value); + if (status == SYSPARAM_OK ) { + ch->value.int_value = int32_value; + } + break; + case homekit_format_string: + printf("Loading string\n"); + status = sysparam_get_string(ch->description, &string_value); + if (status == SYSPARAM_OK) { + ch->value = HOMEKIT_STRING(string_value); + } + break; + case homekit_format_float: + printf("Loading float\n"); + status = sysparam_get_int32(ch->description, &int32_value); + if (status == SYSPARAM_OK ) { + ch->value.float_value = int32_value * 1.0f /100; + } + break; + case homekit_format_uint64: + case homekit_format_int: + case homekit_format_tlv: + default: + printf ("Unknown characteristic format in save_charactersitics_to_flash\n"); + } + if (status != SYSPARAM_OK){ + printf ("Error in sysparams error:%i loading characteristic\n", status); + } + + + +} diff --git a/libs/custom_characteristics/custom_characteristics.h b/libs/custom_characteristics/custom_characteristics.h new file mode 100644 index 0000000..f129cc9 --- /dev/null +++ b/libs/custom_characteristics/custom_characteristics.h @@ -0,0 +1,60 @@ +/* + * HomeKit Custom Characteristics + * + * Copyright 2018 David B Brown (@maccoylton) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#ifndef __HOMEKIT_DBB_CUSTOM_CHARACTERISTICS__ +#define __HOMEKIT_DBB_CUSTOM_CHARACTERISTICS__ + +#define HOMEKIT_CUSTOM_UUID_DBB(value) (value"-4772-4466-80fd-a6ea3d5bcd55") + + +#define HOMEKIT_CHARACTERISTIC_CUSTOM_POLL_PERIOD HOMEKIT_CUSTOM_UUID_DBB("F0000001") +#define HOMEKIT_DECLARE_CHARACTERISTIC_CUSTOM_POLL_PERIOD(_value, ...) .type = HOMEKIT_CHARACTERISTIC_CUSTOM_POLL_PERIOD, \ +.description = "Poll Period", \ +.format = homekit_format_uint8, \ +.unit = homekit_unit_seconds, \ +.permissions = homekit_permissions_paired_read \ +| homekit_permissions_paired_write \ +| homekit_permissions_notify, \ +.min_value = (float[]) {1000}, \ +.max_value = (float[]) {10000}, \ +.min_step = (float[]) {1}, \ +.value = HOMEKIT_UINT8_(_value), \ +##__VA_ARGS__ + + +#define HOMEKIT_CHARACTERISTIC_CUSTOM_SQL_LOG HOMEKIT_CUSTOM_UUID_DBB("F0000002") +#define HOMEKIT_DECLARE_CHARACTERISTIC_CUSTOM_SQL_LOG(_value, ...) \ +.type = HOMEKIT_CHARACTERISTIC_CUSTOM_SQL_LOG, \ +.description = "SQL LOG", \ +.format = homekit_format_bool, \ +.permissions = homekit_permissions_paired_read \ +| homekit_permissions_paired_write \ +| homekit_permissions_notify, \ +.value = HOMEKIT_BOOL_(_value), \ +##__VA_ARGS__ + +#endif + + + +void save_characteristic_to_flash (homekit_characteristic_t *ch, homekit_value_t value); + +void load_characteristic_from_flash (homekit_characteristic_t *ch); diff --git a/libs/http_post/component.mk b/libs/http_post/component.mk new file mode 100644 index 0000000..d73d86d --- /dev/null +++ b/libs/http_post/component.mk @@ -0,0 +1,8 @@ +# Component makefile for http_post + +INC_DIRS += $(http_post_ROOT) + +http_post_INC_DIR = $(http_post_ROOT) +http_post_SRC_DIR = $(http_post_ROOT) + +$(eval $(call component_compile_rules,http_post)) diff --git a/libs/http_post/http_post.c b/libs/http_post/http_post.c new file mode 100644 index 0000000..8555ffb --- /dev/null +++ b/libs/http_post/http_post.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#include + +#include +#include +#include +#include +#include +#include + + +#define WEB_SERVER "192.168.1.2" +#define WEB_PORT "80" +#define WEB_URL "/esplogger/postsql.php" +char request[450]; +char details[150]; +float temperature; +float humidity; +float moisture; + +void http_post_task(void * pvParameters) +{ + int successes = 0, failures = 0; + printf("HTTP get task starting...\r\n"); + + while(1) { +// sdk_wifi_set_sleep_type(WIFI_SLEEP_NONE); + + printf ("Suspending http_post_task\n"); + vTaskSuspend( NULL ); + + printf ("Resumed http_post_task\n"); + + const struct addrinfo hints = { + .ai_family = AF_INET, + .ai_socktype = SOCK_STREAM, + }; + struct addrinfo *res; + + printf("Running DNS lookup for %s...\r\n", WEB_SERVER); + int err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res); + + if(err != 0 || res == NULL) { + printf("DNS lookup failed err=%d res=%p\r\n", err, res); + if(res) + freeaddrinfo(res); + vTaskDelay(1000 / portTICK_PERIOD_MS); + failures++; + continue; + } + /* Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */ + struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; + printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(*addr)); + + int s = lwip_socket(res->ai_family, res->ai_socktype, 0); + if(s < 0) { + printf("... Failed to allocate socket\r\n"); + freeaddrinfo(res); + vTaskDelay(1000 / portTICK_PERIOD_MS); + failures++; + continue; + } + + printf("... allocated socket: %d \r\n", s); + + if(lwip_connect(s, res->ai_addr, res->ai_addrlen) != 0) { + lwip_close(s); + freeaddrinfo(res); + printf("... socket connect failed.\r\n"); + vTaskDelay(4000 / portTICK_PERIOD_MS); + failures++; + continue; + } + + printf("... connected\r\n"); + freeaddrinfo(res); + request[0] = "\0"; +// snprintf(details, 80, "{\"temp\": %.3f, \"hum\": %.3f, \"mois\": %.3f}\r\n", temperature, humidity, moisture); + + snprintf(details, 150, post_string); + snprintf(request, 450, "POST %s HTTP/1.0\r\n" + "Host: %s\r\n" +// "User-Agent: esp-open-rtos/0.1 esp8266\r\n" + "Content-type: application/x-www-form-urlencoded\r\n" + "Content-Length: %d\r\n\r\n" + "%s", WEB_URL, WEB_SERVER, strlen(details), details); + printf(request); + if (lwip_write(s, request, strlen(request)) < 0) { + printf("... socket send failed\r\n"); + lwip_close(s); + vTaskDelay(4000 / portTICK_PERIOD_MS); + failures++; + continue; + } + printf("... socket send success\r\n"); + + static char recv_buf[200]; + int r; + do { + printf("receiving..."); + bzero(recv_buf, 200); + r = lwip_read(s, recv_buf, 199); + if(r > 0) { + printf("%s", recv_buf); + } + } while(r > 0); + + printf("... done reading from socket. Last read return=%d errno=%d\r\n", r, errno); + if(r != 0) + failures++; + else + successes++; + lwip_close(s); + printf("successes = %d failures = %d\r\n", successes, failures); +// sdk_wifi_set_sleep_type(WIFI_SLEEP_LIGHT); + vTaskDelay(10000 / portTICK_PERIOD_MS); + printf("\r\nStarting again!\r\n"); + } +} + diff --git a/libs/http_post/http_post.h b/libs/http_post/http_post.h new file mode 100644 index 0000000..5a5d9df --- /dev/null +++ b/libs/http_post/http_post.h @@ -0,0 +1,4 @@ +void http_post_task (void * pvParameters ); + + +char post_string[150]; diff --git a/libs/led_codes/component.mk b/libs/led_codes/component.mk new file mode 100644 index 0000000..b39adc9 --- /dev/null +++ b/libs/led_codes/component.mk @@ -0,0 +1,8 @@ +# Component makefile for led_codes + +INC_DIRS += $(led_codes_ROOT) + +led_codes_INC_DIR = $(led_codes_ROOT) +led_codes_SRC_DIR = $(led_codes_ROOT) + +$(eval $(call component_compile_rules,led_codes)) diff --git a/libs/led_codes/led_codes.c b/libs/led_codes/led_codes.c new file mode 100644 index 0000000..da3b6a8 --- /dev/null +++ b/libs/led_codes/led_codes.c @@ -0,0 +1,70 @@ +/* + * LED Codes + * + * Copyright 2018 José A. Jiménez (@RavenSystem) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "led_codes.h" + +#define DURATION_OFF 150 + +typedef struct led_params_t { + blinking_params_t blinking_params; + uint8_t gpio; + bool status; + ETSTimer timer; + uint8_t count; + uint32_t delay; +} led_params_t; + +led_params_t led_params; + +void led_code_run() { + led_params.status = !led_params.status; + + gpio_write(led_params.gpio, led_params.status); + + if (led_params.status == 0) { + led_params.delay = (led_params.blinking_params.duration * 1000) + 90; + } else { + led_params.delay = DURATION_OFF; + led_params.count++; + } + + sdk_os_timer_disarm(&led_params.timer); + + if (led_params.count < led_params.blinking_params.times) { + sdk_os_timer_arm(&led_params.timer, led_params.delay, 0); + } +} + +void led_code(uint8_t gpio, blinking_params_t blinking_params) { + led_params.gpio = gpio; + led_params.blinking_params = blinking_params; + + sdk_os_timer_disarm(&led_params.timer); + sdk_os_timer_setfn(&led_params.timer, led_code_run, NULL); + + led_params.status = 1; + led_params.count = 0; + + led_code_run(); +} diff --git a/libs/led_codes/led_codes.h b/libs/led_codes/led_codes.h new file mode 100644 index 0000000..b054e13 --- /dev/null +++ b/libs/led_codes/led_codes.h @@ -0,0 +1,38 @@ +/* + * LED Codes + * + * Copyright 2018 José A. Jiménez (@RavenSystem) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +typedef struct blinking_params_t { + uint8_t times; + uint8_t duration; +} blinking_params_t; + +#define GENERIC_ERROR (blinking_params_t){6,0} +#define SENSOR_ERROR (blinking_params_t){5,0} +#define WIFI_CONNECTED (blinking_params_t){1,2} +#define IDENTIFY_ACCESSORY (blinking_params_t){1,3} +#define RESTART_DEVICE (blinking_params_t){2,2} +#define WIFI_CONFIG_RESET (blinking_params_t){2,0} +#define EXTRA_CONFIG_RESET (blinking_params_t){2,1} +#define FUNCTION_A (blinking_params_t){1,0} +#define FUNCTION_B (blinking_params_t){2,0} +#define FUNCTION_C (blinking_params_t){3,0} +#define FUNCTION_D (blinking_params_t){4,0} + +void led_code(uint8_t gpio, blinking_params_t blinking_params); diff --git a/libs/ota/component.mk b/libs/ota/component.mk new file mode 100644 index 0000000..861596a --- /dev/null +++ b/libs/ota/component.mk @@ -0,0 +1,8 @@ +# Component makefile for ota + +INC_DIRS += $(ota_ROOT) + +ota_INC_DIR = $(ota_ROOT) +ota_SRC_DIR = $(ota_ROOT) + +$(eval $(call component_compile_rules,ota)) diff --git a/src/ota-api.c b/libs/ota/ota-api.c similarity index 100% rename from src/ota-api.c rename to libs/ota/ota-api.c diff --git a/src/ota-api.h b/libs/ota/ota-api.h similarity index 100% rename from src/ota-api.h rename to libs/ota/ota-api.h diff --git a/src/Makefile b/src/Makefile index 10e4745..86f0ad1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,7 +9,11 @@ EXTRA_COMPONENTS = \ extras/i2c \ $(abspath ../components/esp-wolfssl) \ $(abspath ../components/esp-cjson) \ - $(abspath ../components/esp-homekit) + $(abspath ../components/esp-homekit)\ + $(abspath ../libs/button)\ + $(abspath ../libs/led_codes)\ + $(abspath ../libs/ota)\ + $(abspath ../libs/custom_characteristics) FONTS_TERMINUS_BOLD_8X14_ISO8859_1 = 1 FONTS_TERMINUS_BOLD_14X28_ISO8859_1 = 1 diff --git a/src/wifi_thermostat.c b/src/wifi_thermostat.c index b4f7e7b..43ae179 100644 --- a/src/wifi_thermostat.c +++ b/src/wifi_thermostat.c @@ -36,6 +36,7 @@ #include #include #include +#include #include @@ -46,12 +47,17 @@ #include #include "wifi_thermostat.h" -#include "button.h" +#include +#include +#include #define TEMPERATURE_SENSOR_PIN 4 #define TEMPERATURE_POLL_PERIOD 10000 #define BUTTON_UP_GPIO 12 #define BUTTON_DOWN_GPIO 13 +#define BUTTON_RESET 0 +#define LED_GPIO 2 + // 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) @@ -114,7 +120,7 @@ static uint8_t buffer[DISPLAY_WIDTH * DISPLAY_HEIGHT / 8]; -#include "ota-api.h" +#include 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); @@ -122,9 +128,16 @@ homekit_characteristic_t serial = HOMEKIT_CHARACTERISTIC_(SERIAL_NUMBER, D homekit_characteristic_t model = HOMEKIT_CHARACTERISTIC_(MODEL, DEVICE_MODEL); homekit_characteristic_t revision = HOMEKIT_CHARACTERISTIC_(FIRMWARE_REVISION, FW_VERSION); +void identify_task(void *_args) { + + led_code(LED_GPIO, IDENTIFY_ACCESSORY); + + vTaskDelete(NULL); +} void thermostat_identify(homekit_value_t _value) { printf("Thermostat identify\n"); + xTaskCreate(identify_task, "identify", 128, NULL, 2, NULL); } @@ -244,6 +257,7 @@ static void ssd1306_task(void *pvParameters) for (;;) { vTaskDelay(2 * SECOND); printf("%s: error loop\n", __FUNCTION__); + led_code(LED_GPIO, FUNCTION_A); } } @@ -364,26 +378,13 @@ void temperature_sensor_task(void *_args) { } else { printf("Couldnt read data from sensor\n"); + led_code(LED_GPIO, SENSOR_ERROR); } homekit_characteristic_notify(¤t_state, current_state.value); vTaskDelay(TEMPERATURE_POLL_PERIOD / portTICK_PERIOD_MS); } } -void thermostat_init() { - - xTaskCreate(temperature_sensor_task, "Thermostat", 256, NULL, 2, NULL); - gpio_enable(BUTTON_UP_GPIO, GPIO_INPUT); - gpio_enable(BUTTON_DOWN_GPIO, GPIO_INPUT); - if (button_create(BUTTON_UP_GPIO, 0, 4000, button_up_callback)) { - printf("Failed to initialize button Up\n"); - } - - if (button_create(BUTTON_DOWN_GPIO, 0, 4000, button_down_callback)) { - printf("Failed to initialize button down\n"); - } -} - homekit_accessory_t *accessories[] = { HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_thermostat, .services=(homekit_service_t*[]) { @@ -414,7 +415,69 @@ homekit_accessory_t *accessories[] = { NULL }; +void reset_configuration_task() { + + 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"); + + sdk_system_restart(); + + vTaskDelete(NULL); +} + +void reset_configuration() { + printf("Resetting Sonoff configuration\n"); + xTaskCreate(reset_configuration_task, "Reset configuration", 256, NULL, 2, NULL); +} + + +void reset_button_callback(uint8_t gpio, button_event_t event) { + switch (event) { + case button_event_single_press: + printf("Button event: %d, doing nothin\n", event); + break; + case button_event_long_press: + printf("Button event: %d, resetting homekit config\n", event); + reset_configuration(); + break; + default: + printf("Unknown button event: %d\n", event); + } +} + +void thermostat_init() { + + xTaskCreate(temperature_sensor_task, "Thermostat", 256, NULL, 2, NULL); + gpio_enable(BUTTON_UP_GPIO, GPIO_INPUT); + gpio_enable(BUTTON_DOWN_GPIO, GPIO_INPUT); + gpio_enable(BUTTON_RESET, GPIO_INPUT); + gpio_enable(LED_GPIO, GPIO_OUTPUT); + if (button_create(BUTTON_UP_GPIO, 0, 4000, button_up_callback)) { + printf("Failed to initialize button Up\n"); + } + + if (button_create(BUTTON_DOWN_GPIO, 0, 4000, button_down_callback)) { + printf("Failed to initialize button down\n"); + } + if (button_create(BUTTON_RESET, 0, 4000, reset_button_callback)) { + printf("Failed to initialize button\n"); + } + +} void create_accessory_name() { @@ -443,6 +506,30 @@ void create_accessory_name() { serial.value = name.value; } +void get_sysparam_info() { + uint32_t base_addr,num_sectors; + sysparam_iter_t sysparam_iter; + sysparam_status_t sysparam_status; + + sysparam_get_info(&base_addr, &num_sectors); + + printf ("get_sysparam_info - Sysparam base address %i, num_sectors %i\n", base_addr, num_sectors); + sysparam_status = sysparam_iter_start (&sysparam_iter); + while (sysparam_status==0){ + sysparam_status = sysparam_iter_next (&sysparam_iter); + if (sysparam_status==0){ + printf("get_sysparam_info - sysparam name: %s\n", sysparam_iter.key); + } + } + sysparam_iter_end (&sysparam_iter); +} + +void load_settings_from_flash (){ + + printf("load_settings_from_flash - load setting from flash\n"); + load_characteristic_from_flash (&target_state); + +} homekit_server_config_t config = { .accessories = accessories, @@ -453,6 +540,10 @@ void user_init(void) { uart_set_baud(0, 115200); // wifi_init(); + + get_sysparam_info (); + load_settings_from_flash (); + create_accessory_name(); thermostat_init();