项目作者: garrettheath4

项目描述 :
Python library for reading the ambient room temperature from a sensor and publicizing that data to the local network using MQTT.
高级语言: Python
项目地址: git://github.com/garrettheath4/rpi-temp-mqtt-publish.git
创建时间: 2020-07-29T14:24:36Z
项目社区:https://github.com/garrettheath4/rpi-temp-mqtt-publish

开源协议:

下载


Temperature Sensor to MQTT Publisher for Raspberry Pi

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.

Software

Installation

```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

  1. ### Usage ###
  2. ```shell script
  3. pipenv run python3 temp_to_mqtt.py

System Service

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.

Hardware

Sensor Options

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.

TC74 Sensor

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.

TMP36 Sensor (with MCP3008 ADC)

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.

Other Sensors

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.

Board Pins (on Raspberry Pi)

I2C Pins (for TC74)

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 Pins (for TMP36)

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

Firmware

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

Sensor Interfaces

TC74 Pins

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:

  • Pin 1 (left): NC (Not used)
  • Pin 2: SDA (I2C Data)
  • Pin 3 (middle): GND
  • Pin 4: SCLK (I2C Clock)
  • Pin 5 (right): VDD (power supply)

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:

  • TC74 VDD -> 3.3V
  • TC74 GND -> Ground
  • TC74 SDA -> SDA (I2C Data; GPIO 2 on Raspberry Pi)
  • TC74 SCLK -> SCL (I2C Clock; GPIO 3 on Raspberry Pi)

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.

TMP36 Interface

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.

Breadboard Diagrams

TMP36 Diagram

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.

TMP36 and MCP3008 breadboard schematic