From 8b2cbecae32ba463cc463b2148abedfa48b8423f Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sat, 2 Jan 2021 22:48:09 +0100 Subject: [PATCH] switch to optiboot and add support for multiple commands --- gateway/README.md | 9 +++++++++ gateway/platformio.ini | 6 ++++-- gateway/src/gateway.cpp | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 gateway/README.md diff --git a/gateway/README.md b/gateway/README.md new file mode 100644 index 0000000..e8e94c9 --- /dev/null +++ b/gateway/README.md @@ -0,0 +1,9 @@ +# A gateway for 433 MHz devices +It uses [rc_switch](https://github.com/sui77/rc-switch) library for controlling wall sockets. + +It also supports receiving commands from the same protocol. + +It acts as a serial gateway and it is connected to a Raspberry Pi running Home assistant. + +It uses an Arduino Pro Mini 5v 16 Mhz. +The original bootloader has been replaced with Optiboot using this tutorial: https://andreasrohner.at/posts/Electronics/How-to-make-the-Watchdog-Timer-work-on-an-Arduino-Pro-Mini-by-replacing-the-bootloader/ \ No newline at end of file diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 5b594f5..39885e5 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -8,12 +8,14 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:pro16MHzatmega328] +[env:uno] platform = atmelavr -board = pro16MHzatmega328 +board = uno +board_build.mcu = atmega328p framework = arduino lib_deps = sui77/rc-switch@^2.6.3 bblanchon/ArduinoJson@6.16.1 adafruit/Adafruit Unified Sensor@^1.1.4 adafruit/DHT sensor library@1.3.10 +upload_port = /dev/ttyUSB0 diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index f0bdb6d..b79e9cc 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -22,8 +22,9 @@ void readCommand(); void readDht(JsonDocument& jsonDoc); void setup() { + digitalWrite(RESET_PIN, HIGH); pinMode(LED_BUILTIN, OUTPUT); - pinMode(RESET_PIN, INPUT_PULLUP); + pinMode(RESET_PIN, OUTPUT); mySwitch.enableReceive(digitalPinToInterrupt(RECEIVE_PIN)); mySwitch.enableTransmit(SEND_PIN); @@ -70,16 +71,12 @@ void readRcSwitch(JsonDocument& jsonDoc) { } } -void readCommand() { - if (Serial.available() > 0) { - String cmd = Serial.readStringUntil('\n'); - if (cmd == "reset") { - pinMode(RESET_PIN, OUTPUT); - digitalWrite(RESET_PIN, LOW); - } - StaticJsonDocument<256> jsonDoc; - DeserializationError err = deserializeJson(jsonDoc, cmd); - if (err == DeserializationError::Ok) { +void runJsonCommand(String cmd) { + StaticJsonDocument<256> jsonArray; + DeserializationError err = deserializeJson(jsonArray, cmd); + if (err == DeserializationError::Ok) { + JsonArray array = jsonArray.as(); + for(JsonVariant jsonDoc : array) { if (jsonDoc.containsKey("rcSwitch")) { JsonObject rcSwitch = jsonDoc["rcSwitch"]; unsigned int protocol = rcSwitch["protocol"]; @@ -95,14 +92,32 @@ void readCommand() { } else { mySwitch.setProtocol(protocol); mySwitch.send(rcSwitch["value"]); + // Serial.println((const char*)rcSwitch["value"]); } + } else { + Serial.println(err.c_str()); } - } else { - Serial.println(err.c_str()); } } } +void readCommand() { + if (Serial.available() > 0) { + String cmd = Serial.readStringUntil('\n'); + if (cmd == "reset") { + Serial.println("resetting..."); + delay(200); + digitalWrite(RESET_PIN, LOW); + } + if (cmd.endsWith(",")) { + cmd = cmd.substring(0, cmd.lastIndexOf(',')); + } + cmd = "[" + cmd + "]"; + // Serial.println(cmd); + runJsonCommand(cmd); + } +} + void readDht(JsonDocument& jsonDoc) { unsigned long currentTime = millis(); if ((currentTime - lastReadTime) > readInterval) {