forked from pyjamasam/teslamate-abrp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
teslamateMqttToABRP.py
201 lines (186 loc) · 6.62 KB
/
teslamateMqttToABRP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import paho.mqtt.client as mqtt
import requests
import sys
import json
import datetime
import calendar
import os
from time import sleep
apikey = os.environ['API_KEY']
mqttserver = os.environ['MQTT_SERVER']
mqttusername = os.environ['MQTT_USERNAME']
mqttpassword = os.environ['MQTT_PASSWORD']
usertoken = os.environ['USER_TOKEN']
carnumber = os.environ['CAR_NUMBER']
#car model list here curl --location --request GET 'https://api.iternio.com/1/tlm/get_carmodels_list'
carmodel = os.environ['CAR_MODEL']
state = ""# car state
prev_state = ""# car state previous loop for tracking
data = {# dictionary of values sent to ABRP API
"utc": 0,
"soc": 0,
"power": 0,
"speed": 0,
"lat": "",
"lon": "",
"elevation": "",
"is_charging": 0,
"is_dcfc": 0,
"is_parked": 0,
"battery_range": "",
"ideal_battery_range": "",
"ext_temp": "",
"car_model":f"{carmodel}",
"tlm_type": "api",
"voltage": 0,
"current": 0,
"kwh_charged": 0,
"heading": "",
}
#initiate MQTT client
client = mqtt.Client(f"teslamateToABRP-{carnumber}")
if mqttusername is not None:
if mqttpassword is not None:
client.username_pw_set(mqttusername, mqttpassword)
else:
client.username_pw_set(mqttusername)
client.connect(mqttserver)
def on_connect(client, userdata, flags, rc): # The callback for when the client connects to the broker
print("Connected with result code {0}".format(str(rc))) # Print result of connection attempt
client.subscribe(f"teslamate/cars/{carnumber}/#")
#client.subscribe("digitest/test1") # Subscribe to the topic “digitest/test1”, receive any messages published on it
#process MQTT messages
def on_message(client, userdata, message):
global data
global state
try:
#extracts message data from the received message
payload = str(message.payload.decode("utf-8"))
#updates the received data
topic_postfix = message.topic.split('/')[-1]
if topic_postfix == "plugged_in":
a=1#noop
elif topic_postfix == "latitude":
data["lat"] = payload
elif topic_postfix == "longitude":
data["lon"] = payload
elif topic_postfix == "elevation":
data["elevation"] = payload
elif topic_postfix == "speed":
data["speed"] = int(payload)
elif topic_postfix == "power":
data["power"] = int(payload)
if(data["is_charging"]==1 and int(payload)<-22):
data["is_dcfc"]=1
elif topic_postfix == "charger_power":
if(payload!='' and int(payload)!=0):
data["is_charging"]=1
if(int(payload)>22):
data["is_dcfc"]=1
elif topic_postfix == "heading":
data["heading"] = payload
elif topic_postfix == "outside_temp":
data["ext_temp"] = payload
elif topic_postfix == "odometer":
data["odometer"] = payload
elif topic_postfix == "ideal_battery_range_km":
data["ideal_battery_range"] = payload
elif topic_postfix == "est_battery_range_km":
data["battery_range"] = payload
elif topic_postfix == "charger_actual_current":
if(payload!='' and int(payload) > 0):#charging
data["current"] = payload
else:
del data["current"]
elif topic_postfix == "charger_voltage":
if(payload!='' and int(payload) > 5):
data["voltage"] = payload
else:
del data["voltage"]
elif topic_postfix == "shift_state":
if(payload == "P"):
data["is_parked"]="1"
elif(payload == "D" or payload == "R"):
data["is_parked"]="0"
elif topic_postfix == "state":
state = payload
if(payload=="driving"):
data["is_parked"]=0
data["is_charging"]=0
data["is_dcfc"]=0
elif(payload=="charging"):
data["is_parked"]=1
data["is_charging"]=1
data["is_dcfc"]=0
elif(payload=="supercharging"):
data["is_parked"]=1
data["is_charging"]=1
data["is_dcfc"]=1
elif(payload=="online" or payload=="suspended" or payload=="asleep"):
data["is_parked"]=1
data["is_charging"]=0
data["is_dcfc"]=0
elif topic_postfix == "battery_level":
data["soc"] = payload
elif topic_postfix == "charge_energy_added":
data["kwh_charged"] = payload
elif topic_postfix == "inside_temp":
a=0#noop
elif topic_postfix == "since":
a=0#noop
else:
pass
#print("Unneeded topic:", message.topic, payload)
return
except:
print("unexpected exception while processing message:", sys.exc_info()[0], message.topic, message.payload)
#starts the MQTT loop processing messages
client.on_message = on_message
client.on_connect = on_connect # Define callback function for successful connection
client.loop_start()
#function to send data to ABRP
def updateABRP():
global data
global apikey
global usertoken
try:
headers = {"Authorization": "APIKEY "+apikey}
body = {"tlm": data}
requests.post("https://api.iternio.com/1/tlm/send?token="+usertoken, headers=headers, json=body)
except:
print("unexpected exception while calling ABRP API:", sys.exc_info()[0])
print(message.topic)
print(message.payload)
#starts the forever loop updating ABRP
i = -1
while True:
i+=1
sleep(5)#refresh rate of min 5 seconds
#print(state)
if state != prev_state:
i = 120
current_datetime = datetime.datetime.utcnow()
current_timetuple = current_datetime.utctimetuple()
data["utc"] = calendar.timegm(current_timetuple)#utc timestamp must be in every messafge
if(state == "parked" or state == "online" or state == "suspended" or state=="asleep" or state=="offline"):#if parked update every 10min
if "kwh_charged" in data:
del data["kwh_charged"]
if(i%120==0 or i>120):
print("parked, updating every 10min")
print(data)
updateABRP()
i = 0
elif(state == "charging"):
if(i%6==0):
print("charging, updating every 30s")
print(data)
updateABRP()
elif(state == "driving"):
print("driving, updating every 5s")
print(data)
updateABRP()
else:
print("unknown state, not updating abrp")
print(state)
prev_state = state
client.loop_stop()