Created
February 26, 2017 13:17
-
-
Save rondagdag/c9e799e03a1c6b8e9aad574b8d8ada54 to your computer and use it in GitHub Desktop.
Amazon DRS Promise: Never Miss Coffee Break Again! https://www.hackster.io/coffee-drinkers/amazon-drs-promise-never-miss-coffee-break-again-a88584
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************* | |
Arduino Sketch to reorder items via Amazon Dash Service | |
Written by @RonDagDag and Johnathan Hottell | |
Read more at https://www.hackster.io/coffee-drinkers/amazon-drs-promise-never-miss-coffee-break-again-a88584 | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*******************************************************************/ | |
#ifdef ESP8266 | |
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecure.h> | |
#else | |
#include <WiFi101.h> | |
#endif | |
#include "AmazonDRS.h" | |
AmazonDRS DRS = AmazonDRS(); | |
//WiFi creds ---------------------------------------------------------------------------------- | |
char ssid[] = ""; // your network SSID (name) | |
char pass[] = ""; // your network password (use for WPA, or use as key for WEP) | |
//------------------------------------------------------------------------------------------------------ | |
#define slotNumber 1 //This will vary for multi slot devices - dash buttons typically only serve one product/slot | |
static String slotStatus = "true"; //boolean which depicts if slot is available for replenishment | |
static String slotId = "e2d8d2a3-ecf3-7609-bc02-a1bbb776bdf8"; //unique slot id ex: 0a5038b7-7609-0a5038b7-7609-4b81-b87e-3e291f386324-b87e-3e291f386324 | |
int status = WL_IDLE_STATUS; | |
//Distance sensor defs | |
int orderThreshold = 10; //the distance threshold in cm to place an order | |
const int trigPin = 2; | |
const int echoPin = 4; | |
long duration, inches, cm, cmAverage; | |
int distanceArray[11]; | |
int averagingCounter = 0; | |
int placeOrderTrigger = 0; | |
int orderPlaced = 0; | |
//////////////////////////////////////////////// | |
void setup() { | |
Serial.begin(115200); | |
pinMode (12, INPUT_PULLUP); //Set up digital pin for reset button | |
pinMode (13, OUTPUT); //LED used for when an order is placed | |
#ifdef ESP8266 | |
WiFiClientSecure client; | |
#else | |
WiFiSSLClient client; | |
#endif | |
//Start up DRS | |
DRS.begin(&client); | |
//connect to WiFi | |
Serial.println("Initializing DRS... connecting to WiFi"); | |
while (status != WL_CONNECTED) { | |
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | |
status = WiFi.begin(ssid, pass); | |
Serial.println("."); | |
delay(3000); | |
status = WiFi.status(); | |
} | |
Serial.print("Connected to "); | |
Serial.println(WiFi.SSID()); | |
//initialize slots | |
DRS.retrieveSubscriptionInfo(); //check slot statuses | |
slotStatus = DRS.getSlotStatus(slotNumber); | |
slotId = DRS.getSlotId(slotNumber); | |
} | |
//////////////////////////////////////////////////// | |
void loop() { | |
sensorPing(); //take a distance reading | |
// increment counter for averaging | |
if (averagingCounter >= 11) { | |
averagingCounter = 0; | |
} | |
else{ | |
averagingCounter ++; | |
} | |
//put the distance reading into the array | |
distanceArray[averagingCounter] = cm; | |
//calculate the average | |
cmAverage = ((distanceArray[0] + distanceArray[1] + distanceArray[2] + distanceArray[3] + distanceArray[4] + distanceArray[5] + distanceArray[6] + distanceArray[7] + distanceArray[8] + distanceArray[9] + distanceArray[10] + distanceArray[11])/12); | |
Serial.print("Average cm: "); | |
Serial.println(cmAverage); | |
if (cmAverage >= orderThreshold and orderPlaced == 0) | |
{ | |
//Check if slot is available, if so replenish | |
if(slotStatus == "true") //if the product in slot are available | |
{ | |
//we have a match! replenish the products associated with that slot! | |
orderPlaced = 1; | |
digitalWrite(13, HIGH); | |
Serial.println("Placing Order"); | |
DRS.requestReplenishmentForSlot(slotId); | |
} | |
else | |
{ | |
// Serial.print("Sorry, slot "); | |
//Serial.print(slotId); | |
//Serial.println(" is not available at this time"); | |
orderPlaced = 1; //comment these out later, for testing | |
digitalWrite(13, HIGH); //comment these out later, for testing | |
Serial.println("Placing Order"); | |
//Serial.println("Order not placed, locking out"); //comment these out later, for testing | |
} | |
} | |
else if (digitalRead(12) == LOW) | |
{ | |
orderPlaced = 0; | |
digitalWrite(13, LOW); | |
Serial.println("Reseting Order"); | |
} | |
delay(2000); | |
} | |
//////////////////////////////////////////////////// | |
void sensorPing() | |
{ | |
// The sensor is triggered by a HIGH pulse of 10 or more microseconds. | |
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse: | |
pinMode(trigPin, OUTPUT); | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
pinMode(echoPin, INPUT); | |
duration = pulseIn(echoPin, HIGH); | |
cm = microsecondsToCentimeters(duration); // convert the time into a distance | |
Serial.print("cm: "); | |
Serial.println(cm); | |
} | |
///////////////////////////////////////////////// | |
long microsecondsToInches(long microseconds) | |
{ | |
return microseconds / 74 / 2; | |
} | |
////////////////////////////////////////////////// | |
long microsecondsToCentimeters(long microseconds) | |
{ | |
return microseconds / 29 / 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment