Yurik72 / ESPHap

ESP32/ESP8266 Arduino library for native Apple Homekit Accessory Protocol (HAP)
MIT License
265 stars 60 forks source link

Serial Monitor overflowing with "Got characteristic X.XX change event" #5

Closed seamaster101 closed 4 years ago

seamaster101 commented 4 years ago

I'm getting serial monitor overflowing with the following data every time I open the application on my iPhone:


HomeKit: [Client 1073688948] Got characteristic 1.10 change event HomeKit: [Client 1073688948] Got characteristic 1.13 change event HomeKit: [Client 1073688948] Got characteristic 1.10 change event HomeKit: [Client 1073688948] Got characteristic 1.13 change event HomeKit: [Client 1073688948] Got characteristic 1.10 change event HomeKit: [Client 1073688948] Got characteristic 1.13 change event HomeKit: [Client 1073688948] Got characteristic 1.10 change event


and that repeats for long time even I I close the application (maybe still displays what's in the buffer?) then after some time there the following error:


HomeKit: [Client 1073697900] Got characteristic 1.10 change event HomeKit: [Client 1073697900] Got characteristic 1.13 change event !!! HomeKit: [Client 1073697900] error_in_write_data is true, abort write data !!! HomeKit: [Client 1073697900] error_in_write_data is true, abort write data !!! HomeKit: [Client 1073697900] error_in_write_data is true, abort write data HomeKit: [Client 1073697900] Got characteristic 1.10 change event HomeKit: [Client 1073697900] Got characteristic 1.13 change event !!! HomeKit: [Client 1073697900] error_in_write_data is true, abort write data !!! HomeKit: [Client 1073697900] error_in_write_data is true, abort write data !!! HomeKit: [Client 1073697900] error_in_write_data is true, abort write data HomeKit: [Client 1073697900] Got characteristic 1.10 change event HomeKit: [Client 1073697900] Got characteristic 1.13 change event


you have my sketch in #2. it happens even if I remove the second sensor and use only temperature sensor the application appears to work OK, but there is definitely something going wrong for that much chatter on the serial monitor... Please help

Yurik72 commented 4 years ago

I ordered DHT11 for testing, it's on the way to me, In my experience of usage BME280 (my preferbale device) I have sent notification to Apple only if difference between new value and old value For instance HOMEKIT_CHARACTERISTIC_CURRENT_TEMPERATURE); if(ch1->value.float_value!=temp} ///validate if temp is changed ch1->value.float_value=temp; homekit_characteristic_notify(ch1,ch1->value); delay(10); }

The same for humidity

You can see my usage in https://github.com/Yurik72/ESPHomeController

It litle bit complex

seamaster101 commented 4 years ago

Yes, this helped! No more chatter on the serial monitor. You can close that one too. It works perfectly well now with DHT11!

On Apr 11, 2020, at 12:49 AM, Yurik72 notifications@github.com wrote:

I ordered DHT11 for testing, it's on the way to me, In my experience of usage BME280 (my preferbale device) I have sent notification to Apple only if difference between new value and old value For instance HOMEKIT_CHARACTERISTIC_CURRENT_TEMPERATURE); if(ch1->value.float_value!=temp} ///validate if temp is changed ch1->value.float_value=temp; homekit_characteristic_notify(ch1,ch1->value); delay(10); }

The same for humidity

You can see my usage in https://github.com/Yurik72/ESPHomeController https://github.com/Yurik72/ESPHomeController It litle bit complex

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Yurik72/ESPHap/issues/5#issuecomment-612358788, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4H7PZMFU3PMG2QX2FNGDLRMAOJLANCNFSM4MFCIEDA.

Yurik72 commented 4 years ago

Hi, please send me the latest version of your sketch, i will put into examples as well The problem was that is too fast updating apple... After applying my solution, Apple home is not updating such frequently, but potentially it can be. The final solution is to increase a measurement time or/and compare values with some delta before sending to Apple. I suppose is enough for such purpose . It's only "user" monitor and not so important to see values with couple of second of delay or with decreasing of accuracy.. Any how i continue working on library to prevent this "user" usage in such manner and increasing stability on the core

seamaster101 commented 4 years ago

Here it is:


//ESP8266 with DHT11

include

include

//#include //#include

define STATIC_IP_ADDR

///---DHT11-----///

include "PietteTech_DHT.h" // Uncommend if building using CLI

define DHTTYPE DHT11 // Sensor type DHT11/21/22/AM2301/AM2302

define DHTPIN 2 // Digital pin for communications

define PIN 0 // Digital pin for re-format

//declaration void dht_wrapper(); // must be declared before the lib initialization ///---DHT11 stop-----///

ifdef STATIC_IP_ADDR

IPAddress staticIP(192,168,0,91); IPAddress gateway(192,168,0,1); IPAddress subnet(255,255,255,0);

endif

define HOMEKIT_SHORT_APPLE_UUIDS

include "coredecls.h"

const char ssid = "SHAW-62CD10"; const char password = "251169025293"; float hum; float temp;

const long userdelay = 3000; //delay for user input when tarting to format the SPIFFS unsigned long previousMillis = 0; unsigned long prevMillis = 0; const long interval = 15000;
unsigned long currentMillis = 0; unsigned long currMillis = 0;

extern "C"{

include "homeintegration.h"

}

include "homekitintegrationcpp.h"

//homekit_service_t* hapservice={0};

homekit_service_t temperature; homekit_service_t humidity;

String pair_file_name="/pair.dat";

///---DHT11-----/// // Lib instantiate PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); int n; // counter ///---DHT11 stop-----///

//** void init_hap_storage(){ Serial.print("init_hap_storage"); File fsDAT=SPIFFS.open(pair_file_name, "r"); if(!fsDAT){ Serial.println("Failed to read pair.dat"); return; } int size=hap_get_storage_size_ex(); char* buf=new char[size]; memset(buf,0xff,size); int readed=fsDAT.readBytes(buf,size); Serial.print("Readed bytes ->"); Serial.println(readed); hap_init_storage_ex(buf,size); fsDAT.close(); delete []buf; } //*****

//**** void storage_changed(char szstorage,int bufsize){ SPIFFS.remove(pair_file_name); File fsDAT=SPIFFS.open(pair_file_name, "w+"); if(!fsDAT){ Serial.println("Failed to open pair.dat"); return; } fsDAT.write((uint8_t)szstorage, bufsize); fsDAT.close(); } //****

//****

////////////////////////////////// void setup() { disable_extra4k_at_link_time(); Serial.begin(74880); delay(100);

//Enble format only when pairing fails prevMillis=millis(); currMillis = prevMillis + userdelay;

//ask for user input Serial.println("Should I format the SPIFFS? "); //it should wait for userdelay/1000 seconds for user input

while (currMillis >= prevMillis && (digitalRead(PIN) == HIGH) ){ prevMillis = millis();

delay(1000); Serial.println(round((userdelay-prevMillis)/1000)); if (currMillis <= prevMillis) { Serial.println("5 seconds are over"); delay (100); break;
} else{ if (digitalRead(PIN) == LOW){ Serial.println("Formating..."); Format(); delay (100); break; }
} }

//

if (!SPIFFS.begin()) { Serial.print("SPIFFS Mount failed"); } else { Serial.println("SPIFFS Mount OK"); }

Serial.print("Attempting connection to: ");
Serial.println(ssid);
delay(10);
WiFi.mode(WIFI_STA);
delay(10);

// Working every time ESP8266 WIFI initialization

if (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Not connected"); WiFi.begin((char)ssid, (char)password); } else { Serial.println("Connected"); }

ifdef STATIC_IP_ADDR

WiFi.config(staticIP, gateway, subnet);

endif

Serial.println("");
Serial.println("WiFi connected!");

ifdef STATIC_IP_ADDR

Serial.print("Static IP address: ");

else

Serial.print("Dinamic IP address: ");

endif

Serial.println(WiFi.localIP());
Serial.print("WiFi Status: ");    
Serial.println(WiFi.status());

///---Setup homekit device---/// //this is for custom storage usage // In given example we are using \pair.dat file in our spiffs system //see implementation below Serial.print("Free heap: "); Serial.println(system_get_free_heap_size()); init_hap_storage(); set_callback_storage_change(storage_changed); ///---Adding Accessory---/// hap_setbase_accessorytype(homekit_accessory_category_thermostat); ///--- init base properties---/// hap_initbase_accessory_service("Environment","SeaMaster","258521521","DHT11_ESP8266","1.0");

temperature = hap_add_temperature_service("Temperature");

humidity = hap_add_humidity_service("Humidity"); //old definition replaced by the line below

///---Setup homekit device stop---///

///---initialize HAP---///

hap_init_homekit_server();

///---initialize HAP stop---///

///---End of setup()---/// }

///---DHT11 interrupt callback---///

void ICACHE_RAM_ATTR dht_wrapper() { DHT.isrCallback();

} ///---DHT11 interrupt callback stop---///

///////////////////////////////

void loop() {

///---Reading the DHT11 sensor---/// // Reading temperature for humidity takes about 250 milliseconds! // this sensor should not be polled more often then 2 sec apart

unsigned long currentMillis = millis();
int result = DHT.acquireAndWait(0);

if (currentMillis - previousMillis >= interval) { // save the last time you read the sensor previousMillis = currentMillis; temp = DHT.getCelsius(); // Read temperature as Celsius hum = DHT.getHumidity(); // Read humidity (percent) Serial.print("Temperature: "); Serial.println(temp); // Check if any reads failed and exit early (to try again). Serial.print("Humidity: "); Serial.println(hum); // Check if any reads failed and exit early (to try again). if (isnan(hum) || isnan(temp)) { Serial.println("Failed to read from DHT sensor!"); return; } }

homekit_characteristic_t * ch1= homekit_service_characteristic_by_type(temperature, HOMEKIT_CHARACTERISTIC_CURRENT_TEMPERATURE); //ch1->value.float_value=temp; if(ch1->value.float_value != temp){ ///validate if temp is changed ch1->value.float_value = temp; homekit_characteristic_notify(ch1,ch1->value); delay(10); }delay(10);

homekit_characteristic_t * ch2= homekit_service_characteristic_by_type(humidity, HOMEKIT_CHARACTERISTIC_CURRENT_RELATIVE_HUMIDITY); //ch2->value.float_value=hum; if(ch2->value.float_value != hum){ ///validate if temp is changed ch2->value.float_value = hum; homekit_characteristic_notify(ch2,ch2->value); delay(10); }

hap_homekit_loop(); //what does this exactly do????? ///--- end of the main loop ---/// }

///--- Formating function ---/// void Format(){ // Next lines have to be done ONLY ONCE!!!!!When SPIFFS is formatted ONCE you can comment these lines out!! Serial.println("Please wait few seconds for SPIFFS to be formatted"); SPIFFS.format(); Serial.println("SPIFFS formatted"); delay(1000); }


Спасиба, всево харошева, Jordan

On Apr 13, 2020, at 1:03 PM, Yurik72 notifications@github.com wrote:

Hi, please send me the latest version of your sketch, i will put into examples as well The problem was that is too fast updating apple... After applying my solution, Apple home is not updating such frequently, but potentially it can be. The final solution is to increase a measurement time or/and compare values with some delta before sending to Apple. I suppose is enough for such purpose . It's only "user" monitor and not so important to see values with couple of second of delay or with decreasing of accuracy.. Any how i continue working on library to prevent this "user" usage in such manner and increasing stability on the core

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Yurik72/ESPHap/issues/5#issuecomment-613070998, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4H7P6CI6Z75IKESSJWER3RMNVYHANCNFSM4MFCIEDA.