Connect to server via powershell and win7

JD8 years ago

Anton,

Let me thank you first for an outstanding product. I have been using Traccar for the past year or so to monitor several sierra wireless devices. Those devices are being phased out in my department and being replaced by Windows 7 laptops with an internal GPS device. I would like very much to continue using Traccar, but am not completely sure how to proceed.

For the past few days, I have been attempting to find a solution using powershell. I can get a good lat and long, and set a device ID. I can return a value formatted like "http://demo.traccar.org:5055/?id=123456&lat={0}&lon={1}&timestamp={2}&hdop={3}&altitude={4}&speed={5}". But I can't pass that on to the Traccar server at this point.

I have tried several methods, but am still off the mark.

I feel as if I have missed, or failed to understand something important.

I have been looking through the available client sources in an attempt to find some pointers, but much of it is beyond my skill level (and I'm exhausted ATM, so please forgive me if what I have missed is obvious).

My server is up, port 5055 is open, and all is working when using the Android client.

Can you (or anyone else who may read this) provide any direction concerning this issue?

Thank you.

JD

JD8 years ago

I was looking at the wrong log entry when I posted initially. It looks like I have the timestamp format wrong. Changing the output to Unix format hasn't helped so far, but that's probably a M$/powershell thing. Could someone please confirm if I am on the right track with using the Unix format for the timestamp or point me in the right direction?

Thanks

JD8 years ago

Success! I am an idiot. I left the brackets in my script.

At least I'm an idiot well on the way to a working windows client.

JD

Anton Tananaev8 years ago

Am I understanding correctly that you have a PowerShell script that works like a Traccar Client?

JD8 years ago

Yes, the beginnings of one anyway. The script pulls NMEA sentences from a com port and assigns the computer serial number as the ID.

It connects to my server and populates the correct location now, but it is very basic at the moment. With a few more tweaks it will fit my needs though.

I borrowed bits of scripts from all over. I'll post it here when it's done if you wish to have a look at it.

JD8 years ago

I have a script that works fairly well so far. The script should be easy enough to adjust for circumstance. I'm getting some bad locations from it, but I've been testing indoors. I lose my GPS fix if I move the laptop more than 2 feet away from the window. YMMV

If anyone has comments or criticisms, I would love to hear them. I'm fairly new to powershell, or at least I have been stubbornly ignoring it for years. I've never bothered with NMEA before either, so this has been a learning experience.

I hope it helps someone.


Function SendLocation {
#Nmea2Dec from: Jakob Bindslet at http://mspowershell.blogspot.com/
function Nmea2dec {
    Param ([double]$degrees, $o)
    $deg = [math]::floor($degrees/100.0)
    $frac = (($degrees/100.0) - $deg)/0.6
    $ret = $deg + $frac
    if ($o -eq "S" -or $o -eq "W") {$ret = $ret * (-1)}
    return $ret
}

$port = new-Object System.IO.Ports.SerialPort COM10,9600,None,8,one
$port.open()
$line = ""
while (-not ($line -match ".GNRMC.*")) {
    $line = $port.readline()
}
$splitline = $line.split(",")
$latitude = Nmea2dec $splitline[3] $splitline[4]
$longtitude = Nmea2dec $splitline[5] $splitline[6]

#http://stackoverflow.com/questions/11192699/unix-time-in-powershell-giving-gmt
$timestamp = [int][double]::Parse((get-date -date (Get-Date).ToUniversalTime() -UFormat %s))

$SerialRaw = gwmi win32_bios | format-list SerialNumber
$Serial = $SerialRaw | Out-String
$Serial = $Serial.Trim()
$Serial = $Serial.Trim("SerialNumber : ")

#Send to Traccar server
$traccar = "http://demo.traccar.org:5055/?id="+($Serial)+"&lat="+($latitude)+"&lon="+($longtitude)+"&timestamp="+($timestamp)

Invoke-WebRequest -Uri $traccar

$port.Close()

}

#Call SendLocation every 5 minutes
while(1)
{
SendLocation
start-sleep -seconds 300
}

I need to figure out how to do this in Bash too.

Anton Tananaev8 years ago

Thanks for sharing the code. By the way, if you get NMEA strings anyway, it would be easier to just use old Traccar Client protocol:

https://www.traccar.org/traccar-client-protocol/

JD8 years ago

Oh, I saw that, but wasn't clear on what port to use, or if it would still work at all. What port should be used if I wanted to try that method?

Anton Tananaev8 years ago

It is still supported because many other devices use this protocol. Port - 5005.

JD8 years ago

I had left this sitting unfinished due to other projects. The script worked, but wasn't so great at filtering bad/null locations.

I updated my old server today, and really like what has been done with Traccar. The geofencing ability is a great upgrade, and the extra reporting abilities will be helpful.

Anyway, I had some time to rethink the script. This one works much better for me, so I thought I would share.

Function GetLocation {
#Nmea2Dec from: Jakob Bindslet at http://mspowershell.blogspot.com/
function Nmea2dec {
    Param ([double]$degrees, $o)
    $deg = [math]::floor($degrees/100.0)
    $frac = (($degrees/100.0) - $deg)/0.6
    $ret = $deg + $frac
    if ($o -eq "S" -or $o -eq "W") {$ret = $ret * (-1)}
    return $ret
}

$port = new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
$port.open()
$line = ""
$alt = ""
while (-not ($line -match ".*RMC.*")) {
    $line = $port.readline()
}
while (-not ($alt -match ".*GGA.*")) {
    $alt = $port.readline()
}
$splitline = $line.split(",")
$splitalt = $alt.split(",")
$latitude = Nmea2dec $splitline[3] $splitline[4]
$longtitude = Nmea2dec $splitline[5] $splitline[6]

$mph = $splitline[7]
$altitude = $splitalt[9]

$port.Close()

If (!$latitude -or !$longtitude) {return}

SendLocation

}

Function SendLocation {

$SerialRaw = gwmi win32_bios | format-list SerialNumber
$Serial = $SerialRaw | Out-String
$Serial = $Serial.Trim()
$Serial = $Serial.Trim("SerialNumber : ")

$timestamp = [int][double]::Parse((get-date -date (Get-Date).ToUniversalTime() -UFormat %s))
#Create URL for traccar server
$traccar = "http://demo.traccar.org:5055/?id="+($Serial)+"&lat="+($latitude)+"&lon="+($longtitude)+"&timestamp="+($timestamp)+"&altitude="+($altitude)+"&speed="+($mph)
#Send Location
Invoke-WebRequest -Uri $traccar
}

while(1)
{
GetLocation
start-sleep -seconds 10
}
Anton Tananaev8 years ago

Nice, thanks for sharing it.