-
Notifications
You must be signed in to change notification settings - Fork 0
/
strikebutton_serverNEW.ino
92 lines (84 loc) · 2.92 KB
/
strikebutton_serverNEW.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
/* Connects to the home WiFi network
* Asks some network parameters
* Starts WiFi server with fix IP and listens
* Receives and sends messages to the client
* Communicates: wifi_client_01.ino
*/
#include <SPI.h>
#include <ESP8266WiFi.h>
byte ledPin = 2;
int redPin = D0;
int greenPin = D6;
int triggerPin = D1;
bool unlocked = false;
char ssid[] = "SSID"; // SSID of your home WiFi
char pass[] = "PASSWORD"; // password of your home WiFi
WiFiServer server(80);
IPAddress ip(192, 168, 1, 100); // IP address of the server
IPAddress gateway(192,168,1,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(triggerPin, OUTPUT);
Serial.begin(115200); // only for debug
WiFi.config(ip, gateway, subnet); // forces to use the fix IP
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(redPin, HIGH);
Serial.print(".");
delay(500);
digitalWrite(redPin, LOW);
delay(500);
}
server.begin();
/* Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // Network parameters
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());*/
pinMode(ledPin, OUTPUT);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(triggerPin, LOW);
digitalWrite(ledPin,HIGH);
Serial.println("Ready");
}
void loop () {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
Serial.println("client connected");
if (client.readStringUntil('\r').equals("Lock")){
if (unlocked){
digitalWrite(triggerPin, HIGH);
digitalWrite(triggerPin, LOW);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}else{
Serial.println("Lock");
digitalWrite(triggerPin, HIGH);
digitalWrite(triggerPin, LOW);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay(5000);
digitalWrite(triggerPin, HIGH);
digitalWrite(triggerPin, LOW);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
}
}
if (client.readStringUntil('\r').equals("Unlock")){
Serial.println("Unlock");
digitalWrite(triggerPin, HIGH);
digitalWrite(triggerPin, LOW);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
client.flush();
client.stop(); // tarminates the connection with the client
}
}
}