-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi_homekit_Luz_estado.ino
109 lines (95 loc) · 2.39 KB
/
wifi_homekit_Luz_estado.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
#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 = "Light1estado"; // nombre del entorno
int led = 2;
int pulsador = 0;
boolean estado_pulsador = LOW;
boolean estado_luz = 0;
#define BUFFER_SIZE 100
WiFiClient wclient;
PubSubClient client(wclient, server);
void callback(const MQTT::Publish& pub) {
Serial.println (pub.payload_string());
if(pub.payload_string() == "on")
{
digitalWrite(led, HIGH); // en caso de que el modulo rele funcione al reves, cambiarl HIGH por LOW
estado_luz=1;
Serial.println("Switch On");
}
else
{
digitalWrite(led, LOW); // en caso de que el modulo rele funcione al reves, cambiarl LOWGH por HIGH
estado_luz=0;
Serial.println("Switch Off");
}
}
void setup()
{
pinMode(led,OUTPUT);
pinMode(pulsador,INPUT);
digitalWrite(led, LOW);
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)
{
Serial.print("WiFi not connected");
}
else
{
Serial.print("WiFi connected");
}
}
}
void loop() {
estado_pulsador=digitalRead(pulsador);
if (estado_pulsador==LOW)
{
if (estado_luz==0)
{
estado_luz=1;
client.publish("Light1estado","SwitchedOn");
Serial.print("Switch On");
}
else
{
estado_luz=0;
client.publish("Light1estado","SwitchedOff");
Serial.print("Switch Off");
}
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect("ESP8266: Light1estado")) {
client.publish("outTopic",(String)"hello world, I'm "+host);
client.subscribe(host+(String)"/#");
}
}
if (client.connected())
client.loop();
}
else
{
if (estado_luz==0)
{
digitalWrite(led,HIGH); // en caso de que el modulo rele funcione al reves, cambiarl HIGH por LOWGH
}
else
{
digitalWrite(led,LOW); // en caso de que el modulo rele funcione al reves, cambiarl LOW por HIGH
}
}
}