-
Notifications
You must be signed in to change notification settings - Fork 6
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
Changes from 18 commits
cdc966c
1398040
f8d715a
46b0b6e
37e17b2
30e62c9
a16c3e4
2b89341
a27b8b2
6671bb2
7f6b364
e177b1a
e598151
cbfa036
d7d931d
aa4b5fb
5325594
f7efa32
fbdb939
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import os | ||
import paho.mqtt.client as mqtt | ||
import requests | ||
import json | ||
from time import sleep | ||
from docopt import docopt | ||
|
||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -278,13 +279,19 @@ def updateABRP(): | |
global data | ||
global APIKEY | ||
global USERTOKEN | ||
|
||
msgDetails = "Data object to send:" | ||
print(msgDetails, data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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.") | ||
|
There was a problem hiding this comment.
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.