Skip to content

Conversation

@mfalkvidd
Copy link
Member

When temperature in C was below 0, the conversion to F was wrong due to
treating the C value as unsigned integer instead of signed integer.

See http://bit.ly/3ckRfxp for more information.

Thanks to hoggin for reporting this problem.

When temperature in C was below 0, the conversion to F was wrong due to
treating the C value as unsigned integer instead of signed integer.

See http://bit.ly/3ckRfxp for more information.

Thanks to hoggin for reporting this problem.
@mfalkvidd mfalkvidd merged commit bba998b into mysensors:master Jan 29, 2021
@mfalkvidd mfalkvidd deleted the fix-farenheit-conversion branch January 29, 2021 22:46
Copy link
Member

@Yveaux Yveaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! You could also use auto instead of int or include it in the return statement to leave the type up to the compiler

@my-bam
Copy link

my-bam commented Jan 3, 2023

Hi. This isn't working for me. Any ideas?

@mfalkvidd
Copy link
Member Author

@my-bam could you be a bit more specific?

What version of MySensors are you using?
What result did you expect?
Can you post your sketch and the debug output?

@my-bam
Copy link

my-bam commented Jan 3, 2023

  1. VER=2.3.2
  2. Temp 128.86C Hum 118.99
    3.1.-------------------------------------------------------------------------------
/**
 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 *
 * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
 * Copyright (C) 2013-2015 Sensnology AB
 * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
 *
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 *******************************
 *
 * REVISION HISTORY
 * Version 1.0: Yveaux
 * 
 * DESCRIPTION
 * This sketch provides an example of how to implement a humidity/temperature
 * sensor using a Si7021 sensor.
 *  
 * For more information, please visit:
 * http://www.mysensors.org/build/humiditySi7021
 * 
 */

// Enable debug prints
#define MY_DEBUG

// Enable REPORT_BATTERY_LEVEL to measure battery level and send changes to gateway
#define REPORT_BATTERY_LEVEL

// Enable and select radio type attached 
#define MY_RADIO_RF24
//#define MY_RADIO_RFM69
//#define MY_RS485

#include <MySensors.h>  
#include <DallasTemperature.h>
#include <OneWire.h>

#define CHILD_ID_HUM  0
#define CHILD_ID_TEMP 1

//#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No

#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
bool receivedConfig = false;
MyMessage msg(0,V_TEMP);

static bool metric = true;

// Sleep time between sensor updates (in milliseconds)
static const uint64_t UPDATE_INTERVAL = 10000;

#include <SI7021.h>
static SI7021 sensor;

#ifdef REPORT_BATTERY_LEVEL
#include <Vcc.h>
static uint8_t oldBatteryPcnt = 200;  // Initialize to 200 to assure first time value will be sent.
const float VccMin        = 1.8;      // Minimum expected Vcc level, in Volts: Brownout at 1.8V    -> 0%
//const float VccMax        = 2.0*1.6;  // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -> 100%
const float VccMax        = 3.0;  // Maximum expected Vcc level, in Volts: 2xAA fresh Alkaline -> 100%
const float VccCorrection = 1.0;      // Measured Vcc by multimeter divided by reported Vcc
static Vcc vcc(VccCorrection); 
#endif

void before()
{
  // Startup up the OneWire library
  sensors.begin();
}

void presentation()  
{ 
  // Send the sketch info to the gateway
  sendSketchInfo("Sensor s101", "1.0");

  // Present sensors as children to gateway
  present(CHILD_ID_HUM, S_HUM,   "Humidity");
  present(CHILD_ID_TEMP, S_TEMP, "Temperature");

  numSensors = sensors.getDeviceCount();

  // Present all sensors to controller
  for (int i=2; i<(2+numSensors) && i<MAX_ATTACHED_DS18B20; i++) {   
     present(i, S_TEMP, "DS18B20");
  }

  metric = getControllerConfig().isMetric;
}

void setup()
{
  while (not sensor.begin())
  {
    Serial.println(F("Sensor not detected!"));
    delay(5000);
  }
  // requestTemperatures() will not block current thread
  sensors.setWaitForConversion(false);  
}


void loop()      
{  
  // Read temperature & humidity from sensor.
  const float temperature = float( metric ? sensor.getCelsiusHundredths() : sensor.getFahrenheitHundredths() ) / 100.0;
  const float humidity    = float( sensor.getHumidityBasisPoints() ) / 100.0;

#ifdef MY_DEBUG
  Serial.print(F("Temp "));
  Serial.print(temperature);
  Serial.print(metric ? 'C' : 'F');
  Serial.print(F("\tHum "));
  Serial.println(humidity);
#endif

  static MyMessage msgHum( CHILD_ID_HUM,  V_HUM );
  static MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);

  send(msgTemp.set(temperature, 2));
  send(msgHum.set(humidity, 2));

  sensors.requestTemperatures();

  // query conversion time and sleep until conversion completed
  int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
  // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
  sleep(conversionTime);

  // Read temperatures and send them to controller 
  for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
 
    // Fetch and round temperature to one decimal
    float temperature_dallas = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
 
    // Only send data if temperature has changed and no error
    #if COMPARE_TEMP == 1
    if (lastTemperature[i] != temperature_dallas && temperature_dallas != -127.00 && temperature_dallas != 85.00) {
    #else
    if (temperature_dallas != -127.00 && temperature_dallas != 85.00) {
    #endif
 

      // Send in the new temperature
      send(msg.setSensor(i+2).set(temperature_dallas,1));
      // Save new temperatures for next compare
      lastTemperature[i]=temperature_dallas;
    }
  }

#ifdef REPORT_BATTERY_LEVEL
  const uint8_t batteryPcnt = static_cast<uint8_t>(0.5 + vcc.Read_Perc(VccMin, VccMax));

#ifdef MY_DEBUG
  Serial.print(F("Vbat "));
  Serial.print(vcc.Read_Volts());
  Serial.print(F("\tPerc "));
  Serial.println(batteryPcnt);
#endif

  // Battery readout should only go down. So report only when new value is smaller than previous one.
  if ( batteryPcnt < oldBatteryPcnt )
  {
      sendBatteryLevel(batteryPcnt);
      oldBatteryPcnt = batteryPcnt;
  }
#endif

  // Sleep until next update to save energy
  sleep(UPDATE_INTERVAL); 
}

3.2.----------------------------------------------------------------------------------------------

16 MCO:BGN:INIT NODE,CP=RNNNA---,FQ=8,REL=255,VER=2.3.2
28 MCO:BGN:BFR
65 TSM:INIT
65 TSF:WUR:MS=0
73 TSM:INIT:TSP OK
75 TSF:SID:OK,ID=101
77 TSM:FPAR
81 ?TSF:MSG:SEND,101-101-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
866 TSF:MSG:READ,0-0-101,s=255,c=3,t=8,pt=1,l=1,sg=0:0
872 TSF:MSG:FPAR OK,ID=0,D=1
2091 TSM:FPAR:OK
2091 TSM:ID
2093 TSM:ID:OK
2095 TSM:UPL
2099 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
2107 TSF:MSG:READ,0-0-101,s=255,c=3,t=25,pt=1,l=1,sg=0:1
2113 TSF:MSG:PONG RECV,HP=1
2115 TSM:UPL:OK
2117 TSM:READY:ID=101,PAR=0,DIS=1
2123 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
2131 TSF:MSG:READ,0-0-101,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
2140 TSF:MSG:SEND,101-101-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.2
2150 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
4161 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=11,pt=0,l=11,sg=0,ft=0,st=OK:Sensor s101
4171 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0
4182 TSF:MSG:SEND,101-101-0-0,s=0,c=0,t=7,pt=0,l=8,sg=0,ft=0,st=OK:Humidity
4196 TSF:MSG:SEND,101-101-0-0,s=1,c=0,t=6,pt=0,l=11,sg=0,ft=0,st=OK:Temperature
4208 TSF:MSG:SEND,101-101-0-0,s=2,c=0,t=6,pt=0,l=7,sg=0,ft=0,st=OK:DS18B20
4216 MCO:REG:REQ
4220 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
4229 TSF:MSG:READ,0-0-101,s=255,c=3,t=27,pt=1,l=1,sg=0:1
4235 MCO:PIM:NODE REG=1
4237 MCO:BGN:STP
4239 MCO:BGN:INIT OK,TSP=1
Temp 128.86C Hum 118.99
4247 TSF:MSG:SEND,101-101-0-0,s=1,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:128.86
4257 TSF:MSG:SEND,101-101-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:118.99
4268 MCO:SLP:MS=750,SMS=0,I1=255,M1=255,I2=255,M2=255
4274 TSF:TDI:TSL
4278 MCO:SLP:WUP=-1
4280 TSF:TRI:TSB
4315 TSF:MSG:SEND,101-101-0-0,s=2,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:26.3
Vbat 3.35 Perc 100
4327 TSF:MSG:SEND,101-101-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:100
4335 MCO:SLP:MS=10000,SMS=0,I1=255,M1=255,I2=255,M2=255
4341 TSF:TDI:TSL
4343 MCO:SLP:WUP=-1
4345 TSF:TRI:TSB
Temp 128.86CHum 118.99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants