1w,MBUSv1,inputs resp fixes. update libs!

This commit is contained in:
2023-11-07 19:37:19 +03:00
parent 6d4f76c642
commit ca457a2a05
3 changed files with 70 additions and 126 deletions

View File

@@ -1936,7 +1936,7 @@ return itemType;
// Setup FLAG_SEND_RETRY flag to repeat unsucsessfull modbus tranzaction after release line // Setup FLAG_SEND_RETRY flag to repeat unsucsessfull modbus tranzaction after release line
void Item::mb_fail(int result) { void Item::mb_fail(int result) {
debugSerial<<F("Modbus op failed:")<<_HEX(result)<<endl; debugSerial<<F("Modbus op ")<<itemArr->name<<F(" failed:")<<_HEX(result)<<endl;
setFlag(FLAG_SEND_RETRY); setFlag(FLAG_SEND_RETRY);
// isPendedModbusWrites=true; // isPendedModbusWrites=true;
} }
@@ -2132,7 +2132,7 @@ int Item::VacomSetFan(itemCmd st) {
} }
uint8_t result; uint8_t result;
int addr = getArg(); int addr = getArg();
debugSerial<<F("VC#")<<addr<<F("=")<<val<<endl; debugSerial<<F("MB: VC#")<<addr<<F("=")<<val<<endl;
if (modbusBusy) { if (modbusBusy) {
// setCmd(cmd); // setCmd(cmd);
// setVal(val); // setVal(val);
@@ -2152,7 +2152,7 @@ int Item::VacomSetFan(itemCmd st) {
//node.writeSingleRegister(2001-1,1); //node.writeSingleRegister(2001-1,1);
} else result=node.writeSingleRegister(2001 - 1, 0); } else result=node.writeSingleRegister(2001 - 1, 0);
delay(100); delay(100);
if (result == node.ku8MBSuccess) debugSerial << F("MB ok")<<endl; if (result == node.ku8MBSuccess) debugSerial << F("MB: ok")<<endl;
result = node.writeSingleRegister(2003 - 1, val * 100); result = node.writeSingleRegister(2003 - 1, val * 100);
modbusBusy = 0; modbusBusy = 0;
//resumeModbus(); //resumeModbus();
@@ -2179,7 +2179,7 @@ int addr;
if (it.isValid() && it.itemType == CH_VC) addr=it.getArg(); if (it.isValid() && it.itemType == CH_VC) addr=it.getArg();
else return 0; else return 0;
debugSerial<<F("VC_heat#")<<addr<<F("=")<<val<<F(" cmd=")<<cmd<<endl; debugSerial<<F("MB: VC_heat#")<<addr<<F("=")<<val<<F(" cmd=")<<cmd<<endl;
if (modbusBusy) { if (modbusBusy) {
//setCmd(cmd); //setCmd(cmd);
//setVal(val); //setVal(val);
@@ -2219,7 +2219,7 @@ int Item::modbusDimmerSet(int addr, uint16_t _reg, int _regType, int _mask, uint
if (_regType != MODBUS_COIL_REG_TYPE || _regType != MODBUS_HOLDING_REG_TYPE) { if (_regType != MODBUS_COIL_REG_TYPE || _regType != MODBUS_HOLDING_REG_TYPE) {
} }
debugSerial<<F("MB: Addr:")<<addr<<F(" Reg:0x")<<_HEX(_reg)<<F(" T:")<<_regType<<F(" Val:0x")<<_HEX(value)<<endl;
if (modbusBusy) { if (modbusBusy) {
mb_fail(); mb_fail();
return 0; return 0;
@@ -2251,7 +2251,7 @@ int Item::modbusDimmerSet(int addr, uint16_t _reg, int _regType, int _mask, uint
} }
debugSerial<<F("Addr:")<<addr<<F(" Reg:0x")<<_HEX(_reg)<<F(" T:")<<_regType<<F(" Val:0x")<<_HEX(value)<<endl;
switch (_regType) { switch (_regType) {
case MODBUS_HOLDING_REG_TYPE: case MODBUS_HOLDING_REG_TYPE:
result = node.writeSingleRegister(_reg, value); result = node.writeSingleRegister(_reg, value);
@@ -2260,7 +2260,7 @@ int Item::modbusDimmerSet(int addr, uint16_t _reg, int _regType, int _mask, uint
result = node.writeSingleCoil(_reg, value); result = node.writeSingleCoil(_reg, value);
break; break;
default: default:
debugSerial<<F("Not supported reg type\n"); debugSerial<<F("MB: Not supported reg type\n");
} }
modbusBusy = 0; modbusBusy = 0;
//resumeModbus(); //resumeModbus();
@@ -2297,12 +2297,13 @@ int Item::checkFM() {
modbusSerial.begin(MODBUS_FM_BAUD, MODBUS_FM_PARAM); modbusSerial.begin(MODBUS_FM_BAUD, MODBUS_FM_PARAM);
node.begin(getArg(), modbusSerial); node.begin(getArg(), modbusSerial);
debugSerial << F("MB: polling FM ") << itemArr->name<< endl;
delay(50);
result = node.readHoldingRegisters(2101 - 1, 10); result = node.readHoldingRegisters(2101 - 1, 10);
// do something with data if read is successful // do something with data if read is successful
if (result == node.ku8MBSuccess) { if (result == node.ku8MBSuccess) {
debugSerial<<F(" FM Val :"); debugSerial<<F("MB: FM Val :");
for (j = 0; j < 10; j++) { for (j = 0; j < 10; j++) {
data = node.getResponseBuffer(j); data = node.getResponseBuffer(j);
debugSerial<<_HEX(data)<<F("-"); debugSerial<<_HEX(data)<<F("-");
@@ -2330,10 +2331,12 @@ int Item::checkFM() {
} }
} }
} else } else
debugSerial << F("Modbus polling error=") << _HEX(result) << endl; debugSerial << F("MB: polling ") << itemArr->name<< F(" error=") << _HEX(result) << endl;
if (node.getResponseBuffer(0) & 8) //Active fault if (node.getResponseBuffer(0) & 8) //Active fault
{ {
debugSerial << F("MB: polling FM fault ") << itemArr->name<< endl;
delay(50);
result = node.readHoldingRegisters(2111 - 1, 1); result = node.readHoldingRegisters(2111 - 1, 1);
if (result == node.ku8MBSuccess) aJson.addNumberToObject(out, "flt", (long int) node.getResponseBuffer(0)); if (result == node.ku8MBSuccess) aJson.addNumberToObject(out, "flt", (long int) node.getResponseBuffer(0));
modbusBusy=0; modbusBusy=0;
@@ -2343,12 +2346,13 @@ int Item::checkFM() {
modbusBusy=1; modbusBusy=1;
} else aJson.addNumberToObject(out, "flt", (long int)0); } else aJson.addNumberToObject(out, "flt", (long int)0);
debugSerial << F("MB: polling PI ") << itemArr->name<< endl;
delay(50); delay(50);
result = node.readHoldingRegisters(20 - 1, 4); result = node.readHoldingRegisters(20 - 1, 4);
// do something with data if read is successful // do something with data if read is successful
if (result == node.ku8MBSuccess) { if (result == node.ku8MBSuccess) {
debugSerial << F(" PI Val :"); debugSerial << F("MB: PI Val :");
for (j = 0; j < 4; j++) { for (j = 0; j < 4; j++) {
data = node.getResponseBuffer(j); data = node.getResponseBuffer(j);
debugSerial << data << F("-"); debugSerial << data << F("-");
@@ -2371,7 +2375,8 @@ int Item::checkFM() {
Off(); //Shut down Off(); //Shut down
} }
} else } else
debugSerial << F("Modbus polling error=") << _HEX(result); debugSerial << F("MB: polling PI ") << itemArr->name<< F(" error=") << _HEX(result) << endl;
outch = aJson.print(out); outch = aJson.print(out);
if (mqttClient.connected() && !ethernetIdleCount) if (mqttClient.connected() && !ethernetIdleCount)
mqttClient.publish(addrstr, outch); mqttClient.publish(addrstr, outch);
@@ -2388,7 +2393,7 @@ int Item::checkModbusDimmer() {
short numpar = 0; short numpar = 0;
if ((itemArg->type != aJson_Array) || ((numpar = aJson.getArraySize(itemArg)) < 2)) { if ((itemArg->type != aJson_Array) || ((numpar = aJson.getArraySize(itemArg)) < 2)) {
debugSerial<<F("Illegal arguments\n"); debugSerial<<F("MB: Illegal arguments\n");
return -4; return -4;
} }
@@ -2415,7 +2420,7 @@ int Item::checkModbusDimmer() {
modbusSerial.begin(MODBUS_SERIAL_BAUD, MODBUS_SERIAL_PARAM); modbusSerial.begin(MODBUS_SERIAL_BAUD, MODBUS_SERIAL_PARAM);
node.begin(addr, modbusSerial); node.begin(addr, modbusSerial);
debugSerial << F("MB: polling dimmer ") << itemArr->name<< endl;
switch (_regType) { switch (_regType) {
case MODBUS_HOLDING_REG_TYPE: case MODBUS_HOLDING_REG_TYPE:
result = node.readHoldingRegisters(reg, 1); result = node.readHoldingRegisters(reg, 1);
@@ -2430,7 +2435,7 @@ int Item::checkModbusDimmer() {
result = node.readInputRegisters(reg, 1); result = node.readInputRegisters(reg, 1);
break; break;
default: default:
debugSerial<<F("Not supported reg type\n"); debugSerial<<F("MB: Not supported reg type\n");
} }
if (result == node.ku8MBSuccess) { if (result == node.ku8MBSuccess) {
@@ -2448,7 +2453,8 @@ int Item::checkModbusDimmer() {
pollingItem = items->child; pollingItem = items->child;
} }
} else } else
debugSerial << F("Modbus polling error=") << _HEX(result) << endl; debugSerial << F("MB: polling ") << itemArr->name<< F(" error=") << _HEX(result) << endl;
modbusBusy = 0; modbusBusy = 0;
//resumeModbus(); //resumeModbus();
@@ -2457,6 +2463,8 @@ return 1;
int Item::checkModbusDimmer(int data) { int Item::checkModbusDimmer(int data) {
if (getFlag(FLAG_SEND_RETRY)) return 0; //Active send transaction
short mask = getArg(2); short mask = getArg(2);
itemCmd st; itemCmd st;

View File

@@ -140,6 +140,7 @@ volatile unsigned long timerCount=0;
volatile int16_t timerNumber=-1; volatile int16_t timerNumber=-1;
volatile int8_t timerHandlerBusy=0; volatile int8_t timerHandlerBusy=0;
volatile uint32_t cryptoSalt=0; volatile uint32_t cryptoSalt=0;
//uint32_t timerCtr=0;
aJsonObject *pollingItem = NULL; aJsonObject *pollingItem = NULL;
@@ -1978,18 +1979,19 @@ void postTransmission() {
void TimerHandler(void) void TimerHandler(void)
{ {
timerHandlerBusy++;
interrupts();
timerCount=micros(); timerCount=micros();
if (configLoaded && !timerHandlerBusy) if (configLoaded && !timerHandlerBusy)
{ {
timerHandlerBusy++;
interrupts();
inputLoop(CHECK_INTERRUPT); inputLoop(CHECK_INTERRUPT);
#ifdef DMX_SMOOTH #ifdef DMX_SMOOTH
DMXOUT_propagate(); DMXOUT_propagate();
#endif #endif
timerHandlerBusy--;
} }
timerCount=micros()-timerCount; timerCount=micros()-timerCount;
timerHandlerBusy--;
} }
#if defined(__SAM3X8E__) && defined (TIMER_INT) #if defined(__SAM3X8E__) && defined (TIMER_INT)
@@ -2273,9 +2275,7 @@ while ((digitalRead(CONFIG_CLEAN_PIN)==LOW) && !needClean)
delay(20); delay(20);
//owReady = 0; //owReady = 0;
#ifdef _owire
setupOwIdle(&owIdle);
#endif
mqttClient.setCallback(mqttCallback); mqttClient.setCallback(mqttCallback);
@@ -2674,13 +2674,32 @@ if (initializedListeners) ipmodbusLoop();
} }
//static uint32_t tm=0;
void owIdle(void) { void owIdle(void) {
// timerCtr++;
#ifdef _artnet #ifdef _artnet
if (artnet && (lanStatus>=HAVE_IP_ADDRESS)) artnet->read(); if (artnet && (lanStatus>=HAVE_IP_ADDRESS)) artnet->read();
#endif #endif
wdt_res(); wdt_res();
inputLoop(CHECK_INTERRUPT); inputLoop(CHECK_INPUT);
//inputLoop(CHECK_INTERRUPT);
/*
if (isTimeOver(tm,millis(),100))
{
tm=millis();
debugSerial<<F("1WT: Tick")<<endl;
}
*/
#if defined (_espdmx)
yield();
dmxout.update();
#endif
return; //????? return; //?????
#ifdef _dmxin #ifdef _dmxin
@@ -2688,10 +2707,7 @@ inputLoop(CHECK_INTERRUPT);
DMXCheck(); DMXCheck();
#endif #endif
#if defined (_espdmx)
yield();
dmxout.update();
#endif
#ifdef IPMODBUS #ifdef IPMODBUS
if (initializedListeners) ipmodbusLoop(); if (initializedListeners) ipmodbusLoop();
@@ -2707,14 +2723,9 @@ ethernetIdleCount++;
ethernetIdleCount--; ethernetIdleCount--;
}; };
//static uint32_t tm=0;
void modbusIdle(void) { void modbusIdle(void) {
/*
if (isTimeOver(tm,millis(),500))
{
tm=millis();
debugSerial<<F("MB: Tick")<<endl;
} */
wdt_res(); wdt_res();
statusLED.poll(); statusLED.poll();
yield(); yield();

View File

@@ -28,6 +28,7 @@ e-mail anklimov@gmail.com
#include "aJSON.h" #include "aJSON.h"
extern aJsonObject *owArr; extern aJsonObject *owArr;
extern uint32_t timerCtr;
aJsonObject *dev2Check = NULL; aJsonObject *dev2Check = NULL;
OneWire *oneWire = NULL; OneWire *oneWire = NULL;
@@ -96,6 +97,7 @@ int owUpdate() {
while (oneWire->wireSearch(dev) > 0) while (oneWire->wireSearch(dev) > 0)
{ {
wdt_res(); wdt_res();
// owIdle();
char addrstr[17]; char addrstr[17];
SetBytes(dev, 8, addrstr); SetBytes(dev, 8, addrstr);
addrstr[16] = 0; addrstr[16] = 0;
@@ -112,45 +114,7 @@ int owUpdate() {
} }
} }
/*
if (oneWire) oneWire->reset_search();
for (short i = 0; i < t_count; i++) wstat[i] &= ~SW_FIND; //absent
while (oneWire && oneWire->wireSearch(term[t_count]) > 0 && (t_count < t_max) && !isTimeOver(finish,millis(), OW_UPDATE_INTERVAL))//&& finish > millis())
{
short ifind = -1;
if (oneWire->crc8(term[t_count], 7) == term[t_count][7]) {
for (short i = 0; i < t_count; i++)
if (!memcmp(term[i], term[t_count], 8)) {
ifind = i;
wstat[i] |= SW_FIND;
debugSerial.print(F(" Node:"));
PrintBytes(term[t_count], 8,0);
processTemp(-1, term[t_count], 0.0); //print note
debugSerial.println(F(" alive"));
break;
}; //alive
if (ifind < 0 && sensors && !zero(term[t_count],8))
{
wstat[t_count] = SW_FIND; //Newly detected
debugSerial<<F("dev#")<<t_count<<F(" Addr:");
PrintBytes(term[t_count], 8,0);
if processTemp(-1, term[t_count], 0.0); //print note
debugSerial.println();
if (term[t_count][0] == 0x28) {
sensors->setResolution(term[t_count], TEMPERATURE_PRECISION);
#ifdef DS2482_100_I2C_TO_1W_BRIDGE
oneWire->setStrongPullup();
#endif
// sensors.requestTemperaturesByAddress(term[t_count]);
}
t_count++;
}
}//if
} //while
debugSerial<<F("1-wire count: ")<<t_count<<endl;
*/
#endif #endif
return true; return true;
} }
@@ -176,19 +140,6 @@ if (!oneWire)
return false; return false;
} }
/*
term = new DeviceAddress[t_max];
debugSerial<<F("Term. Free:")<<freeRam()<<endl;
//regs = new int [t_max];
wstat = new uint16_t[t_max];
debugSerial<<F("wstat. Free:")<<freeRam()<<endl;
if (!term || ! wstat)
{
errorSerial<<F("Error 1-w init #2 Free:")<<freeRam()<<endl;
return false;
}
owChanged = owCh;
*/
#ifdef DS2482_100_I2C_TO_1W_BRIDGE #ifdef DS2482_100_I2C_TO_1W_BRIDGE
Wire.begin(); Wire.begin();
@@ -241,6 +192,9 @@ debugSerial<<F("1WT: setup resolution ")<<item->name<<endl;
sensors->setResolution(curDev,TEMPERATURE_PRECISION); sensors->setResolution(curDev,TEMPERATURE_PRECISION);
item=item->next; item=item->next;
} }
//owUpdate();
return true; return true;
#else //1w is not disabled #else //1w is not disabled
@@ -293,13 +247,8 @@ if (!sensors || !owArr)
///owUpdate(); //every check circle - scan for new devices ///owUpdate(); //every check circle - scan for new devices
} }
/*
if (si >= t_count) { setupOwIdle(&owIdle);
owUpdate(); //every check circle - scan for new devices
si = 0;
return 8000;
}
*/
DeviceAddress curDev; DeviceAddress curDev;
if (dev2Check && SetAddr(dev2Check->name,curDev)) if (dev2Check && SetAddr(dev2Check->name,curDev))
@@ -308,14 +257,17 @@ if (!sensors || !owArr)
switch (curDev[0]) { switch (curDev[0]) {
case 0x28: // Thermomerer case 0x28: // Thermomerer
//debugSerial<<millis()<<" "<<timerCtr<<endl;
t = sensors->getTempC(curDev);//*10.0; t = sensors->getTempC(curDev);//*10.0;
//owIdle();
//debugSerial<<millis()<<" "<<timerCtr<<endl;
processTemp(dev2Check, t); processTemp(dev2Check, t);
//owIdle();
sensors->requestTemperaturesByAddress(curDev); sensors->requestTemperaturesByAddress(curDev);
//owIdle();
//debugSerial<<millis()<<" "<<timerCtr<<endl;
} //switch } //switch
} }
@@ -334,34 +286,7 @@ void owLoop() {
} }
} }
/*
int owFind(DeviceAddress addr) {
for (short i = 0; i < t_count; i++) if (!memcmp(term[i], addr, 8)) return i;//find
return -1;
}
void owAdd(DeviceAddress addr) {
#ifndef OWIRE_DISABLE
infoSerial<<F("dev#")<<t_count<<F(" Addr:");
PrintBytes(term[t_count], 8,0);
infoSerial<<endl;
if (t_count>=t_max) return;
if (zero(term[t_count],8)) return;
wstat[t_count] = SW_FIND; //Newly detected
memcpy(term[t_count], addr, 8);
#ifdef DS2482_100_I2C_TO_1W_BRIDGE
if (term[t_count][0] == 0x28)
oneWire->setStrongPullup();
#endif
t_count++;
#endif
}
*/
#endif #endif
void setupOwIdle (void (*ptr)()) void setupOwIdle (void (*ptr)())