Skip to content

Commit

Permalink
Issue #69. Allow temperatures to be reported in Fahrenheit degrees
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Feb 14, 2017
1 parent 4a65bdf commit 6402af4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
8 changes: 8 additions & 0 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
// 0 means no pulses, 1 means normally off, 2 normally on
#define RELAY_PULSE_MODE RELAY_PULSE_NONE

//--------------------------------------------------------------------------------
// I18N
//--------------------------------------------------------------------------------

#define TMP_CELSIUS 0
#define TMP_FAHRENHEIT 1
#define TMP_UNITS TMP_CELSIUS

//--------------------------------------------------------------------------------
// LED
//--------------------------------------------------------------------------------
Expand Down
8 changes: 5 additions & 3 deletions code/espurna/dht.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ void dhtLoop() {
if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
last_update = millis();

unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();

// Read sensor data
double h = dht.readHumidity();
double t = dht.readTemperature();
double t = dht.readTemperature(tmpUnits == TMP_FAHRENHEIT);

// Check if readings are valid
if (isnan(h) || isnan(t)) {
Expand All @@ -64,7 +66,7 @@ void dhtLoop() {
dtostrf(t, 4, 1, temperature);
itoa((unsigned int) h, humidity, 10);

DEBUG_MSG("[DHT] Temperature: %s\n", temperature);
DEBUG_MSG("[DHT] Temperature: %s%s\n", temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF");
DEBUG_MSG("[DHT] Humidity: %s\n", humidity);

// Send MQTT messages
Expand Down Expand Up @@ -93,7 +95,7 @@ void dhtLoop() {

// Update websocket clients
char buffer[100];
sprintf_P(buffer, PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s}"), temperature, humidity);
sprintf_P(buffer, PSTR("{\"dhtVisible\": 1, \"dhtTmp\": %s, \"dhtHum\": %s, \"tmpUnits\": %d}"), temperature, humidity, tmpUnits);
wsSend(buffer);

}
Expand Down
8 changes: 5 additions & 3 deletions code/espurna/ds18b20.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ void dsLoop() {
if ((millis() - last_update > DS_UPDATE_INTERVAL) || (last_update == 0)) {
last_update = millis();

unsigned char tmpUnits = getSetting("tmpUnits", TMP_UNITS).toInt();

// Read sensor data
ds18b20.requestTemperatures();
double t = ds18b20.getTempCByIndex(0);
double t = (tmpUnits == TMP_CELSIUS) ? ds18b20.getTempCByIndex(0) : ds18b20.getTempFByIndex(0);

// Check if readings are valid
if (isnan(t)) {
Expand All @@ -55,7 +57,7 @@ void dsLoop() {

char temperature[6];
dtostrf(t, 5, 1, temperature);
DEBUG_MSG("[DS18B20] Temperature: %s\n", temperature);
DEBUG_MSG("[DS18B20] Temperature: %s%s\n", temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF");

// Send MQTT messages
mqttSend(getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), temperature);
Expand All @@ -67,7 +69,7 @@ void dsLoop() {

// Update websocket clients
char buffer[100];
sprintf_P(buffer, PSTR("{\"dsVisible\": 1, \"dsTmp\": %s}"), temperature);
sprintf_P(buffer, PSTR("{\"dsVisible\": 1, \"dsTmp\": %s, \"tmpUnits\": %d}"), temperature, tmpUnits);
wsSend(buffer);

}
Expand Down
7 changes: 2 additions & 5 deletions code/espurna/web.ino
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,6 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
setCurrentRatio(getSetting("emonRatio").toFloat());
#endif

#if LED_PULSE
byte relayPulseMode = getSetting("relayPulseMode", String(RELAY_PULSE_MODE)).toInt();
digitalWrite(LED_PULSE, relayPulseMode != RELAY_PULSE_NONE);
#endif

// Check if we should reconfigure MQTT connection
if (changedMQTT) {
mqttDisconnect();
Expand Down Expand Up @@ -339,6 +334,8 @@ void _wsStart(uint32_t client_id) {
root["apiEnabled"] = getSetting("apiEnabled").toInt() == 1;
root["apiKey"] = getSetting("apiKey");

root["tmpUnits"] = getSetting("tmpUnits", TMP_UNITS).toInt();

#if ENABLE_DOMOTICZ

root["dczVisible"] = 1;
Expand Down
6 changes: 6 additions & 0 deletions code/html/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ function processData(data) {
if (key == "mqttStatus") {
data.mqttStatus = data.mqttStatus ? "CONNECTED" : "NOT CONNECTED";
}
if (key == "tmpUnits") {
$("span#tmpUnit").html(data[key] == 1 ? "ºF" : "ºC");
}

// Look for INPUTs
var element = $("input[name=" + key + "]");
Expand All @@ -305,6 +308,8 @@ function processData(data) {
element
.prop("checked", data[key])
.iphoneStyle("refresh");
} else if (element.attr('type') == 'radio') {
element.val([data[key]]);
} else {
element.val(data[key]);
}
Expand Down Expand Up @@ -342,6 +347,7 @@ function connect(host, port) {
if (typeof port === 'undefined') {
port = location.port;
}
if (websock) websock.close();
websock = new WebSocket('ws://' + host + ':' + port + '/ws');
websock.onopen = function(evt) {
console.log("Connected");
Expand Down
10 changes: 8 additions & 2 deletions code/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ <h2>Current configuration</h2>
</div>

<div class="pure-g module module-ds">
<label class="pure-u-1 pure-u-sm-1-4" for="dsTmp">Temperature (ºC)</label>
<label class="pure-u-1 pure-u-sm-1-4" for="dsTmp">Temperature (<span id="tmpUnit"></span>)</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="dsTmp" readonly />
</div>

<div class="pure-g module module-dht">
<label class="pure-u-1 pure-u-sm-1-4" for="dhtTmp">Temperature (ºC)</label>
<label class="pure-u-1 pure-u-sm-1-4" for="dhtTmp">Temperature (<span id="tmpUnit"></span>)</label>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="dhtTmp" readonly />
</div>

Expand Down Expand Up @@ -247,6 +247,12 @@ <h2>General configuration values</h2>
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="fauxmoEnabled" tabindex="6" /></div>
</div>

<div class="pure-g module module-ds module-dht">
<div class="pure-u-1 pure-u-sm-1-4"><label for="tmpUnits">Temperature units</label></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="radio" name="tmpUnits" tabindex="7" value="0"> Celsius (ºC)</input></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="radio" name="tmpUnits" tabindex="8" value="1"> Fahrenheit (ºF)</input></div>
</div>

</fieldset>
</div>
</div>
Expand Down

0 comments on commit 6402af4

Please sign in to comment.