-
Notifications
You must be signed in to change notification settings - Fork 3
/
Firmware.ino
94 lines (73 loc) · 1.96 KB
/
Firmware.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
/*
Simple Wifi Switch
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
MDNSResponder mdns;
// Replace with your network credentials
const char* ssid = "wifi_ssid";
const char* password = "wifi_password";
String response_json_on = "{\"status\" : \"on\"}";
String response_json_off = "{\"status\" : \"off\"}";
int gpio_13_led = 13;
int gpio_12_relay = 12;
ESP8266WebServer server(80);
void setup(void){
// Init
pinMode(gpio_13_led, OUTPUT);
digitalWrite(gpio_13_led, HIGH);
pinMode(gpio_12_relay, OUTPUT);
digitalWrite(gpio_12_relay, HIGH);
Serial.begin(115200);
delay(5000);
WiFi.begin(ssid, password);
Serial.println("Connecting to wifi");
Serial.println(ssid);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(gpio_13_led, LOW);
delay(500);
Serial.print(".");
Serial.println(WiFi.localIP());
Serial.println(WiFi.status());
digitalWrite(gpio_13_led, HIGH);
delay(500);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", [](){
if(digitalRead(gpio_12_relay)==HIGH)
{
server.send(200, "application/json", response_json_on);
}
else
{
server.send(200, "application/json", response_json_off);
}
});
server.on("/on", [](){
server.send(200, "application/json", response_json_on);
digitalWrite(gpio_13_led, LOW);
digitalWrite(gpio_12_relay, HIGH);
delay(1000);
});
server.on("/off", [](){
server.send(200, "application/json", response_json_off);
digitalWrite(gpio_13_led, HIGH);
digitalWrite(gpio_12_relay, LOW);
delay(1000);
});
server.begin();
Serial.println("Server ready..");
}
void loop(void){
server.handleClient();
}