Python Forum
Pymodbus read and save to database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pymodbus read and save to database
#3
for i in range(len(meters)):
    sql = ("INSERT INTO meter (meter, power, energy, date_of_reading) VALUES (%s, %s, %s, %s)",
    (meters[i], power_calculation[i], energy_calculation[i], date_of_reading))
 
mycursor.execute(*sql)
This will insert just the values for the last meter. You either move the execute inside the loop, or look at changing your code to using executemany
Something like this (code not tested):

import mysql.connector
from pymodbus.client import ModbusTcpClient
from datetime import datetime

 
M01 = '192.168.30.156' #LOCATION 1
M02 = '192.168.22.95' #LOCATION 2
 
ips = [M01, M02]
 
date_of_reading = datetime.now()
meters = [ModbusTcpClient(ip) for ip in ips]
readings = []
 
for meter in meters:
    power_register = meter.read_holding_registers(23322, 2)
    energy_register = meter.read_holding_registers(20480, 4)
    power_calculation = round((power_register.registers[0] * pow(2, 16) + power_register.registers[1]) * 0.01 / 1000, 2)
    energy_calculation = round((energy_register.registers[2] * pow(2, 16) + energy_register.registers[3]) * 0.01, 2)
    reading = meter, power_calculation, energy_calculation, date_of_reading
    readings.append(reading)
 
sql = "INSERT INTO meter (meter, power, energy, date_of_reading) VALUES (%s, %s, %s, %s)"

mydb = mysql.connector.connect(
  host="192.168.25.2",
  user="xxxx",
  password="xxxx",
  database="energetics"
)
 
mycursor = mydb.cursor()
mycursor.executemany(sql, readings)
mydb.commit()
mycursor.close()

print("Success !")
From here you can improve the code - using e.g. named tuple for readings, make your code use functions and/or OOP, etc.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Pymodbus read and save to database - by stewietopg - Mar-02-2023, 07:22 AM
RE: Pymodbus read and save to database - by rob101 - Mar-02-2023, 07:49 AM
RE: Pymodbus read and save to database - by buran - Mar-02-2023, 09:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with pymodbus - ModuleNotFoundError: No module named 'pymodbus.client.sync' stsxbel 2 23,991 Nov-02-2023, 08:20 AM
Last Post: South_east
  how to save to multiple locations during save cubangt 1 580 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Pymodbus Write value to register stsxbel 10 8,308 Aug-18-2022, 01:42 PM
Last Post: DeaD_EyE
  Read JSON via API and write to SQL database TecInfo 5 2,263 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  How to save specific variable in for loop in to the database? ilknurg 1 1,170 Mar-09-2022, 10:32 PM
Last Post: cubangt
  SaltStack: MySQL returner save less data into Database table columns xtc14 2 2,204 Jul-02-2021, 02:19 PM
Last Post: xtc14
  Looping to read data in database CEC68 1 1,739 Sep-24-2020, 08:54 PM
Last Post: scidam
  sqlite3 database does not save data across restarting the program SheeppOSU 1 3,481 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  Read and save file in chucksize zinho 3 2,496 Apr-02-2020, 08:12 AM
Last Post: DeaD_EyE
  Save a file uploaded from client-side without having to read into memory andym118 3 5,102 Nov-21-2019, 07:34 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020