Simulating a package delivery van

How to build a logistic application via simulating a delivery van

Improving Transport Logistics

As e-commerce proliferates, logistic companies are under an immense pressure to deliver packages faster to more customers. Tracking the real time location and delivery performance of the vehicle in the fleet is vital to save cost and improve logistic efficiency. There are many fleet tracking solutions in the market, many of them rely on a GPS+UMTS based OBD2 dongle.

However, only monitoring these vehicles is not enough. One of the key objective of IoT systems today is to optimize the delivery based on feedback and provide a real time insights to end customer while improving the system efficiency. Imagine if your delivery company could accurately predict when the courier is going to arrive, within a 5 minute time slot. Wouldn’t that solve a big pain point in today’s system?

Modelling with such accuracy needs a lot of data to be captured from existing systems. For this purpose, accurately simulating vehicle movement under real traffic conditions is a major challenge. In this simulation exercise, we will model a package delivery van delivering parcels to various location in Zurich, Switzerland in real time.

The Concept

We will model a situation where a delivery van driver needs to deliver 10 packets around central Zurich area.

The van will start from one address to another and deliver a packet at each location. We wouldn’t tackle the route optimization problem at the moment. The destination address is randomly choosen, upon start of each trip, with 1 km radius of current location.

The waiting time once vehicle reaches the target location is currently fixed, implying it takes equal time to deliver packet for the driver to the door steps.
The delivery vehicle will report its GPS location to a cloud platform every 10 seconds. We will track and monitor the vehicle’s postion live as well as number of pending delivery counts. Here’s how our dashboard is going to look like.

Let’s get started

In this case, we are publishing the data to Losant as a cloud platform, but the simulation is generic and can be easily adapted to fit any other cloud platform.

Configure Losant and setup the dashboard

If you haven’t yet, sign up for free with and create a new Application. Let’s call it Connected truck.

Within the application create a new device named Truck. (use blank device when asked about recipe)

Choose device type as Standalone. It is important to specifiy following attributes for the device Truck:

Note that the names of parameters (location, contents, speed) should exactly match as in above. As you might have correctly imagined, we will report above 3 parameters to the losant platform.

Make sure you copy your Device ID from the top right corner of your device tab.

Next, Go to Tab Security and create a Device Access Key. The key should be tied to the device type Truck we just created. Make sure you download the access key and secret to your local computer. We will need it later. 

At the end of this step you should have following:

A Device ID which looks like: 58a6c6e6217f8521311f28cd
An Access key which looks like: e4fc60fe-11111-22222-8480-16710b46908d
An Application Secret: XXXXXX… (A really long string)

Now go to the Dashboard Menu, create New Dashboard. Then you will see a list of gadgets to add. Simply pick GPS History, Gauge and Indicator Gadgets. There are plenty of gadgets in Losant so feel free to experiment.

Alright, we are finished with Losant for the moment. Let’s get to IoTIFY.

Signup with IOTIFY

If you haven’t signed up yet, please request a free account with [][2]  Once signed up, please go to Network simulator tab and create a new template.

Give a unique name to the template. We’ll call it logistics.

Set the protocol to MQTT. Change the MQTT Address to following:

Further in the MQTT parameters:

In the Client ID: Provide Device ID as obtained from Losant
In the Username: Provide Access Key as obtained from Losant
In the Password: Provide Access secret as obtained from Losant.

Set MQTT Topic

Provide following value: losant/[Device ID]/state
E.g. The topic string will look like following:-

MQTT Message

Copy and paste following template to model a delivery van for this scenario.

// change this to your city if you need
const city= "Zurich, CH";

// delivery radius around the city center
const radius = 1000;

// the very first iteration.
if (state.contents === undefined){
state.contents = 10;
state.trip = 0;
state._$wait = 0;
state.start = city;
var dest = location({address:city, accuracy: radius});
state.dest = dest.latitude + ","+ dest.longitude;
// simply drive
state.path = drive({start: state.start, end: state.dest});

// if last position is equal to the new one, we have reached the current destination.
if (state.path.finished){

//change wait limits to stay longer for delivery
if (state.contents > 0 && state._$wait++ == 3){
state._$wait = 0;
console.log("Trip finished at ",state.path.latitude + ","+ state.path.longitude);
//increment trip index, loop to trip 0 once last trip ends
state.start = state.path.latitude + ","+ state.path.longitude;
var dest = location({address:city, accuracy: radius});

state.dest = dest.latitude.toFixed(6) + ","+ dest.longitude.toFixed(6);
state.path = drive({start: state.start, end: state.dest});
console.log("Now driving ", state.start, " to ", state.dest);

//publish our information to Losant.
var retval = {};
retval.time = { "$date" :}; = {}; = state.path.latitude + ","+ state.path.longitude; = state.path.speed ; = state.contents;
return JSON.stringify(retval, null, 2);


The template may look like complex at a first glance but it’s quite simple actually. In the very first iteration, we will setup our state variables. The drive function will generate the GPS coordinates for vehicle. Once the drive is finished (the function will return a value with finished set to true), we will simply pick another trip after a slight wait. We will continue to drive untill we have delivered all the parcels. Once all the delivery is completed, the vehicle will simply wait at the last known location.

That’s it. Hit the preview button and you should see sample message along with Success result in the preview window. If there is any error, please check you have correctly followed the above steps.

Start the simulation.

Once the template is ready, hit Save and go to the generate tab. Specify the number of client to be 1 and number of iteration to be 100. You could reduce or increase the number of iterations if you want, usually it depends upon how many contents are to be delivered and how big is the radius of the delivery.

Make sure to click Collect extended results with state information per client checkbox so that you could monitor all the state variables in the simulation detailed result tab.

That’s it. If everything works well, after a while, the delivery vehicle will be visible in the Losant Application dashboard.

Once the simulation is finished, here is how your dashboard will look like.

Modelling the real world conditions

Once you have played around with how the simulation works, you could add some real world conditions and add intelligence to your cloud platform to create more value for the solution. E.g.

We will be happy to hear your thoughts about what application ideas you could build on top of this simulation! Please keep your comments coming.