-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYeebutton.ino
126 lines (115 loc) · 3.41 KB
/
Yeebutton.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#define ssid "YourWiFiSSID"
#define password "YourWifiPassword"
int butts[4] = {0,14,12,13};
// GPIO pins D3,D5,D6,D7
const byte ip[4] = {192, 168, 20, 114}; // use the Python script to find out the IP and port number
const int port = 55443;
void setup() {
for(int i = 0; i < 4; i++)
{
pinMode(butts[i],INPUT_PULLUP);
}
//Serial.begin(115200);
//while(!Serial) { }
//Serial.println(" ");
//Serial.println("Waking up");
//Serial.println(" ");
connectToWifi();
}
void connectToWifi() {
//Serial.print("Connecting to: ");
//Serial.print(ssid);
WiFi.begin(ssid, password);
//Serial.println(" ");
//Serial.print("Attempting to connect: ");
//try to connect for 10 seconds
int i = 10;
while(WiFi.status() != WL_CONNECTED && i >=0) {
delay(1000);
//Serial.print(i);
//Serial.print(", ");
i--;
}
//Serial.println(" ");
//print connection result
if(WiFi.status() == WL_CONNECTED){
//Serial.print("Connected.");
//Serial.println(" ");// print an empty line
//Serial.print("NodeMCU ip address: ");
//Serial.println(WiFi.localIP());
}
else {
//Serial.println("Connection failed - check your credentials or connection");
}
}
void loop() {
for(int i = 0; i < 4; i++)
{
if(!digitalRead(butts[i]))
{
WiFiClient client;
switch(i)
{
case 0 :
{
//Serial.println("Shutting down lights");
//Serial.println("connecting to host");
if (!client.connect(ip, port))
{
//Serial.println("connection failed");
return;
}
//Serial.println("connection to host established");
String payload = "{\"id\":1,\"method\":\"set_power\",\"params\":[\"off\"]}";
client.println(payload);
break;
}
case 1 :
{
//Serial.println("Low lights");
//Serial.println("connecting to host");
if (!client.connect(ip, port))
{
//Serial.println("connection failed");
return;
}
//Serial.println("connection to host established");
String payload = "{\"id\":2,\"method\":\"set_scene\",\"params\":[\"color\",16726528,1]}";
client.println(payload);
break;
}
case 2 :
{
//Serial.println("Mid lights");
//Serial.println("connecting to host");
if (!client.connect(ip, port))
{
//Serial.println("connection failed");
return;
}
//Serial.println("connection to host established");
String payload = "{\"id\":3,\"method\":\"set_scene\",\"params\":[\"color\",16736256,100]}";
client.println(payload);
break;
}
case 3 :
{
//Serial.println("Full lights");
//Serial.println("connecting to host");
if (!client.connect(ip, port))
{
//Serial.println("connection failed");
return;
}
//Serial.println("connection to host established");
String payload = "{\"id\":3,\"method\":\"set_scene\",\"params\":[\"ct\",3200,100]}";
client.println(payload);
break;
}
}
delay(300);
}
}
}