From 8afb6fdb1248302773c2573085160f48395468de Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <67171972+IoTManagerProject@users.noreply.github.com> Date: Thu, 1 Dec 2022 17:01:22 +0100 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5=D1=80=20=D0=B4?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20mysensors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/MyGatewayTransportSerial.cpp | 84 +++++++++---------- src/modules/exec/MySensors/MySensorsGate.cpp | 75 ++++++++++------- src/modules/exec/MySensors/MySensorsGate.h | 35 ++++++++ 3 files changed, 118 insertions(+), 76 deletions(-) create mode 100644 src/modules/exec/MySensors/MySensorsGate.h diff --git a/lib/MySensors/core/MyGatewayTransportSerial.cpp b/lib/MySensors/core/MyGatewayTransportSerial.cpp index dec57b51..d59500fe 100644 --- a/lib/MySensors/core/MyGatewayTransportSerial.cpp +++ b/lib/MySensors/core/MyGatewayTransportSerial.cpp @@ -26,57 +26,53 @@ // global variables extern MyMessage _msgTmp; -char _serialInputString[MY_GATEWAY_MAX_RECEIVE_LENGTH]; // A buffer for incoming commands from serial interface +char _serialInputString[MY_GATEWAY_MAX_RECEIVE_LENGTH]; // A buffer for incoming commands from serial interface uint8_t _serialInputPos; MyMessage _serialMsg; -bool gatewayTransportSend(MyMessage &message) -{ - setIndication(INDICATION_GW_TX); - MY_SERIALDEVICE.print(protocolMyMessage2Serial(message)); - // Serial print is always successful - return true; +bool gatewayTransportSend(MyMessage &message) { + setIndication(INDICATION_GW_TX); + // MY_SERIALDEVICE.print(protocolMyMessage2Serial(message)); + // Serial print is always successful + return true; } -bool gatewayTransportInit(void) -{ - (void)gatewayTransportSend(buildGw(_msgTmp, I_GATEWAY_READY).set(MSG_GW_STARTUP_COMPLETE)); - // Send presentation of locally attached sensors (and node if applicable) - presentNode(); - return true; +bool gatewayTransportInit(void) { + (void)gatewayTransportSend(buildGw(_msgTmp, I_GATEWAY_READY).set(MSG_GW_STARTUP_COMPLETE)); + // Send presentation of locally attached sensors (and node if applicable) + presentNode(); + return true; } -bool gatewayTransportAvailable(void) -{ - while (MY_SERIALDEVICE.available()) { - // get the new byte: - const char inChar = (char)MY_SERIALDEVICE.read(); - // if the incoming character is a newline, set a flag - // so the main loop can do something about it: - if (_serialInputPos < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) { - if (inChar == '\n') { - _serialInputString[_serialInputPos] = 0; - const bool ok = protocolSerial2MyMessage(_serialMsg, _serialInputString); - if (ok) { - setIndication(INDICATION_GW_RX); - } - _serialInputPos = 0; - return ok; - } else { - // add it to the inputString: - _serialInputString[_serialInputPos] = inChar; - _serialInputPos++; - } - } else { - // Incoming message too long. Throw away - _serialInputPos = 0; - } - } - return false; +bool gatewayTransportAvailable(void) { + while (MY_SERIALDEVICE.available()) { + // get the new byte: + const char inChar = (char)MY_SERIALDEVICE.read(); + // if the incoming character is a newline, set a flag + // so the main loop can do something about it: + if (_serialInputPos < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) { + if (inChar == '\n') { + _serialInputString[_serialInputPos] = 0; + const bool ok = protocolSerial2MyMessage(_serialMsg, _serialInputString); + if (ok) { + setIndication(INDICATION_GW_RX); + } + _serialInputPos = 0; + return ok; + } else { + // add it to the inputString: + _serialInputString[_serialInputPos] = inChar; + _serialInputPos++; + } + } else { + // Incoming message too long. Throw away + _serialInputPos = 0; + } + } + return false; } -MyMessage & gatewayTransportReceive(void) -{ - // Return the last parsed message - return _serialMsg; +MyMessage &gatewayTransportReceive(void) { + // Return the last parsed message + return _serialMsg; } diff --git a/src/modules/exec/MySensors/MySensorsGate.cpp b/src/modules/exec/MySensors/MySensorsGate.cpp index 193cead0..efc00cf3 100644 --- a/src/modules/exec/MySensors/MySensorsGate.cpp +++ b/src/modules/exec/MySensors/MySensorsGate.cpp @@ -1,40 +1,51 @@ #include "Global.h" #include "classes/IoTItem.h" #include "Arduino.h" +#include "MySensorsGate.h" -// Enable debug prints to serial monitor -#define MY_DEBUG - -//#define MY_RF24_CE_PIN 26 // Двигать пин CE на D4 чтобы освободить I2C -//#define MY_RF24_CS_PIN 9 - -// Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h - -#define MY_BAUD_RATE 115200 - -// Enables and select radio type (if attached) -#define MY_RADIO_RF24 -//#define MY_RADIO_RFM69 -//#define MY_RADIO_RFM95 - -//#define MY_RF24_DATARATE RF24_1MBPS // Для платы KeyWish скорость 1 мегабит - -// How many clients should be able to connect to this gateway (default 1) -#define MY_GATEWAY_MAX_CLIENTS 10 - -// Set LOW transmit power level as default, if you have an amplified NRF-module and -// power your radio separately with a good regulator you can turn up PA level. -#define MY_RF24_PA_LEVEL RF24_PA_LOW - -// Enable serial gateway -#define MY_GATEWAY_SERIAL - -#define CHILD_ID 1 - -#include - +// callback библиотеки mysensors void receive(const MyMessage& message) { - Serial.println("receive"); + String inMsg = String(message.getSender()) + "," + // node-id + String(message.getSensor()) + "," + // child-sensor-id + String(message.getType()) + "," + // type of var + String(message.getCommand()) + "," + // command + parseToString(message) + ";"; // value + + Serial.println("=>" + inMsg); + + mysensorBuf += inMsg; +} + +String parseToString(const MyMessage& message) { + String value = "error"; + switch (message.getPayloadType()) { + case 0: // Payload type is string + value = message.getString(); + return value; + case 1: // Payload type is byte + value = String(message.getByte()); + return value; + case 2: // Payload type is INT16 + value = String(message.getInt()); + return value; + case 3: // Payload type is UINT16 + value = String(message.getUInt()); + return value; + case 4: // Payload type is INT32 + value = String(message.getInt()); + return value; + case 5: // Payload type is UINT32 + value = String(message.getUInt()); + return value; + case 6: // Payload type is binary + value = String(message.getBool()); + return value; + case 7: // Payload type is float32 + value = String(message.getFloat()); + return value; + default: + return value; + } } class MySensorsGate : public IoTItem { diff --git a/src/modules/exec/MySensors/MySensorsGate.h b/src/modules/exec/MySensors/MySensorsGate.h new file mode 100644 index 00000000..8c1a2ae2 --- /dev/null +++ b/src/modules/exec/MySensors/MySensorsGate.h @@ -0,0 +1,35 @@ +#pragma once + +// Enable debug prints to serial monitor +//#define MY_DEBUG + +//#define MY_RF24_CE_PIN 26 // Двигать пин CE на D4 чтобы освободить I2C +//#define MY_RF24_CS_PIN 9 + +// Use a bit lower baudrate for serial prints on ESP8266 than default in MyConfig.h + +#define MY_BAUD_RATE 115200 + +// Enables and select radio type (if attached) +#define MY_RADIO_RF24 +//#define MY_RADIO_RFM69 +//#define MY_RADIO_RFM95 + +//#define MY_RF24_DATARATE RF24_1MBPS // Для платы KeyWish скорость 1 мегабит + +// How many clients should be able to connect to this gateway (default 1) +#define MY_GATEWAY_MAX_CLIENTS 10 + +// Set LOW transmit power level as default, if you have an amplified NRF-module and +// power your radio separately with a good regulator you can turn up PA level. +#define MY_RF24_PA_LEVEL RF24_PA_LOW + +// используем гейт в режиме serial хотя нам этот режим не нужен, поэтому в библиотеки отключаем MY_SERIALDEVICE.print +// в файле MyGatewayTransportSerial.cpp в строчке 35 +#define MY_GATEWAY_SERIAL + +#define CHILD_ID 1 + +#include + +extern String parseToString(const MyMessage& message); \ No newline at end of file