Browse Source

Added support for DS18B20 temperature sensor. Thanks to Francesco Boscarino

fastled
Xose Pérez 8 years ago
parent
commit
51073a8f48
8 changed files with 112 additions and 15 deletions
  1. BIN
      code/data/index.html.gz
  2. +5
    -0
      code/html/index.html
  3. +2
    -0
      code/platformio.ini
  4. +9
    -0
      code/src/config/sensors.h
  5. +13
    -13
      code/src/dht.ino
  6. +70
    -0
      code/src/ds18b20.ino
  7. +6
    -0
      code/src/main.ino
  8. +7
    -2
      code/src/web.ino

BIN
code/data/index.html.gz View File


+ 5
- 0
code/html/index.html View File

@ -112,6 +112,11 @@
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="mqttStatus" readonly />
</div>
<div class="pure-g module module-ds">
<label class="pure-u-1 pure-u-sm-1-4" for="dsTmp">Temperature (ºC)</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>
<input class="pure-u-1 pure-u-sm-3-4" type="text" name="dhtTmp" readonly />


+ 2
- 0
code/platformio.ini View File

@ -13,6 +13,8 @@ lib_deps =
ESPAsyncUDP
Embedis
NtpClientLib
OneWire
DallasTemperature
https://bitbucket.org/xoseperez/justwifi.git
https://bitbucket.org/xoseperez/nofuss.git
https://bitbucket.org/xoseperez/hlw8012.git


+ 9
- 0
code/src/config/sensors.h View File

@ -20,6 +20,15 @@
#define DHT_TEMPERATURE_TOPIC "/temperature"
#define DHT_HUMIDITY_TOPIC "/humidity"
//--------------------------------------------------------------------------------
// DS18B20 temperature sensor
// Enable support by passing ENABLE_DS18B20=1 build flag
//--------------------------------------------------------------------------------
#define DS_PIN 14
#define DS_UPDATE_INTERVAL 60000
#define DS_TEMPERATURE_TOPIC "/temperature"
//--------------------------------------------------------------------------------
// Custom current sensor
// Check http://tinkerman.cat/your-laundry-is-done/


+ 13
- 13
code/src/dht.ino View File

@ -14,19 +14,19 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING);
char temperature[6];
char humidity[6];
char dhtTemperature[6];
char dhtHumidity[6];
// -----------------------------------------------------------------------------
// DHT
// -----------------------------------------------------------------------------
char * getTemperature() {
return temperature;
char * getDHTTemperature() {
return dhtTemperature;
}
char * getHumidity() {
return humidity;
char * getDHTHumidity() {
return dhtHumidity;
}
void dhtSetup() {
@ -53,19 +53,19 @@ void dhtLoop() {
} else {
dtostrf(t, 4, 1, temperature);
itoa((int) h, humidity, 10);
dtostrf(t, 4, 1, dhtTemperature);
itoa((int) h, dhtHumidity, 10);
DEBUG_MSG("[DHT] Temperature: %s\n", temperature);
DEBUG_MSG("[DHT] Humidity: %s\n", humidity);
DEBUG_MSG("[DHT] Temperature: %s\n", dhtTemperature);
DEBUG_MSG("[DHT] Humidity: %s\n", dhtHumidity);
// Send MQTT messages
mqttSend((char *) getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
mqttSend((char *) getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
mqttSend((char *) getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), dhtTemperature);
mqttSend((char *) getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), dhtHumidity);
// 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}"), dhtTemperature, dhtHumidity);
wsSend(buffer);
}


+ 70
- 0
code/src/ds18b20.ino View File

@ -0,0 +1,70 @@
/*
ESPurna
SD18B20 MODULE
Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
*/
#if ENABLE_DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(DS_PIN);
DallasTemperature ds18b20(&oneWire);
char dsTemperature[6];
// -----------------------------------------------------------------------------
// DS18B20
// -----------------------------------------------------------------------------
char * getDSTemperature() {
return dsTemperature;
}
void dsSetup() {
ds18b20.begin();
}
void dsLoop() {
if (!mqttConnected()) return;
// Check if we should read new data
static unsigned long last_update = 0;
if ((millis() - last_update > DS_UPDATE_INTERVAL) || (last_update == 0)) {
last_update = millis();
// Read sensor data
ds18b20.requestTemperatures();
double t = ds18b20.getTempCByIndex(0);
// Check if readings are valid
if (isnan(t)) {
DEBUG_MSG("[DS18B20] Error reading sensor\n");
} else {
dtostrf(t, 4, 1, dsTemperature);
DEBUG_MSG("[DS18B20] Temperature: %s\n", dsTemperature);
// Send MQTT messages
mqttSend((char *) getSetting("dsTmpTopic", DS_TEMPERATURE_TOPIC).c_str(), dsTemperature);
// Update websocket clients
char buffer[100];
sprintf_P(buffer, PSTR("{\"dsVisible\": 1, \"dsTmp\": %s}"), dsTemperature);
wsSend(buffer);
}
}
}
#endif

+ 6
- 0
code/src/main.ino View File

@ -155,6 +155,9 @@ void setup() {
#if ENABLE_POW
powSetup();
#endif
#if ENABLE_DS18B20
dsSetup();
#endif
#if ENABLE_DHT
dhtSetup();
#endif
@ -183,6 +186,9 @@ void loop() {
#if ENABLE_POW
powLoop();
#endif
#if ENABLE_DS18B20
dsLoop();
#endif
#if ENABLE_DHT
dhtLoop();
#endif


+ 7
- 2
code/src/web.ino View File

@ -207,10 +207,15 @@ void _wsStart(uint32_t client_id) {
root["fauxmoEnabled"] = getSetting("fauxmoEnabled", String(FAUXMO_ENABLED)).toInt() == 1;
#endif
#if ENABLE_DS18B20
root["dsVisible"] = 1;
root["dsTmp"] = getDSTemperature();
#endif
#if ENABLE_DHT
root["dhtVisible"] = 1;
root["dhtTmp"] = getTemperature();
root["dhtHum"] = getHumidity();
root["dhtTmp"] = getDHTTemperature();
root["dhtHum"] = getDHTHumidity();
#endif
#if ENABLE_RF


Loading…
Cancel
Save