Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed calculations on AC charging #39

Merged
merged 19 commits into from
Oct 20, 2023
Merged
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions teslamate_mqtt2abrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import os
import paho.mqtt.client as mqtt
import requests
import json
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to import json here, our only use of json ("response.json()") is part of request imported above.

from time import sleep
from docopt import docopt

Expand Down Expand Up @@ -215,7 +216,7 @@ def on_message(client, userdata, message):
#print("Unneeded topic:", message.topic, payload)

# Calculate acurrate power on AC charging
if data["power"] != 0.0 and data["is_charging"] == True and "voltage" in data and "current" in data:
if data["is_charging"]==True and data["is_dcfc"]==False and "voltage" in data and "current" in data:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple formatting fix to keep in line with the rest.

data["power"] = float(data["current"] * data["voltage"] * charger_phases) / 1000.0 * -1

return
Expand Down Expand Up @@ -278,13 +279,19 @@ def updateABRP():
global data
global APIKEY
global USERTOKEN

msgDetails = "Data object to send:"
print(msgDetails, data)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think moving the print of the data object to the updateABRP() function is a great idea; but I would rather have the print happen when the message is actually sent, I'll move some stuff around to accommodate for this.


try:
headers = {"Authorization": "APIKEY "+APIKEY}
body = {"tlm": data}
response = requests.post("https://api.iternio.com/1/tlm/send?token="+USERTOKEN, headers=headers, json=body)
resp = response.json()
if resp["status"] != "ok":
print("Response from ABRP:", response.text)
else:
print("Response from ABRP:", resp["status"])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here the message should be more explicit, and signal that where was an issue with the POST request to the API.

except Exception as ex:
print("Unexpected exception while calling ABRP API:", sys.exc_info()[0])
print(ex)
Expand All @@ -301,26 +308,23 @@ def updateABRP():
current_datetime = datetime.datetime.now(datetime.UTC)
current_timetuple = current_datetime.timetuple()
data["utc"] = calendar.timegm(current_timetuple) #utc timestamp must be in every message

str_now = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
msg = str_now + ": Car is " + state
msgDetails = "Data object sent:"
if(state == "parked" or state == "online" or state == "suspended" or state=="asleep" or state=="offline"): #if parked, update every 30 cylces/seconds
if data["power"] != 0: #Sometimes after charging it keeps the last Power and not refresh any more until new drive or charge.
data["power"] = 0.0
if "kwh_charged" in data:
del data["kwh_charged"]
if(i%30==0 or i>30):
print(msg + ", updating every 30s.")
print(msgDetails, data)
updateABRP()
i = 0
elif state == "charging": #if charging, update every 6 cycles/seconds
if i%6==0:
print(msg +", updating every 6s.")
print(msgDetails, data)
updateABRP()
elif state == "driving": #if driving, update every cycle/second
print(msg + ", updating every second.")
print(msgDetails, data)
updateABRP()
else:
print(msg + " (unknown state), not sending any update to ABRP.")
Expand Down