-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifi-key.ino
More file actions
157 lines (123 loc) · 3.69 KB
/
wifi-key.ino
File metadata and controls
157 lines (123 loc) · 3.69 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
---------------------------------------------------------------------------------------------------
This Sketch enables your board to act as wifi-lock.
It provides a WiFi hotspot and checks if a device's MAC-address is whitelisted.
You can modify it use your smartphone as a key to open and close your garage door.
Board: ESP8266
INSTRUCTION:
- put allowed MAC-addresses in the array "macWhitelist"
- connect your device to WiFi "ESP-Access" and enter the password "password"
Authors:
- Manuel Kienlein (@ManuelK2000)
- Richard Lang (@Michlbauer)
---------------------------------------------------------------------------------------------------
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
extern "C" {
#include "user_interface.h"
}
// WiFi network name and password (minimum of 8 characters)
const char* ssid = "ESP-Access";
const char* password = "password";
int oldStatus = 0;
int Status = 0; // 0= no connections, 1= connected, 2= client successful conected (mac checked)
// Whitelist of MAC-addresses
String macWhitelist[2] = {
"40:45:AD:7D:BF:71",
"BC:FE:D9:15:35:EB"
};
void setup() {
Serial.begin(9600);
// Set led pins as output
pinMode(LED_BUILTIN, OUTPUT);
pinMode(16, OUTPUT);
// Start WiFi access point
WiFi.softAP(ssid, password);
Serial.println("----------------------------");
Serial.print("WiFi name: ");
Serial.println(ssid);
Serial.print("Router IP: ");
Serial.println(WiFi.softAPIP());
Serial.println("----------------------------");
Serial.println("Setup completed");
}
void loop() {
// Update status
oldStatus = Status;
Status = 0;
// Print list of clients
printConnectedClients();
if(oldStatus != Status){
if(Status > oldStatus){
Serial.println("RISING EDGE");
}
if(Status < oldStatus){
Serial.println("FALLEN EDGE");
}
// Do something when status has changed
if(Status == 2){
Serial.println("logged in");
}
if(Status == 0){
Serial.println("logged out");
}
}
/*
* Status 0: No connections
* Status 1: Client successful connected
* Status 2: Client contained in whitelist
*/
// Turn LED_BUILTIN on when MAC-address is whitelisted
if(Status == 2){
digitalWrite(LED_BUILTIN, LOW);
}
if(Status == 0){
digitalWrite(LED_BUILTIN, HIGH);
}
// Turn led on GIPO 16 on, if device is connected
if(Status == 0){
digitalWrite(16, LOW);
}else{
digitalWrite(16, HIGH);
}
delay(1000);
}
boolean hasAccess(String mac){
for (int i = 0; i < sizeof(macWhitelist); i = i + 1){
if(mac == macWhitelist[i]){
return true;
}
}
return false;
}
void printConnectedClients(){
Serial.println("-------- Network Client List --------");
Serial.println("Connected clients: " + String(wifi_softap_get_station_num()));
// Load station info
struct station_info *station_list = wifi_softap_get_station_info();
// If struct is not empty, output data
while(station_list != NULL){
// Determine MAC-address
char client_mac[18] = {0};
sprintf(client_mac, "%02X:%02X:%02X:%02X:%02X:%02X", MAC2STR(station_list->bssid));
// Determine IP-address
String client_ip = IPAddress((&station_list->ip)->addr).toString();
// Check if MAC is contained in whitelist
bool access = hasAccess(String(client_mac));
String access_message = (access)?"ACCESS GRANTED":"ACCESS DENIED";
Status = 1;
if(access == true){
Status = 2;
}
// Output data
Serial.println("MAC-address: " + String(client_mac) + " | IP-address: " + client_ip + " | Access: " + access_message);
// Continue with next client
station_list = STAILQ_NEXT(station_list, next);
}
// Reset station info
wifi_softap_free_station_info();
Serial.println("-------------------------------------");
Serial.println();
Serial.println();
}