mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 11:49:51 +03:00
modbus serial parameters in config
This commit is contained in:
@@ -452,7 +452,7 @@ void Input::dht22Poll() {
|
||||
setNextPollTime(millis() + DHT_POLL_DELAY_DEFAULT / 3);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
bool Input::executeCommand(aJsonObject* cmd, int8_t toggle, char* defCmd)
|
||||
{
|
||||
if (!cmd) return false;
|
||||
@@ -500,7 +500,8 @@ if (emit) debugSerial << emit->valuestring<< F(" -> ")<<emitCommand<<endl;
|
||||
|
||||
|
||||
if (emit && emitCommand && emit->type == aJson_String) {
|
||||
/*
|
||||
|
||||
// *
|
||||
TODO implement
|
||||
#ifdef WITH_DOMOTICZ
|
||||
if (getIdxField())
|
||||
@@ -511,7 +512,7 @@ TODO implement
|
||||
getIdxField());
|
||||
} else
|
||||
#endif
|
||||
*/
|
||||
* //
|
||||
|
||||
{
|
||||
char addrstr[MQTT_TOPIC_LENGTH];
|
||||
@@ -535,6 +536,7 @@ return false;
|
||||
} //switch type
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
// TODO Polling via timed interrupt with CHECK_INTERRUPT cause
|
||||
bool Input::changeState(uint8_t newState, short cause)
|
||||
|
||||
@@ -181,5 +181,5 @@ protected:
|
||||
|
||||
char* getIdxField();
|
||||
bool changeState(uint8_t newState, short cause);
|
||||
bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
|
||||
//bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
|
||||
};
|
||||
|
||||
@@ -14,23 +14,6 @@ extern ModbusMaster node;
|
||||
extern short modbusBusy;
|
||||
extern void modbusIdle(void) ;
|
||||
|
||||
/*
|
||||
const char float_P[] PROGMEM = "i16";
|
||||
const char hsv_P[] PROGMEM = "i32";
|
||||
const char int_P[] PROGMEM = "u16";
|
||||
const char enum_P[] PROGMEM = "u32";
|
||||
const char format_P[] PROGMEM = "i8h";
|
||||
const char true_P[] PROGMEM = "i8l";
|
||||
const char false_P[] PROGMEM = "false";
|
||||
|
||||
const char 8E1_P[] PROGMEM = "8E1";
|
||||
const char 8N1_P[] PROGMEM = "8N1";
|
||||
const char int_P[] PROGMEM = "8O1";
|
||||
const char enum_P[] PROGMEM = "u32";
|
||||
const char format_P[] PROGMEM = "$i8h";
|
||||
const char true_P[] PROGMEM = "i8l";
|
||||
const char false_P[] PROGMEM = "false";
|
||||
*/
|
||||
struct reg_t
|
||||
{
|
||||
const char verb[4];
|
||||
@@ -40,7 +23,7 @@ struct reg_t
|
||||
struct serial_t
|
||||
{
|
||||
const char verb[4];
|
||||
const int mode;
|
||||
const uint16_t mode;
|
||||
};
|
||||
|
||||
#define PAR_I16 1
|
||||
@@ -68,28 +51,35 @@ const reg_t regSize_P[] PROGMEM =
|
||||
|
||||
const serial_t serialModes_P[] PROGMEM =
|
||||
{
|
||||
{ "8E1", SERIAL_8E1 },
|
||||
{ "8N1", SERIAL_8N1 },
|
||||
{ "8O1", SERIAL_8O1 },
|
||||
{ "8M1", SERIAL_8M1 },
|
||||
{ "8S1", SERIAL_8S1 }
|
||||
{ "8E1", SERIAL_8E1},//(uint16_t) US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT | UART_MR_PAR_EVEN },
|
||||
{ "8N1", SERIAL_8N1},
|
||||
{ "8E2", SERIAL_8E2},
|
||||
{ "8N2", SERIAL_8N2},
|
||||
{ "8O1", SERIAL_8O1},
|
||||
{ "8O2", SERIAL_8O2},
|
||||
{ "8M1", SERIAL_8M1},
|
||||
{ "8S1", SERIAL_8S1}
|
||||
} ;
|
||||
|
||||
#define serialModesNum sizeof(serialModes_P)/sizeof(serial_t)
|
||||
|
||||
int str2SerialParam(char * str)
|
||||
{
|
||||
for(uint8_t i=0; i<serialModesNum;i++)
|
||||
uint16_t str2SerialParam(char * str)
|
||||
{ debugSerial<<str<<F(" =>");
|
||||
for(uint8_t i=0; i<serialModesNum && str;i++)
|
||||
if (strcmp_P(str, serialModes_P[i].verb) == 0)
|
||||
return pgm_read_byte_near(serialModes_P[i].mode);
|
||||
return (int) SERIAL_8N1;
|
||||
}
|
||||
{
|
||||
|
||||
debugSerial<< i << F(" ") << pgm_read_word_near(&serialModes_P[i].mode)<< endl;
|
||||
return pgm_read_word_near(&serialModes_P[i].mode);
|
||||
}
|
||||
debugSerial<< F("Default serial mode N81 used");
|
||||
return static_cast<uint16_t> (SERIAL_8N1);
|
||||
}
|
||||
int str2regSize(char * str)
|
||||
{
|
||||
for(uint8_t i=0; i<regSizeNum;i++)
|
||||
for(uint8_t i=0; i<regSizeNum&& str;i++)
|
||||
if (strcmp_P(str, regSize_P[i].verb) == 0)
|
||||
return pgm_read_byte_near(regSize_P[i].id);
|
||||
return pgm_read_byte_near(®Size_P[i].id);
|
||||
return (int) PAR_I16;
|
||||
}
|
||||
|
||||
@@ -109,7 +99,7 @@ bool out_Modbus::getConfig()
|
||||
}
|
||||
|
||||
aJsonObject * serialParamObj=aJson.getObjectItem(templateObj, "serial");
|
||||
if (serialParamObj && serialParamObj->type == aJson_String && serialParamObj->valuestring) store->serialParam = (UARTClass::UARTModes) str2SerialParam(serialParamObj->valuestring);
|
||||
if (serialParamObj && serialParamObj->type == aJson_String) store->serialParam = str2SerialParam(serialParamObj->valuestring);
|
||||
else store->serialParam = SERIAL_8N1;
|
||||
|
||||
aJsonObject * baudObj=aJson.getObjectItem(templateObj, "baud");
|
||||
@@ -131,7 +121,6 @@ bool out_Modbus::getConfig()
|
||||
|
||||
int out_Modbus::Setup()
|
||||
{
|
||||
Serial.println("Modbus Init");
|
||||
if (!store) store= (mbPersistent *)item->setPersistent(new mbPersistent);
|
||||
if (!store) return 0;
|
||||
store->timestamp=millis();
|
||||
@@ -139,7 +128,7 @@ if (getConfig())
|
||||
{
|
||||
//item->clearFlag(ACTION_NEEDED);
|
||||
//item->clearFlag(ACTION_IN_PROCESS);
|
||||
debugSerial<<F("Modbus config loaded")<<endl;
|
||||
debugSerial<<F("Modbus config loaded ")<< item->itemArr->name<<endl;
|
||||
store->driverStatus = CST_INITIALIZED;
|
||||
return 1;
|
||||
}
|
||||
@@ -158,8 +147,6 @@ Serial.println("Modbus De-Init");
|
||||
delete store;
|
||||
item->setPersistent(NULL);
|
||||
store = NULL;
|
||||
|
||||
//driverStatus = CST_UNKNOWN;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -226,7 +213,9 @@ if (store->pollingRegisters && !modbusBusy && (Status() == CST_INITIALIZED) && i
|
||||
{
|
||||
debugSerial<<F("Poll ")<< item->itemArr->name << endl;
|
||||
modbusBusy=1;
|
||||
modbusSerial.begin(store->baud, store->serialParam);
|
||||
//store->serialParam=(USARTClass::USARTModes) SERIAL_8N1;
|
||||
modbusSerial.begin(store->baud, static_cast <USARTClass::USARTModes> (store->serialParam));
|
||||
debugSerial<< store->baud << F("---")<< store->serialParam<<endl;
|
||||
node.begin(item->getArg(0), modbusSerial);
|
||||
|
||||
aJsonObject * reg = store->pollingRegisters->child;
|
||||
|
||||
@@ -11,7 +11,7 @@ public:
|
||||
// int addr
|
||||
int8_t driverStatus;
|
||||
int baud;
|
||||
UARTClass::UARTModes serialParam;
|
||||
uint16_t serialParam;
|
||||
uint16_t pollingInterval;
|
||||
uint32_t timestamp;
|
||||
aJsonObject * pollingRegisters;
|
||||
|
||||
@@ -23,6 +23,14 @@ e-mail anklimov@gmail.com
|
||||
#include "stdarg.h"
|
||||
#include <Wire.h>
|
||||
|
||||
#include "item.h"
|
||||
#include <PubSubClient.h>
|
||||
extern int8_t configLocked;
|
||||
extern int8_t ethernetIdleCount;
|
||||
extern PubSubClient mqttClient;
|
||||
|
||||
|
||||
|
||||
#if defined(__SAM3X8E__) || defined(ARDUINO_ARCH_STM32)
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
@@ -512,5 +520,100 @@ bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t m
|
||||
}
|
||||
|
||||
|
||||
bool executeCommand(aJsonObject* cmd, int8_t toggle, char* defCmd)
|
||||
{
|
||||
switch (cmd->type)
|
||||
{
|
||||
case aJson_String: //legacy - no action
|
||||
break;
|
||||
case aJson_Array: //array - recursive iterate
|
||||
{
|
||||
configLocked++;
|
||||
aJsonObject * command = cmd->child;
|
||||
while (command)
|
||||
{
|
||||
executeCommand(command,toggle,defCmd);
|
||||
command = command->next;
|
||||
}
|
||||
configLocked--;
|
||||
}
|
||||
break;
|
||||
case aJson_Object:
|
||||
{
|
||||
aJsonObject *item = aJson.getObjectItem(cmd, "item");
|
||||
aJsonObject *icmd = aJson.getObjectItem(cmd, "icmd");
|
||||
|
||||
aJsonObject *emit = aJson.getObjectItem(cmd, "emit");
|
||||
aJsonObject *ecmd = aJson.getObjectItem(cmd, "ecmd");
|
||||
|
||||
|
||||
aJsonObject *irev = NULL;
|
||||
aJsonObject *erev = NULL;
|
||||
|
||||
if (toggle>0){
|
||||
aJsonObject *irev = aJson.getObjectItem(cmd, "irev");
|
||||
aJsonObject *erev = aJson.getObjectItem(cmd, "erev");
|
||||
}
|
||||
|
||||
char * itemCommand;
|
||||
if (irev && toggle && irev->type == aJson_String) itemCommand = irev->valuestring;
|
||||
else if(icmd && icmd->type == aJson_String) itemCommand = icmd->valuestring;
|
||||
else itemCommand = defCmd;
|
||||
|
||||
char * emitCommand;
|
||||
if (erev && toggle && erev->type == aJson_String) emitCommand = erev->valuestring;
|
||||
else if(ecmd && ecmd->type == aJson_String) emitCommand = ecmd->valuestring;
|
||||
else emitCommand = defCmd;
|
||||
|
||||
//debugSerial << F("IN:") << (pin) << F(" : ") <<endl;
|
||||
if (item) debugSerial << item->valuestring<< F(" -> ")<<itemCommand<<endl;
|
||||
if (emit) debugSerial << emit->valuestring<< F(" -> ")<<emitCommand<<endl;
|
||||
|
||||
|
||||
|
||||
|
||||
if (emit && emitCommand && emit->type == aJson_String) {
|
||||
/*
|
||||
TODO implement
|
||||
#ifdef WITH_DOMOTICZ
|
||||
if (getIdxField())
|
||||
{ (newValue) ? publishDataToDomoticz(0, emit, "{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"On\"}",
|
||||
: publishDataToDomoticz(0,emit,"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",getIdxField()); getIdxField())
|
||||
: publishDataToDomoticz(0, emit,
|
||||
"{\"command\":\"switchlight\",\"idx\":%s,\"switchcmd\":\"Off\"}",
|
||||
getIdxField());
|
||||
} else
|
||||
#endif
|
||||
*/
|
||||
|
||||
{
|
||||
char addrstr[MQTT_TOPIC_LENGTH];
|
||||
strncpy(addrstr,emit->valuestring,sizeof(addrstr));
|
||||
if (mqttClient.connected() && !ethernetIdleCount)
|
||||
{
|
||||
if (!strchr(addrstr,'/')) setTopic(addrstr,sizeof(addrstr),T_OUT,emit->valuestring);
|
||||
mqttClient.publish(addrstr, emitCommand , true);
|
||||
}
|
||||
}
|
||||
} // emit
|
||||
if (item && itemCommand && item->type == aJson_String) {
|
||||
//debugSerial <<F("Controlled item:")<< item->valuestring <<endl;
|
||||
Item it(item->valuestring);
|
||||
if (it.isValid()) it.Ctrl(itemCommand, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
} //switch type
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#pragma message(VAR_NAME_VALUE(debugSerial))
|
||||
#pragma message(VAR_NAME_VALUE(SERIAL_BAUD))
|
||||
|
||||
@@ -60,3 +60,4 @@ void printUlongValueToStr(char *valstr, unsigned long value);
|
||||
void scan_i2c_bus();
|
||||
void softRebootFunc();
|
||||
bool isTimeOver(uint32_t timestamp, uint32_t currTime, uint32_t time, uint32_t modulo = 0xFFFFFFFF);
|
||||
bool executeCommand(aJsonObject* cmd, int8_t toggle = -1, char* defCmd = NULL);
|
||||
|
||||
Reference in New Issue
Block a user