Netmodule NB2700/NB2800 and others - Script for Tracking via Traccar

cguenther3 years ago

Hi,

after enabling my Netmodule NB2700 to upload its position through the NMEA-Port, we got a much more simpler variant to do the upload through osmand-Protocol. Netmodules Support was very helpful, as this basic-variant was an "idea" of their support-staff. credits for the script goes to Fabian Klüppel of Netmodule-Support Switzerland.

HOST="hostname.de";  /* your traccar-server-url here */ 
PORT="5055"; /* do it the osmand way... */
SLEEP=6;  /* adjust as required. the nb2700 is able to do uploads every 2 sec. */
RESPONSE_FILE="traccar.response";

while (1) {
    system=nb_status("system");
    gnss=nb_status("gnss");
    serial_number=struct_get(system, "SERIAL_NUMBER");
    lat=struct_get(gnss,"GNSS1_LATITUDE");
    lon=struct_get(gnss,"GNSS1_LONGITUDE");
    altitude=struct_get(gnss,"GNSS1_ALTITUDE");
    hdop=struct_get(gnss,"GNSS1_HDOP");
    speed=struct_get(gnss,"GNSS1_HORIZONTAL_SPEED");
    track_angle=struct_get(gnss, "GNSS1_TRACK_ANGLE");
    f = fopen(RESPONSE_FILE, "w");
    fclose(f);
    if (lat != "n/a" && lon != "n/a") {
        URL=sprintf("http://%s:%s/?id=%s&lat=%s&lon=%s&hdop=%s&altitude=%s&speed=%s&heading=%s",
            HOST, PORT, serial_number, lat, lon, hdop, altitude, speed, track_angle);
        nb_syslog("Sending data to traccar!");
        rc=nb_transfer_get("","", URL, RESPONSE_FILE);
    
        // check for traccar command
        if (rc == 0) {
            f = fopen(RESPONSE_FILE, "r");
            cmd = fgets(f);
            fclose(f);
            if (strlen(cmd) > 0) {
                nb_syslog("Traccar cmd is: %s\n", cmd);
                if (cmd == "reboot") {
                    nb_reboot();
                } else {
                    nb_syslog("Command '%s' not implemented", cmd);
                }
            }
        }
    }
    sleep(SLEEP);
}

One of the highlights is the return-command-ability of osmand. so it is not only very easy to extend the script above to deliver a number of parameters from the router but it is also possible to steer the router directly through Traccar. An example is the implementation of the "reboot"-cmd, initiated directly through traccar's web-interface.

best regards
Carsten

cguenther3 years ago

Slightly extended version with command-parsing for ignition-cutout. Device is wired to keep powered for configurable amount of time on after ignition-cutout. Additionally update-rate depends on ignition-status.

HOST="traccar.xxxx.xxx";
PORT="5055";
SLEEP_MOTION=1;
SLEEP_STANDSTILL=60;
RESPONSE_FILE="traccar.response";

while (1) {

    system=nb_status("system");
    gnss=nb_status("gnss");
    wan=nb_status("wan");
    wwan=nb_status("wwan");
    io=nb_status("dio");

    serial_number=struct_get(system, "SERIAL_NUMBER");

    lat=struct_get(gnss,"GNSS1_LATITUDE");
    lon=struct_get(gnss,"GNSS1_LONGITUDE");
    altitude=struct_get(gnss,"GNSS1_ALTITUDE");
    hdop=struct_get(gnss,"GNSS1_HDOP");
    speed=struct_get(gnss,"GNSS1_HORIZONTAL_SPEED");
    track_angle=struct_get(gnss, "GNSS1_TRACK_ANGLE");
    sat_inview=struct_get(gnss, "GNSS1_SATELLITES_INVIEW");
    sat_used=struct_get(gnss, "GNSS1_SATELLITES_USED");

    wan_ip=struct_get(wan, "WANLINK1_ADDRESS");
    wan_signal=struct_get(wan, "WANLINK1_SIGNAL_QUALITY");

    wwan_band=struct_get(wwan, "MOBILE1_BAND");
    wwan_rssi=struct_get(wwan, "MOBILE1_SIGNAL_RSSI");
    wwan_level=struct_get(wwan, "MOBILE1_SIGNAL_LEVEL");

    wwan_lai=struct_get(wwan, "MOBILE1_LAI");
    wwan_lac=struct_get(wwan, "MOBILE1_LAC");
    wwan_cid=struct_get(wwan, "MOBILE1_CID");

    BANDINFO = sprintf("%s/%sdBi/%s", wwan_band, wwan_rssi, wwan_level);
    CELLINFO = sprintf("%s/%s/%s", wwan_lai, wwan_lac, wwan_cid);

    io_in1=struct_get(io, "IN1");
    io_in2=struct_get(io, "IN2");
    io_out1=struct_get(io, "OUT1");
    io_out2=struct_get(io, "OUT2");

    f = fopen(RESPONSE_FILE, "w");
    fclose(f);

    if (lat != "n/a" && lon != "n/a") {
        URL=sprintf("http://%s:%s/?id=%s&lat=%s&lon=%s&hdop=%s&altitude=%s&speed=%s&heading=%s&satellites=%s/%s&WAN-IP=%s&Bandinfo=%s&Cellinfo=%s&Signal-Quality=%s&Klemme-15=%s&Engine-ENABLE=%s&Engine-ENABLE-AfterBoot=%s",
            HOST, PORT, serial_number, lat, lon, hdop, altitude, speed, track_angle, sat_inview, sat_used, wan_ip, BANDINFO, CELLINFO, wan_signal, io_in1, io_out2, nb_config_get("dio.out2"));
        nb_syslog("<%s>", URL);
        rc=nb_transfer_get("","", URL, RESPONSE_FILE);
    
        // check for traccar command
        if (rc == 0) {
            f = fopen(RESPONSE_FILE, "r");
            cmd = fgets(f);
            fclose(f);

            if (strlen(cmd) > 0) {
                nb_syslog("Traccar cmd is: %s\n", cmd);

                if (cmd == "reboot") {
                    nb_reboot();
                }

                if (cmd == "off") {
                     nb_dio_set("out2",0);
                }

                if (cmd == "on") {
                     nb_dio_set("out2",1);
                }

                if (cmd == "toggle") {
                     nb_dio_set("out2",0);
                     sleep(5);
                     nb_dio_set("out2",1);
                } 

                if (cmd == "permoff") {
                    nb_dio_set("out2", 0);
                    sleep(1);
                    nb_config_set("dio.out2=off");
                }
                
                if (cmd == "permon") {
                    nb_dio_set("out2", 1);
                    sleep(1);
                    nb_config_set("dio.out2=on");
                }

                if (cmd != "reboot" && cmd != "toggle" && cmd != "off" && cmd != "on" && cmd != "permoff" && cmd != "permon") {
                    nb_syslog("Command '%s' not implemented", cmd);
                }
            }
        }
    }
    
    if (io_in1 == "on") {
        sleep(SLEEP_MOTION);
    }
    else {
        sleep(SLEEP_STANDSTILL);
    }
}
swayers3 years ago

Thank you for sharing, that is a very good code for netmodule :)

In-fact a good one to use as a reference point for other similar hardware/devices too

Henrick Langnera year ago

Hi, I'm using a NB2800 and I just tried to use your script. I'm not so into this and I'm not quiet sure wether I understand the process behind "Jobs" and "Triggers" in the NB2800...

I just copied your script and it started working... Some kind of magic for me :-D

Do I have to set a trigger to run the script or is it running by itself, caused through the paraphrase "SLEEP=6"...?

And another question is, which part of the script describes the frequency of uploading data to the traccar-server? Is it "SLEEP=" and time is given in seconds?

Thanks a lot!

cguenthera year ago

Hi Henrick,

yes, the frequency is set with the two sleep-parameters in the top of the script. as you can see, it is already made as fast as possible.

using the nb2800 in the car works ok, but you will notice an awful long time for the unit to boot up. i am about to change the hardware, installing somewhat like the rutx11 or rutx50 in the car, because i have great results with teltonika-products on a similar basis. otherwise i tend to use the FMM920/FMC920 as tracker-only.

teltonika is well supported by traccar, and the management-abilities like FOTA given by teltonika itself are great.

Henrick Langnera year ago

Hi,
first of all thanks for your fast reply!

I configured SLEEP to 240 seconds an it’s working very well.
The script never stops, except rebooting the device or stuff like this, right?

Nevertheless I configured a trigger to start the script daily at 00:00. Right now I receive the error „script is already running“ but I think it will work after rebooting caused threw a power failure or stuff like this, right?

The router is working 24/7 because it’s mounted in a vehicle from some kind of federal crisis response unit (some kind of operations management for paramedics). That’s also the reason why changing to Teltonika or another different router is not that easy.
But also I have to tell, that it’s working very well even configuration of netModule Nb2700/2800 isn‘t that uncomplicated… :‘D