-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp8266_http_button.ino
117 lines (94 loc) · 2.31 KB
/
esp8266_http_button.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
/*
OFF: http://192.168.0.111:51828/?accessoryId=switch1&state=false
ON: http://192.168.0.111:51828/?accessoryId=switch1&state=true
*/
#include <SPI.h>
#include <Wire.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
const char* ssid = "<YOUR_SSID>";
const char* password = "<YOUR_PASSWORD>";
String payload = "NONE";
HTTPClient http;
int ledPin = 2;
int buttonPin = 0;
int ledState = 0;
int buttonState = 0;
WiFiServer server(80);
String get_request(String url) {
http.begin(url);
int httpCode = http.GET();
String kek = "FAILED";
if (httpCode > 0) {
kek = http.getString();
}
http.end();
return kek;
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.print("Hi!");
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
while (payload != "{\"success\":true}") {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
Serial.println(ledState);
if (ledState == HIGH) {
payload = get_request("http://192.168.0.111:51828/?accessoryId=switch1&state=true");
} else {
payload = get_request("http://192.168.0.111:51828/?accessoryId=switch1&state=false");
}
delay(100);
}
if (ledState == HIGH) {
ledState = LOW;
} else {
ledState = HIGH;
}
payload = "NONE";
digitalWrite(ledPin, ledState);
delay(1000);
}
delay(10);
}