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 " +DWM.name)
DWM.write("\r\r".encode())
time.sleep(1)
DWM.write("lec\r".encode())
time.sleep(1)
while True:
    try:
        line=DWM.readline()
        if(line):
            if len(line)>=140:
                parse=line.decode().split(",")
                x_pos=parse[parse.index("POS")+1]
                y_pos=parse[parse.index("POS")+2]
                val = (x_pos,y_pos)
                mycursor=mydb.cursor()
                mycursor.execute(sql, val)
                mydb.commit()
                print(datetime.datetime.now().strftime("%H:%M:%S"),"(",x_pos,", ",y_pos,")")
            else:
                print("Position not calculated: ",line.decode())
    except Exception as ex:
        print(ex)
        break
DWM.write("\r".encode())
DWM.close()
6 Likes

Thanks a lot for sharing !
Yves

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?
Thanks.

@jackman

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/dwm1001dev_rpi.py”, 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/init.py”, line 179, in connect
return MySQLConnection(*args, **kwargs)
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/connection.py”, line 95, in init
self.connect(**kwargs)
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/abstracts.py”, line 716, in connect
self._open_connection()
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/connection.py”, line 210, in _open_connection
self._ssl)
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/connection.py”, line 144, in _do_auth
self._auth_switch_request(username, password)
File “/home/pi/.local/lib/python3.7/site-packages/mysql/connector/connection.py”, line 177, in _auth_switch_request
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1698 (28000): Access denied for user ‘USER’@‘localhost’

@newbie
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:
http://recipes.item.ntnu.no/setting-up-an-sql-database-on-the-raspberry-pi/

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/trial.py”, 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 " + DWM.name). The Raspi is connected to a listener via USB. Do you know what is wrong?

same problem here whats the solution?