Merge pull request #22192 from alim0x/master

[translated]How I monitor my greenhouse with CircuitPython and open s…
This commit is contained in:
Xingyu.Wang 2021-06-12 11:39:51 +08:00 committed by GitHub
commit 9efbbf6e40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 280 additions and 287 deletions

View File

@ -1,287 +0,0 @@
[#]: subject: (How I monitor my greenhouse with CircuitPython and open source tools)
[#]: via: (https://opensource.com/article/21/5/monitor-greenhouse-open-source)
[#]: author: (Darin London https://opensource.com/users/dmlond)
[#]: collector: (lujun9972)
[#]: translator: (alim0x)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
How I monitor my greenhouse with CircuitPython and open source tools
======
Keep track of your greenhouse's temperature, humidity, and ambient light
using a microcontroller, sensors, Python, and MQTT.
![Greenhouse garden with tomatoes][1]
CircuitPython provides a revolutionary way to interact with microcontroller boards. This article explains how to use CircuitPython to measure a greenhouse's temperature, humidity, and ambient light and publish the results to an [MQTT][2] broker using a CircuitPython MQTT client. You can subscribe any number of programs to the MQTT queues to process the information further.
This project uses a simple Python program that runs a web server that publishes a Prometheus-formatted scrape endpoint and pulls these metrics into [Prometheus][3] for ongoing monitoring.
### About CircuitPython
[CircuitPython][4] is an open source Python distribution created by [Adafruit][5] to run on low-cost microcontroller boards. CircuitPython provides a simple development experience for interacting with [compatible boards][6]. You can start a program on the board by creating a `code.py` file in the `CIRCUITPYTHON` root drive that mounts when you connect your board. CircuitPython also provides a serial connection from your board that includes an interactive read-evaluate-print loop (REPL) session that you can use to interact with your board in real time using Python code.
Adafruit's website offers extensive documentation to help you get started with CircuitPython. First, consult the [Welcome to CircuitPython][7] guide. This will get you started running code on your microcontroller with CircuitPython and interacting with the REPL. It also documents how to install Adafruit's bundle of CircuitPython libraries and examples for many of the boards and sensors it sells. Next, read the [CircuitPython Essentials][8] guide to learn more about its capabilities and link to information about using CircuitPython with specific, compatible boards. Finally, as with all open source software, you can dig into [CircuitPython's code][9], post issues, and contribute.
### Microcontroller setup
The microcontroller system is very simple. To follow along with this demo, you will need:
* **Raspberry Pi 4:** You need a computer to program the microcontroller system, and this is what I used. 
* **A CircuitPython-compatible microcontroller:** I used the [Adafruit FeatherS2][10] with built-in WiFi, ambient light sensor, and Qwiic cable input.
* **Microcontroller WiFi:** The FeatherS2 has a built-in WiFi radio. If your microcontroller does not, you will need to find a WiFi expansion board for it.
* **Sensors:** The Feather S2 has a built-in ambient light sensor, so I needed to get a temperature and humidity sensor. A variety is available from vendors, including Adafruit, SparkFun, and Amazon. I used an [Adafruit sensor][11] with a Qwiic cable connection compatible with the Feather S2 input. You may have to find CircuitPython-compatible Python libraries for sensors not purchased from Adafruit, although many SparkFun sensors work with Adafruit libraries.
* **Jumpers and cables:** To avoid using a breadboard or soldering, I used an [Adafruit Qwiic cable][12]. SparkFun also sells them in a [bundle of cables][13] of different lengths.
Before plugging the microcontroller into your computer, connect the sensors to the microcontroller.
![Connecting sensors to microcontroller][14]
(Darin London, [CC BY-SA 4.0][15])
Now you can plug the microcontroller into your computer using a USB data cable.
### The MQTT Broker
You can use [these instructions][16] to install the [Mosquitto MQTT broker][17] and Mosquitto clients on a Raspberry Pi 4 running Raspberry Pi OS. If you want to use the Raspberry Pi as a long-term server, set a static IP address for the Raspberry Pi 4 on your network. Once the Mosquitto broker is running, create a [user/password file][18] that sets the authentication parameters for clients to use when publishing and subscribing to the broker.
You can test the MQTT broker using the Mosquitto clients on the Raspberry Pi. Open two terminals (or SSH sessions if you are running headless):
On Terminal 1, enter:
```
`mosquitto_sub -h localhost -u $user -P $pass -t "mqtt/test"`
```
This will start a long-running process that listens for messages published to the `mqtt/test` queue.
On Terminal 2, enter:
```
`mosquitto_pub -h localhost -u $user -P $pass -t "mqtt/test" -m hello`
```
This will publish a message to the `mqtt/test` queue, which should show up in Terminal 1's output.
You can then kill the `sub` command running on Terminal 1.
The Mosquitto broker allows clients to publish messages to any queue, even if it has no subscribers. These messages will be lost forever, but they will not stop the client from publishing.
Start a third terminal and subscribe to the following queues (the queues your microcontroller will publish messages to):
* greenhouse/temperature
* greenhouse/light
* greenhouse/humidity
### Code the microcontroller
You are now ready to code your microcontroller to publish its metrics to the MQTT broker running on your Raspberry Pi 4.
Adafruit has [excellent documentation][19] on using the [CircuitPython Library Bundle][20]'s libraries to connect your microcontroller to your WiFi router and get it publishing metrics to your MQTT broker.
Install the following libraries, which the greenhouse monitor will use, into the `CIRCUITPYTHON/lib` directory. These are all available in the Adafruit CircuitPython Library Bundle:
* **adafruit_bus_device:** A Python package directory with multiple .mpy files (.mpy is a compressed Python file that saves space on your microcontroller)
* **adafruit_requests:** A single .mpy file
* **adafruit_register:** Another package directory
* **adafruit_minimqtt:** Another package directory
* **adafruit_si7021:** A single .mpy file that works with the temperature and humidity sensors
Once these libraries are installed, write the following into `code.py` in the `CIRCUITPYTHON` directory:
```
import time
import ssl
import socketpool
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import board
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn
import adafruit_si7021
 
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
        from secrets import secrets
except ImportError:
        print("WiFi secrets are kept in secrets.py, please add them there!")
        raise
 
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
### Feeds ###
light_feed = "greenhouse/light"
temp_feed = "greenhouse/temperature"
humidity_feed = "greenhouse/humidity"
 
# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connected(client, userdata, flags, rc):
        # This function will be called when the client is connected
        # successfully to the broker.
        print("Connected to MQTT!")
 
def disconnected(client, userdata, rc):
        # This method is called when the client is disconnected
        print("Disconnected from MQTT!")
 
 
def get_voltage(pin):
        return (pin.value * 3.3) / 65536
 
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
 
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
        broker=secrets["broker"],
        port=secrets["port"],
        username=secrets["aio_username"],
        password=secrets["aio_key"],
        socket_pool=pool,
        ssl_context=ssl.create_default_context(),
)
 
# Setup the callback methods above
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
 
# Connect the client to the MQTT broker.
print("Connecting to MQTT...")
mqtt_client.connect()
 
# Create library object using our Bus I2C port
sensor = adafruit_si7021.SI7021(board.I2C())
light_pin = AnalogIn(board.IO4)
 
while True:
        # Poll the message queue
        mqtt_client.loop()
 
        # get the current temperature
        light_val = get_voltage(light_pin)
        temp_val = ((sensor.temperature * 9)/5) + 32
        humidity_val = sensor.relative_humidity
 
        # Send a new messages
        mqtt_client.publish(light_feed, light_val)
        mqtt_client.publish(temp_feed, temp_val)
        mqtt_client.publish(humidity_feed, humidity_val)
        time.sleep(0.5)
```
Save your code. Then attach to the serial monitor and watch it connect to your MQTT broker. You can also see the output by switching to the terminals on your Raspberry Pi 4 subscribed to the queues where this publishes.
### Process the metrics
Publish/subscribe workflows like MQTT offer many advantages for microcontroller systems. You can have multiple microcontroller + sensor installations reporting different metrics about the same system or reporting many readings of the same metric in parallel. You can also have many different processes that subscribe to each queue to respond to these messages in parallel. It is even possible to have multiple different processes subscribed to the same queue for different actions, such as sending an email when a value gets too high or publishing a message to another MQTT queue.
Another option is to have a microcontroller subscribe to an external queue that sends signals to tell the microcontroller to perform an action, such as turning off or starting a new session. Finally, pub/sub workflows can be better for low-power microcontroller installations (such as those using battery or solar power) because these devices can send metrics in batches separated by long delays and turn off the power-hungry WiFi radio during the intervals between reports.
To process these metrics, I created a Python client that uses the [Paho Python MQTT client][21] to subscribe to the metrics queues. I also use the official [Prometheus Python client][22] to create a web server that produces a Prometheus-compliant scrape endpoint with these metrics as gauges. I run this, a [Prometheus server][23], and the Mosquitto MQTT broker on the same Raspberry Pi 4.
```
from prometheus_client import start_http_server, Gauge
import random
import time
import paho.mqtt.client as mqtt
gauge = {
  "greenhouse/light": Gauge('light','light in lumens'),
  "greenhouse/temperature": Gauge('temperature', 'temperature in fahrenheit'),
  "greenhouse/humidity": Gauge('humidity','relative % humidity')
}
try:
        from mqtt_secrets import mqtt_secrets
except ImportError:
        print("WiFi secrets are kept in secrets.py, please add them there!")
        raise
def on_connect(client, userdata, flags, rc):
        print("Connected with result code "+str(rc))
        # Subscribing in on_connect() means that if we lose the connection and
        # reconnect then subscriptions will be renewed.
        client.subscribe("greenhouse/light")
        client.subscribe('greenhouse/temperature')
        client.subscribe('greenhouse/humidity')
def on_message(client, userdata, msg):
        topic = msg.topic
        payload = msg.payload
        gauge[topic].set(payload)
client = mqtt.Client()
client.username_pw_set(mqtt_secrets["mqtt_user"],mqtt_secrets['mqtt_password'])
client.on_connect = on_connect
client.on_message = on_message
client.connect('localhost',1883,60)
if __name__ == '__main__':
        # Start up the server to expose the metrics.
        client = mqtt.Client()
        client.username_pw_set('london','abc123')
        client.on_connect = on_connect
        client.on_message = on_message
        client.connect('localhost',1883,60)
        start_http_server(8000)
        client.loop_forever()
```
Then I configure the Prometheus server to scrape that endpoint on localhost:8000.
You can access all the code for this project in my MIT-licensed [Greenhouse MQTT Microcontroller][24] repository on GitHub.
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/5/monitor-greenhouse-open-source
作者:[Darin London][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/dmlond
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/zanda-photography-unsplash-greenhouse.jpg?itok=ZMnZyNMM (Greenhouse garden with tomatoes)
[2]: https://mqtt.org/
[3]: https://prometheus.io/
[4]: https://circuitpython.io/
[5]: https://adafruit.com
[6]: https://circuitpython.org/downloads
[7]: https://learn.adafruit.com/welcome-to-circuitpython
[8]: https://learn.adafruit.com/circuitpython-essentials/circuitpython-essentials
[9]: https://github.com/adafruit/circuitpython
[10]: https://www.adafruit.com/product/4769
[11]: https://www.adafruit.com/product/3251
[12]: https://www.adafruit.com/product/4399
[13]: https://www.sparkfun.com/products/15081
[14]: https://opensource.com/sites/default/files/uploads/connectsensors-microcontroller.jpg (Connecting sensors to microcontroller)
[15]: https://creativecommons.org/licenses/by-sa/4.0/
[16]: https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/
[17]: https://mosquitto.org/
[18]: https://mosquitto.org/documentation/authentication-methods/
[19]: https://learn.adafruit.com/mqtt-in-circuitpython
[20]: https://circuitpython.org/libraries
[21]: https://pypi.org/project/paho-mqtt/
[22]: https://pypi.org/project/prometheus-client
[23]: https://opensource.com/article/21/3/iot-measure-raspberry-pi
[24]: https://github.com/dmlond/greenhouse_mqtt_microcontroller

View File

@ -0,0 +1,280 @@
[#]: subject: (How I monitor my greenhouse with CircuitPython and open source tools)
[#]: via: (https://opensource.com/article/21/5/monitor-greenhouse-open-source)
[#]: author: (Darin London https://opensource.com/users/dmlond)
[#]: collector: (lujun9972)
[#]: translator: (alim0x)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
我如何用 CircuitPython 和开源工具监控温室
======
使用微控制器、传感器、Python 以及 MQTT 持续追踪温室的温度、湿度以及环境光。
![种有西红柿的温室花园][1]
CircuitPython 提供了一种革命性的方式来和微控制器板进行交互。这篇文章介绍了如何使用 CircuitPython 来监测温室的温度、湿度以及环境光,并且使用 CircuitPython MQTT 客户端将结果发布到一个 [MQTT][2] broker。你可以在若干个程序中订阅 MQTT 队列并进一步处理信息。
这个项目使用一个简单的 Python 程序来运行 web 服务器,它发布一个 Prometheus 格式的采集端点,拉取监控指标到 [Prometheus][3] 进行不间断的监控。
### 关于 CircuitPython
[CircuitPython][4] 是一个由 [Adafruit][5] 创建的开源 Python 发行版用于在低成本微控制器开发板上运行。CircuitPython 在与[兼容的开发板][6]的交互上,提供了一个简单的开发体验。你可以在连接你的开发板时挂载的 `CIRCUITPYTHON` 根驱动器上创建一个 `code.py` 文件来开始你的程序。CircuitPython 还为开发板提供了一个串行连接包含一个交互式解释器REPL会话你可以使用 Python 代码实时和开发板进行交互。
Adafruit 的网站提供了更广泛的文档来帮助你开始使用 CircuitPython。首先考虑看下这份指南[欢迎来到 CircuitPython][7]。这份指南能够帮助你开始使用 CircuitPython 在开发板上运行代码以及和 REPL 交互。它还记录了如何安装 Adafruit 的 CircuitPython 库文件包,以及出售的开发板和传感器的一些示例。接下来,阅读 [CircuitPython 基础][8]来学习更多关于它的能力的信息,里面还有链接指向在特定及兼容的开发板上使用 CircuitPython 的相关信息。最后,就如所有开源软件一样,你可以深入 [CircuitPython 的源码][9],发布 issue以及做出贡献。
### 微控制器设置
微控制器系统非常简单。要完成这个示例项目,你会需要:
* **Raspberry Pi 4**你需要一台电脑来给微控制器系统编程,我用的是树莓派 4。
* **CircuitPython 兼容的微控制器:**我用的是 [Adafruit FeatherS2][10]带有内置环境光传感器Qwiic 线缆输入。
* **微控制器 WiFi**FeatherS2 内置了 WiFi。如果你的微控制器没有你需要给开发板找个 WiFi 扩展板。
* **传感器:**Feather S2 有个内置的环境光传感器,所以我还需要一个温湿度传感器。有很多不同厂商的产品可以选择,包括 Adafruit、SparkFun、亚马逊。我用的是一个 [Adafruit 传感器][11],带有 Feather S2 输入兼容的 Qwiic 线缆。尽管多数 SparkFun 传感器可以在 Adafruit 库下工作,但如果你不是从 Adafruit 购买的传感器,你可能还是需要自己去找到它兼容 CircuitPython 的 Python 库。
* **跳线和线缆:**为了避免使用面包板或焊接,我使用 [Adafruit Qwiic 线缆][12]。SparkFun 销售的包含不同长度的[线缆套装][13]中也有它。
在将微控制器连接到你的电脑之前,将传感器连接到微控制器上。
![将传感器连接到微控制器上][14]
(Darin London, [CC BY-SA 4.0][15])
现在你可以将微控制器用 USB 数据线连接到你的电脑。
### MQTT Broker
你可以使用[这份说明][16]来在树莓派的系统上安装 [Mosquitto MQTT broker][17] 和 Mosquitto 客户端。如果你想把树莓派做为长期服务器使用,在你的网络上给树莓派 4 设置一个静态 IP 地址。Mosquitto broker 运行起来之后,创建一份[用户名/密码文件][18],设置客户端向 broker 发布和订阅消息时用的认证信息。
你可以用树莓派上的 Mosquitto 客户端来测试 MQTT broker。打开两个终端如果你是无界面运行的话打开两个 SSH 会话):
在终端一输入:
```
`mosquitto_sub -h localhost -u $user -P $pass -t "mqtt/test"`
```
这条命令会启动一个持续运行的进程,监听发布到 `mqtt/test` 队列的消息。
在终端二输入:
```
`mosquitto_pub -h localhost -u $user -P $pass -t "mqtt/test" -m hello`
```
这条命令会向 `mqtt/test` 队列发布一条消息,它应该会显示在终端一的输出里。
现在你可以中止终端一运行的 `sub` 命令了。
Mosquitto broker 允许客户端发布消息到任何队列,甚至没有任何订阅的队列也可以。这些消息会永久丢失,但这不会阻止客户端继续发布消息。
打开第三个终端,订阅下列队列(你的控制器会发布消息到这些队列上):
* greenhouse/temperature
* greenhouse/light
* greenhouse/humidity
### 给微控制器编码
现在你已经准备好给微控制器编码,发布它的监测指标到树莓派 4 上运行的 MQTT broker 上了。
Adafruit 有[出色的文档][19],指导你使用 [CircuitPython Library Bundle][20] 的库来将你的微控制器连接到 WiFi 路由器,并发布监测指标到 MQTT broker 上。
安装下列库到 `CIRCUITPYTHON/lib` 目录,温室监控会用到它们。这些库在 Adafruit 的 CircuitPython Library Bundle 中都有提供:
* **adafruit_bus_device**一个带有多个 .mpy 文件的 Python 包文件夹(.mpy 是经过压缩的 Python 文件,用以节省空间)
* **adafruit_requests**单个 .mpy 文件
* **adafruit_register**一个包文件夹
* **adafruit_minimqtt**一个包文件夹
* **adafruit_si7021**单个 .mpy 文件,用来支持温湿度传感器
库装好了之后,将以下代码写入 `CIRCUITPYTHON` 文件夹的 `code.py` 文件中:
```
import time
import ssl
import socketpool
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import board
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn
import adafruit_si7021
 
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
        from secrets import secrets
except ImportError:
        print("WiFi secrets are kept in secrets.py, please add them there!")
        raise
 
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
### Feeds ###
light_feed = "greenhouse/light"
temp_feed = "greenhouse/temperature"
humidity_feed = "greenhouse/humidity"
 
# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connected(client, userdata, flags, rc):
        # This function will be called when the client is connected
        # successfully to the broker.
        print("Connected to MQTT!")
 
def disconnected(client, userdata, rc):
        # This method is called when the client is disconnected
        print("Disconnected from MQTT!")
 
 
def get_voltage(pin):
        return (pin.value * 3.3) / 65536
 
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
 
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
        broker=secrets["broker"],
        port=secrets["port"],
        username=secrets["aio_username"],
        password=secrets["aio_key"],
        socket_pool=pool,
        ssl_context=ssl.create_default_context(),
)
 
# Setup the callback methods above
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
 
# Connect the client to the MQTT broker.
print("Connecting to MQTT...")
mqtt_client.connect()
 
# Create library object using our Bus I2C port
sensor = adafruit_si7021.SI7021(board.I2C())
light_pin = AnalogIn(board.IO4)
 
while True:
        # Poll the message queue
        mqtt_client.loop()
 
        # get the current temperature
        light_val = get_voltage(light_pin)
        temp_val = ((sensor.temperature * 9)/5) + 32
        humidity_val = sensor.relative_humidity
 
        # Send a new messages
        mqtt_client.publish(light_feed, light_val)
        mqtt_client.publish(temp_feed, temp_val)
        mqtt_client.publish(humidity_feed, humidity_val)
        time.sleep(0.5)
```
保存你的代码。然后连接到串行监视器,看程序连接到你的 MQTT broker。你还可以将树莓派 4 上的终端切换到订阅了它的发布队列的终端来查看输出。
### 处理监测指标
像 MQTT 的发布/订阅工作流给微控制器系统提供了诸多好处。你可以有多个微控制器+传感器来回报同一个系统的不同指标或并行回报相同指标的若干读数。你还可以有多个不同进程订阅各个队列,并行地对这些消息进行回应。甚至还可以有多个进程订阅相同的队列,对消息做出不同的动作,比如数值过高时发送通知邮件或将消息发送到另一个 MQTT 队列上去。
另一个选项是让一个微控制器订阅一个外部队列,可以发送信号告诉微控制器做出动作,比如关闭或开始一个新会话。最后,发布/订阅工作流对低功耗微控制器系统更佳(比如那些使用电池或太阳能的系统),因为这些设备可以在更长的延迟周期后批量发布监测指标,并在回报的间隔期间关闭大量消耗电量的 WiFi 广播。
要处理这些监测指标,我创建了一个 Python 客户端,使用 [Paho Python MQTT 客户端][21]订阅监测指标队列。我还使用官方的 [Prometheus Python 客户端][22] 创建了一个 web 服务器,它产生一个符合 Prometheus 标准的采集端点,使用这些监测指标作为面板信息。[Prometheus 服务器][23]和 Mosquitto MQTT broker 我都是运行在同一个树莓派 4 上的。
```
from prometheus_client import start_http_server, Gauge
import random
import time
import paho.mqtt.client as mqtt
gauge = {
  "greenhouse/light": Gauge('light','light in lumens'),
  "greenhouse/temperature": Gauge('temperature', 'temperature in fahrenheit'),
  "greenhouse/humidity": Gauge('humidity','relative % humidity')
}
try:
        from mqtt_secrets import mqtt_secrets
except ImportError:
        print("WiFi secrets are kept in secrets.py, please add them there!")
        raise
def on_connect(client, userdata, flags, rc):
        print("Connected with result code "+str(rc))
        # Subscribing in on_connect() means that if we lose the connection and
        # reconnect then subscriptions will be renewed.
        client.subscribe("greenhouse/light")
        client.subscribe('greenhouse/temperature')
        client.subscribe('greenhouse/humidity')
def on_message(client, userdata, msg):
        topic = msg.topic
        payload = msg.payload
        gauge[topic].set(payload)
client = mqtt.Client()
client.username_pw_set(mqtt_secrets["mqtt_user"],mqtt_secrets['mqtt_password'])
client.on_connect = on_connect
client.on_message = on_message
client.connect('localhost',1883,60)
if __name__ == '__main__':
        # Start up the server to expose the metrics.
        client = mqtt.Client()
        client.username_pw_set('london','abc123')
        client.on_connect = on_connect
        client.on_message = on_message
        client.connect('localhost',1883,60)
        start_http_server(8000)
        client.loop_forever()
```
然后我配置 Prometheus 服务器采集端点数据到 localhost:8000。
你可以在 Github 上访问 [Greenhouse MQTT Microcontroller][24] 这个项目的代码,项目采用 MIT 许可证授权。
--------------------------------------------------------------------------------
via: https://opensource.com/article/21/5/monitor-greenhouse-open-source
作者:[Darin London][a]
选题:[lujun9972][b]
译者:[alim0x](https://github.com/alim0x)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/dmlond
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/zanda-photography-unsplash-greenhouse.jpg?itok=ZMnZyNMM (Greenhouse garden with tomatoes)
[2]: https://mqtt.org/
[3]: https://prometheus.io/
[4]: https://circuitpython.io/
[5]: https://adafruit.com
[6]: https://circuitpython.org/downloads
[7]: https://learn.adafruit.com/welcome-to-circuitpython
[8]: https://learn.adafruit.com/circuitpython-essentials/circuitpython-essentials
[9]: https://github.com/adafruit/circuitpython
[10]: https://www.adafruit.com/product/4769
[11]: https://www.adafruit.com/product/3251
[12]: https://www.adafruit.com/product/4399
[13]: https://www.sparkfun.com/products/15081
[14]: https://opensource.com/sites/default/files/uploads/connectsensors-microcontroller.jpg (Connecting sensors to microcontroller)
[15]: https://creativecommons.org/licenses/by-sa/4.0/
[16]: https://pimylifeup.com/raspberry-pi-mosquitto-mqtt-server/
[17]: https://mosquitto.org/
[18]: https://mosquitto.org/documentation/authentication-methods/
[19]: https://learn.adafruit.com/mqtt-in-circuitpython
[20]: https://circuitpython.org/libraries
[21]: https://pypi.org/project/paho-mqtt/
[22]: https://pypi.org/project/prometheus-client
[23]: https://opensource.com/article/21/3/iot-measure-raspberry-pi
[24]: https://github.com/dmlond/greenhouse_mqtt_microcontroller