From 687ef7cd99e5491895350280dd4a012b1413db91 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 14 Oct 2018 03:20:50 +0300 Subject: [PATCH] DS18B20 TEMPERATURE_PRECISION increased to 12 bits and temp converted to float --- lighthub/inputs.cpp | 18 +------- lighthub/inputs.h | 1 - lighthub/main.cpp | 100 +++++++++++++++++++++++--------------------- lighthub/main.h | 5 +-- lighthub/options.h | 6 ++- lighthub/owTerm.cpp | 12 +++--- lighthub/owTerm.h | 4 +- lighthub/utils.cpp | 28 ++++++++++--- lighthub/utils.h | 3 +- platformio.ini | 4 +- 10 files changed, 93 insertions(+), 88 deletions(-) diff --git a/lighthub/inputs.cpp b/lighthub/inputs.cpp index fc5bec1..d6ca993 100644 --- a/lighthub/inputs.cpp +++ b/lighthub/inputs.cpp @@ -265,22 +265,7 @@ void Input::setNextPollTime(unsigned long pollTime) { } } -void Input::printFloatValueToStr(float value, char *valstr) { - #if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32) - sprintf(valstr, "%2.1f", value); - #endif - #if defined(__AVR__) - sprintf(valstr, "%d", (int)value); - int fractional = 10.0*((float)abs(value)-(float)abs((int)value)); - int val_len =strlen(valstr); - valstr[val_len]='.'; - valstr[val_len+1]='0'+fractional; - valstr[val_len+2]='\0'; - #endif - #if defined(__SAM3X8E__) - sprintf(valstr, "%2.1f",value); - #endif -} + void Input::contactPoll() { boolean currentInputState; @@ -415,4 +400,3 @@ void Input::printUlongValueToStr(char *valstr, unsigned long value) { } valstr[i]='\0'; } - diff --git a/lighthub/inputs.h b/lighthub/inputs.h index 94bbf32..3e37ee8 100644 --- a/lighthub/inputs.h +++ b/lighthub/inputs.h @@ -112,7 +112,6 @@ protected: void dht22Poll(); - void printFloatValueToStr(float value, char *valstr); void counterPoll(); diff --git a/lighthub/main.cpp b/lighthub/main.cpp index c93ecdc..e560a14 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -267,7 +267,7 @@ lan_status lanLoop() { mqttClient.unsubscribe(buf); lanStatus = OPERATION;//3; - debugSerial<valuestring; + if (owEmit) { - strncpy(addrbuf, owEmit, sizeof(addrbuf)); - debugSerial<valuestring; - } else debugSerial<child; owReady = owSetup(&Changed); - if (owReady) debugSerial<type == aJson_Object)) { @@ -731,6 +733,7 @@ void printConfigSummary() { printBool(owArr); debugSerial< 1)) { int thermoPin = aJson.getArrayItem(thermoItem, I_ARG)->valueint; - int thermoSetting = aJson.getArrayItem(thermoItem, I_VAL)->valueint; + float thermoSetting = aJson.getArrayItem(thermoItem, I_VAL)->valueint; /// int thermoStateCommand = aJson.getArrayItem(thermoItem, I_CMD)->valueint; - int curTemp = aJson.getArrayItem(thermoExtensionArray, IET_TEMP)->valueint; + float curTemp = aJson.getArrayItem(thermoExtensionArray, IET_TEMP)->valuefloat; if (!aJson.getArrayItem(thermoExtensionArray, IET_ATTEMPTS)->valueint) { - debugSerial<name<name<valueint)) @@ -1389,7 +1393,7 @@ void thermoLoop(void) { if (curTemp > THERMO_OVERHEAT_CELSIUS) mqttClient.publish("/alarm/ovrht", thermoItem->name); - debugSerial << endl << thermoItem->name << F("Set:") << thermoSetting << F(" Cur:") << curTemp + debugSerial << endl << thermoItem->name << F(" Set:") << thermoSetting << F(" Cur:") << curTemp << F(" cmd:") << thermoStateCommand; pinMode(thermoPin, OUTPUT); if (thermoDisabledOrDisconnected(thermoExtensionArray, thermoStateCommand)) { @@ -1418,7 +1422,7 @@ void thermoLoop(void) { #endif } -short thermoSetCurTemp(char *name, short t) { +short thermoSetCurTemp(char *name, float t) { if (items) { aJsonObject *thermoItem = aJson.getObjectItem(items, name); if (isThermostatWithMinArraySize(thermoItem, 4)) { @@ -1428,7 +1432,7 @@ short thermoSetCurTemp(char *name, short t) { { extArray = aJson.createArray(); //Create Ext Array - aJsonObject *ocurt = aJson.createItem(t); //Create int + aJsonObject *ocurt = aJson.createItem(t); //Create float aJsonObject *oattempts = aJson.createItem(T_ATTEMPTS); //Create int aJson.addItemToArray(extArray, ocurt); aJson.addItemToArray(extArray, oattempts); @@ -1436,7 +1440,7 @@ short thermoSetCurTemp(char *name, short t) { } else if (extArray = aJson.getArrayItem(thermoItem, I_EXT)) { aJsonObject *att = aJson.getArrayItem(extArray, IET_ATTEMPTS); - aJson.getArrayItem(extArray, IET_TEMP)->valueint = t; + aJson.getArrayItem(extArray, IET_TEMP)->valuefloat = t; if (att->valueint == 0) mqttClient.publish("/alarmoff/snsr", thermoItem->name); att->valueint = (int) T_ATTEMPTS; } diff --git a/lighthub/main.h b/lighthub/main.h index e56b11d..b47984c 100644 --- a/lighthub/main.h +++ b/lighthub/main.h @@ -156,7 +156,7 @@ void restoreState(); lan_status lanLoop(); #ifndef OWIRE_DISABLE -void Changed(int i, DeviceAddress addr, int val); +void Changed(int i, DeviceAddress addr, float val); #endif void modbusIdle(void); @@ -213,7 +213,7 @@ void pollingLoop(void); void thermoLoop(void); -short thermoSetCurTemp(char *name, short t); +short thermoSetCurTemp(char *name, float t); void modbusIdle(void); @@ -238,4 +238,3 @@ void ip_ready_config_loaded_connecting_to_broker(); void printCurentLanConfig(); void printFreeRam(); - diff --git a/lighthub/options.h b/lighthub/options.h index b991274..ec87564 100644 --- a/lighthub/options.h +++ b/lighthub/options.h @@ -13,8 +13,8 @@ #define IET_TEMP 0 #define IET_ATTEMPTS 1 -#define THERMO_GIST_CELSIUS 2 -#define THERMO_OVERHEAT_CELSIUS 38 +#define THERMO_GIST_CELSIUS 1. +#define THERMO_OVERHEAT_CELSIUS 38. #define FM_OVERHEAT_CELSIUS 40. #define MIN_VOLUME 10 @@ -151,3 +151,5 @@ #ifndef Wiz5500 #define W5100_ETHERNET_SHIELD #endif + +#define eol "\n" diff --git a/lighthub/owTerm.cpp b/lighthub/owTerm.cpp index e2d44d6..17578bd 100644 --- a/lighthub/owTerm.cpp +++ b/lighthub/owTerm.cpp @@ -27,11 +27,9 @@ e-mail anklimov@gmail.com OneWire *net = NULL; -// Pass our oneWire reference to Dallas Temperature. -//DallasTemperature sensors(&net); DeviceAddress *term = NULL; -//int *regs = NULL; + uint16_t *wstat = NULL; DallasTemperature *sensors = NULL; @@ -46,8 +44,8 @@ int owUpdate() { unsigned long finish = millis() + OW_UPDATE_INTERVAL; short sr; - //net.setStrongPullup(); - debugSerial.println(F("Searching")); + + Serial.println(F("Searching")); if (net) net->reset_search(); for (short i = 0; i < t_count; i++) wstat[i] &= ~SW_FIND; //absent @@ -153,12 +151,12 @@ int sensors_loop(void) { return 8000; } - int t; + float t; switch (term[si][0]) { case 0x28: // Thermomerer t = sensors->getTempC(term[si]);//*10.0; - //debugSerial.println("o"); + //Serial.println("o"); if (owChanged) owChanged(si, term[si], t); sensors->requestTemperaturesByAddress(term[si]); si++; diff --git a/lighthub/owTerm.h b/lighthub/owTerm.h index 12588aa..34d6ac5 100644 --- a/lighthub/owTerm.h +++ b/lighthub/owTerm.h @@ -48,7 +48,7 @@ e-mail anklimov@gmail.com #define check_circle 2000/t_count #define t_max 20 //Maximum number of 1w devices -#define TEMPERATURE_PRECISION 9 +#define TEMPERATURE_PRECISION 12 //9 #ifndef OWIRE_DISABLE @@ -61,7 +61,7 @@ e-mail anklimov@gmail.com extern aJsonObject *owArr; -typedef void (*owChangedType) (int , DeviceAddress, int) ; +typedef void (*owChangedType) (int , DeviceAddress, float) ; #ifndef USE_1W_PIN #define DS2482_100_I2C_TO_1W_BRIDGE // HW driver #endif diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index d959d67..feb1351 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -87,11 +87,11 @@ unsigned long freeRam () #endif #if defined(__AVR__) -unsigned long freeRam () +unsigned long freeRam () { - extern int __heap_start, *__brkval; - int v; - return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); + extern int __heap_start, *__brkval; + int v; + return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); } #endif @@ -136,6 +136,24 @@ void parseBytes(const char *str, char separator, byte *bytes, int maxBytes, int } } + +void printFloatValueToStr(float value, char *valstr) { + #if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32) + sprintf(valstr, "%2.1f", value); + #endif + #if defined(__AVR__) + sprintf(valstr, "%d", (int)value); + int fractional = 10.0*((float)abs(value)-(float)abs((int)value)); + int val_len =strlen(valstr); + valstr[val_len]='.'; + valstr[val_len+1]='0'+fractional; + valstr[val_len+2]='\0'; + #endif + #if defined(__SAM3X8E__) + sprintf(valstr, "%2.1f",value); + #endif +} + #define ARDBUFFER 16 //Buffer for storing intermediate strings. Performance may vary depending on size. int log(const char *str, ...)//TODO: __FlashStringHelper str support @@ -192,4 +210,4 @@ int log(const char *str, ...)//TODO: __FlashStringHelper str support #pragma message(VAR_NAME_VALUE(debugSerial)) -#pragma message(VAR_NAME_VALUE(SERIAL_BAUD)) \ No newline at end of file +#pragma message(VAR_NAME_VALUE(SERIAL_BAUD)) diff --git a/lighthub/utils.h b/lighthub/utils.h index 4a1945a..3401d12 100644 --- a/lighthub/utils.h +++ b/lighthub/utils.h @@ -32,4 +32,5 @@ uint8_t HEX2DEC(char i); int getInt(char ** chan); unsigned long freeRam (); void parseBytes(const char* str, char separator, byte* bytes, int maxBytes, int base); -int log(const char *str, ...); \ No newline at end of file +int log(const char *str, ...); +void printFloatValueToStr(float value, char *valstr); diff --git a/platformio.ini b/platformio.ini index 81421d7..e5ca88a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,12 +11,12 @@ src_dir = lighthub env_default = ; megaatmega2560 -; megaatmega2560-net + megaatmega2560-net ; due ; esp8266 ; esp32 ; megaatmega2560-5500 - due-5500 +; due-5500 ; controllino ; stm32