Alex Merchen
Published

Amazon Dash Replenishment Pens

A pen holder that determines when there are two or less pens and triggers an Amazon pen set replacement.

IntermediateFull instructions provided4 hours1,332
Amazon Dash Replenishment Pens

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
Crayola Air Dry Clay
×1
Micro Limit Switch
OpenBuilds Micro Limit Switch
×5

Software apps and online services

Arduino IDE
Arduino IDE
AWS SNS
Amazon Web Services AWS SNS

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematic layout for Pen replacement

Code

Amazon Multiple Limit Switch Code

Arduino
Adapted from the Amazon Dash Button example from the instructions I followed, this was changed so that it would take in five inputs and convert it to a number and perform logic based on the number/state.
#include <AmazonDRS.h>
#include <AmazonTokens.h>

#include <WiFi101.h>

#include "AmazonDRS.h"

AmazonDRS DRS = AmazonDRS();

//WiFi creds ----------------------------------------------------------------------------------
char ssid[] = "WiFi"; //  your network SSID (name)
char pass[] = "password";    // 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

int y = 0;
int x = 0;

int input0 = 6; // pin for the first input
int input1 = 7; // pin for the second input
int input2 = 8; // pin for the third input
int input3 = 9; // pin for the fourth input
int input4 = 10;// pin for the fifth input

debouncing
static String slotStatus = "true"; //boolean which depicts if slot is available for replenishment
static String slotId = "abddw91c-cdd1-3asd-b937-bef481699f5d";     //unique slot id ex: 0a5038b7-7609-4b81-b87e-3e291f386324
int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(115200);

  pinMode(input0, INPUT); // set the first DI/DO as an input
  pinMode(input1, INPUT); // set the second DI/DO as an input
  pinMode(input2, INPUT); // set the third DI/DO as an input
  pinMode(input3, INPUT); // set the fourth DI/DO as an input
  pinMode(input4, INPUT); // set the fifth DI/DO as an input
  
   #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() {

  //Check for button push on the arduino dash button
  //if the slot status is true proceed to request replenishment for the associated slot

  // wait a second
  delay(1000);
  // set x to be the sum of all of the inputs
  x = convertDigitalRead(input0) + convertDigitalRead(input1) + convertDigitalRead(input2) + convertDigitalRead(input3) + convertDigitalRead(input4);

  // print the value of x so that we know how many inputs are triggered
  Serial.println(x);
  
  // if the sum of the triggered inputs are less than 3 and if y is equal to 0 then
  if ((x < 3) && (y == 0))
  {
    // set y to be 1 which stands in as the current state (I don't want multiple orders if the pens aren't replaced)
    y = 1;
    
    // print that the switch was triggered
    Serial.println("Switch was triggered");
       //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!
            DRS.requestReplenishmentForSlot(slotId);
        }
        else
        {
          Serial.print("Sorry, slot ");
          Serial.print(slotId);
          Serial.println(" is not available at this time");
        }
  }
  else if ((x > 3) && (y = 1))  // if y = 1 (I've already ordered one and I'm waiting for a replacement) and x is greater than 3, this means that the pens have been replaced and I can wait to order another replacement
  {
    // set y to zero to reset the replacement state.
    y = 0;
  }

}

// set a function called convertDigitalRead that takes in an integer and outputs an integer
int convertDigitalRead(int in)
{
  // the limit switch is normally closed so if it's low then I want to return a 1
  if (digitalRead(in) == LOW)
  {
    return 1;
  }
  // if the limit switch is closed then return a 0
  else
  {
    return 0;
  }
}

Credits

Alex Merchen

Alex Merchen

22 projects • 37 followers
I'm an EE with a Masters in ECE. I like building things.

Comments