|
Example02_Switch下面的代码:
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include "wifi_info.h"
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
void setup() {
Serial.begin(115200);
wifi_connect(); // in wifi_info.h
my_homekit_setup();
}
void loop() {
my_homekit_loop();
delay(10);
}
extern "C" homekit_server_config_t config;
extern "C" homekit_characteristic_t cha_switch_on;
static uint32_t next_heap_millis = 0;
#define PIN_SWITCH 0
void cha_switch_on_setter(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on.value.bool_value = on; //sync the value
LOG_D("Switch: %s", on ? "ON" : "OFF");
digitalWrite(PIN_SWITCH, on ? LOW : HIGH);
}
void my_homekit_setup() {
pinMode(PIN_SWITCH, OUTPUT);
digitalWrite(PIN_SWITCH, HIGH);
cha_switch_on.setter = cha_switch_on_setter;
arduino_homekit_setup(&config);
}
void my_homekit_loop() {
arduino_homekit_loop();
const uint32_t t = millis();
if (t > next_heap_millis) {
// show heap info every 5 seconds
next_heap_millis = t + 5 * 1000;
LOG_D("Free heap: %d, HomeKit clients: %d",
ESP.getFreeHeap(), arduino_homekit_connected_clients_count());
}
}
my_accessory.c下面的代码:
#include <homekit/homekit.h>
#include <homekit/characteristics.h>
void my_accessory_identify(homekit_value_t _value) {
printf("accessory identify\n");
}
homekit_characteristic_t cha_switch_on = HOMEKIT_CHARACTERISTIC_(ON, false);
homekit_characteristic_t cha_name = HOMEKIT_CHARACTERISTIC_(NAME, "Switch");
homekit_accessory_t *accessories[] = {
HOMEKIT_ACCESSORY(.id=1, .category=homekit_accessory_category_switch, .services=(homekit_service_t*[]) {
HOMEKIT_SERVICE(ACCESSORY_INFORMATION, .characteristics=(homekit_characteristic_t*[]) {
HOMEKIT_CHARACTERISTIC(NAME, "Switch"),
HOMEKIT_CHARACTERISTIC(MANUFACTURER, "Arduino HomeKit"),
HOMEKIT_CHARACTERISTIC(SERIAL_NUMBER, "0123456"),
HOMEKIT_CHARACTERISTIC(MODEL, "ESP8266/ESP32"),
HOMEKIT_CHARACTERISTIC(FIRMWARE_REVISION, "1.0"),
HOMEKIT_CHARACTERISTIC(IDENTIFY, my_accessory_identify),
NULL
}),
HOMEKIT_SERVICE(SWITCH, .primary=true, .characteristics=(homekit_characteristic_t*[]){
&cha_switch_on,
&cha_name,
NULL
}),
NULL
}),
NULL
};
homekit_server_config_t config = {
.accessories = accessories,
.password = "111-11-111"
};
wifi_info.h下面的代码:
#ifndef WIFI_INFO_H_
#define WIFI_INFO_H_
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
const char *ssid = "ChinaNet-5aUh";
const char *password = "k4zbzecl";
void wifi_connect() {
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(true);
WiFi.begin(ssid, password);
Serial.println("WiFi connecting...");
while (!WiFi.isConnected()) {
delay(100);
Serial.print(".");
}
Serial.print("\n");
Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str());
}
#endif /* WIFI_INFO_H_ */
我自己加的小爱同学代码:
#define BLINKER_MIOT_LIGHT //小爱灯类库
#define BLINKER_PRINT Serial //串口协议库
#include <Blinker.h> //官方库
char auth[] = "b013a1c2b6c3"; //设备key号
//**软重启定义
uint32_t con_time = 0; //断网记时
int con_flag = 0; //断网标记,1为断网
void(*resetFunc) (void) = 0;//定义一个reset函数(用于重启)
//*******新建组件对象
BlinkerButton Button1("btn-abc"); //设置app按键的键名
//*******app上按下按键即会执行该函数app里按键有2种模式3钟不同样式,下面写出所有的开关状态。
void button1_callback(const String & state) //用state来存储组键的值按键 : "tap"(点按); "pre"(长按); "pup"(释放)开关 : "on"(打开); "off"(关闭)
{
BLINKER_LOG("app操作了!: ", state); //串口打印
if (digitalRead(PIN_SWITCH)==LOW) { //如果state是on状态
BLINKER_LOG("灭灯!"); //串口打印
digitalWrite(PIN_SWITCH, HIGH);
Button1.text("灭");
Button1.print("off"); //反馈回按键状态是开
}
else if (digitalRead(PIN_SWITCH)==HIGH) { //如果state是off状态
BLINKER_LOG("亮灯!"); //串口打印
digitalWrite(PIN_SWITCH,LOW);
Button1.text("亮");
Button1.print("on"); //反馈回按键状态是关
}
}
//*******如果小爱有对设备进行操作就执行下面
void miotPowerState(const String & state)
{
BLINKER_LOG("小爱语音操作!"); //串口打印
if (state == BLINKER_CMD_ON){
digitalWrite(PIN_SWITCH,LOW);
BlinkerMIOT.powerState("on");
BLINKER_LOG("亮灯!");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF){
digitalWrite(PIN_SWITCH,HIGH);
BlinkerMIOT.powerState("off");
BLINKER_LOG("灭灯!");
BlinkerMIOT.print();
}
}
//*******app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态30s~60s一次
void heartbeat()
{
BLINKER_LOG("状态同步!");
if (digitalRead(PIN_SWITCH)==LOW)
{
Button1.print("on");
Button1.text("亮");
}
else
{
Button1.print("off");
Button1.text("灭");
}
}
void setup()
{
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback); //app上操作必须的注册回调函数关联按键名“Button1”和判断程序“button1_callback"
BlinkerMIOT.attachPowerState(miotPowerState); //小爱语音操作注册函数
Blinker.attachHeartbeat(heartbeat); //app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态进行语音操作和app操作同步。
}
//**软重启
if (Blinker.connected())//Blinker.connected()函数反馈网络连接状态
{
con_flag = 0;
} else
{
if (con_flag == 0)
{
con_time = millis(); //给断网时间赋初始值
con_flag = 1;
} else
{
if ((millis() - con_time) >= 90000) //判断断网时间超90秒后执行重启,这个时间可根据实际需要调整
{
resetFunc();//启用这个函数就软重启设备
}
}
}
Blinker.run();
}
编译时报错文件:
In file included from C:\Users\三哥\Desktop\Arduino-HomeKit-ESP8266-master\examples\Example02_Switch\xiao_ai.ino:3:0:
C:\Users\����\Documents\Arduino\libraries\Blinker\src/Blinker.h:215:6: error: #error Please set a mode BLINKER_BLE/BLINKER_WIFI/BLINKER_MQTT ! Please check your mode setting.
#error Please set a mode BLINKER_BLE/BLINKER_WIFI/BLINKER_MQTT ! Please check your mode setting.
^
"WiFiServer.h" 对应多个库
已使用: C:\Users\三哥\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\ESP8266WiFi
未使用:D:\Arduino\libraries\WiFi
exit status 1
为开发板 Generic ESP8266 Module 编译时出错。
求大神 帮我看看我的代码哪里出问题了,并帮忙修改一下 谢谢
|
|