I have a python script which collects data(solar,battery,load,currenttime) from my solar controller via modbus and writes the data onto my oracle DB. The current time is from my system time. At the moment the code runs 24/7 (with one minute interval) and if the connection is lost the code tries 5 times to reconnect, and then stops executing.
My Goal :At the moment i have blank data as the script stops when connection is out. Is there a way i can keep the script running (even when there is no connection ) and put values like 0 into the solar and battery values and pass the current time regularly to the database so i can plot graphs based on 24 hours. And is there a way that my script can reconnect automatically as well? Thanks a lot for your precious time !!
Or is there a way to keep the script running even though there is no modbus connection. Then i can write if no connection input values 0 to battery and solar.
My code
from pymodbus.constants import Defaults
from pymodbus.constants import Endian
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.payload import BinaryPayloadDecoder
from datetime import datetime
from datetime import date
import schedule
import time
import cx_Oracle
con = cx_Oracle.connect("user/pas") #connects to database
cursor = con.cursor()
Defaults.Timeout = 25
Defaults.Retries = 5
def loaddata():
today = date.today()
now = datetime.now()
client = ModbusClient('10.8.0.14', port='83')
result = client.read_input_registers(840, 2) # Read battery voltage
trailerone = 1
decoder = BinaryPayloadDecoder.fromRegisters(result.registers, byteorder=Endian.Big)
voltage=decoder.decode_16bit_uint()
####################### Battery voltage ####################################
battery1 = str("{0:.1f}".format(voltage/10.0)) #battery1 has current battery voltage
#############################################################################
result2 = client.read_input_registers(776,3,unit=226) # Read Solar data
decoder2 = BinaryPayloadDecoder.fromRegisters(result2.registers, byteorder=Endian.Big)
voltage2=decoder2.decode_16bit_uint()
#######################Solar voltage#####################################
solar1 = str("{0:.1f}".format(voltage2/100.0)) # has solar voltage
##########################################################################
result3 = client.read_input_registers(860, 1) #Load Watts data
decoder3 = BinaryPayloadDecoder.fromRegisters(result3.registers, byteorder=Endian.Big)
voltage3=decoder3.decode_16bit_uint()
#########################LOAD, DATE , TIME #####################################
load1 = str("{0:.1f} W".format(voltage3)) # has load wattage
date1 = today.strftime("%d/%m/%Y") # has todays date
current_time = now.strftime("%H:%M:%S") # has current time
#################################################################################
##################### Inserting values into database table ####################
cursor.execute('INSERT INTO Mytable(TRAILERID,PANELVOLTAGE,BATTERYVOLTAGE,DATADATE,DATATTIME) VALUES ( :trailerone,:solar1,:battery1,:date1, :current_time)',[trailerone,solar1,battery1,date1,current_time])
con.commit()
f.close()
schedule.every(60).seconds.do(loaddata) #runs script every one minute endlessly
while 1:
schedule.run_pending()
time.sleep(1)
To anyone wondering how to do it. I have used the try and except method to implement what i was looking for.