-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_homekit_enchufe2.ino
62 lines (53 loc) · 1.43 KB
/
wifi_homekit_enchufe2.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
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
IPAddress server(192, 168, 0, 56); // IP de la raspberry Pi
const char* ssid = "SSID"; // Your ssid
const char* pass = "PASSWORD"; // Your Password
const char* host = "Enchufe2"; // nombre del entorno
int relay = 2;
#define BUFFER_SIZE 100
WiFiClient wclient;
PubSubClient client(wclient, server);
void callback(const MQTT::Publish& pub) {
if(pub.payload_string() == "on")
{
digitalWrite(relay, LOW); // en caso de que el modulo rele funcione al reves, cambiarl LOW por HIGH
}
else
{
digitalWrite(relay, HIGH); // en caso de que el modulo rele funcione al reves, cambiarl HIGH por LOW
}
}
void setup()
{
pinMode(relay,OUTPUT);
digitalWrite(relay, HIGH);
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
client.set_callback(callback);
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect("ESP8266: Enchufe2")) {
client.publish("outTopic",(String)"hello world, I'm "+host);
client.subscribe(host+(String)"/#");
}
}
if (client.connected())
client.loop();
}
}