friedpenguin

chamomile_solar

Mar 12th, 2020 (edited)
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # For the record, capnchamomile made this work well enough to
  3. # finish the required hacking. Thanks Dan!
  4. # You're welcome!
  5.  
  6. from pymodbus.client import ModbusSerialClient as ModbusClient #latest pymodbus change
  7. import paho.mqtt.client as mqtt
  8. from time import sleep, gmtime, strftime
  9. import datetime
  10.  
  11. mqtt_client = mqtt.Client()
  12. mqtt_client.connect("192.168.10.202")
  13. mqtt_client.loop_start()
  14.  
  15. client = ModbusClient(method='rtu', port='/dev/ttyUSB0', baudrate=115200, stopbits=1, bytesize=8, timeout=2)
  16. client.connect()
  17.  
  18. while True:
  19. result = client.read_input_registers(0x3100,20, unit=1)
  20. if not hasattr(result, 'registers'):
  21. continue
  22. solarWatt = float(result.registers[2] / 100.0)
  23. solarVoltage = float(result.registers[0] / 100.0)
  24. solarCurrent = float(result.registers[1] / 100.0)
  25. batteryVoltage = float(result.registers[4] / 100.0)
  26. chargeCurrent = float(result.registers[5] / 100.0)
  27. loadWatts = float(result.registers[19] / 100.0)
  28.  
  29. # mqtt_client.publish("solar", "|-------------------------------|")
  30. mqtt_client.publish("solar/solarVoltage", str(solarVoltage))
  31. mqtt_client.publish("solar/solarCurrent", str(solarCurrent))
  32. mqtt_client.publish("solar/solarWattage", str(solarWatt))
  33. mqtt_client.publish("solar/batteryVoltage", str(batteryVoltage))
  34. mqtt_client.publish("solar/chargeCurrent", str(chargeCurrent))
  35. mqtt_client.publish("solar/loadWatts", str(loadWatts))
  36. # mqtt_client.publish("solar", strftime("%m-%d-%Y %H:%M:%S", gmtime()))
  37. sleep(60)
  38.  
  39. mqtt_client.disconnect()
  40. client.close()
Advertisement
Add Comment
Please, Sign In to add comment