HomeACcessoryKid / Arduino-HomeKit

This is to gather those capable of making an Arduino library to create a HomeKit foundation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Discussion

AdySan opened this issue · comments

Following up on the discussion here.

For starters, the first issue I see is that the ESP8266/Arduino Core is not based on the RTOS SDK. Unless someone plans to change that (@me-no-dev ?), I'm not sure how we could go about integrating this in the Arduino Core.

Code can be compiled to NoOS as well with very little modifications (as far as I looked), then it would just need a CPP wrapper to expose it in more Arduino way.
As for RTOS Arduino, I have tried that and have it on my computer, but CPP is no-go there for some reason, so it could not be done at this point.

Thanks @me-no-dev, perhaps @HomeACcessoryKid can look into getting ESP8266-HomeKit to compile on the NONOS SDK. As for the CPP wrapper to turn it into an Arduino library, is there a good reading resource about these things? Like what classes need to be exposed, etc.

commented

regarding NONOS SDK. I am by no means an expert, but considering that the ESP32 is supposed to be RTOS only, isn't NONOS a dead end street? Putting energy in it requires a bit more faith in its use...
But if someone feels it is worth it, do ask for RTOS details to get it done.

ESP32 modules right now are >4-5x the price of ESP8266 modules. For the price of an ESP32 module, we can already get decent Linux based modules with Ethernet and WLAN built in, which is much more powerful. So I'd pretty much be interested on ESP8266 at this point.

commented

Yet, what is the point of NONOS if even Espressif chooses to use RTOS for ESP32.
Of course we all love the ESP8266, but how shall we use it. I chose RTOS. If others feel motivated by NONOS, go ahead, I won't be an obstacle.

@HomeACcessoryKid unfortunately C++ does not run on RTOS, so because of that Arduino for ESP8266 is based on NONOS. The one for ESP32 is RTOS based.

(semi-OT) @me-no-dev it's a bummer that the software stacks for ESP8266 and ESP32 are not 1:1 compatible...

@probonopd they could never ever be :) ESP32 is dual-core and that fundamentally changes everything :)

@HomeACcessoryKid It might be useful to consider ESP32 as a different platform.

One of the major attractions of ESP8266 was and is its low price. The price difference with respect to the ESP32 will probably asymptotically narrow over time, but for now, esp8266 is significantly cheaper and ~two years ahead in terms of maturity and design-wins in shipping products people might love to hack (ie Sonoff). It is likely to remain in production and available for lower cost, for a good long time. Remember, many 8-bit micro-controllers are still in production!

Within a year or two, the difference in price and community support between ESP8266 and ESP32 will probably become relatively insignificant for hobbyists, building their own devices from modules or dev boards. Even so, I'd guess ESP8266 will still have strong attractions for many such users because of the availability of hackable 8266 based commercial devices.

(Or, my analysis could be too naive. ESP8266 die is ~50% analog circuitry, and as such, is much harder to miniaturize on smaller process nodes. Of the digital circuitry, probably ~50% is RAM. Given that, the extra core + memory + peripherals of ESP32 could represent a relatively small difference in manufacturing cost. Still, ESP8266 will probably live for a long time to supply existing higher-volume customers.)

In any case, there really isn't one right answer, and it's your project, so you should make the choices that make sense for your goals. I just thought my perspective might be useful to you figuring those things out.

Hi

I have started to check the code and play with the Arduino. Actually I have a working - at least it can do some calculation - solution for wolfssl. The main problem was the watchdog (SW & HW ). The espconn can be also used in Arduino, so basically I think with a small effort you can convert the existing solution to a Arduino library / project.

commented

@MetaImi That is great!

I hope you can share some code (if only to my mail).
However, I want to emphasise that my worry for Arduino is that there will not be enough RAM during pairing. Arduino will probably have more overhead and even in the RTOS mode I only have 5k RAM left at the deepest level of SRP calculations during pairing.

So, if you would manage to get at least the pairing stage as a proof of working code, that would give us all the hope it can actually be done based on the current code.
If not, we might need to go assembly etc...

Again, good to hear from you and hope you can also join that SHA2017 event.

@HomeACcessoryKid sent a mail with my results. Not much but maybe it can help :)

Any news on the Arduino IDE front? I am also interested to use it as a library. -- THANKS

commented

Of course, there is an Arduino IDE: https://github.com/esp8266/Arduino

Using it, this would be a framework for the HomeKit server (as much as I understand it):
I started to cut-n-paste the hkc.c into this but I cannot see clearly enough, what needs to be achieved.

The IDE already handles quite a few things by itself, eg. mDNS and a web server.

How can I help?

 -M

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "W.Tell3";
const char *password = "xxx";
const char *hostname = "HKSwitch";

ESP8266WebServer server ( 661 );

#define BUTTON_PIN 2

void handlePair(void) {
char buf[255];

server.send ( 200, "text/html", buf);
}

void handlePairSetup() {

}

void handlePairVerify() {
char buf[255];
// TODO
server.send (200, "application/hap+json", buf);
}

void handleCharekteristics() {
if (pcryp->encrypted) decrypt(pcryp, pusrdata, &length); //length will be updated
parse_chas(pcryp, pParseBuffer);
server.send (204, "application/hap+json", "");
}

void handleAcessories() {
if (halfpaired) {
#ifdef DEBUG1
os_printf("halfpaired\n");
#endif
flash[0]=0x00;flash[1]=0x7f;flash[2]=0xff;flash[3]=0xff;
spi_flash_write(start,(uint32 *)flash,4);
os_printf("postwrite\n");
halfpaired=0;
pairing=0;
}
pcryp->state=6;
xQueueSendToFront(crypto_queue,&pcryp,0);
delegated=true;
#ifdef DEBUG1
os_printf("out of TaskCreate - Free heap:%d\n", system_get_free_heap_size());
#endif

}

void setup () {
Serial.begin (9600);
WiFi.begin (ssid, password);
Serial.println ("\n\nHomeKit Accessory Server\nV 0.0.0\n");

// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
	delay ( 500 );
	Serial.print ( "." );
}

Serial.print ( "\nSSID=" );
Serial.println ( ssid );
Serial.print ( "IPaddress=" );
Serial.println ( WiFi.localIP() );

WiFi.hostname(hostname);

MDNS.addService("hap", "tcp", 661);
MDNS.addServiceTxt("hap", "tcp", "md", hostname); // TODO: add chipID() or something
MDNS.addServiceTxt("hap", "tcp", "pv", "1.0");
MDNS.addServiceTxt("hap", "tcp", "id", "CC:22:3D:E4:CF:33"); // TODO: add chipID() somehow
MDNS.addServiceTxt("hap", "tcp", "c#", "12");
MDNS.addServiceTxt("hap", "tcp", "s#", "1");
MDNS.addServiceTxt("hap", "tcp", "ff", "0");
MDNS.addServiceTxt("hap", "tcp", "ci", "2");
MDNS.addServiceTxt("hap", "tcp", "sf", "0");

if ( MDNS.begin ( "HKSwitch" ) ) {
	Serial.println ( "MDNSresponder=OK" );
} else {
Serial.println ( "MDNSresponder=Error" );
}

server.on ("/pair", handlePair);

server.on ("/pair-verify", handlePairVerify);
server.on ("/pair-setup", handlePairSetup);
server.on ("/characteristics", handleCharercteristics);
server.on ("/config", handleConfig); // internal
server.on ("/info", handleInfo); // internal
server.on ("/debug", handleDebug); // internal
server.begin();
Serial.println ( "HTTP server started" );
}

void loop ( void ) {
server.handleClient();
}

As said in the first posts, @AdySan thinks RTOS is needed for this, and @me-no-dev says RTOS is not possible with the Arduino IDE at the moment.

So

  • Why do we need RTOS?
  • Why is RTOS not possible with the Arduino IDE?
commented
commented

FreeRTOS Arduino is not possible because of the inability to run C++ code in it :( reason is unknown to me ;) it compiles and all, but does not work.

Is there a way in esp8266/Arduino to execute code that takes 500ms without triggering the watchdog @me-no-dev?

Not without yield ;) 10ms is doable but 500 is not. What is the case?

But even if it was rtos, you would get that task switched a few times

commented

Well, are you saying with RTOS and the current code the chip can handle multiple simultaneous http requests?

I agree that if it cannot handle multiple simultaneous requests, then in the long run ESP8266 is not the right solution. Maybe we should all get a Raspberry Pi Zero W instead. It also has WLAN and a chip that can handle the full UNIX OS with the full HomeBridge, Full Wifi without RAM or other issues. It's supposed to be 10 bucks, but I didn't see it for that price yet. Nearly the same as an ESP32.

Going down the assembler route and trying to squeeze out the last bit of the ESP module sounds more frustrating than it is worth it!?

-M

Or possibly Realtek RTL8710AF Cortex-M3 WiFi SoC, a cheaper FreeRTOS-ready competitor to the ESP8266. http://linuxgizmos.com/tiny-iot-module-runs-freertos-on-realtek-ameba-wifi-soc/

The Realtek chip is in no way better ;) if you want C++ in esp rtos, i advise to make some noise in the repo and i will try to push for fixes. Watchdog is required for nonos to keep the wifi running. Cant block the wifi or it will fail on protocol level (beacon and packet handling)

Hello
I wish I could use homekit on the arduino IDE, and I found the code published by @michaeljanich only there is an error while compiling on the IDE: "pcrypt was not declared in the script", by what can i replace pcrypt to run homekit on the arduino IDE?

Thanking you in advance

cordially

commented

I have found the code in this discution , in this commentary of @michaeljanich of July 14th.
Cordially

commented

Ah, I see the confusion. You refer to the code that michael posted here in this issue.

That has never been tried since we all agreed that without RTOS capabilities the attempt seems not viable. So, a dead end in my opinion. Wouldn't spend time on it...

Unless someone comes with real code, or real foundations, I think this will not happen...

Sorry

ok for arduino but I ordered a esp32, what should I use to use it with homekit? I thought about using micropython, what do you think?

After discussion with a few kind people on this channel I have abandoned the idea of using a ESP8266 module (or ESP32) since I never had a stable result.

I've moved on to use a Raspberry Pi ZeroW, should be around 11USD (but is still more expensive, but under 20 bucks). I use homebridge on it with gpio.

I have two modules now in operation: 1 week and 1 day. Still fixing a few software bugs and getting used to the sequence the messages are sent from the apple devices. But so far I'm happily surprised, how little problem's I've had so far.

A linux system is much easier to debug and you can hope for some debug message in the log file, if something goes wrong. ESPs or Arduinos are hard to debug when something goes wrong after 2 weeks and the circumstances are hard to recreate.

So for the time being: Adieu ESP8266. I never had anything stable working on you. Your ultra-low price will be missed.

currently for my tests I use arduino-based sensors that communicate with a raspberry 3 homebridge server via mqtt, I would have liked to spend without raspberry using my arduino autonomously but I can not I saw that esp32 works under micropython so I think it should be possible.

commented

I think that ESP8266 is perfectly capable of meeting all the needs of the typical accessories, see my other repositories. And at the ultimate low prize that does not have to be missed.
What is the issue is that most people don't want to learn that much in order to get it to work.

Arduino is indeed a lower threshold for many people but at the cost of more overhead.

The point of this repository is to bring those capable of bringing a FOUNDATION on ESP8266 (orESP32 if so desired) to support Arduino and not run out of memory.

The few messages here show that there is a certain interest from 'consumers' but no-one that is willing to fill the gap. I hope this will change, but it requires a skilled person, not just an Arduino 'consumer'. Until then, this discussion is here to attract that person.

i am waiting to receive my esp32 and esp8266 and i keep you informed of the result of my tests of different libraries in different languages.

In the meantime, if someone finds something to do under arduino, I'm obviously interested.

About the wdt question, maybe there are some ways to bypass it, don’t know if it applies to your efforts @HomeACcessoryKid but take a look at this:
https://techtutorialsx.com/2017/01/21/esp8266-watchdog-functions/
I really hope you can develop this arduino ide library for Homekit without bridging!

Hi, I just published an Arduino library of Apple HomeKit for the ESP8266 Arduino core. This library is mainly based on esp-homekit for esp-open-rtos. I ported this implementation to work fine in the pure Arduino environment (without RTOS).

See https://github.com/Mixiaoxiao/Arduino-HomeKit-ESP8266.

commented