-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doorbell-Transmitter.ino
109 lines (98 loc) · 2.4 KB
/
Doorbell-Transmitter.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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoOTA.h>
#include "Secrets.h"
/* Secrets.h file should contain data as below: */
#ifndef WIFI_SSID
#define WIFI_SSID "xxxxxxxxxx"
#define WIFI_PASSWORD "xxxxxxxxxx"
#define URL "http://doorbellreceiver.lan/ring?batt="
#endif
#define LED_PIN 0
#define RELAY_PIN 2
#define OTA_HOSTNAME "Doorbell-Transmitter"
int battVoltage;
HTTPClient http;
WiFiClient wClient;
WiFiClientSecure wClientSecure;
ADC_MODE(ADC_VCC);
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
Serial.begin(115200);
pinMode(3, FUNCTION_3);
pinMode(LED_PIN, OUTPUT);
setupWifi();
bool httpSucceeded = false;
battVoltage = ESP.getVcc();
String url = String(URL) + String(battVoltage);
for (int j = 0; j < 5; j++) {
if (postHTTP(url)) {
httpSucceeded = true;
break;
}
delay(2000); // wait 2 seconds before we retry
}
if (httpSucceeded) {
Serial.println("HTTP Succeeded!");
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
} else {
Serial.println("HTTP Failed!");
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
}
delay(1000);
digitalWrite(LED_PIN, LOW);
Serial.println("Powering off!");
delay(1000);
digitalWrite(RELAY_PIN, HIGH);
delay(1000);
Serial.println("Since there is still power, we are moving into loop() and waiting for OTA...");
digitalWrite(LED_PIN, HIGH);
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
}
void loop() {
ArduinoOTA.handle();
}
bool postHTTP(String url) {
http.begin(wClient, url);
uint httpCode = http.GET();
http.end();
if (httpCode == 200) {
Serial.println(httpCode);
return true;
} else {
Serial.println(httpCode);
return false;
}
}
void setupWifi() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
digitalWrite(LED_PIN, HIGH);
delay(10);
digitalWrite(LED_PIN, LOW);
delay(10);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(700);
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
}