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()
3 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.