Skip to content

Commit

Permalink
added BME and LDR supports to Apple home kit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurik72 committed Jan 31, 2020
1 parent c47fe43 commit feb0e80
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 11 deletions.
12 changes: 8 additions & 4 deletions HomeController/BME280Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,12 @@ void BME280Controller::setup_hap_service(){

}
else{
this->hapservice_temp=hap_add_temperature_service(this->get_name());
this->hapservice_hum=hap_add_humidity_service(this->get_name());
String tempName = this->get_name();
tempName += "temp";
String humName= this->get_name();
humName += "hum";
this->hapservice_temp = hap_add_temperature_service(tempName.c_str());
this->hapservice_hum=hap_add_humidity_service(humName.c_str());
}
if(this->hapservice_temp)
this->hap_temp=homekit_service_characteristic_by_type(this->hapservice_temp, HOMEKIT_CHARACTERISTIC_CURRENT_TEMPERATURE);
Expand All @@ -408,14 +412,14 @@ void BME280Controller::setup_hap_service(){

}
void BME280Controller::notify_hap(){
if(this->ishap && this->hapservice_temp && this->hap_temp){
if(this->ishap && this->hapservice_temp && this->hap_temp && this->isinit){
BMEState newState=this->get_state();
if(this->hap_temp->value.float_value!=newState.temp){
this->hap_temp->value.float_value=newState.temp;
homekit_characteristic_notify(this->hap_temp,this->hap_temp->value);
}
}
if(this->ishap && this->hapservice_hum && this->hap_hum){
if(this->ishap && this->hapservice_hum && this->hap_hum && this->isinit){
BMEState newState=this->get_state();
if(this->hap_hum->value.float_value!=newState.hum){
this->hap_hum->value.float_value=newState.hum;
Expand Down
10 changes: 10 additions & 0 deletions HomeController/BaseController.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class CBaseController
void report_monitor_on(uint channel);
void report_monitor_off(uint channel);
void report_monitor_shortblink(uint channel);

#if !defined ASYNC_WEBSERVER
#if defined(ESP8266)
virtual void setuphandlers(ESP8266WebServer& server);
Expand Down Expand Up @@ -449,4 +450,13 @@ class CManualStateController : public CController<T, P, M>
};


#ifdef ENABLE_NATIVE_HAP

#define HAP_NOTIFY_CHANGES(name,home_characteristic,val,tollerance) \
if (home_characteristic && abs(home_characteristic->value.name ##_value - val)>tollerance){ \
home_characteristic->value.name ##_value = val ;\
homekit_characteristic_notify(home_characteristic, home_characteristic->value); \
};
#endif

#endif
2 changes: 1 addition & 1 deletion HomeController/Controllers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Controllers::setup() {
void Controllers::loadconfig() {
String filename = "/services.json";
//int capacity = JSON_ARRAY_SIZE(5) + 5 * JSON_OBJECT_SIZE(70);
int capacity = JSON_ARRAY_SIZE(8) + 2 * JSON_OBJECT_SIZE(40) + 262;
int capacity = JSON_ARRAY_SIZE(40) + 2 * JSON_OBJECT_SIZE(80) + 262;
if (SPIFFS.exists(filename)) {
//file exists, reading and loading
DBG_OUTPUT_PORT.println("Read services configuration: ");
Expand Down
5 changes: 3 additions & 2 deletions HomeController/HomeController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ WebServer server(80);
startwifimanager();
#endif
//setup mdns
#ifndef ENABLE_NATIVE_HAP
DBG_OUTPUT_PORT.print("Starting MDNS host:");
DBG_OUTPUT_PORT.println(HOSTNAME);
if (!isAPMode) {
Expand All @@ -175,9 +176,9 @@ WebServer server(80);
}

}
#endif


#if !defined(ESP8266_)
#if !defined(ESP8266)
const String FILES[] = { "/index.html", "/js/bundle.min.js.gz","/filebrowse.html" };//"/filebrowse.html"
if (!isAPMode){
for(int i=0;i<sizeof(FILES)/sizeof(*FILES);i++)
Expand Down
105 changes: 104 additions & 1 deletion HomeController/LDRController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ LDRController::LDRController() {
this->pin = 0;
this->cvalmin = 0.0;
this->cvalmax = MAX_LDRVAL;
#ifdef ENABLE_NATIVE_HAP
this->ishap = true;
this->hapservice = NULL;
this->hap_level = NULL;
this->hapservice_type = "light";
//this->accessory_type = 0;
#endif

}
String LDRController::serializestate() {

Expand Down Expand Up @@ -70,6 +78,9 @@ void LDRController::loadconfig(JsonObject& json) {
cvalmin= json["cvalmin"].as<float>();
cvalmax = json["cvalmax"].as<float>();
cfmt = json["cfmt"].as<String>();
loadif(hapservice_type, json, "haptype");


#ifdef LDRCONTROLLER_DEBUG
DBG_OUTPUT_PORT.println("LDRController load config");
#endif
Expand All @@ -81,6 +92,7 @@ void LDRController::getdefaultconfig(JsonObject& json) {
json["cvalmin"]= cvalmin;
json["cvalmax"]= cvalmax;
json["cfmt"] = cfmt;
json["haptype"] = hapservice_type;
LDR::getdefaultconfig(json);
}
void LDRController::setup() {
Expand Down Expand Up @@ -124,4 +136,95 @@ bool LDRController::onpublishmqtt(String& endkey, String& payload) {
endkey = szStatusText;
payload = String(this->get_state().ldrValue);
return true;
}
}

#ifdef ENABLE_NATIVE_HAP
void LDRController::setup_hap_service() {


DBG_OUTPUT_PORT.println("LDRController::setup_hap_service()");
if (!ishap) {

DBG_OUTPUT_PORT.println("LDRController::NO Hap support");
return;
}
DBG_OUTPUT_PORT.println(this->hapservice_type);
// DBG_OUTPUT_PORT.println("LDRController ishap");
//DBG_OUTPUT_PORT.println(hapservice_type);
if (this->hapservice_type == "light") {
DBG_OUTPUT_PORT.println("LDRController light");
if (this->accessory_type > 1) {
DBG_OUTPUT_PORT.println("LDRController accessory_type > 1");
this->hapservice = hap_add_light_service_as_accessory(this->accessory_type, this->get_name(), LDRController::hap_callback, this);
}
else
{
this->hapservice = hap_add_light_service(this->get_name(), LDRController::hap_callback, this);
}
this->hap_level= homekit_service_characteristic_by_type(this->hapservice, HOMEKIT_CHARACTERISTIC_CURRENT_AMBIENT_LIGHT_LEVEL);
}
if (this->hapservice_type == "bat") {
DBG_OUTPUT_PORT.println("LDRController bat");
this->hapservice = hap_add_battery_service(this->get_name(), LDRController::hap_callback, this);
this->hap_level = homekit_service_characteristic_by_type(this->hapservice, HOMEKIT_CHARACTERISTIC_BATTERY_LEVEL);
}

}
void LDRController::notify_hap() {
if (this->ishap && this->hapservice) {
//DBG_OUTPUT_PORT.println("LDRController::notify_hap");
LDRState lstate = this->get_state();
if (this->hapservice_type == "light" && this->hap_level) {
if (this->hap_level->value.float_value != lstate.cvalue) {
this->hap_level->value.float_value = lstate.cvalue;
homekit_characteristic_notify(this->hap_level, this->hap_level->value);
}
}
if (this->hapservice_type == "bat" && this->hap_level) {
float chargepercent = 100.0*((cvalmax + cvalmin) / 2.0) / lstate.cvalue;
if (this->hap_level->value.float_value != chargepercent) {
this->hap_level->value.float_value = chargepercent;
homekit_characteristic_notify(this->hap_level, this->hap_level->value);
}
homekit_characteristic_t* hc_charg = homekit_service_characteristic_by_type(this->hapservice, HOMEKIT_CHARACTERISTIC_CHARGING_STATE);
homekit_characteristic_t* hc_lb = homekit_service_characteristic_by_type(this->hapservice, HOMEKIT_CHARACTERISTIC_STATUS_LOW_BATTERY);

HAP_NOTIFY_CHANGES(bool, hc_charg, false, 0);
//if (hc_charg && hc_charg->value.bool_value) {
// hc_charg->value.bool_value = false;
// homekit_characteristic_notify(this->hap_level, hc_charg->value);
//}
if (hc_lb && cvalmin != cvalmax) {
bool blowlevel = lstate.cvalue < (cvalmax*2.0 / 3.0);
HAP_NOTIFY_CHANGES(bool, hc_lb, blowlevel, 0);
//if (hc_lb->value.bool_value != blowlevel) {
// hc_lb->value.bool_value = blowlevel;
// homekit_characteristic_notify(this->hap_level, hc_lb->value);
//}
}
}
}
}
void LDRController::hap_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context) {
DBG_OUTPUT_PORT.println("RGBStripController::hap_callback");

if (!context) {
return;
};

LDRController* ctl = (LDRController*)context;

/*
if (ch == ctl->hap_br && ch->value.int_value != newState.brightness) {
newState.brightness = ch->value.int_value;
cmd = DimSetBrigthness;
isSet = true;
}
// newState.isOn=value.bool_value;
if (isSet)
ctl->AddCommand(newState, cmd, srcHAP);
*/

}
#endif
19 changes: 18 additions & 1 deletion HomeController/LDRController.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
#include <ArduinoJson.h>
#include "BaseController.h"


#ifdef ENABLE_NATIVE_HAP
extern "C" {
#include "homeintegration.h"
}
#endif
struct LDRState
{
bool isOn=true;
Expand All @@ -28,11 +32,24 @@ class LDRController : public LDR
virtual void run();
virtual void set_state(LDRState state);
virtual bool onpublishmqtt(String& endkey, String& payload);
#ifdef ENABLE_NATIVE_HAP
virtual void setup_hap_service();
static void hap_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context);

virtual void notify_hap();

#endif
protected:
uint pin;
float cvalmin;
float cvalmax;
String cfmt;
#ifdef ENABLE_NATIVE_HAP
homekit_service_t* hapservice;
homekit_characteristic_t * hap_level;
String hapservice_type ;

#endif
};
DEFINE_CONTROLLER_FACTORY(LDRController)

Expand Down
2 changes: 1 addition & 1 deletion HomeController/RGBStripController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ void RGBStripController::notify_hap(){
}
}
void RGBStripController::hap_callback(homekit_characteristic_t *ch, homekit_value_t value, void *context){
DBG_OUTPUT_PORT.println("RGBStripController::hap_callback");
//DBG_OUTPUT_PORT.println("RGBStripController::hap_callback");

if(!context){
return;
Expand Down
2 changes: 1 addition & 1 deletion HomeController/Triggers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void CBaseTimeTrigger<TM>::processrecord(time_t currentTime, TM& rec, Controller

if (rec.timeToTrigger < currentTime) {
this->dotrigger(rec, pctlss);
rec.lastTriggered = currentTime;
rec.lastTriggered = currentTime+2000;
}
}
else if (rec.timetype == dailly) {
Expand Down

0 comments on commit feb0e80

Please sign in to comment.