Harsh Mangukiya
Published © GPL3+

Dash Replenish using Amazon DRS and Sensor

This is a guide to show how we can replenish our items using Amazon's Dash Replenish Service and using an ultrasonic distance meter.

IntermediateFull instructions provided4 hours1,935
Dash Replenish using Amazon DRS and Sensor

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Resistor 475 ohm
Resistor 475 ohm
×1

Software apps and online services

AWS SNS
Amazon Web Services AWS SNS
Amazon Web Services Login with Amazon
Amazon Dash Replenishment Service
Amazon Web Services Amazon Dash Replenishment Service

Story

Read more

Schematics

Schematics

Code

webhost.py

Python
Python code for running Web Server using Raspberry PI 3 and get replenish using Ultrasonic Distance Meter.
from gpiozero import DistanceSensor
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
ultrasonic = DistanceSensor(echo=17, trigger=4, threshold_distance=0.2)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)

from flask import Flask, render_template, redirect

app = Flask(__name__)

dis = ultrasonic.distance
if dis<0.15:
    #no replenishment, green led
    GPIO.output(27,GPIO.LOW)
    GPIO.output(18,GPIO.HIGH)
    homepage = 'index.html'
else:
    #replenish, red led
    GPIO.output(27,GPIO.HIGH)
    GPIO.output(18,GPIO.LOW)
    homepage = 'replenish.html'

@app.route('/')
def index():
    return render_template(homepage)

@app.route('/authresponse')
def authresponse():
    return render_template('response.html')

@app.route('/accesstoken/<var>')
def accesstoken(var):
    import requests
    r = requests.post("https://api.amazon.com/auth/o2/token", data={'grant_type': 'authorization_code', 'code': var, 'client_id': 'Client_ID', 'client_secret':'Client_Secret','redirect_uri':'https://192.168.1.35:8100/authresponse'})
    data = r.json()
    accesstoken = data['access_token']
    token = 'Bearer ' + accesstoken
    x = requests.post("https://dash-replenishment-service-na.amazon.com/replenish/Slot_id", headers={'Authorization': token, 'x-amzn-accept-type': 'com.amazon.dash.replenishment.DrsReplenishResult@1.0', 'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsReplenishInput@1.0'})
    return redirect("https://192.168.1.35:8100/sucess")

@app.route('/sucess')
def sucess():
    return render_template('sucess.html')

if __name__ == '__main__':
    context = ('/home/pi/web/server.crt','/home/pi/web/server.key')
    app.run(host='0.0.0.0', debug=True, port=8100, ssl_context=context)

index.html

HTML
Homepage for Raspberry PI 3 Server
<html>
<head>
	<title>Welcome to Amazon DRS</title>
</head>
<body>
	<div style="height: 100%;width: 100%;justify-content: center;align-items: center;display: flex;">
		<h1 style="color: forestgreen;">Items in the device doesn't need to Replenish</h1>
	</div>
</body>
</html>

replenish.html

HTML
Homepage for Raspberry PI 3 Server at time of replenish is required
<html>
<head>
	<title>Welcome to Amazon DRS</title>
</head>
<body>
	<div style="text-align: center;">
			<div style="height: 50%;width: 100%;justify-content: center;align-items: flex-end;display: flex;">
			<h1 style="color: darkred;">Items in the device need to Replenish</h1>
			</div>
			<form action="https://www.amazon.com/ap/oa" method="POST">
				<input id="client_id" name="client_id" value="" type="hidden"> <!-- Enter your client ID here in value -->
			  	<input id="scope" name="scope" value="dash:replenish" type="hidden">
			  	<input id="scope_data" name="scope_data" value='{"dash:replenish":{"device_model":"","serial":"RP3","is_test_device":true}}' type="hidden"><!-- Enter your Device Model ID here in value -->
			  	<input id="response_type" name="response_type" value="code" type="hidden">
			  	<input id="redirect_uri" name="redirect_uri" value="https://192.168.1.35:8100/authresponse" type="hidden"><!-- Enter your Response URL in value -->
			  	<input type="submit" value="Press button to Replanish item">
			</form>
	</div>
</body>
</html>

response.html

HTML
Response page and redirect to Replenish to DRS
<html>
<head>
	<title>Welcome to Amazon DRS</title>
</head>
<body>
	<script>
		var url = window.location.href;
		var pos = url.indexOf("code=");
		var authcode = url.substring(pos+5,pos+25);
		window.onload = function(){
			location.href = "https://192.168.1.35:8100/accesstoken/" + authcode; // Enter your RP3 ip here as Https:// + ip + '/accesstoken/'
		}
	</script>
</body>
</html>

sucess.html

HTML
Successfully order placed page
<html>
<head>
	<title>Welcome to Amazon DRS</title>
</head>
<body>
	<div style="height: 100%;width: 100%;justify-content: center;align-items: center;display: flex;">
		<h1 style="color: forestgreen;">Thank you for using Amazon's Dash Replenish Service. Your order has been placed sucessfully.</h1>
	</div>
</body>
</html>

ultrasonic_led.py

Python
Normal LED program of Raspberry PI using Ultrasonic Distance Meter.
This is only for information. Not required for project.
from gpiozero import DistanceSensor
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
ultrasonic = DistanceSensor(echo=17, trigger=4, threshold_distance=0.3)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
while True:
  dis = ultrasonic.distance
  print(dis)
  if dis<0.15:
          #no replenishment, green led
          GPIO.output(27,GPIO.LOW)
          GPIO.output(18,GPIO.HIGH)
  else:
          #replenish, red led
          GPIO.output(27,GPIO.HIGH)
          GPIO.output(18,GPIO.LOW)

webserver.py

Python
Normal Web Server program of Raspberry PI 3.
This is only for information. Not required for project.
from flask import Flask, render_template, redirect

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/replenish')
def replenish():
    return render_template('replenish.html')

@app.route('/authresponse')
def authresponse():
    return render_template('response.html')

@app.route('/accesstoken/<var>')
def accesstoken(var):
    import requests
    r = requests.post("https://api.amazon.com/auth/o2/token", data={'grant_type': 'authorization_code', 'code': var, 'client_id': 'Client_ID', 'client_secret':'Client_Secret','redirect_uri':'https://192.168.1.35:8100/authresponse'})
    data = r.json()
    accesstoken = data['access_token']
    token = 'Bearer ' + accesstoken
    x = requests.post("https://dash-replenishment-service-na.amazon.com/replenish/Slot_id", headers={'Authorization': token, 'x-amzn-accept-type': 'com.amazon.dash.replenishment.DrsReplenishResult@1.0', 'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsReplenishInput@1.0'})
    return redirect("https://192.168.1.35:8100/sucess")

@app.route('/sucess')
def sucess():
    return render_template('sucess.html')

if __name__ == '__main__':
    context = ('/home/pi/web/server.crt','/home/pi/web/server.key')
    app.run(host='0.0.0.0', debug=True, port=8100, ssl_context=context)

Amazon DRS

Code GitHub Repository of Amazon DRS and Ultrasonic Distance Meter

Credits

Harsh Mangukiya

Harsh Mangukiya

2 projects • 34 followers
Founder@ Iotguider - Getting Started Guide for Internet of Things. Student, Technology Lover, Innovator, Designer, Maker, Photographer...

Comments