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 objectives of IoT systems today is to optimize the delivery based on feedback and provide 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 chosen, upon start of each trip, with 1 km radius of current location.

The waiting time once the vehicle reaches the target location is currently fixed, implying it takes equal time to deliver packets 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 (select "Create Blank Device" when asked to choose between create from recipe and create from scratch).

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

  • GPS String : location

  • Number - speed

  • Number - contents

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 Access Keys and create an Access Key. The key should be restricted 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-1111-2222-8480-16710b46908d An Access Secret: XXXXXX… (A really long string)

Now go to the Dashboards Menu, create New Dashboard. Then click Add Block and 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. For each gadget that you choose, click on Customize, then in the gadget configuration screen select the truck device in the Device IDs / Tags field and a device attribute in the Attribute field; finally, click on Add Block.

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 From the Sign In / Join menu, select Network Simulator and then register an account. 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 and click on CREATE.

In the MQTT Parameters section, change the endpoint URL to following:; in the Client ID field, provide the Device ID as obtained from Losant; in the MQTT Topic field, provide the following value: losant/[Device ID]/state E.g. The topic string will look like following:- losant/58a6c6e6217f8500018f28cd/state

In the Credentials section: In the Username: Provide access key as obtained from Losant In the Password: Provide access secret as obtained from Losant.

Message Contents

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 set up 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 until 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 Simulate tab. Select the network template you just created, then specify the number of clients to be 1 and number of iterations 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 enable the Save state information for each client and iteration checkbox so that you could monitor all the state variables in the simulation detailed result tab.

Finally, click START SIMULATION at the bottom of the page. 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.

  • Analyze the route taken and suggest an optimized route.

  • Slow down or accelerate the simulated vehicle speed (ask us how in the slack channel) and generate alerts when driver is driving too fast.

  • Create an app which predicts the estimated arrival of delivery van to the customer.

  • Change wait times at delivery stop to random values and flag excessive wait times at any stop.

  • Get a delivery confirmation and put it on blockchain (moonshot, but who knows:-)).

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.