-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266_influxdb_pushover_doorbell.ino
248 lines (208 loc) · 7.42 KB
/
esp8266_influxdb_pushover_doorbell.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
#include <FS.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <ArduinoJson.h>
#include <WiFiUdp.h>
const int buttonPin = 12;
const int relayPin = 5;
int doRing = 0;
int ringing = 0;
unsigned long buttonPushed = millis();
unsigned long startRing = millis();
long debouncing_time = 10000; //Debouncing Time in Milliseconds
volatile unsigned long last_micros;
// InfluxDB Server
char INFLUXDB_SERVER[40]; // Your InfluxDB Server FQDN
char INFLUXDB_PORT[5] = "8089"; // Default InfluxDB UDP Port
char SENSOR_LOCATION[20] = "location"; // This location is used for the "device=" part of the InfluxDB update
// Pushover
char PUSHOVER_TOKEN[40]; // Your Pushover Token
char PUSHOVER_USER[40]; // Your Pushover User
int length;
//flag for saving data
bool shouldSaveConfig = false;
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
WiFiClientSecure client;
WiFiUDP udp;
void setup() {
Serial.begin ( 115200 );
Serial.println("mounting FS...");
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
strcpy(INFLUXDB_SERVER, json["INFLUXDB_SERVER"]);
strcpy(INFLUXDB_PORT, json["INFLUXDB_PORT"]);
strcpy(SENSOR_LOCATION, json["SENSOR_LOCATION"]);
strcpy(PUSHOVER_TOKEN, json["PUSHOVER_TOKEN"]);
strcpy(PUSHOVER_USER, json["PUSHOVER_USER"]);
} else {
Serial.println("failed to load json config");
}
}
}
} else {
Serial.println("failed to mount FS");
}
WiFiManagerParameter custom_influxdb_server("server", "InfluxDB Server", INFLUXDB_SERVER, 40);
WiFiManagerParameter custom_influxdb_port("port", "8089", INFLUXDB_PORT, 5);
WiFiManagerParameter custom_sensor_location("location", "Sensor Location", SENSOR_LOCATION, 20);
WiFiManagerParameter custom_pushover_token("token", "Pushover Token", PUSHOVER_TOKEN, 40);
WiFiManagerParameter custom_pushover_user("user", "Pushover User", PUSHOVER_USER, 40);
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&custom_influxdb_server);
wifiManager.addParameter(&custom_influxdb_port);
wifiManager.addParameter(&custom_sensor_location);
wifiManager.addParameter(&custom_pushover_token);
wifiManager.addParameter(&custom_pushover_user);
String hostname = "Doorbell-" + String(ESP.getChipId());
WiFi.hostname("Doorbell-" + String(ESP.getChipId()));
String ssid = "Doorbell-" + String(ESP.getChipId());
if(!wifiManager.autoConnect(ssid.c_str())) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//read updated parameters
strcpy(INFLUXDB_SERVER, custom_influxdb_server.getValue());
strcpy(INFLUXDB_PORT, custom_influxdb_port.getValue());
strcpy(SENSOR_LOCATION, custom_sensor_location.getValue());
strcpy(PUSHOVER_TOKEN, custom_pushover_token.getValue());
strcpy(PUSHOVER_USER, custom_pushover_user.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["INFLUXDB_SERVER"] = INFLUXDB_SERVER;
json["INFLUXDB_PORT"] = INFLUXDB_PORT;
json["SENSOR_LOCATION"] = SENSOR_LOCATION;
json["PUSHOVER_TOKEN"] = PUSHOVER_TOKEN;
json["PUSHOVER_USER"] = PUSHOVER_USER;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
Serial.println("");
Serial.print ( "Connected to your network" );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(relayPin, HIGH);
delay(10);
digitalWrite(relayPin, LOW);
attachInterrupt(buttonPin, buttonPress, RISING);
if(strlen(INFLUXDB_SERVER) == 0 || strlen(INFLUXDB_PORT) == 0 || strlen(SENSOR_LOCATION) == 0 || strlen(PUSHOVER_TOKEN) == 0 || strlen(PUSHOVER_USER) == 0) {
Serial.print("Config Faulty, Kicking config");
SPIFFS.format();
wifiManager.resetSettings();
delay(2000);
ESP.reset();
}
}
void loop() {
if(doRing == 1) {
Serial.println("doRing started");
ringOn();
startRing = millis();
doRing = 0;
}
if(ringing == 1) {
if(millis() - 500 >= startRing) {
ringOff();
}
}
}
void ringOn() {
Serial.println("ringOn started");
digitalWrite(relayPin, HIGH);
ringing = 1;
}
void ringOff() {
Serial.println("ringOff started");
digitalWrite(relayPin, LOW);
ringing = 0;
pushover("Someone is at the door!");
sendData();
}
void buttonPress () {
if(ringing == 1) {
Serial.println("Doorbell was pushed again, doing nothing because throttling");
} else {
if((long)(micros() - last_micros) >= debouncing_time * 1000) {
doRing = 1;
}
last_micros = micros();
Serial.print("last Micros is : ");
Serial.println(last_micros);
Serial.println("Interrupted");
}
}
byte pushover(char *pushovermessage) {
String Msg = pushovermessage;
String Message = "token="+String(PUSHOVER_TOKEN)+"&user="+String(PUSHOVER_USER)+"&sound=bugle&message="+Msg;
Serial.println(Message);
length = Message.length();
if (client.connect("api.pushover.net", 443)) {
Serial.println("Sending message…");
client.println("POST /1/messages.json HTTP/1.1");
client.println("Host: api.pushover.net");
client.println("Connection: close\r\nContent-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.print(length);
client.println("\r\n");
client.print(Message);
/* Uncomment this to receive a reply from Pushover server:
while(client.connected()) {
while(client.available()) {
char ch = client.read();
Serial.write(ch);
}
}
*/
client.stop();
Serial.println("Done");
Serial.println("");
delay(100);
}
}
void sendData() {
String line;
line = String("doorbell,device=" + String(SENSOR_LOCATION) + " value=1");
Serial.println(line);
// send the packet
Serial.println("Sending first UDP packet...");
udp.beginPacket(INFLUXDB_SERVER, atoi(INFLUXDB_PORT));
udp.print(line);
udp.endPacket();
}