This is a guide to make automatic measuring an available item using Ultrasonic Distance meter and replenish the item with Amazon's Dash Replenish Service.
Have a look at the demo of this project
Making Your Own DeviceFirst of all, you have to create your Amazon DRS device. If you don't know how to make one, then please have a look at Getting Started With Amazon DRS. Link: https://www.hackster.io/harshmangukiya/getting-started-with-amazon-drs-5078f0
You need a memory card loaded with Raspbian Jessie OS.
Connect the Raspberry Pi 3, ultrasonic distance meter, resistors and LEDs as per the below given image with jumper wires.
First assign a static IP to your Raspberry Pi 3, connect it to your router and restart the Raspberry Pi 3.
Now start Python 3 from the Programming menu and create a new python file.
Write the below given code in the file:
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)
Make a new folder in Raspberry Pi and save the file. (Here I have written the filename as webhost.py)
This python file has a combination of ultrasonic distance meter and web server.
As you can see in the Python code, we have to secure the server so we have to make SSL certificate and change the path in the python file also. (Here you can also use the SSL certificate which I have provided in the GitHub Repository. SSL Certificate passphrase is 'raspberry'.)
Note: you have to store HTML pages in 'templates' folder.
Making HTML Webpages for the Web ServerHere, I am making four HTML web pages for the web server:
(1) index.html
(2) replenish.html
(3) response.html
(4) success.html
You can check their codes in the link or also available in code section.
Running the Web ServerSimply you have to open on Terminal on the Raspberry Pi 3, go to the web server Python file and run the Python file.
When we run the python code, it will automatically check if a replenishment of item is needed or not using the ultrasonic distance meter.
At first, I have a jar full of items inserted in it. So, it doesn't need replenishing. It will show a page like the below given image.
The second time, I have an empty jar, meaning there are no items in jar. So, it needs replenishing. It will show a page like the below given image.
For more information on the code, please visit the GitHub Repository and see the video given at the start of the page.
Thank you for reading the article.
Comments