############################################################################ # OpenEVSE/emonpi MQTT PV using Pearl # sudo pip install paho-mqtt # If PV power > 500W turn on EVSE Charging # Modulate EV charging with PV generation # If PV Power < 100W turn off PV Charging ############################################################################# # ImRAPI # $GS State - 1 Not Connected - 2 Connected - 3 Charging - 4 Error - 5 Error ############################################################################# # Author : Paul Richards paulb.richards@gmx.com # Version : 1.0 # Date : 24th January 2019 ############################################################################# import paho.mqtt.client as mqtt # Globals nFirstLoop = True; # One time flag nEVSEOn = False; # 1=On 0=Off nEvsePower = 1.0; nEvseCurrent = 1.0; nVoltsRms = 240; nPVPower = 1.0; nPVCurrent = 1.0; nSetEVCurrent = 6; nPreviousSetEVCurrent = 6; # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): # Connect to client and subscribe to emonpi feeds print("Connected with result code "+str(rc)) nImportExportPower = client.subscribe("emon/emontx3/power1") # Main Power Import/Export print("Suscribed with result code "+str(nImportExportPower)) nPVPower = client.subscribe("emon/emontx3/power2") # PV Power print("Suscribed with result code "+str(nPVPower)) nVoltsRms = client.subscribe("emon/emontx3/vrms") # RMS Voltage print("Suscribed with result code "+str(nVoltsRms)) substr = client.subscribe("openevse/rapi/out") # Rapi response print("Suscribed with result code "+str(substr)) nEvseCurrent = client.subscribe("openevse/amp") # EVSE Charge Current print("Suscribed with result code "+str(nEvseCurrent)) # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global nEvsePower, nEvseCurrent, nPVPower, nPVCurrent, nVoltsRms, nSetEVCurrent, nPreviousSetEVCurrent, nEVSEOn, nFirstLoop if msg.topic == "openevse/rapi/out": # work in progress print("PAYLOAD next...") nData = msg.payload print(nData) # [4:5]) if msg.topic == "emon/emontx3/power2": # PV Power from emontx3 nPVPower = float(msg.payload) print("PV Power "+str(nPVPower)) nPVCurrent = nPVPower/nVoltsRms # Calc the PV Current print("PV Current "+str(nPVCurrent)) # client.publish("openevse/rapi/in/$GS") # Work in Progress...... # print("EVSE STATE.....") if nFirstLoop: # First ever loop - EVSE to Sleep nFirstLoop = False # Never come here again nEVSEOn = False # Flag EVSE as turned Off client.publish("openevse/rapi/in/$FS") # Send Sleep command print("EVSE SLEEPING") if nPVPower < 100 and nEVSEOn == True: # If PV Power < 100W and EVSE is On nEVSEOn = False # Flag EVSE as turned Off client.publish("openevse/rapi/in/$FS") # Sen Sleep command print("EVSE SLEEPING") if nPVPower > 500 and nEVSEOn == False: # If PV Power > 500W and EVSE is Off nEVSEOn = True # Flag EVSE as Turned On client.publish("openevse/rapi/in/$FE") # Enable EVSE print("EVSE AWAKEN") if msg.topic == "emon/emontx3/vrms": # Volts RMS nVoltsRms = float(msg.payload) print("Volts "+str(nVoltsRms)) if msg.topic == "openevse/amp": # EVSE actual Current nEvseCurrent = float(msg.payload)/1000 print("EVSE Current "+str(nEvseCurrent)) nEvsePower = nEvseCurrent * nVoltsRms print("EVSE Power "+str(nEvsePower)) nPreviousSetEVCurrent = nSetEVCurrent nSetEVCurrent = int(nPVCurrent) if nSetEVCurrent > 32: # Modulate Charhe based on int of PV Current nSetEVCurrent = 32 # within 6A to 32A elif nSetEVCurrent < 6: nSetEVCurrent = 6 if (nPreviousSetEVCurrent != nSetEVCurrent): # only send EVSE Current change if it has changed print("Set Current "+str(nSetEVCurrent)) print(client.publish("openevse/rapi/in/$SC",str(nSetEVCurrent))) else: print("No PV Current Change") # Create an MQTT client and attach our routines to it. client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message client.username_pw_set(username="emonpi",password="emonpimqtt2016") client.connect("192.168.1.199", 1883, 60) # Process network traffic and dispatch callbacks. client.loop_forever() # EOF