4402| 0
|
SHT31-F新品测评(2)通过ESP32接入HomeKit |
本帖最后由 vany5921 于 2020-6-6 13:38 编辑 1.测试视频: 介绍一下将SHT31-F接入HomeKit的方法,这里使用了以前我用的SHT3X的库。乐鑫有一套基于ESP-IDF开发HomeKit的SDK,对于许多用户来说过于复杂,因此有大神进行了Arduino版本的移植,并封装好了编译库,我们只需要根据自己使用的功能进行添加即可。该库作者还在不断完善功能中,基本的数据获取和开关控制是不影响的。 2.电路连接: 我依然选择了简洁的一体化主机作为控制节点,通过GROVE线反插连接到Gravity接口,非常简单 3.程序 程序部分先介绍一个库ESPHAP,github地址https://github.com/Yurik72/ESPHap,支持ESP32与ESP8266。由于封装的比较完整,整个使用并不复杂,基本处理过程: •在文件系统中开辟一块空间存储配对码 •连接WIFI •注册接入的产品类型 •写入产品的描述信息 •注册HomeKit相关服务 •回调HomeKit服务 主要代码部分: [mw_shl_code=c,false] #include <M5Atom.h> #include "SHT3X.h" #include <Wire.h> //The DHT12 uses I2C comunication. #include <WiFi.h> #include <FS.h> #include <SPIFFS.h> const char* ssid = "GB3-14FF"; const char* password = "12345678"; const int identity_led=-1;//LED提示灯,我的是RGB因此用不到 float tmp = 0.0; float hum = 0.0; 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(); } SHT3X sht30; homekit_service_t* temperature; homekit_service_t* humidity; void setup() { M5.begin(true, false, true); Wire.begin(26, 32, 100000); M5.dis.drawpix(0, 0xff11ff);//设置RGB灯颜色 M5.update(); // We start by connecting to a WiFi network 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()); M5.dis.drawpix(0, 0x0011ff); M5.update(); 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","DFROBOT","20200605","SHT31-F","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() { if(sht30.get() == 0){ tmp = sht30.cTemp; hum = sht30.humidity; 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); } }[/mw_shl_code] 相关文件: ESPHAP与wolfSSL解压后放入Arduino的library文件夹下 SHT3X.zipwolfSSL.zipESPHap.zip |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed