-
Notifications
You must be signed in to change notification settings - Fork 3
/
4way-switch-with-temp-humi.ino
138 lines (122 loc) · 4.69 KB
/
4way-switch-with-temp-humi.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <SimpleDHT.h>
#include <Arduino.h>
#include <arduino_homekit_server.h>
#include "wifi_info.h"
#define LOG_D(fmt, ...) printf_P(PSTR(fmt "\n") , ##__VA_ARGS__);
int pinDHT11 = 14; //define pin of DHT11
SimpleDHT11 dht11(pinDHT11); // define device
void setup() {
Serial.begin(115200);
wifi_connect(); // in wifi_info.h
//homekit_storage_reset(); // to remove the previous HomeKit pairing storage when you first run this new HomeKit example
my_homekit_setup();
}
void loop() {
my_homekit_loop();
delay(10);
}
//==============================
// HomeKit setup and loop
//==============================
// access your HomeKit characteristics defined in my_accessory.c
extern "C" homekit_server_config_t config;
extern "C" homekit_characteristic_t cha_switch_on1; //The firt relay
extern "C" homekit_characteristic_t cha_switch_on2; //The secondry relay
extern "C" homekit_characteristic_t cha_switch_on3; //The third relay
extern "C" homekit_characteristic_t cha_switch_on4; //The fouth relay
extern "C" homekit_characteristic_t temperature; //temperature
extern "C" homekit_characteristic_t humidity; //humidity
static uint32_t next_heap_millis = 0;
#define PIN_SWITCH1 16 //The first relay
#define PIN_SWITCH2 5 //The secondry relay
#define PIN_SWITCH3 4 //The third relay
#define PIN_SWITCH4 0 //The fouth relay
//Called when the switch value is changed by iOS Home APP
void cha_switch_on_setter1(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on1.value.bool_value = on; //sync the value
LOG_D("Switch1: %s", on ? "ON" : "OFF");
digitalWrite(PIN_SWITCH1, on ? HIGH : LOW);
}
void cha_switch_on_setter2(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on2.value.bool_value = on; //sync the value
LOG_D("Switch2: %s", on ? "ON" : "OFF");
digitalWrite(PIN_SWITCH2, on ? HIGH : LOW);
}
void cha_switch_on_setter3(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on3.value.bool_value = on; //sync the value
LOG_D("Switch3: %s", on ? "ON" : "OFF");
digitalWrite(PIN_SWITCH3, on ? HIGH : LOW);
}
void cha_switch_on_setter4(const homekit_value_t value) {
bool on = value.bool_value;
cha_switch_on4.value.bool_value = on; //sync the value
LOG_D("Switch4: %s", on ? "ON" : "OFF");
digitalWrite(PIN_SWITCH4, on ? HIGH : LOW);
}
void my_homekit_setup() {
pinMode(PIN_SWITCH1, OUTPUT);
digitalWrite(PIN_SWITCH1, LOW);
pinMode(PIN_SWITCH2, OUTPUT);
digitalWrite(PIN_SWITCH2, LOW);
pinMode(PIN_SWITCH3, OUTPUT);
digitalWrite(PIN_SWITCH3, LOW);
pinMode(PIN_SWITCH4, OUTPUT);
digitalWrite(PIN_SWITCH4, LOW);
//Add the .setter function to get the switch-event sent from iOS Home APP.
//The .setter should be added before arduino_homekit_setup.
//HomeKit sever uses the .setter_ex internally, see homekit_accessories_init function.
//Maybe this is a legacy design issue in the original esp-homekit library,
//and I have no reason to modify this "feature".
cha_switch_on1.setter = cha_switch_on_setter1;
cha_switch_on2.setter = cha_switch_on_setter2;
cha_switch_on3.setter = cha_switch_on_setter3;
cha_switch_on4.setter = cha_switch_on_setter4;
arduino_homekit_setup(&config);
//report the switch value to HomeKit if it is changed (e.g. by a physical button)
//bool switch_is_on = true/false;
//cha_switch_on.value.bool_value = switch_is_on;
//homekit_characteristic_notify(&cha_switch_on, cha_switch_on.value);
}
//get temperature and humidity first.then give parm to HomeKit
void get_sensor()
{
byte temperature_value = 0;
byte humidity_value = 0;
int err = SimpleDHTErrSuccess;
delay(1000);
if ((err = dht11.read(&temperature_value, &humidity_value, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.println(err);
delay(1000);
}
temperature.value.float_value = temperature_value;
humidity.value.float_value = humidity_value;
homekit_characteristic_notify(&temperature, temperature.value);
homekit_characteristic_notify(&humidity, humidity.value);
// Serial.print(temperature_value); Serial.print(" *C, ");
// Serial.print(humidity_value); Serial.println(" H");
}
void my_homekit_loop() {
arduino_homekit_loop();
const uint32_t t = millis();
if(GPIO_INPUT_GET(GPIO_ID_PIN(12)) == 0x01)
{
delay(3000);
if(GPIO_INPUT_GET(GPIO_ID_PIN(12)) == 0x01)
{homekit_server_reset();
delay(500);
WiFi.disconnect();
delay(500);
wifi_connect(); // in wifi_info.h
}
}
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());
get_sensor();
}
}