добавляем парсер данных mysensors

This commit is contained in:
Dmitry Borisenko
2022-12-01 17:01:22 +01:00
parent 35f8424d56
commit 8afb6fdb12
3 changed files with 118 additions and 76 deletions

View File

@@ -30,24 +30,21 @@ char _serialInputString[MY_GATEWAY_MAX_RECEIVE_LENGTH]; // A buffer for incom
uint8_t _serialInputPos; uint8_t _serialInputPos;
MyMessage _serialMsg; MyMessage _serialMsg;
bool gatewayTransportSend(MyMessage &message) bool gatewayTransportSend(MyMessage &message) {
{
setIndication(INDICATION_GW_TX); setIndication(INDICATION_GW_TX);
MY_SERIALDEVICE.print(protocolMyMessage2Serial(message)); // MY_SERIALDEVICE.print(protocolMyMessage2Serial(message));
// Serial print is always successful // Serial print is always successful
return true; return true;
} }
bool gatewayTransportInit(void) bool gatewayTransportInit(void) {
{
(void)gatewayTransportSend(buildGw(_msgTmp, I_GATEWAY_READY).set(MSG_GW_STARTUP_COMPLETE)); (void)gatewayTransportSend(buildGw(_msgTmp, I_GATEWAY_READY).set(MSG_GW_STARTUP_COMPLETE));
// Send presentation of locally attached sensors (and node if applicable) // Send presentation of locally attached sensors (and node if applicable)
presentNode(); presentNode();
return true; return true;
} }
bool gatewayTransportAvailable(void) bool gatewayTransportAvailable(void) {
{
while (MY_SERIALDEVICE.available()) { while (MY_SERIALDEVICE.available()) {
// get the new byte: // get the new byte:
const char inChar = (char)MY_SERIALDEVICE.read(); const char inChar = (char)MY_SERIALDEVICE.read();
@@ -75,8 +72,7 @@ bool gatewayTransportAvailable(void)
return false; return false;
} }
MyMessage & gatewayTransportReceive(void) MyMessage &gatewayTransportReceive(void) {
{
// Return the last parsed message // Return the last parsed message
return _serialMsg; return _serialMsg;
} }

View File

@@ -1,40 +1,51 @@
#include "Global.h" #include "Global.h"
#include "classes/IoTItem.h" #include "classes/IoTItem.h"
#include "Arduino.h" #include "Arduino.h"
#include "MySensorsGate.h"
// Enable debug prints to serial monitor // callback библиотеки mysensors
#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 <MySensors.h>
void receive(const MyMessage& message) { 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 { class MySensorsGate : public IoTItem {

View File

@@ -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 <MySensors.h>
extern String parseToString(const MyMessage& message);