spacepaste

  1.  
  2. #!/usr/bin/python2.7
  3. import sys
  4. import time as clock
  5. from datetime import datetime
  6. from datetime import time
  7. from pyModbusTCP.client import ModbusClient
  8. from econ7chargerConf import *
  9. pulse = 0
  10. dpulse = 1
  11. mode = 0
  12. SIUpdated = False
  13. econ7 = False
  14. firstRun = True
  15. def importFromGrid(setPoint, SOCDischarge, SOCCharge):
  16. log.write('%02d, %02d, %02d, ' %(setPoint, SOCDischarge, SOCCharge))
  17. #log.write(str(setPoint)+', '+str(SOCDischarge)+', '+str(SOCCharge)+', ')
  18. hub4 = ModbusClient()
  19. hub4.host(farmIP)
  20. hub4.port(hub4Port)
  21. hub4.unit_id(hub4Id)
  22. inverter = ModbusClient()
  23. inverter.host(farmIP)
  24. inverter.port(invPort)
  25. inverter.unit_id(invId)
  26. success = False
  27. if inverter.open():
  28. r=inverter.read_input_registers(30,1)
  29. soc = r[0] / 10.0 # convert to a percentage
  30. log.write('%.1f, inverter, ' %(soc))
  31. print 'SOC=', (soc)
  32. else:
  33. log.write('failed to open inverter coms')
  34. #sort the chargeing
  35. if hub4.open():
  36. success = True
  37. if soc < SOCCharge:
  38. #allow chargeing at max power set point
  39. log.write('charging, ')
  40. success = success & hub4.write_single_register(2700, setPoint)
  41. else:
  42. #battery sufficiently charged set charging power to 0
  43. log.write('not charging, ')
  44. success = success & hub4.write_single_register(2700, 0)
  45. if soc > SOCDischarge:
  46. #allow battery to discharge
  47. log.write('discharging, ')
  48. success = success & hub4.write_single_register(2702, 100)
  49. else:
  50. #disallow discharge
  51. log.write('not discharging, ')
  52. success = success & hub4.write_single_register(2702, 0)
  53. hub4.close()
  54. log.write('hub4, ')
  55. else:
  56. log.write('hub4 failed to open hub4 comms')
  57. return success
  58. startTime = time(e7StartHr,e7StartMin,0)
  59. endTime = time(e7StopHr,e7StopMin,0)
  60. e7_spans_midnight = e7StartHr*60+e7StartMin > e7StopHr*60+e7StopMin
  61. while 1:
  62. now = datetime.now()
  63. #print (now)
  64. year = now.year
  65. year = year%100
  66. month = now.month
  67. day = now.day
  68. #month = 12
  69. #day = 1
  70. lf = '%02d%02dEcon7Log.txt' %(year, month)
  71. log = open(lf, 'a')
  72. log.write ('%02d-%02d-%02d, %02d:%02d:%02d, ' %(year, month, day, now.hour, now.minute, now.second))
  73. todaysDate = datetime(now.year,month,day)
  74. #print 'the date is', (todaysDate),
  75. winterEndDate = datetime(now.year,lastMonthOfWinter,lastDayOfWinter)
  76. springEndDate = datetime(now.year,lastMonthOfSpring,lastDayOfSpring)
  77. summerEndDate = datetime(now.year,lastMonthOfSummer,lastDayOfSummer)
  78. autumnEndDate = datetime(now.year,lastMonthOfAutumn,lastDayOfAutumn)
  79. h = now.hour
  80. m = now.minute
  81. checkTime = time(h,m,0)
  82. #check economy7
  83. if e7_spans_midnight:
  84. if checkTime>startTime or checkTime<endTime:
  85. econ7 = True
  86. elif checkTime>startTime and checkTime<endTime:#if inside e7 times:
  87. econ7 = True
  88. else:
  89. econ7 = False
  90. if econ7:
  91. tariff='night'
  92. else:
  93. tariff='day'
  94. print 'tariff: '+ str(tariff)+'\n',
  95. #find the season
  96. season = 'winter'
  97. if todaysDate > winterEndDate:
  98. season = 'spring'
  99. if todaysDate > springEndDate:
  100. season = 'summer'
  101. if todaysDate > summerEndDate:
  102. season = 'autumn'
  103. if todaysDate > autumnEndDate:
  104. season = 'winter'
  105. print 'season: ', (season)
  106. log.write(str(season)+', '+str(tariff)+', ')
  107. #do the doings
  108. if econ7 and (season == 'winter'):
  109. if day == 13 or day == 27 :
  110. importFromGrid(winterSetPoint, 102, 101) # force full charge twice a month
  111. log.write('full charge, ')
  112. else:
  113. importFromGrid(winterSetPoint, winterSOCDischarge, winterSOCCharge)
  114. elif econ7 and (season == 'spring'):
  115. if day == 12 or day == 26 :
  116. importFromGrid(springSetPoint, 102, 101) # force full charge twice a month
  117. log.write('full charge, ')
  118. else:
  119. importFromGrid(springSetPoint, springSOCDischarge, springSOCCharge)
  120. elif econ7 and (season == 'summer'):
  121. importFromGrid(summerSetPoint, summerSOCDischarge, summerSOCCharge)
  122. elif econ7 and (season == 'autumn'):
  123. if day == 12 or day == 26 :
  124. importFromGrid(autumnSetPoint, 102, 101) # force full charge twice a month
  125. log.write('full charge, ')
  126. else:
  127. importFromGrid(autumnSetPoint, autumnSOCDischarge, autumnSOCCharge)
  128. else:
  129. importFromGrid(daySetPoint, daySOCDischarge, daySOCCharge)
  130. log.write('\n')
  131. log.close()
  132. #wait time between loops
  133. clock.sleep(loopInterval)
  134.