1-w refactoring

This commit is contained in:
Климов Андрей Николаевич
2023-10-29 15:36:04 +03:00
parent 9204bd0898
commit 256ab175ce
5 changed files with 178 additions and 85 deletions

View File

@@ -1237,7 +1237,7 @@ void resetHard() {
}
#ifdef _owire
/*
void Changed(int i, DeviceAddress addr, float currentTemp) {
char addrstr[32] = "NIL";
//char addrbuf[17];
@@ -1248,21 +1248,23 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
SetBytes(addr, 8, addrstr);
addrstr[17] = 0;
if (!root) return;
//printFloatValueToStr(currentTemp,valstr);
debugSerial<<endl<<F("T:")<<currentTemp<<F("<")<<addrstr<<F(">")<<endl;
aJsonObject *owObj = aJson.getObjectItem(owArr, addrstr);
if (i<0) //just print note about address
{
if (owObj && owObj->child && owObj->child->type==aJson_String && owObj->child->valuestring)
debugSerial<<F(" ")<<owObj->child->valuestring<<F(" ");
return;
}
debugSerial<<endl<<F("T:")<<currentTemp<<F("<")<<addrstr<<F(">")<<endl;
if ((currentTemp != -127.0) && (currentTemp != 85.0) && (currentTemp != 0.0))
executeCommand(owObj,-1,itemCmd(currentTemp).setSuffix(S_VAL));
else
if (owObj && owObj->child && owObj->child->type==aJson_String && owObj->child->valuestring)
errorSerial<<F("Read error for ")<<owObj->child->valuestring<<endl;
/*
if (owObj) {
owEmitString = getStringFromConfig(owObj, "emit");
debugSerial<<owEmitString<<F(">")<<endl;
if ((currentTemp != -127.0) && (currentTemp != 85.0) && (currentTemp != 0.0))
{
if (owEmitString) // publish temperature to MQTT if configured
{
////
#ifdef WITH_DOMOTICZ
aJsonObject *idx = aJson.getObjectItem(owObj, "idx");
if (idx && && idx->type ==aJson_String && idx->valuestring) {//DOMOTICZ json format support
@@ -1275,23 +1277,9 @@ void Changed(int i, DeviceAddress addr, float currentTemp) {
return;
}
#endif
//strcpy_P(addrstr, outprefix);
setTopic(addrstr,sizeof(addrstr),T_OUT);
strncat(addrstr, owEmitString, sizeof(addrstr));
if (mqttClient.connected() && !ethernetIdleCount)
mqttClient.publish(addrstr, valstr);
}
// And translate temp to internal items
owItem = getStringFromConfig(owObj, "item");
if (owItem)
thermoSetCurTemp(owItem, currentTemp); ///TODO: Refactore using Items interface
} // if valid temperature
} // if Address in config
else debugSerial<<addrstr<<F(">")<<endl; // No item found
*/
////
}
*/
#endif //_owire
int cmdFunctionHelp(int arg_cnt, char **args)
@@ -1310,6 +1298,7 @@ int cmdFunctionHelp(int arg_cnt, char **args)
"'log [serial_loglevel] [udp_loglevel]' - define log level (0..7)\n"
"'kill' - test watchdog\n"
"'clear' - clear EEPROM\n"
"'search' - search 1-wire dev\n"
"'reboot' - reboot controller");
return 200;
}
@@ -1339,6 +1328,12 @@ int cmdFunctionReboot(int arg_cnt, char **args) {
return 500;
}
int cmdFunctionSearch(int arg_cnt, char **args) {
infoSerial<<F("searching");
owSearch();
return 200;
}
void applyConfig() {
if (!root || configLocked) return;
configLocked++;
@@ -1403,10 +1398,10 @@ setupSyslog();
owArr = aJson.getObjectItem(root, "ow");
if (owArr && !owReady) {
aJsonObject *item = owArr->child;
owReady = owSetup(&Changed);
owReady = owSetup();
if (owReady) infoSerial<<F("One wire Ready\n");
t_count = 0;
/// t_count = 0;
/*
while (item && owReady) {
if ((item->type == aJson_Object)) {
DeviceAddress addr;
@@ -1417,6 +1412,8 @@ setupSyslog();
yield();
item = item->next;
}
*/
}
#endif
@@ -2658,6 +2655,7 @@ void setupCmdArduino() {
cmdAdd("clear",cmdFunctionClearEEPROM);
cmdAdd("reboot",cmdFunctionReboot);
cmdAdd("log",cmdFunctionLoglevel);
cmdAdd("search",cmdFunctionSearch);
}
void loop_main() {

View File

@@ -25,20 +25,28 @@ e-mail anklimov@gmail.com
#include "utils.h"
#include "options.h"
#include "main.h"
#include "aJSON.h"
extern aJsonObject *owArr;
aJsonObject *dev2Check = NULL;
OneWire *oneWire = NULL;
DeviceAddress *term = NULL;
//DeviceAddress *term = NULL;
//uint16_t *wstat = NULL;
uint16_t *wstat = NULL;
DallasTemperature *sensors = NULL;
short si = 0;
int t_count = 0;
//short si = 0;
//int t_count = 0;
unsigned long owTimer = 0;
owChangedType owChanged;
//owChangedType owChanged;
void owSearch()
{
owUpdate();
}
bool zero(const uint8_t *addr, uint8_t len)
{
@@ -47,17 +55,75 @@ bool zero(const uint8_t *addr, uint8_t len)
return true;
}
char * getReableNote(aJsonObject * owObj)
{
if (owObj && owObj->child)
{
if (owObj->child->type==aJson_String && owObj->child->valuestring) return owObj->child->valuestring;
if (owObj->child->child && owObj->child->child->type==aJson_String && owObj->child->child->valuestring) return owObj->child->child->valuestring;
return NULL;
}
}
void processTemp(aJsonObject * owObj, float currentTemp) {
if (!owObj || !owArr) return;
char* note = getReableNote(owObj);
debugSerial<<endl<<F("1WT:")<<currentTemp<<F(" <")<<owObj->name<<F("> ");
if ((currentTemp != -127.0) && (currentTemp != 85.0) && (currentTemp != 0.0))
{
if (note) debugSerial<<note;
debugSerial<<endl;
executeCommand(owObj,-1,itemCmd(currentTemp).setSuffix(S_VAL));
}
else
if (note) errorSerial<<F("1WT: Read error for ")<<note<<endl;
/*
#ifdef WITH_DOMOTICZ
aJsonObject *idx = aJson.getObjectItem(owObj, "idx");
if (idx && && idx->type ==aJson_String && idx->valuestring) {//DOMOTICZ json format support
debugSerial << endl << idx->valuestring << F(" Domoticz valstr:");
char valstr[50];
sprintf(valstr, "{\"idx\":%s,\"svalue\":\"%.1f\"}", idx->valuestring, currentTemp);
debugSerial << valstr;
if (mqttClient.connected() && !ethernetIdleCount)
mqttClient.publish(owEmitString, valstr);
return;
}
#endif
*/
}
int owUpdate() {
#ifndef OWIRE_DISABLE
unsigned long finish = millis();// + OW_UPDATE_INTERVAL;
/*
if (oneWire->getError() == DS2482_ERROR_SHORT)
DeviceAddress dev;
//unsigned long finish = millis();// + OW_UPDATE_INTERVAL;
if (!oneWire || !owArr) return 0;
oneWire->reset_search();
debugSerial << F("1WT: Searching dev")<<endl;
while (oneWire->wireSearch(dev) > 0)
{
debugSerial<<F("1-wire shorted.")<<endl;
return false;
char addrstr[17];
SetBytes(dev, 8, addrstr);
addrstr[16] = 0;
aJsonObject * owObj=aJson.getObjectItem(owArr,addrstr);
debugSerial<<F("1WT:")<<addrstr;
if (owObj)
{
char * note = getReableNote(owObj);
if (note) debugSerial<<F(" is ")<<note<<endl;
}
*/
//Serial.println(F("Searching"));
else
{
debugSerial<<F(" new")<<endl;
}
}
/*
if (oneWire) oneWire->reset_search();
for (short i = 0; i < t_count; i++) wstat[i] &= ~SW_FIND; //absent
@@ -71,6 +137,7 @@ int owUpdate() {
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
@@ -79,6 +146,7 @@ int owUpdate() {
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);
@@ -93,12 +161,13 @@ int owUpdate() {
} //while
debugSerial<<F("1-wire count: ")<<t_count<<endl;
*/
#endif
return true;
}
int owSetup(owChangedType owCh) {
int owSetup() {
#ifndef OWIRE_DISABLE
//// todo - move memory allocation to here
if (oneWire) return true; // Already initialized
@@ -117,9 +186,13 @@ if (!oneWire)
errorSerial<<F("Error 1-w init #1")<<endl;
return false;
}
// Pass our oneWire reference to Dallas Temperature.
// sensors = new DallasTemperature(oneWire);
/*
term = new DeviceAddress[t_max];
debugSerial<<F("Term. Free:")<<freeRam()<<endl;
//regs = new int [t_max];
@@ -132,6 +205,7 @@ if (!term || ! wstat)
}
owChanged = owCh;
*/
#ifdef DS2482_100_I2C_TO_1W_BRIDGE
Wire.begin();
@@ -173,45 +247,57 @@ int sensors_loop(void) {
}
#endif
if (!sensors)
if (!sensors)
{
// Setup sensors library and resolution
sensors = new DallasTemperature(oneWire);
sensors->begin();
// IC Default 9 bit. If you have troubles consider upping it 12. Ups the delay giving the IC more time to process the temperature measurement
for (short i = 0; i < t_count; i++) sensors->setResolution(term[i],TEMPERATURE_PRECISION);
//for (short i = 0; i < t_count; i++) sensors->setResolution(term[i],TEMPERATURE_PRECISION);
sensors->setWaitForConversion(false);
}
if (!dev2Check && owArr)
{
if (owArr && owArr->type == aJson_Object && owArr->child) dev2Check = owArr->child;
///owUpdate(); //every check circle - scan for new devices
}
/*
if (si >= t_count) {
owUpdate(); //every check circle - scan for new devices
si = 0;
return 8000;
}
*/
DeviceAddress curDev;
if (dev2Check && SetAddr(dev2Check->name,curDev))
{
float t;
switch (term[si][0]) {
switch (curDev[0]) {
case 0x28: // Thermomerer
t = sensors->getTempC(term[si]);//*10.0;
if (owChanged) owChanged(si, term[si], t);
sensors->requestTemperaturesByAddress(term[si]);
si++;
return 2500;
sensors->setResolution(curDev,TEMPERATURE_PRECISION);
t = sensors->getTempC(curDev);//*10.0;
processTemp(dev2Check, t);
sensors->requestTemperaturesByAddress(curDev);
// default
// return sensors_ext();
} //switch
}
si++;
return check_circle;
//si++;
dev2Check=dev2Check->next;
return INTERVAL_1W;
}
void owLoop() {
//if (millis() >= owTimer) owTimer = millis() + sensors_loop();
if (isTimeOver(owTimer,millis(),INTERVAL_1W))
{
sensors_loop();
@@ -219,7 +305,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;
@@ -245,6 +331,8 @@ void owAdd(DeviceAddress addr) {
t_count++;
#endif
}
*/
#endif
void setupOwIdle (void (*ptr)())
@@ -253,3 +341,4 @@ void setupOwIdle (void (*ptr)())
if (oneWire) oneWire->idle(ptr);
#endif
}

View File

@@ -31,7 +31,7 @@ e-mail anklimov@gmail.com
#define SW_INMASK 0xFC
#define recheck_interval 5
#define check_circle 2000/t_count
//#define check_circle 2000/t_count
#define SW_FIND 1
#define SW_DOUBLECHECK 2 //Doublecheck required
@@ -45,8 +45,8 @@ e-mail anklimov@gmail.com
#define SW_PULSE1_R 0x200 //Pulse Reset requested
#define recheck_interval 5
#define check_circle 2000/t_count
//#define recheck_interval 5
//#define check_circle 2000/t_count
#if not defined (OW_DEVICES_LIMIT)
#define t_max 20 //Maximum number of 1w devices
@@ -63,7 +63,7 @@ e-mail anklimov@gmail.com
extern aJsonObject *owArr;
typedef void (*owChangedType) (int , DeviceAddress, float) ;
//typedef void (*owChangedType) (int , DeviceAddress, float) ;
#ifndef USE_1W_PIN
#define DS2482_100_I2C_TO_1W_BRIDGE // HW driver
@@ -81,21 +81,22 @@ typedef void (*owChangedType) (int , DeviceAddress, float) ;
extern OneWire *oneWire;
extern DallasTemperature *sensors;
extern DeviceAddress *term ;
extern int *regs ;
extern uint16_t *wstat;
extern int t_count;
extern short si;
//extern DeviceAddress *term ;
//extern int *regs ;
//extern uint16_t *wstat;
//extern int t_count;
//extern short si;
extern owChangedType owChanged;
//extern owChangedType owChanged;
int owUpdate();
int owSetup(owChangedType owCh);
int owSetup();
void owLoop();
void setupOwIdle(void (*)()) ;
int owFind(DeviceAddress addr);
void owAdd (DeviceAddress addr);
//int owFind(DeviceAddress addr);
//void owAdd (DeviceAddress addr);
void owSearch();
#endif

View File

@@ -60,6 +60,7 @@ extern aJsonObject *topics;
void PrintBytes(uint8_t *addr, uint8_t count, bool newline) {
if (!addr) return;
for (uint8_t i = 0; i < count; i++) {
infoSerial<< _HEX(addr[i] >> 4);
infoSerial<< _HEX(addr[i] & 0x0f);
@@ -72,6 +73,7 @@ const char HEXSTR[] = "0123456789ABCDEF";
void SetBytes(uint8_t *addr, uint8_t count, char *out) {
// debugSerialPort.println("SB:");
if (!addr || !out) return;
for (uint8_t i = 0; i < count; i++) {
*(out++) = HEXSTR[(addr[i] >> 4)];
*(out++) = HEXSTR[(addr[i] & 0x0f)];
@@ -81,20 +83,23 @@ void SetBytes(uint8_t *addr, uint8_t count, char *out) {
}
byte HEX2DEC(char i) {
byte HEX2DEC(char i, bool *err) {
byte v=0;
if ('a' <= i && i <= 'f') { v = i - 97 + 10; }
else if ('A' <= i && i <= 'F') { v = i - 65 + 10; }
else if ('0' <= i && i <= '9') { v = i - 48; }
else if (err) *err = true;
return v;
}
void SetAddr(char *out, uint8_t *addr) {
bool SetAddr(char *in, uint8_t *addr) {
bool err=false;
if (!addr || !in) return false;
for (uint8_t i = 0; i < 8; i++) {
*addr = HEX2DEC(*out++) << 4;
*addr++ |= HEX2DEC(*out++);
*addr = HEX2DEC(*in++,&err) << 4;
*addr++ |= HEX2DEC(*in++,&err);
}
return !err;
}
// chan is pointer to pointer to string

View File

@@ -51,8 +51,8 @@ enum topicType {
void PrintBytes(uint8_t* addr, uint8_t count, bool newline);
void SetBytes(uint8_t* addr, uint8_t count, char * out);
void SetAddr(char * out, uint8_t* addr);
uint8_t HEX2DEC(char i);
bool SetAddr(char * in, uint8_t* addr);
uint8_t HEX2DEC(char i, bool* err);
long getInt(char ** chan);
itemCmd getNumber(char ** chan);
unsigned long freeRam ();