python 使用Python中的Modbus室内温控器的InfluxDb示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Python中的Modbus室内温控器的InfluxDb示例相关的知识,希望对你有一定的参考价值。
import os
import sys
import time
import datetime
import minimalmodbus
from influxdb import InfluxDBClient
minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True
PORT_NAME = 'com7'
SLAVE1_ADDRESS = 1
SLAVE2_ADDRESS = 2
SLAVE3_ADDRESS = 3
BAUDRATE = 9600 # baud (pretty much bits/s).
TIMEOUT = 2 # seconds.
STOPBITS = 1
PARITY = 'N'
MODE = minimalmodbus.MODE_RTU
instrument1 = minimalmodbus.Instrument(PORT_NAME, SLAVE3_ADDRESS, MODE)
instrument1.serial.baudrate = BAUDRATE
instrument1.serial.timeout = TIMEOUT
instrument1.serial.stopbits = STOPBITS
instrument1.serial.parity = PARITY
instrument1.serial.xonxoff = False
instrument1.serial.rtscts = False
instrument1.serial.dsrdtr = False
instrument1.debug = False
instrument1.precalculate_read_size = True
influx_host = 'localhost'
port = 8086
dbname = "environment"
user = "root"
password = "root"
# Generates the necessary payload to post
# temperature data into the InfluxDB
def get_data_points(temperature):
iso = time.ctime()
json_body = [
{
"measurement": "ambient_celcius",
"tags": {"host": influx_host},
"time": iso,
"fields": {
"value": temperature,
}
}
]
return json_body
# Defines the interval on which the capture logic
# will occur
capture_interval = 5.0 # Every 5 seconds
# InfluxDB instance
client = InfluxDBClient(influx_host, port, user, password, dbname)
client.create_database(dbname)
# Read, Report, Repeat
while 1:
temp = instrument1.read_register(40005)
print temp
temperature_data = get_data_points(temp)
client.write_points(temperature_data)
time.sleep(capture_interval)
以上是关于python 使用Python中的Modbus室内温控器的InfluxDb示例的主要内容,如果未能解决你的问题,请参考以下文章
Python3通过Modbus协议获取寄存器数据
带有 Python 的 Modbus TCP 服务器
在树莓派上使用Python实现Modbus TCP Server
python 基于modbus_tk库实现modbusTCP 主站和从站[非常详细]
Python_Example_Modbus_CRC协议
python modbus协议---点亮LED灯