summaryrefslogtreecommitdiffstats
path: root/martin/door/src/main.cpp
blob: 449178528b6e61f488a6a53f22afc241a39f5c5c (plain)
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
#include <Arduino.h>
#include <WiFiManager.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>

#include "cc1101.h"
#include "hcs200.h"

void http_door_open();
void serial_door_open();

ESP8266WebServer http_service(80);
CC1101 radio(D0 /* GDO0 */, D2 /* GDO2 */, D8 /* SS */);

#define PIN_HCS200_ENABLE D3
#define PIN_HCS200_DATA   D1
//#define HCS200_TEST
HCS200 hcs200;

void setup()
{
  Serial.begin(9600);

  pinMode(PIN_HCS200_ENABLE, OUTPUT);
  digitalWrite(PIN_HCS200_ENABLE, LOW);
  pinMode(PIN_HCS200_DATA, INPUT);

  if (!radio.setup())
    Serial.println("Radio not found!");
  else if (!radio.setISM(CC1101::FREQ_434MHZ))
    Serial.println("Unable to set radio frequency");
  else
  {
    Serial.println("Radio found");
    (void)radio.idle();
  }

  WiFiManager wifiManager;
  wifiManager.setConfigPortalTimeout(600);
  Serial.printf_P(PSTR("Setting up WiFi\n"));
  WiFi.hostname("door");
  if (!wifiManager.autoConnect("a-door-able"))
  {
    Serial.printf_P(PSTR("Failed to connect and hit timeout\n"));
    delay(5000);
    ESP.restart();
  }

  if (!MDNS.begin("door"))
    Serial.println("Error setting up MDNS responder!");
  MDNS.addService("http", "tcp", 80);

  http_service.on("/", HTTP_GET, []() {
    http_service.send_P(200, PSTR("text/plain"), PSTR("I'm a-door-able"));
  });
  http_service.on("/door/open", HTTP_PUT, http_door_open);
  http_service.begin();

  Serial.println("Started listening");
}

void ICACHE_RAM_ATTR on_hcs200_isr()
{
  hcs200.on_isr(digitalRead(PIN_HCS200_DATA));
}

bool hcs200_get_keycode(HCS200_Keycode &keycode)
{
#if !defined(HCS200_TEST)
  hcs200.reset();
  attachInterrupt(digitalPinToInterrupt(PIN_HCS200_DATA), on_hcs200_isr, CHANGE);
  digitalWrite(PIN_HCS200_ENABLE, HIGH);
  uint8_t maxloop = 255;
  while(--maxloop)
  {
    if (hcs200.decode(keycode))
      break;
    delay(1);
  }
  digitalWrite(PIN_HCS200_ENABLE, LOW);
  detachInterrupt(digitalPinToInterrupt(PIN_HCS200_DATA));
  return (maxloop != 0);
#else
  keycode.encrypted = 0xDEAD;
  keycode.serial    = 0xC0DE;
  keycode.buttons   = HCS200_Keycode::BM_S0;
  keycode.lowbat    = false;
  return true;
#endif
}

void http_door_open()
{
  if (!radio.register_check())
  {
    http_service.send_P(500, PSTR("text/plain"), PSTR("Radio error"));
    return;
  }

  HCS200_Keycode keycode;
  if (!hcs200_get_keycode(keycode))
  {
    http_service.send_P(500, PSTR("text/plain"), PSTR("No data from HCS200"));
    return;
  }

  Serial.print("Got keycode: ");
  keycode.print(Serial);

  if (!radio.transmit())
  {
    http_service.send_P(500, PSTR("text/plain"), PSTR("Unable to enable radio transmit mode"));
    return;
  }

  long count = constrain(http_service.arg("count").toInt(), 2, 10);
  for (unsigned short i = 0; i < count; i++)
    keycode.send([&](int value) { radio.setGDO0(value); });

  (void)radio.idle();
  Serial.println("Sending done");
  http_service.send_P(200, PSTR("text/plain"), PSTR("OK"));
}

void serial_door_open(Print &stream)
{
  if (!radio.register_check())
  {
    stream.println("Radio error");
    return;
  }

  HCS200_Keycode keycode;
  bool key_valid = hcs200_get_keycode(keycode);
  if (!key_valid)
    stream.println("No data from HCS200");
  stream.print("HCS200 decoder state: ");
  hcs200.print_state(stream);
  if (!key_valid)
    return;

  stream.print("Got keycode: ");
  keycode.print(stream);

  if (!radio.transmit())
  {
    stream.println("Unable to enable radio transmit mode");
    return;
  }

  /* send twice */
  keycode.send([&](int value) { radio.setGDO0(value); });
  keycode.send([&](int value) { radio.setGDO0(value); });

  (void)radio.idle();
  stream.println("Sending done");
}

void loop()
{
  MDNS.update();
  http_service.handleClient();

  if (Serial.available() > 0)
  {
    (void)Serial.read();
    serial_door_open(Serial);
  }

  delay(1);
}