forked from AdySan/HomeKitLight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeKitLight.ino
More file actions
72 lines (54 loc) · 1.35 KB
/
HomeKitLight.ino
File metadata and controls
72 lines (54 loc) · 1.35 KB
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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char *ssid = "*******"; // cannot be longer than 32 characters!
const char *pass = "*******"; //
// Update these with values suitable for your network.
IPAddress server(192, 168, 1, 155);
const int relay = D3;
#define BUFFER_SIZE 100
void callback(const MQTT::Publish& pub) {
// handle message arrived
Serial.print(pub.topic());
Serial.print(" => ");
Serial.println(pub.payload_string());
if(pub.payload_string() == "on")
{
digitalWrite(relay, HIGH);
}
else
{
digitalWrite(relay, LOW);
}
}
WiFiClient wclient;
PubSubClient client(wclient, server);
void setup() {
pinMode(relay, OUTPUT);
// Setup console
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
client.set_callback(callback);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect("ESP8266: AdyLight")) {
client.publish("outTopic","hello world");
client.subscribe("AdyLight");
}
}
if (client.connected())
client.loop();
}
}