Python library for reading the ambient room temperature from a sensor and publicizing that data to the local network using MQTT.
This is a Python utility for communicating with a temperature sensor on a
Raspberry Pi in order to read the ambient (room) temperature. This project
supports two different temperature electronic components: a TC74 or a TMP36.
Read below for the pros and cons of each.
```shell script
sudo pip3 install —upgrade pip
curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python
pipenv install
pipenv run python3 temp_to_mqtt.py
### Usage ###
```shell script
pipenv run python3 temp_to_mqtt.py
shell script
cd /etc/systemd/system
sudo ln -s <REPO_ROOT>/temp-mqtt-publish.service
sudo systemctl daemon-reload
sudo systemctl start temp-mqtt-publish.service
To view the logs from the systemctl service, run journalctl -u temp-mqtt-publish.service
.
Sensor | Type | Precision (ºC) | Protocol |
---|---|---|---|
TC74 | digital | ±2 | I2C |
TMP36 | analog | ±2 | SPI (for ADC) |
ADT7410 | digital | ±0.5 | I2C |
HTS221⁺ | digital | ±0.5 | I2C or SPI |
TMP117 | digital | ±0.1 | I2C |
MCP9808 | digital | ±0.0625 | I2C |
⁺ Note: The HTS221 also measuers humidity in addition to temperature.
The TC74 aka TC74A0 aka TC74A0-5.0VAT is a digital temperature sensor from
Adafruit that uses the I2C data protocol to send and receive data from a
controller device (e.g. a Raspberry Pi). Because it is a digital sensor, it
spits out a ready-to-use digital reading of the temperature. This makes it easy
to work with but it is not the most precise temperature sensor.
The TMP36, on the other hand, is a much more precise analog temperature
sensor. Because it’s an analog sensor, you’ll have to use some sort of
Analog-to-Digital Converter (ADC) to convert the variable analog output to a
digital form that you can read and use in scripts. If your microcontroller or
single-board computer (like a Raspberry Pi) does not have a built-in ADC, you
can use the MCP3008 ADC to digitize the analog signals for you.
Here are additional temperature sensors that have not yet been tested. Support
for these sensors will be added to this library after they have been tested.
I2C ports on Raspberry Pi are: GPIO (aka BCM) 2 for Data and GPIO 3 for Clock.
Run the pinout
command to see the pins in the terminal on Rasperry Pi OS.
pinout.xyz is also a good reference.
I2C Purpose | GPIO/BCM | Board Pin |
---|---|---|
SDA (Data) | GPIO 2 | Pin 3 |
SCL (Clock) | GPIO 3 | Pin 5 |
SPI Purpose | GPIO/BCM | Board Pin |
---|---|---|
SCLK (Clock) | GPIO 11 | Pin 23 |
MISO (Master Input Slave Output) | GPIO 9 | Pin 21 |
MOSI (Master Output Slave Input) | GPIO 10 | Pin 19 |
In order to enable the I2C or SPI functionality on a Raspberry Pi, you need to
run sudo raspi-config
and go to Interfacing Options > I2C/SPI > Enable.
Then reboot with sudo reboot
. If you’re using I2C then you should be able to
run sudo i2cdetect -y 1
without getting an error. It should display a blank
I2C address grid.
Source: Adafruit Learn: GPIO Setup
Assuming the TC74 chip used is of the TO-220 form factor, the 5 pins will be in
a straight line and are numbered 1 through 5 starting with 1 on the left and 5
on the right. According to the TC74 datasheet, these pins are:
Note: You probably need to add pull-down resistors to the SDA (I2C Data)
and SCLK (I2C Clock) pins. Otherwise RF interference from the environment might
cause those floating pins to always be “high” (positive voltage).
Connect these pins on the TC74 to your I2C controller or Raspberry Pi as
follows:
Then follow the instructions from the TC74 Library README file
to install and run the code necessary to read the temperature data from the
TC74.
To learn how to connect and interface with the TMP36, follow the TMP36 Guide
by Adafruit. This guide assumes that your microcontroller has an
ADC built-in. If your microcontroller does not or if you are using a Raspberry
Pi, you’ll need to get an MCP3008 and follow the Analog Inputs Guide for
Raspberry Pi, also by Adafruit.
If you’re using the TMP36 sensor with a Raspberry Pi, you’ll have to also use
an ADC chip like the MCP3008. Here is a diagram for how to wire up these
components on a breadboard.