Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
InfluxDb Example with Modbus Room Thermostat in Python
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.