from pymodbus.client.sync import ModbusTcpClient as ModbusClient from pymodbus.payload import BinaryPayloadDecoder from influxdb import InfluxDBClient Defaults.Timeout = 25 Defaults.Retries = 5 client = ModbusClient('169.254.094.238', port='502') result = client.read_input_registers(261, 1, unit=243) decoder = BinaryPayloadDecoder.fromRegisters(result.registers, endian=Endian.Big) value = decoder.decode_16bit_int() current = value / 10.0 dbname = 'the name of your influxdb here' dbuser = '******' dbuser_password = '******' sensor = "batamp" json_body = [{ "measurement": "current", "tags": { "sensor": sensor }, "fields": { "value": current } }] client = InfluxDBClient('the ip address of the machine influxdb is running on', '8086', dbuser, dbuser_password, dbname) client.write_points(json_body)
# print("unknown type", ty) # exit(-1) values = {} for start, size, regs in blocks: try: data = read_block(client, start, size) for addr, reg in regs.items(): reg_data = data[2 * (addr - start):2 * (addr - start + reg["size"])] if reg["name"].lower() != "reserved": val = decode(reg["type"], reg_data) if val is not None: values[reg["name"]] = val continue except AttributeError: continue data = [{ "measurement": "pv_monitoring", "time": datetime.datetime.now().astimezone().isoformat() }] data[0]["fields"] = values client = InfluxDBClient(INFLUXDBIP, INFLUXDBPORT, INFLUXDBUSER, INFLUXDBPASS, INFLUXDBDB) client.write_points(data)