добавляем парсер данных 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

@@ -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;
}

View File

@@ -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 <MySensors.h>
// 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 {

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);