Advertisement
friedpenguin

Openhab epsolar to mqtt python script finally working

Feb 4th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 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.sync import ModbusSerialClient as ModbusClient
  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.  
  14. client = ModbusClient(method='rtu', port='/dev/ttyUSB0', baudrate=115200, stopbits=1, bytesize=8, timeout=2)
  15. client.connect()
  16.  
  17. while True:
  18. result = client.read_input_registers(0x3100,20, unit=1)
  19. solarWatt = float(result.registers[2] / 100.0)
  20. solarVoltage = float(result.registers[0] / 100.0)
  21. solarCurrent = float(result.registers[1] / 100.0)
  22. batteryVoltage = float(result.registers[4] / 100.0)
  23. chargeCurrent = float(result.registers[5] / 100.0)
  24. loadWatts = float(result.registers[19] / 100.0)
  25.  
  26. # mqtt_client.publish("solar", "|-------------------------------|")
  27. mqtt_client.publish("solar/solarVoltage", str(solarVoltage))
  28. mqtt_client.publish("solar/solarCurrent", str(solarCurrent))
  29. mqtt_client.publish("solar/solarWattage", str(solarWatt))
  30. mqtt_client.publish("solar/batteryVoltage", str(batteryVoltage))
  31. mqtt_client.publish("solar/chargeCurrent", str(chargeCurrent))
  32. mqtt_client.publish("solar/loadWatts", str(loadWatts))
  33. # mqtt_client.publish("solar", strftime("%m-%d-%Y %H:%M:%S", gmtime()))
  34. sleep(60)
  35.  
  36. mqtt_client.disconnect()
  37. client.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement