There are many companies that offer cars to their employees on a daily work. So maybe it’s a good idea to have vehicle data on the Grafana Labs dashboard. This way you can query and control the status vehicle. Exemple. the vehicle’s is locked, secured, brake fluid, oil and checks if the vehicle’s status is OK. In addition, there are other details about the remaining range-hybrid and electric and much more…
So far I have made a test Grafana dashboard for my vehicle BMW 745Le xDrive. My solution only works with BMW (and MINI) vehicles with a Connected Drive Account.
Lets get it started!- Install Bimmer Connected
pip3 install --upgrade bimmer_connected
- Find and replace in file /usr/local/lib/python3.8/dist-packages/bimmer_connected/cli.py
def fingerprint(args) -> None: """Save the vehicle fingerprint.""" time_dir = Path.home() / 'vehicle_fingerprint' / time.strftime("%Y-%m-%d_%H-%M-%S") time_dir.mkdir(parents=True)
Replace with
def fingerprint(args) -> None: """Save the vehicle fingerprint.""" time_dir = Path.home() / 'BMW' if not os.path.exists(time_dir): os.makedirs(time_dir) else: shutil.rmtree(time_dir) os.makedirs(time_dir)
- Register your email on json-csv.com, to convert JSON to CSV inside my created Bash Script (get_data_from_BMW.sh)
- Copy below codes to creating bmw table
CREATE TABLE bmw ( DCS_CCH_Activation varchar(10) DEFAULT NULL, DCS_CCH_Ongoing varchar(10) DEFAULT NULL, cbsData_cbsDescription varchar(200) DEFAULT NULL, cbsData_cbsDueDate varchar(10) DEFAULT NULL, cbsData_cbsRemainingMileage varchar(10) DEFAULT NULL, cbsData_cbsState varchar(10) DEFAULT NULL, cbsData_cbsType varchar(20) DEFAULT NULL, chargingConnectionType varchar(20) DEFAULT NULL, chargingInductivePositioning varchar(20) DEFAULT NULL, chargingLevelHv varchar(10) DEFAULT NULL, chargingStatus varchar(20) DEFAULT NULL, connectionStatus varchar(20) DEFAULT NULL, doorDriverFront varchar(10) DEFAULT NULL, doorDriverRear varchar(10) DEFAULT NULL, doorLockState varchar(10) DEFAULT NULL, doorPassengerFront varchar(10) DEFAULT NULL, doorPassengerRear varchar(10) DEFAULT NULL, fuelPercent varchar(10) DEFAULT NULL, hood varchar(10) DEFAULT NULL, internalDataTimeUTC varchar(30) DEFAULT NULL, lastChargingEndReason varchar(10) DEFAULT NULL, lastChargingEndResult varchar(10) DEFAULT NULL, mileage varchar(10) DEFAULT NULL, parkingLight varchar(10) DEFAULT NULL, position_heading varchar(10) DEFAULT NULL, position_lat varchar(10) DEFAULT NULL, position_lon varchar(10) DEFAULT NULL, position_status varchar(10) DEFAULT NULL, positionLight varchar(10) DEFAULT NULL, rearWindow varchar(10) DEFAULT NULL, remainingFuel varchar(10) DEFAULT NULL, remainingRangeElectric varchar(10) DEFAULT NULL, remainingRangeElectricMls varchar(10) DEFAULT NULL, remainingRangeFuel varchar(10) DEFAULT NULL, remainingRangeFuelMls varchar(10) DEFAULT NULL, singleImmediateCharging varchar(10) DEFAULT NULL, sunroof varchar(10) DEFAULT NULL, trunk varchar(10) DEFAULT NULL, updateReason varchar(30) DEFAULT NULL, updateTime varchar(30) DEFAULT NULL, vehicleCountry varchar(10) DEFAULT NULL, vin varchar(20) DEFAULT NULL, windowDriverFront varchar(10) DEFAULT NULL, windowDriverRear varchar(10) DEFAULT NULL, windowPassengerFront varchar(10) DEFAULT NULL, windowPassengerRear varchar(10) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- Create get_data_from_BMW.sh file
cd /home touch get_data_from_BMW.sh chmod +x get_data_from_BMW.sh nano get_data_from_BMW.sh
- Then copy following codes into get_data_from_BMW.sh and run it (./get_data_from_BMW.sh). To get data from BMW, and send it to database.
#!/bin/bash #You have to insert your BMW connecter email and passowrd #The region of your Connected Drive account. #Please use one of these values: north_america, china, rest_of_world bmw=$(bimmerconnected fingerprint YOURS@gmail.com PASS rest_of_world) echo $bmw #Possibly find location this file (status_0.txt) and replace it change=$(mv /root/BMW/status_0.txt /home/status.json) echo $change #Insert your e-mail address that you have registred on json-csv.com (STEP 5) csv=$(curl -X POST https://json-csv.com/api/getcsv -F email=YOURS@gmail.com -F json=@status.json -o output.csv) echo $csv #Possibly change schema/database or table name and infile location mysql << EOF use mysql; use dev; truncate table bmw; load data local infile "/home/output.csv" into table bmw character set utf8 fields terminated by',' ENCLOSED BY '"' lines terminated by'\r\n' IGNORE 1 ROWS; EOF
- Then add into get_data_from_BMW.sh script to Cronjob to get new data and update Grafana dashboard automatic
Below example is running Cron Job every 15 minutes
crontab -e 15 * * * * /bin/bash /home/get_data_from_BMW.sh >/dev/null 2>&1
- Login to Grafana Cloud
- Add MySQL details inside Grafana datasources. Check my preview post about it
- Add my created Grafana Dashboard (13278) and import it into Grafana Dashboard as shown i the image below.
Enjoy it, and like it if you want too Linkedin!