-
Notifications
You must be signed in to change notification settings - Fork 0
/
mitchine.ino
284 lines (224 loc) · 7.23 KB
/
mitchine.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <ESP8266WebServer.h>
#include <Ticker.h>
#include <EEPROM.h>
#include <WiFiUdp.h>
#include "helpers.h"
#include "global.h"
/*
Include the HTML, STYLE and Script "Pages"
*/
#include "Page_Admin.h"
#include "Page_Information.h"
#include "PAGE_NetworkConfiguration.h"
/*
* 1 = 16 = Mom = Red = Mitchine Chicago
* 2 = 14 = Dad = Orange = Mitchine Amalfi
* 3 = 0 = Ant = Green = Mitchine Berkeley
* 4 = 13 = Tri = Light Blue = Mitchine Oakland
* 5 = 4 = Bri = Yellow = Mitchine San Francisco
* 6 = 15 = Izz = Purple = Mitchine Tokyo
* 7 = 2 = Gra = Brown = Mitchine Seoul
* 8 = 5 = KL = Pink = Mitchine Osaka
*/
#define ACCESS_POINT_PASSWORD "mitchinexmas15"
#define ACCESS_POINT_NAME "Mitchine Kevin"
#define AdminTimeOut 180 // Defines the Time in Seconds, when the Admin-Mode will be diabled
WiFiClient espClient;
PubSubClient client(espClient);
const int maximumDevices = 8;
//The number of samples we'll take each time we check the capacitive button
const int capSamples = 10;
//The pin to be used for capacitive input
const int capInputPin = 12;
//The threshold for the total the capacitive input adds up to with capSamples attempts
const int threshHold = 15;
//time millis of last touch detection event
long lastCapTouch = 0;
long lastReconnect = 0;
long lastMsg = 0;
char msg[50];
int value = 0;
/*
* 1 = 16 = Mom = Red = Mitchine Chicago
* 2 = 14 = Dad = Orange = Mitchine Amalfi
* 3 = 0 = Ant = Green = Mitchine Berkeley
* 4 = 13 = Tri = Light Blue = Mitchine Oakland
* 5 = 4 = Bri = Yellow = Mitchine San Francisco
* 6 = 15 = Izz = Purple = Mitchine Tokyo
* 7 = 2 = Gra = Brown = Mitchine Seoul
* 8 = 5 = KL = Pink = Mitchine Osaka
*/
int outputPins[] = {16,14,0,13,4,15,2,5};
int ledOnState[] = {HIGH, HIGH, LOW, HIGH, HIGH, HIGH, LOW, HIGH};
void setup ( void ) {
pinMode(16, OUTPUT);
pinMode(14, OUTPUT);
pinMode(0, OUTPUT);
pinMode(13, OUTPUT);
pinMode(4, OUTPUT);
pinMode(15, OUTPUT);
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
EEPROM.begin(512);
Serial.begin(115200);
delay(500);
Serial.println("Starting Mitchine");
if (!ReadConfig())
{
// DEFAULT CONFIG
config.ssid = "Mitchine";
config.password = "Mitchine";
config.dhcp = true;
config.IP[0] = 192;config.IP[1] = 168;config.IP[2] = 1;config.IP[3] = 100;
config.Netmask[0] = 255;config.Netmask[1] = 255;config.Netmask[2] = 255;config.Netmask[3] = 0;
config.Gateway[0] = 192;config.Gateway[1] = 168;config.Gateway[2] = 1;config.Gateway[3] = 1;
config.mqttServer = "mqtt.develpr.com";
config.deviceIdentifier = 1;
WriteConfig();
Serial.println("General config applied");
}
if (AdminEnabled)
{
WiFi.mode(WIFI_AP_STA);
WiFi.softAP( ACCESS_POINT_NAME , ACCESS_POINT_PASSWORD);
}
else
{
WiFi.mode(WIFI_STA);
}
ConfigureWifi();
server.on ( "/", send_network_configuration_html );
server.on ( "/favicon.ico", []() { Serial.println("favicon.ico"); server.send ( 200, "text/html", "" ); } );
server.on ( "/admin.html", []() { Serial.println("admin.html"); server.send_P ( 200, "text/html", PAGE_AdminMainPage ); } );
server.on ( "/config.html", send_network_configuration_html );
server.on ( "/info.html", []() { Serial.println("info.html"); server.send_P ( 200, "text/html", PAGE_Information ); } );
server.on ( "/admin/values", send_network_configuration_values_html );
server.on ( "/admin/connectionstate", send_connection_state_values_html );
server.on ( "/admin/infovalues", send_information_values_html );
server.onNotFound ( []() { Serial.println("Page Not Found"); server.send_P ( 400, "text/html", "Page not Found" ); } );
server.begin();
Serial.println( "HTTP server started" );
char charMqttServer[config.mqttServer.length()];
config.mqttServer.toCharArray(charMqttServer, config.mqttServer.length()+1);
client.setServer(charMqttServer, 1883);
client.setCallback(callback);
reconnect();
disableAllButMe();
}
static bool checkTouch() {
long riseTime = 0;
//capSamples = int 20 - just the number of samples to take - adjust as desried
for(int i = 0; i < capSamples; i++){
//capInputPin = int 12 in my case, just the pin # to use
pinMode(capInputPin, OUTPUT);
digitalWrite(capInputPin, LOW);
//This delay which may or may not need to be here.
delayMicroseconds(500);
pinMode(capInputPin, INPUT_PULLUP);
while(digitalRead(capInputPin) != HIGH){
++riseTime;
}
}
//"threshHold" is some number that you will likely need to
//experiment with. For 20 samples, somewhere around 1000
//might be good.
return (riseTime > threshHold);
}
void disableAllButMe(){
for(int i = 0; i < 8; i++){
if(outputPins[i] != getPin(config.deviceIdentifier)){
digitalWrite(outputPins[i], !ledOnState[i]);
}
}
}
void publishTouched(){
snprintf (msg, 75, "%ld", config.deviceIdentifier);
client.publish("mitchinexmas15", msg);
}
void callback(char* topic, byte* payload, unsigned int length) {
int mqttIdentifier = ((char)payload[0] - '0');
int pin = getPin(mqttIdentifier);
if( pin != -2 ){
digitalWrite(pin, ledOnState[mqttIdentifier-1]);
delay(300);
digitalWrite(pin, !ledOnState[mqttIdentifier-1]);
delay(300);
digitalWrite(pin, ledOnState[mqttIdentifier-1]);
delay(300);
digitalWrite(pin, !ledOnState[mqttIdentifier-1]);
delay(300);
digitalWrite(pin, ledOnState[mqttIdentifier-1]);
}
}
void reconnect() {
// Loop until we're reconnected
int attempts = 0;
while (!client.connected() && attempts < 4) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("espKevin")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("heartBeat", "Welcome Kevin!");
// ... and resubscribe
client.subscribe("mitchinexmas15");
} else {
// Wait a few seconds before retrying
delay(3000);
attempts++;
}
}
}
int getPin(int mqttIdentifier){
int result = -2;
if(mqttIdentifier < 1 || mqttIdentifier > 8){
result = result;
}
else{
result = outputPins[(mqttIdentifier-1)];
}
return result;
}
void loop ( void ) {
if (AdminEnabled)
{
if (AdminTimeOutCounter > AdminTimeOut)
{
AdminEnabled = false;
Serial.println("Admin Mode disabled!");
WiFi.mode(WIFI_STA);
}
}
server.handleClient();
if(WiFi.status() == WL_CONNECTED){
long now = millis();
if (!client.connected() && (now - lastReconnect > 30000)) {
Serial.println("Reconnecting...");
lastReconnect = now;
reconnect();
}
client.loop();
now = millis();
if (now - lastMsg > 90000) {
lastMsg = now;
++value;
if(value > 9999){
value = 1;
}
snprintf (msg, 75, "Hello Kevin #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("heartBeat", msg);
}
if((now - lastCapTouch) > 3000){
if(checkTouch()){
publishTouched();
disableAllButMe();
lastCapTouch = now;
}
}
}
}