Man Orders iPhone but Delivered a Stone

Man Orders iPhone but Delivered a Stone

Recently frauds in eCommerce delivery have increased drastically. What can Azure do about it? Check it out.

  • 1 comment
  • 0 respects

Components and supplies

Ph a000066 iso (1) ztbmubhmho
Arduino UNO
Ard wifi101
Arduino Wifi Shield 101
BC547 Transistor
100 k Resistor
470 ohm Resistor
09590 01
LED (generic)
LDR - Light Dependent Resistor

Apps and online services

About this project


There are many incidents where people ordered things online but instead they recieve something else they never expected.

Even if the eCommerce website corrects this and sends to them with the proper product again, this news goes viral in the social network, and due to this the company diminishes their brand, report with customers and revenue.

This simple graph from a recent online frauds report shows clearly that with increase in fraud your revenue is affected.

If we analyse some cases, review probablity and look at the suspects for the fraud, it might be:

  • the seller
  • the delivery person between seller to store
  • someone at the store
  • the delivery person between store house to customer
  • the customer

We have enough cases and evidence where the person involved in the fraud might be any of them, but if we calculate the probability of who might have been the suspect its 1/5 for each, which is 0.2.

It is so hard to find the person in the dark commiting the fraud.

So what can be done to make a safe delivery of the product?

One way is once the seller packs the product, it should not be opened on the way to the customer.

How we can verify it?

Cameras inside the delivery box? Nope, too costly and hard to maintain. Motion sensor? No. We need to find the individual commiting these frauds in the dark.

Idea! Light sensor!

Cheap, easy to handle and reusable too!

All we need to do is, while packing we need to keep the light sensor inside the box and pack it.

So if someone tries to open the package, the sensor will detect the light and it will alert us about the fraud.

So, what are you waiting for? Come let’s build a light sensor.

Light sensor :- LED will glow in the absence of light

During the absence of light, this device will make the bulb glow, and during the presence of light, it will bulb will turn off. This means 1 in the dark and 0 in the light.

While the seller is packing the package, switch on the sensor and place it with package. Once the sensor is on, map the sensor ID with the order ID and complete the packing within 5 mins of grace time. It will send this signal to the cloud and track if the package is broken on the way.

Let's send this data to the Microsoft Azure:

So the status of the LED is our data. LED on = package is safe. LED off = package broken.

Now will connect this pin to the Arduino:

Creating a Mobile Service Backend

Using the web management portal, click on New, then Compute, Mobile Service, Create. You will need to choose a public URL for your service, a region where your service will be created, and the language you want to use: in this example I will use JavaScript. As part of the service, you are asked to create or connect to an Azure SQL Database that will be used to store your data. If you don’t have a database yet, you can opt to create a free 20MB database.

The next screen will ask you the details about your database: most importantly the server that will host it. You can choose an existing server if you have one, or create a new one on the fly (in this case you will have to select a login/password combination).

Mobile Services offers a built-in NoSQL data API in the form of simple tables. Since we want to send some data, let’s create a new table: in your service management page, go to the Data tab, and click on the big “+” sign at the bottom of the page to create a new table. Since this is NoSQL, all you need to do is to give your table a name: we don’t have to specify a schema for the data. We are going to ignore permissions for the moment, but as you can see in the interface you could restrict access to the insert/update/delete/read operations.

Now you have all the elements you need. If you have cURL installed, you can insert a record from the command line like so. Command to create a new Mobile Service backend using that server:

azure mobile create myarduinosvc --sqlServer kv5xxxxbkk \ 
curl -v -X POST -H Content-Type:application/json \ 
-H X-ZUMO-APPLICATION:xxxxzKyLxxxxRxiAxxxxWkrixxxxFa50 \ 
-d '{"test": 42}' \ 

In the code below, I am using the Ethernet library to open a connection to the Azure Mobile Services endpoint and send the HTTP POST requests to store the values read from the LDR into our table. If you want to run it, you will need to change the following variables as explained in the comments:

  • Server is the address of your Mobile Services endpoint
  • table_name is the name of the table you created above
  • ams_key is your Mobile Services application key

You should also probably change the MAC address in mac for the Ethernet shield (your MAC address is usually on a sticker at the back of the shield).

#include <SPI.h>
#include <Ethernet.h> 
// Ethernet shield MAC address (sticker in the back) 
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
// Azure Mobile Service address 
// You can find this in your service dashboard 
const char *server = ""; 
// Azure Mobile Service table name 
// The name of the table you created 
const char *table_name = "telemetry"; 
// Azure Mobile Service Application Key 
// You can find this key in the 'Manage Keys' menu on the dashboard 
const char *ams_key = "YaRWxxxxzKGRxxxxLPiNxxxxXYOvxxxx"; 
EthernetClient client; 
char buffer[64]; 
** Send an HTTP POST request to the Azure Mobile Service data API 
void send_request(int value) 
 if (client.connect(server, 80)) { 
   Serial.print("sending "); 
   // POST URI 
   sprintf(buffer, "POST /tables/%s HTTP/1.1", table_name); 
   // Host header 
   sprintf(buffer, "Host: %s", server); 
   // Azure Mobile Services application key 
   sprintf(buffer, "X-ZUMO-APPLICATION: %s", ams_key); 
   // JSON content type 
   client.println("Content-Type: application/json"); 
   // POST body 
   sprintf(buffer, "{\"value\": %d}", value); 
   // Content length 
   client.print("Content-Length: "); 
   // End of headers 
   // Request body 
 } else { 
   Serial.println("connection failed"); 
** Wait for response 
void wait_response() 
 while (!client.available()) { 
   if (!client.connected()) { 
** Read the response and dump to serial 
void read_response() 
 bool print = true; 
 while (client.available()) { 
   char c =; 
   // Print only until the first carriage return 
   if (c == '\n') 
     print = false; 
   if (print) 
** Close the connection 
void end_request() 
** Arduino Setup 
void setup() 
 while (!Serial) { 
   ; // wait for serial port to connect. 
 if (Ethernet.begin(mac) == 0) { 
   Serial.println("ethernet failed"); 
   for (;;) ; 
 // give the Ethernet shield a second to initialize: 
** Arduino Loop 
void loop() 
 int val = analogRead(A0); 

Sample Arduino code to connect with Azure

Here is a quick overview of the different functions in the code:

  • setup initializes the Serial port and the Ethernet shield using the DHCP method (you should have a working DHCP server on your network).
  • loop reads the sensor value on line A0 and then calls the following network functions.
  • send_request is where most of the work happens. A TCP connection is established to the Mobile Services endpoint, and the HTTP POST request is emitted. The headers are the same as shown in the cURL example above, e.g. the Host with the name of your endpoint, the X-ZUMO-APPLICATION key, and the JSON Content-Type. The body contains a simple JSON object with the value from the sensor.
  • wait_response just blocks until some bytes are available on the connection.
  • read_response will read all the response bytes, and just print the HTTP response code to the serial console for debugging.

Story Continues...

When the package arrives at the customer's place, they need to sign for the delivery. Then in the cloud the package will be set to open mode and will send a notification message to the customer to open it saying:

Our product is ready to open now, and it has been securely couriered by us. Thanks for choosing"

Can we make it a little romantic?

Let's say you make a gift order to someone special. Once they sign for delivery, the box will be set to open mode. When the special person opens the package, the sensor will detect the light... of love. This sends 0 data to the cloud, and we notify the special person with a message that:

Rahul has ordered a gift for your birthday. And here is a message from him: Hapieeee B’day my love. Keep smiling & stay healthy.

It would be so romantic if the person opens the gift and when they open it, they get a surprise wish notification and the gift.

Wow, that'd be so nice. I’m panning to make one like this and send it.

Okay, now let’s come back to business.

These kinds of service make the customer feel more secure and trustworthy on the eCommerce website. This will also increase return on investment to the company. If the package is broken without being set to open mode, then that’s it - case closed.

But wait the game is not yet over; let’s calculate the probability now.

Our suspect list:

  • the seller
  • the delivery person between seller to store
  • someone at the store
  • the delivery person between store house to customer
  • the customer

Since we can’t break a package while the courier travel and we eliminate the suspect numbers at 2, 3, 4 and we have 1 and 5, i.e. the seller and customer, which makes a probability of 1/2 = 0.5.

How to identify the suspect? Yes, we have increased the probability of prediction with the help of a light sensor, but to make it more accurate we need to think more to find the culprit!

I’m very lazy about thinking, so let’s make the machine do the thinking. It’s time to implement Microsoft machine learning.

We will train the machine with data on successful orders, fraud orders with solved and unsolved cases in identifying the suspect (customer or seller).

Many other factors can also be added to identify the suspect. Even we can make a prediction when an order is placed what can be the probability that it might be a fraud through machine learning. If the probability is high we can try avoiding in taking that order.

Simple steps to perform machine learning:

Login on to Azure machine learning algorithm Studio and click on experiments -> new -> blank experiment

1. Import the data

2. Split the data as 0.75 to train the model & rest 0.25 to test it.

3. Connect the machine learning algorithm and split data left point to the train model.

4. Now it's time to score the prediction with the test data, connect the output of the split data right node to score model right node and output of train model to the left node of score model and run.

Thus buy using machine learning to find out whether the suspect is the customer or seller, we predict the probability to be 1.

Thus we help the delivery system to be more secure and trustworthy to make sure no fraud happens throughout delivery.

With the help of Microsoft Azure, we can be sure that your package is secure and leading to better business growth.


Similar projects you might like


Project showcase by Bill Lovegrove

  • 82 respects

TIA Weak Artificial Intelligence IoT Assistant

Project tutorial by Adam Milton-Barker

  • 26 respects

My motorbike Telemetry

Project tutorial by Davide Longo

  • 132 respects

GPS Datalogger, Spatial Analysis, and Azure IoT Hub.

Project tutorial by Shawn Cruise

  • 99 respects

Precision Agriculture – A Prototype Solution

Project in progress by Eric Hall

  • 74 respects
Add projectSign up / Login