summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--martin/door/src/cc1101.cpp2
-rw-r--r--martin/door/src/main.cpp83
2 files changed, 79 insertions, 6 deletions
diff --git a/martin/door/src/cc1101.cpp b/martin/door/src/cc1101.cpp
index 0f76d38..16e405f 100644
--- a/martin/door/src/cc1101.cpp
+++ b/martin/door/src/cc1101.cpp
@@ -10,7 +10,7 @@
10 10
11static const uint8_t cc1101_init[] = { 11static const uint8_t cc1101_init[] = {
12 // IDX NAME RESET COMMENT 12 // IDX NAME RESET COMMENT
13 0x0D, // 00 IOCFG2 29 GDO2 as serial output 13 0x2E, // 00 IOCFG2 29 Tri-State
14 0x2E, // 01 IOCFG1 Tri-State 14 0x2E, // 01 IOCFG1 Tri-State
15 0x2E, // 02 IOCFG0 3F GDO0 for input 15 0x2E, // 02 IOCFG0 3F GDO0 for input
16 0x47, // 03 FIFOTHR RX filter bandwidth = 325 kHz, FIFOTHR = 0x47 16 0x47, // 03 FIFOTHR RX filter bandwidth = 325 kHz, FIFOTHR = 0x47
diff --git a/martin/door/src/main.cpp b/martin/door/src/main.cpp
index 5b1efad..6a6eed3 100644
--- a/martin/door/src/main.cpp
+++ b/martin/door/src/main.cpp
@@ -2,17 +2,35 @@
2#include <WiFiManager.h> 2#include <WiFiManager.h>
3#include <ESP8266mDNS.h> 3#include <ESP8266mDNS.h>
4#include <ESP8266WebServer.h> 4#include <ESP8266WebServer.h>
5#include <ESP8266httpUpdate.h>
5#include <ping.h> 6#include <ping.h>
6 7
7#include "cc1101.h" 8#include "cc1101.h"
8#include "hcs200.h" 9#include "hcs200.h"
10#include "secrets.h"
11
12// fw update
13// - increment number + build
14// - scp .pio/build/$ENV/firmware.bin manuel@mausz.at:public_html/coding/.firmware/martindoor.bin
15#define FIRMWARE_VERSION 1
16//#define FIRMWARE_URL ""
17
18#define _STR(s) #s
19#define STR(s) _STR(s)
9 20
10void http_door_open(); 21void http_door_open();
11void serial_door_open(); 22void serial_door_open();
23void checkFirmwareUpdate();
12 24
13ESP8266WebServer http_service(80); 25ESP8266WebServer http_service(80);
26
27#define RADIO_CC1101
14CC1101 radio(D0 /* GDO0 */, D2 /* GDO2 */, D8 /* SS */); 28CC1101 radio(D0 /* GDO0 */, D2 /* GDO2 */, D8 /* SS */);
15 29
30#ifndef RADIO_CC1101
31#define PIN_RADIO_OUT D2
32#endif
33
16#define PIN_HCS200_ENABLE D3 34#define PIN_HCS200_ENABLE D3
17#define PIN_HCS200_DATA D1 35#define PIN_HCS200_DATA D1
18//#define HCS200_TEST 36//#define HCS200_TEST
@@ -40,6 +58,11 @@ void setup()
40 (void)radio.idle(); 58 (void)radio.idle();
41 } 59 }
42 60
61#ifndef RADIO_CC1101
62 pinMode(PIN_RADIO_OUT, OUTPUT);
63 digitalWrite(PIN_RADIO_OUT, LOW);
64#endif
65
43 WiFiManager wifiManager; 66 WiFiManager wifiManager;
44 wifiManager.setConfigPortalTimeout(600); 67 wifiManager.setConfigPortalTimeout(600);
45 Serial.printf_P(PSTR("Setting up WiFi\n")); 68 Serial.printf_P(PSTR("Setting up WiFi\n"));
@@ -51,6 +74,10 @@ void setup()
51 ESP.restart(); 74 ESP.restart();
52 } 75 }
53 76
77 yield();
78 checkFirmwareUpdate();
79 yield();
80
54 if (!MDNS.begin("door")) 81 if (!MDNS.begin("door"))
55 Serial.println("Error setting up MDNS responder!"); 82 Serial.println("Error setting up MDNS responder!");
56 MDNS.addService("http", "tcp", 80); 83 MDNS.addService("http", "tcp", 80);
@@ -124,6 +151,7 @@ void http_door_open()
124 Serial.print("Got keycode: "); 151 Serial.print("Got keycode: ");
125 keycode.print(Serial); 152 keycode.print(Serial);
126 153
154#ifdef RADIO_CC1101
127 if (http_service.hasArg("power")) 155 if (http_service.hasArg("power"))
128 { 156 {
129 static const uint8_t patable_power_n30[8] = { 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 157 static const uint8_t patable_power_n30[8] = { 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
@@ -163,6 +191,7 @@ void http_door_open()
163 http_service.send_P(500, PSTR("text/plain"), PSTR("Unable to enable radio transmit mode")); 191 http_service.send_P(500, PSTR("text/plain"), PSTR("Unable to enable radio transmit mode"));
164 return; 192 return;
165 } 193 }
194#endif
166 195
167 if (http_service.hasArg("button1")) 196 if (http_service.hasArg("button1"))
168 keycode.buttons = HCS200_Keycode::BM_S0; 197 keycode.buttons = HCS200_Keycode::BM_S0;
@@ -176,7 +205,13 @@ void http_door_open()
176 unsigned long count = constrain(http_service.arg("count").toInt(), 2, 400); 205 unsigned long count = constrain(http_service.arg("count").toInt(), 2, 400);
177 for (unsigned int i = 0; i < count; i++) 206 for (unsigned int i = 0; i < count; i++)
178 { 207 {
179 keycode.send([&](int value) { radio.setGDO0(value); }); 208 keycode.send([&](int value) {
209#ifdef RADIO_CC1101
210 radio.setGDO0(value);
211#else
212 digitalWrite(PIN_RADIO_OUT, value);
213#endif
214 });
180 yield(); 215 yield();
181 } 216 }
182 217
@@ -205,20 +240,57 @@ void serial_door_open(Print &stream)
205 stream.print("Got keycode: "); 240 stream.print("Got keycode: ");
206 keycode.print(stream); 241 keycode.print(stream);
207 242
243#ifdef RADIO_CC1101
208 if (!radio.transmit()) 244 if (!radio.transmit())
209 { 245 {
210 stream.println("Unable to enable radio transmit mode"); 246 stream.println("Unable to enable radio transmit mode");
211 return; 247 return;
212 } 248 }
249#endif
213 250
214 /* send twice */ 251 /* send twice */
215 keycode.send([&](int value) { radio.setGDO0(value); }); 252 for (unsigned int i = 0; i < 1; i++) {
216 keycode.send([&](int value) { radio.setGDO0(value); }); 253 keycode.send([&](int value) {
254 #ifdef RADIO_CC1101
255 radio.setGDO0(value);
256 #else
257 digitalWrite(PIN_RADIO_OUT, value);
258 #endif
259 });
260 }
217 261
218 (void)radio.idle(); 262 (void)radio.idle();
219 stream.println("Sending done"); 263 stream.println("Sending done");
220} 264}
221 265
266void checkFirmwareUpdate()
267{
268 BearSSL::WiFiClientSecure update_client;
269 update_client.setInsecure();
270
271 ESPhttpUpdate.setLedPin(LED_BUILTIN, HIGH);
272 ESPhttpUpdate.rebootOnUpdate(true);
273 t_httpUpdate_return ret = ESPhttpUpdate.update(update_client, FIRMWARE_URL, STR(FIRMWARE_VERSION));
274 switch(ret)
275 {
276 case HTTP_UPDATE_FAILED:
277 {
278 Serial.printf_P(PSTR("HTTP_UPDATE_FAILED Error (%d): %s\n"),
279 ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
280 String tmp = String("fwupdate error: ") + ESPhttpUpdate.getLastErrorString();
281 }
282 break;
283
284 case HTTP_UPDATE_NO_UPDATES:
285 Serial.printf_P(PSTR("HTTP_UPDATE_NO_UPDATES\n"));
286 break;
287
288 case HTTP_UPDATE_OK:
289 Serial.printf_P(PSTR("HTTP_UPDATE_OK\n"));
290 break;
291 }
292}
293
222void loop() 294void loop()
223{ 295{
224 MDNS.update(); 296 MDNS.update();
@@ -232,8 +304,9 @@ void loop()
232 304
233 if (Serial.available() > 0) 305 if (Serial.available() > 0)
234 { 306 {
235 while (Serial.read() >= 0) {} 307 String command = Serial.readStringUntil('\n');
236 //serial_door_open(Serial); 308 if (command == "door" || command == "door\r")
309 serial_door_open(Serial);
237 } 310 }
238 311
239 delay(1); 312 delay(1);