summaryrefslogtreecommitdiffstats
path: root/martin/door/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'martin/door/src/main.cpp')
-rw-r--r--martin/door/src/main.cpp171
1 files changed, 171 insertions, 0 deletions
diff --git a/martin/door/src/main.cpp b/martin/door/src/main.cpp
new file mode 100644
index 0000000..4491785
--- /dev/null
+++ b/martin/door/src/main.cpp
@@ -0,0 +1,171 @@
1#include <Arduino.h>
2#include <WiFiManager.h>
3#include <ESP8266mDNS.h>
4#include <ESP8266WebServer.h>
5
6#include "cc1101.h"
7#include "hcs200.h"
8
9void http_door_open();
10void serial_door_open();
11
12ESP8266WebServer http_service(80);
13CC1101 radio(D0 /* GDO0 */, D2 /* GDO2 */, D8 /* SS */);
14
15#define PIN_HCS200_ENABLE D3
16#define PIN_HCS200_DATA D1
17//#define HCS200_TEST
18HCS200 hcs200;
19
20void setup()
21{
22 Serial.begin(9600);
23
24 pinMode(PIN_HCS200_ENABLE, OUTPUT);
25 digitalWrite(PIN_HCS200_ENABLE, LOW);
26 pinMode(PIN_HCS200_DATA, INPUT);
27
28 if (!radio.setup())
29 Serial.println("Radio not found!");
30 else if (!radio.setISM(CC1101::FREQ_434MHZ))
31 Serial.println("Unable to set radio frequency");
32 else
33 {
34 Serial.println("Radio found");
35 (void)radio.idle();
36 }
37
38 WiFiManager wifiManager;
39 wifiManager.setConfigPortalTimeout(600);
40 Serial.printf_P(PSTR("Setting up WiFi\n"));
41 WiFi.hostname("door");
42 if (!wifiManager.autoConnect("a-door-able"))
43 {
44 Serial.printf_P(PSTR("Failed to connect and hit timeout\n"));
45 delay(5000);
46 ESP.restart();
47 }
48
49 if (!MDNS.begin("door"))
50 Serial.println("Error setting up MDNS responder!");
51 MDNS.addService("http", "tcp", 80);
52
53 http_service.on("/", HTTP_GET, []() {
54 http_service.send_P(200, PSTR("text/plain"), PSTR("I'm a-door-able"));
55 });
56 http_service.on("/door/open", HTTP_PUT, http_door_open);
57 http_service.begin();
58
59 Serial.println("Started listening");
60}
61
62void ICACHE_RAM_ATTR on_hcs200_isr()
63{
64 hcs200.on_isr(digitalRead(PIN_HCS200_DATA));
65}
66
67bool hcs200_get_keycode(HCS200_Keycode &keycode)
68{
69#if !defined(HCS200_TEST)
70 hcs200.reset();
71 attachInterrupt(digitalPinToInterrupt(PIN_HCS200_DATA), on_hcs200_isr, CHANGE);
72 digitalWrite(PIN_HCS200_ENABLE, HIGH);
73 uint8_t maxloop = 255;
74 while(--maxloop)
75 {
76 if (hcs200.decode(keycode))
77 break;
78 delay(1);
79 }
80 digitalWrite(PIN_HCS200_ENABLE, LOW);
81 detachInterrupt(digitalPinToInterrupt(PIN_HCS200_DATA));
82 return (maxloop != 0);
83#else
84 keycode.encrypted = 0xDEAD;
85 keycode.serial = 0xC0DE;
86 keycode.buttons = HCS200_Keycode::BM_S0;
87 keycode.lowbat = false;
88 return true;
89#endif
90}
91
92void http_door_open()
93{
94 if (!radio.register_check())
95 {
96 http_service.send_P(500, PSTR("text/plain"), PSTR("Radio error"));
97 return;
98 }
99
100 HCS200_Keycode keycode;
101 if (!hcs200_get_keycode(keycode))
102 {
103 http_service.send_P(500, PSTR("text/plain"), PSTR("No data from HCS200"));
104 return;
105 }
106
107 Serial.print("Got keycode: ");
108 keycode.print(Serial);
109
110 if (!radio.transmit())
111 {
112 http_service.send_P(500, PSTR("text/plain"), PSTR("Unable to enable radio transmit mode"));
113 return;
114 }
115
116 long count = constrain(http_service.arg("count").toInt(), 2, 10);
117 for (unsigned short i = 0; i < count; i++)
118 keycode.send([&](int value) { radio.setGDO0(value); });
119
120 (void)radio.idle();
121 Serial.println("Sending done");
122 http_service.send_P(200, PSTR("text/plain"), PSTR("OK"));
123}
124
125void serial_door_open(Print &stream)
126{
127 if (!radio.register_check())
128 {
129 stream.println("Radio error");
130 return;
131 }
132
133 HCS200_Keycode keycode;
134 bool key_valid = hcs200_get_keycode(keycode);
135 if (!key_valid)
136 stream.println("No data from HCS200");
137 stream.print("HCS200 decoder state: ");
138 hcs200.print_state(stream);
139 if (!key_valid)
140 return;
141
142 stream.print("Got keycode: ");
143 keycode.print(stream);
144
145 if (!radio.transmit())
146 {
147 stream.println("Unable to enable radio transmit mode");
148 return;
149 }
150
151 /* send twice */
152 keycode.send([&](int value) { radio.setGDO0(value); });
153 keycode.send([&](int value) { radio.setGDO0(value); });
154
155 (void)radio.idle();
156 stream.println("Sending done");
157}
158
159void loop()
160{
161 MDNS.update();
162 http_service.handleClient();
163
164 if (Serial.available() > 0)
165 {
166 (void)Serial.read();
167 serial_door_open(Serial);
168 }
169
170 delay(1);
171}