Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Dallas 18B20 temperature sensor
- Loading branch information
Showing
10 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright © 2017 Simon Rupf | ||
* | ||
* This file is part of MonSens. | ||
* | ||
* MonSens is free software: you can redistribute it and/or modify it under the | ||
* terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* MonSens is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with MonSens. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* @author Simon Rupf <simon@rupf.net> | ||
* @brief MonSens implementation of the Dallas 18B20 temperature sensor. | ||
* | ||
* The Dallas 18B20 temperature sensor allows multiple sensors to be connected | ||
* on a shared bus. These can be powered descretely on a separate wire or via | ||
* a "parasitic" mode over the serial pin, allowing 3 or 2 wire setups. | ||
*/ | ||
|
||
#include <MonSens_DallasTemperature.h> | ||
|
||
/** | ||
* Inject the configured Dallas temperature sensor bus. | ||
*/ | ||
void MonSens_DallasTemperature::setDallas(DallasTemperature &dallas) { | ||
sensor = dallas; | ||
} | ||
|
||
/** | ||
* Set the index of the current sensor (optional), if more then one. | ||
*/ | ||
void MonSens_DallasTemperature::setIndex(uint8_t index) { | ||
sensorIndex = index; | ||
} | ||
|
||
/** | ||
* After it is registered in the communicator, the sensor gets initialized. | ||
*/ | ||
void MonSens_DallasTemperature::init() { | ||
} | ||
|
||
/** | ||
* Take a sensor reading, to be returned by the communicator. | ||
*/ | ||
bool MonSens_DallasTemperature::measure(const char* input) { | ||
if (strstr(input, "C") != NULL) { | ||
sensor.requestTemperaturesByIndex(sensorIndex); | ||
reading = sensor.getTempCByIndex(sensorIndex); | ||
} else if (strstr(input, "K") != NULL) { | ||
sensor.requestTemperaturesByIndex(sensorIndex); | ||
reading = sensor.getTempCByIndex(sensorIndex) + 273.15F; | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* If no sensor supports the input value, usage instructions are collected. | ||
*/ | ||
const char* MonSens_DallasTemperature::getUsage() { | ||
return MonSens_DallasTemperature_Usage; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright © 2017 Simon Rupf | ||
* | ||
* This file is part of MonSens. | ||
* | ||
* MonSens is free software: you can redistribute it and/or modify it under the | ||
* terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation, either version 3 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* MonSens is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with MonSens. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* @author Simon Rupf <simon@rupf.net> | ||
* @brief MonSens implementation of the Dallas 18B20 temperature sensor. | ||
* | ||
* The Dallas 18B20 temperature sensor allows multiple sensors to be connected | ||
* on a shared bus. These can be powered descretely on a separate wire or via | ||
* a "parasitic" mode over the serial pin, allowing 3 or 2 wire setups. | ||
*/ | ||
|
||
#ifndef MONSENS_DALLASTEMPERATURE_H | ||
#define MONSENS_DALLASTEMPERATURE_H | ||
|
||
#include <MonSens.h> | ||
#include <OneWire.h> | ||
#include <DallasTemperature.h> | ||
|
||
const char MonSens_DallasTemperature_Usage[] PROGMEM = { | ||
"C - temperature in Celsius\r\nK - temperature in Kelvin" | ||
}; | ||
|
||
/** | ||
* MonSens implementation of the Dallas 18B20 temperature sensor. | ||
*/ | ||
class MonSens_DallasTemperature: public IMonSens_Sensor { | ||
public: | ||
/** | ||
* Inject the configured Dallas temperature sensor bus. | ||
*/ | ||
void setDallas(DallasTemperature &dallas); | ||
|
||
/** | ||
* Set the index of the current sensor (optional), if more then one. | ||
*/ | ||
void setIndex(uint8_t index); | ||
|
||
/** | ||
* After it is registered in the communicator, the sensor gets initialized. | ||
*/ | ||
void init(); | ||
|
||
/** | ||
* Take a sensor reading, to be returned by the communicator. | ||
*/ | ||
bool measure(const char* input); | ||
|
||
/** | ||
* If no sensor supports the input value, usage instructions are collected. | ||
*/ | ||
const char* getUsage(); | ||
|
||
private: | ||
/** | ||
* Index of the current sensor on the bus, defaults to 0 | ||
*/ | ||
uint8_t sensorIndex = 0; | ||
|
||
/** | ||
* sensor interface | ||
*/ | ||
DallasTemperature sensor; | ||
}; | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
Dallas 18B20 | ||
============ | ||
|
||
The Maxim (formerly Dallas) Dallas 18B20 sensor measures temperature. There are | ||
a few variants of the sensor around and it is available in multiple ready-to-use | ||
packagings, like in a water-tight metal cylinder or on a break-out board. | ||
|
||
Use cases | ||
--------- | ||
|
||
This sensor is relatively precise and very cheap, making it ideal for remote | ||
thermometer use. | ||
|
||
Using it with DigiSpark MCUs and USB extension cables where needed, you could | ||
place sensors in front and back, top and bottom of a server rack, to monitor | ||
temperatures of the air in- and out-flow. | ||
|
||
When used with ESP8266 MCUs, it could be used to monitor temperature in various | ||
rooms, even without a network port. Optionally batteries could be used in such | ||
a case, to make to project truly wireless. | ||
|
||
Limitations | ||
----------- | ||
|
||
The sensor uses one wire protocol to both sending and receiving on a single pin. | ||
Multiple sensors may be connected to the same wire and addressed by index, but | ||
at this time only a single temperature can be requested, so multiple Dallas | ||
18B20 on the same MCU can't be differenciated. | ||
|
||
The default power mode requires three connections to the sensor: Vcc (both 3.3 | ||
and 5 V will work), ground and the serial signal. Optionally the sensor can be | ||
used in "parasite" mode: You shorten the _sensors_ Vcc to ground and connect | ||
ground and serial signal to the MCU (note that this still requires a resistor | ||
between Vcc and the serial pin on the MCU side). This allows you to use just to | ||
wires to the sensor. On the other hand, many issues were reported with | ||
"parasite" mode and longer wire lengths between MCU and sensor, when this might | ||
be considered a benefit. | ||
|
||
Wiring | ||
------ | ||
|
||
When using a bare Dallas 18B20 (TO-92 package, frequently used for transistors) | ||
you need to have a 4.7 kΩ resistor between Vcc and the pin used for serial | ||
communication. Breakout boards usually have such a resistor on board. | ||
|
||
The following is an example for connecting Dallas 18B20 to a NodeMCU on `D4`. | ||
|
||
![Dallas 18B20 connected to a NodeMCU](https://raw.githubusercontent.com/elrido/MonSens/master/sensors/ds18B20/ds18B20%20nodemcu.png) | ||
|
||
Here below is an example for connecting Dallas 18B20 to a DigiSpark. | ||
|
||
![Dallas 18B20 connected to a DigiSpark](https://raw.githubusercontent.com/elrido/MonSens/master/sensors/ds18B20/ds18B20%20digispark.png) | ||
|
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* MonSens Dallas 18B20 temperature sensor on a ESP8266 MCU example sketchbook | ||
* | ||
* This sketchbook shows you how to use the Dallas 18B20 on a ESP8266 and | ||
* expose it via TCP port 30303 to read the sensor data. | ||
*/ | ||
|
||
// the following constants need to be changed according to your WiFi settings | ||
const char* ssid = "your WiFi's SSID"; | ||
const char* password = "your own super secret WiFi password"; | ||
|
||
// this is the port that your monitoring system may read the sensors at | ||
const int port = 30303; | ||
|
||
// (optional) conserve memory by reducing the reserved space for the following | ||
// variables | ||
// maximum sensors supported, defaults to 10 (this example only uses 2) | ||
#define MONSENS_MAX_SENSORS 10 | ||
// maximum length of measurement strings, defaults to 9 (5 digits, 1 decimal | ||
// point, 2 digits after the point and 1 string termination character) | ||
#define MONSENS_MAX_MEASUREMENT_WIDTH 9 | ||
|
||
#include <MonSens_ESP8266.h> | ||
MonSens_ESP8266 mcu; | ||
|
||
#include <MonSens_DallasTemperature.h> | ||
|
||
// Digital pin connected to the serial pin of the Dallas temperature sensor | ||
OneWire oneWire(D4); | ||
DallasTemperature dallas(&oneWire); | ||
|
||
MonSens_DallasTemperature temp; | ||
|
||
/** | ||
* initialize ESP8266 and sensors | ||
*/ | ||
void setup() { | ||
Serial.begin(115200); | ||
delay(10); | ||
|
||
mcu.setSsid(ssid); | ||
mcu.setPassword(password); | ||
mcu.setPort(port); | ||
mcu.init(); | ||
|
||
temp.setDallas(dallas); | ||
mcu.addSensor(temp); | ||
} | ||
|
||
/** | ||
* main loop | ||
* | ||
* read string sent to server, then evaluate it and print an according result | ||
*/ | ||
void loop() { | ||
mcu.communicate(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters