Parsing locations from DWM1001-DEV to SQL database using Python on Raspberry Pi

Hi everyone,

I have been using the MDEK1001 to read locations that are displayed on a map on a web page hosted on an RPi. I figured this may be helpful to someone that is trying to accomplish something similar.

This method utilizes the UART interface with a DWM1001-DEV as a Tag connected to a Raspberry Pi (running stock Raspbian Stretch) via USB. I used the Android application to initialize Anchor positions.

#DWM Serial Parser by Brian H. | Updated 3/6/19
import serial
import time
import datetime
import mysql.connector

mydb=mysql.connector.connect(host="localhost", user="USER", passwd="PASS",database="DB")
sql = "update location set X_Coord= %s, Y_Coord= %s"

DWM=serial.Serial(port="/dev/ttyACM0", baudrate=115200)
print("Connected to "
while True:
            if len(line)>=140:
                val = (x_pos,y_pos)
                mycursor.execute(sql, val)
                print("%H:%M:%S"),"(",x_pos,", ",y_pos,")")
                print("Position not calculated: ",line.decode())
    except Exception as ex:

Thanks a lot for sharing !

hii brianh,
I need the distance of tag from each anchor(four), I tentatively know that it can be done using lec or les command. But also I need to get this distance on a variable so that I can calculate position using least sq algorithm. I don’t know much about SQL database you used above. Can you please help me in learning the above method.
Also, can we use" dist_AN0=parse[parse.index(“AN0”)+5] " this command to get distance, I fond this on a topic, if YES how?


The SQL database part is so I can access the locations through my server. It is not necessary - if you do not need this, simply delete all lines with (mysql, mydb, sql, val, mycursor).

Yes, to get the distance just replace “x_pos=…” and “y_pos=…” lines in my example to “dist_AN0=…” (right after the parse line)

If you check the API docs, you can see that the first number after AN0 is the anchor ID. Second, third, and fourth numbers are the (x,y,z) initialized positions of the anchors, and the fifth number is measured position.

This is why you can add 5 to the AN0 index to get the distance. Also why adding 2, 3, 4 gives you (x,y,z) in my answer in your topic.

Noob question - where is this code supposed to be run on?

I have been getting the following error when trying to run this code and i cannot figure out why

Traceback (most recent call last):
File “/home/pi/Documents/”, line 7, in
mydb=mysql.connector.connect(host=“localhost”, user=“USER”, passwd=“PASS”,database=“DB”)
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/”, line 179, in connect
return MySQLConnection(*args, **kwargs)
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/”, line 95, in init
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/”, line 716, in connect
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/”, line 210, in _open_connection
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/”, line 144, in _do_auth
self._auth_switch_request(username, password)
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/”, line 177, in _auth_switch_request
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied for user ‘USER’@‘localhost’

The code can be run on any computer with Python installed. I used RPi 3.

It looks like you do not have a MySQL DB. That part is to make the data available to other applications. You can delete those lines and handle the data some other way in Python. The parsing of the DWM module itself does not require this piece.

Here is a tutorial on how to set up MySQL if you do want to use it:

1 Like

Thanks a lot. It worked now.

However, it keeps showing ‘Position not calculated:’ followed by a string of numbers that seem to show the position. I’m not sure what I might be doing wrong.

Hello bro, Thank you for the code. I run and got the following error
Traceback (most recent call last):
File “/home/pi/Downloads/”, line 4, in
import mysql.connector
ModuleNotFoundError: No module named 'mysql

@brianh I used the same code and it works perfectly fine. But what is the latency that you are observing in location update? In my case it is 10 Sec . Do you have any idea if this can be reduced.

Hi brianh. I copied your code and deleted the MySQL things. but when I run it on my raspberry pi 3B+, it just progresses until it comes to print("Connected to " + The Raspi is connected to a listener via USB. Do you know what is wrong?

same problem here whats the solution?