[HomeKit] Connect your Photon to HomeKit without homebridge

photon
Tags: #<Tag:0x00007fe22bd9ab60>

#41

Hi, can you explain more briefly, how to do that? In repo, there’s example folder, which shows several usages…


#42

Sure! If you’d rename your example folder to examples (notice the ‘s’), and place your examples into separate folders, they should show up as examples on the Web IDE.
The docs on the structure are here, if that helps: https://docs.particle.io/tutorials/device-os/libraries/#overview-1


#43

It’s done


#44

Thanks, that should help a fair bit!


#45

I got it working with the library (did had to add the separate files myself from the GitHub folder to the tabs in the Web IDE). But very nice that it runs entirely on the Photon!

I do have one question: is it possible to have two accessories (simulated/running) on one Photon? And if so, what would be the best approach to reach this.


#46

Hi, thanks for feedback.

Yep actually you can do that with two different ways (composite accessory or bridge). I would recoomend to look at the pdf document. But basically every Accessory is described by Services and every Service has several Characteristics. Apple defines these services and characteristics.
I.E. a Fan Accessory can have two Services - a Fan service and Lightbulb service. i.e Lightbulb service must have defined On/Off characteristics and optionally can have a Colour characteristics. All the Services, Characteristics and their constrains are describe in that document.

Also there’s special Accessory called Bridge, which is just a set of Accessories. i.e. one bridge can handle several lightbulbs in different rooms.

For Accessory with several Services, i’ve created HAPCompositeAccessory (composition of services). Look at [HomeStation] - It is my home Photon which controls 3 Windows Shutter and 2 Relays controlled using RF 433mhz.

But there’s a limitation, it’s the length of SHARED_RESPONSE_BUFFER, buffer for JSON response. It seems around 5 accessories is the current limit - depends on count of services and characteristics.

Also about the issue you’ve had, can you please create a pull request which fixes it?


#47

Thank you for your extensive respons, I got your HomeStation example working and I will try to adjust it to my own needs! Thanks for the help. :smiley:

Also about the issue you’ve had, can you please create a pull request which fixes it?

If you mean the fact that I had to add the separate files myself: I think (not sure, not a very experienced user)

  1. That the individual example folders should only contain one single .ino file with all the customization also in that file and the .cpp and .h files in src. (I assume this because the .cpp and .h in the examples folders will not be added automatic now in the web IDE)
  2. When you add the library this will be added to the code:
// This #include statement was automatically added by the Particle IDE.
#include <particle-hap.h>

But there is no particle-hap.h file (which then gives an error when compiling), you (the user who just added the library) can of course remove those lines after adding the library, but maybe you could add an empty particle-hap.h file to the folder to make the library more easy to use?

I would like to help you with this but I am afraid that my knowledge / experience doesn’t really is sufficient :sweat_smile: I hope someone with more library knowledge / experience can support you with this, because it is a beautiful library to have available! :relaxed:


#48

Oki, it seems i’ve fixed that particle-hap.h problem.


#49

I was finally able to get something working. I tried Workbench but could not get the code to compile. I probably need more exercise with Workbench.

Then I tried the particle-hap library with the lightbulb example and could get it working on a Photon, but the photon is very unstable. The Home app is losing the connection very regularly, so that doesn’t work well yet. The library is a huge improvement but I still had to include some of the libraries in the example manually.

I then tried the same code but with a different accessory name (but the same connection code) with an Argon. The Home app responded with “wrong connection code”. I then removed the Photon device and tried the Argon device again. Success! The Argon seems very stable so far!

I have several Argons and Photons that I would like to add to Homekit. How would I avoid the “wrong connection code” problem? And how can I avoid the instability issues encountered with the Photon? Any ideas welcome.

I will now proceed to experiment with the code and try dimming.


#50

It is impossible to quantify or qualify this statement and as such no one can possibly help unless you post your code?


#51

Hi,

last week i’ve fixed the “wrong code” for Photon issue. It was due to new Photon OS and homekit pairing failed due to low memory conditions - cryptography failed due to out of memory, which resulted in invalid cryptographic response to homekit, which failed into Wrong code problem.

Please make sure, you are using latest version of my library - 1.0.13
Also please try my RelaySwitch example, which has a logging enabled. If this example fails, please provide a Serial console output, so i can investigate.

Currently i am successfully using my homekit accessories with 1.4 DeviceOS on Photon.


#52

Hi Lukas,
great work! Thanks for sharing this nice project!
I just connect a DHT22 (Temp&Hum) via Photon as HomeKit Accessory and it work pretty good. But unfortunately after a several time, HomeKit loose connection (and cannot reconnect so the only solution is to reset the photon). I use the current provided lib.
Could one use any checks in the loop() testing if their is a working connection and if not to restart() the photon ?
Thanks for your ideas on that,
Best
Michael


#53

Hi, please check that you are using latest version of my library.


#54

Hi,
I use 1.0.14 so that couldn’t be the problem. Moreover, I disconnected the DHT22, connected a 4 lightbulb controller via an other photon and… the same behavior: works fine for a while and disconnected after appr. 1day. So restarting and it works again.
Discovery App shows a working pairing.
I use an iPad as HK-FrontEnd and an Wlan with 2 Airports (one Repeater).
Not sure if your lib requires a photon firmware version (I use 0.6.5)
Thanks for ideas,
Michael


#55

Oki, next time it happens please check:

  1. If your Photon is connected to cloud.
  2. Download Discovery app from Apple Appstore for your Mac.
    You should be able to see a _hap.tcp device advertised. i.e. On my screenshot you can see Zen-179 accessory. You can also find its ip address.
  3. Please try a telnet to accessory. In my case it would be ‘telnet 10.11.18.74 5556’ and check if connection is accepted/refused.
  4. If you can provide USB serial log, it would be great.
  5. Please check if photon is connected with stable wifi connection.
  6. Also i had a huge problems with photon, when there’s a repeater in my wifi. I had to connect external antenna to photon and disable repeater.

#56

Hi Lukas,
thank you for your reply. I checked the telnet connection and it worked fine.
It may has to do with the check routine

void checkParticleConnect()

which is called via timer callback every 20secs (to make sure particle is Re-Connected() if wifi connection is lost…)
It seems that the photon runs into the “Wifi.off()” - Wifi.on()" snippet with the result that WiFi is lost for 10 secs and probably the hkserver loose connection at this point…
So at this point a mechanism to “restart” the server I initiated in setup () might be helpful.
I’ll try delete hkServer; hkServer = NULL;
and in loop()…
if (!hkServer) hkServer = new HKServer(acc1->getDeviceType(),"particle1","523-12-643",progress);

and hopefully this is a workaround…

Let’s see if this will work. I’ll keep you informed.

Ideas welcome,

Best, Michael

void checkParticleConnect()
{
    if(retry_count < 10)
    {
        if(!WiFi.ready())
        {
            WiFi.connect();
            retry_count++;
        }
        else if (!Particle.connected())
        {
            waitFor(Particle.connected,30000);
            retry_count++;
        }
    }
    else
    {
        WiFi.off();
        delay(10000L);
        retry_count = 0;
        WiFi.on();
    }
}

#57

Hi, yes, your proposed solution might work. I am using this kind of workaround in another project and it is working. Could you please provide your workaround within my example projects, and i will merge it.


#58

Hi, unfortunately that was not the solution.
The photon still works fine (I can remote via blynk), I can see the pairing in discovery but the accessories are not available in HomeKit after several hours.
Hmm, unlucky I’ve got other 7 photons to connect to HomeKit and I’d love to do so but if there is no chance to check the status and to reconnect in loop() I have to do without :frowning:
If there are any other ideas where and how to extend your project pls. (HKServer.* ?) let me know,
Best, Michael


#59

…this is the output of dns-sd -Z hap._tcp.

_hap._tcp                                       PTR     particle2._hap._tcp
particle2._hap._tcp                             SRV     0 0 5556 particle2.local. ; Replace with unicast FQDN of target host
particle2._hap._tcp                             TXT     "sf=0" "id=99:3D:ED:DF:B1:20" "pv=1.0" "c#=2" "s#=1" "ff=0" "md=particle2" "ci=10"

_hap._tcp                                       PTR     particle1._hap._tcp
particle1._hap._tcp                             SRV     0 0 5556 particle1.local. ; Replace with unicast FQDN of target host
particle1._hap._tcp                             TXT     "sf=0" "id=54:62:33:D0:5A:56" "pv=1.0" "c#=2" "s#=1" "ff=0" "md=particle1" "ci=5"

so everything seems to be fine. Two accessories which ran yesterday pretty well under HomeKit. Launching HK this morning and the same story: connection lost and no chance to reconnect…
I even resetted the network connection and reconnected. No chance; have to restart the photons…
Best, Michael


#60

Hi,
I think, I found a solution. It seems, that TCPClient.status() does not work in the way one might expected. Whenever the Client is disconnected the isConnected() function of HKConnection still returns true leading to an overflow of the connections array in HKServer. I tried this out by switching to flight mode -> the connection array grows with dublettes of the same IP-Adresses.
So I tried (I know, in a very crude manner) in HKServer-handle()

if(newClient) {
        hkLog.info("Client connected.");
        
        HKConnection *c = new HKConnection(this,newClient);
        i = clients.size() - 1;
        while(i>=0)
        {
            HKConnection *conn = clients.at(i);
            if (!strcmp(conn->clientID(),c->clientID()))
            {
                snprintf(data, sizeof(data),"{\"%02d:%02d:%02d  - Server 2:Client delete dublette -ID\":\"%s\}",Hours,Minutes,Seconds, c->clientID() ); 
                Particle.publish("homekit/accept", data, PRIVATE);
                conn->close();
                clients.erase(clients.begin() + i);
                
                delete conn;
            }
            i--;
        }
        
        clients.insert(clients.end(),c);
....

and everything seems to work stable. (Now 3 Particles, 4 Relay-Switches, 1 DHT, 2 light switches and 3 surge switches(!))

Maybe an improvement. In my case, it seems to work :-).

Thanks Lucas for that great project,

Best, Michael