|
本帖最后由 vany5921 于 2020-5-13 13:41 编辑
以前想接入苹果的HomeKit要使用HomeAssistant、HomeBridge等服务来实现,配置的复杂程度对于新人来说并不友好,同时搭建一台连线的服务器也是不小的开销。而现在只要使用ESPHap这个库就能轻松实现接入HomeKit服务,你可以通过相关苹果产品对M5Stack进行控制。以下以M5StickC为例介绍如何使用ESPHap这个库文件首先我们需要到github下载相关文件https://github.com/Yurik72/ESPHap,下载完成后找到wolfssl文件夹,该文件下有一个wolfSSL_3_13_0.rar压缩包,将压缩包解压至Arduino Library下作为一个独立的wolfssl库存在。
操作流程:
->连接WIFI->注册配置状态指示灯引脚(可省略)->初始化hap开辟SPIFFS空间存储必要的HomeKit配对信息->设置回调函数用于移除设备(必须添加,否则苹果端删除设备后无法再次配对使用,必须擦除flash NVS分区重新烧录)->设置设备类型(枚举了32类)->注册homekit硬件(Sensor
、厂家、序列号、型号、固件信息)->添加HomeKit服务信息->完成最终初始化。更多详情参考homeintegration.c文件,默认配对密码11111111,可在配置文件中修改.首次使用需要格式化SPIFFS存储配置文件,大概30秒,耐心等待。以下示例接入ENV-UNIT显示温湿度。
[mw_shl_code=arduino,true]#include <M5StickC.h>
#include "DHT12.h"
#include <Wire.h> //The DHT12 uses I2C comunication.
#include <WiFi.h>
#include <FS.h>
#include <SPIFFS.h>
const char* ssid = "YOURSSID";
const char* password = "YOURPASSWORD";
const int identity_led=10;
const int led_gpio = 10;
extern "C"{
#include "homeintegration.h"
}
homekit_service_t* hapservice={0};
String pair_file_name="/pair.dat";
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 30 secs for SPIFFS to be formatted");
SPIFFS.format();
Serial.println("Spiffs formatted");
}
void init_hap_storage(){
Serial.print("init_hap_storage");
if (!SPIFFS.begin(true)) {
Serial.print("SPIFFS Mount failed");
}
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 size){
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, size);
fsDAT.close();
}
DHT12 dht12;
homekit_service_t* temperature;
homekit_service_t* humidity;
void setup() {
M5.begin();
Wire.begin(32, 33, 100000);
M5.Lcd.setRotation(3);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
pinMode(led_gpio,OUTPUT);
hap_set_identity_gpio(identity_led);
init_hap_storage();
set_callback_storage_change(storage_changed);
hap_setbase_accessorytype(homekit_accessory_category_thermostat);
hap_initbase_accessory_service("Sensor","M5Stack","20200513","StickC-ENV","1.0.1");
temperature = hap_add_temperature_service("Temperature Sensor");
humidity = hap_add_humidity_service("Humidity Sensor");
//and finally init HAP
hap_init_homekit_server();
}
void loop() {
float tmp = dht12.readTemperature();
float hum = dht12.readHumidity();
M5.Lcd.setCursor(0, 6, 2);
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.printf("Temp: %2.1fC\n", tmp);
M5.Lcd.printf("Humi: %2.0f%%\n", hum);
homekit_characteristic_t * ch= homekit_service_characteristic_by_type(temperature, HOMEKIT_CHARACTERISTIC_CURRENT_TEMPERATURE);
ch->value.float_value=tmp;
homekit_characteristic_notify(ch,ch->value);
homekit_characteristic_t * ch2= homekit_service_characteristic_by_type(humidity, HOMEKIT_CHARACTERISTIC_CURRENT_RELATIVE_HUMIDITY);
ch2->value.float_value=hum;
homekit_characteristic_notify(ch2,ch2->value);
delay(500);
}[/mw_shl_code]
|
|