[PYTHON] Home hack to buy automatically when it gets cheaper with Amazon Dash Button and Raspberry Pi

What I made

Amazon Dash Button has been released and various people are hacking it, but the weak point of this device is that it is a simple UI that pushes a button, so I do not know the amount ...

Amazon prices change frequently, so you want to buy as cheaply as possible, right? So I physically hacked the Amazon Dash Button and created a device that will automatically purchase when the price drops to the specified amount.

タイトルなし.gif

material

name parts price
Amazon Dash Button image ¥500
Raspberry Pi 3 image ¥4,800
Micro Servo SG92G ¥500
Arduino Uno image ¥3,240

For the time being, the servo motor and Amazon Dash Button are fixed with a cable tie.

Run the servo motor with Arduino

I didn't have enough parts to connect to GPIO of Raspberry Pi, so it's a little troublesome, but I decided to operate the servo motor via Arduino. Arduino is not necessary if you are prepared properly.

When ʻA` is received by serial communication, the servo motor is moved a little and the Amazon Dash Button is pressed.

#include <Servo.h> 
const int pin = 8;
Servo myservo;

void setup() {
  myservo.attach(pin);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    byte res = Serial.read();
    if (res == 'A') {
      myservo.write(20);
      delay(500);
    }
  } else {
    int val = myservo.read();
    if (val != 0) {
      myservo.write(0);
    }
  }
}

When I actually send ʻA` from the serial monitor of Arduino IDE The servo motor works like this

タイトルなし.gif

Serial communication with Raspberry Pi and Arduino

This is the procedure to connect the Raspberry Pi and Arduino with a USB cable and operate the servo motor via serial communication.

Install pyserial with pip

First, install the necessary libraries on the Raspberry Pi side.

$ sudo apt-get install python-pip
$ sudo pip install pyserial

Check the Arduino device name with dmesg

Connect the Raspberry Pi and Arduino with a USB cable and check the connection. When you type the command, it will be displayed as straw, but you can see that it is connected with the device name ttyACM0 around where ʻArduino` is displayed.

$ dmesg
[ 3401.469507] usb 1-1.5: new full-speed USB device number 4 using dwc_otg
[ 3401.583400] usb 1-1.5: New USB device found, idVendor=2341, idProduct=0043
[ 3401.583420] usb 1-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=220
[ 3401.583433] usb 1-1.5: Manufacturer: Arduino (www.arduino.cc)
[ 3401.583446] usb 1-1.5: SerialNumber: 554313134383513061F0
[ 3401.601770] cdc_acm 1-1.5:1.0: ttyACM0: USB ACM device

Try serial communication

For serial.Serial, set the device name obtained earlier and the value specified in Arduino.

dashpush.py


import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600)
time.sleep(3)
ser.write(b'A')
ser.close()

Try to move

$ sudo python dashpush.py

If the servo motor works with everything connected, you're ready to go.

Determine if the item is cheap

Use Amazon Product Advertising API to get the price of the product and If it is cheaper than the set amount, we will make it so that you can purchase it by pressing the button.

Get ASIN

Get the ASIN of the product set in the Amazon Dash Button. Copy it because it is in the product information on the page

This Suntory Tennensui image In the case of, it becomes B0043TXMWM.

Library installation

Install bottlenose and BeautifulSoup with pip

$ sudo pip install bottlenose
$ sudo pip install BeautifulSoup

Get product information and purchase

Parse the XML of Amazon product information obtained with bottlenose with Beautiful Soup If the price of water is less than 1200 yen, I will throw the serial communication and buy it.

dashpush.py


import serial
import time
from bottlenose import api
from BeautifulSoup import BeautifulStoneSoup

AMAZON_ACCESS_KEY_ID = "xxxxxxxxxx"
AMAZON_SECRET_KEY = "xxxxxxxxxx"
AMAZON_ASSOC_TAG = "corelar-22"
ASIN = "B0043TXMWM"
PURCHASE_PRICE = 1200

# get api
amazon = api.Amazon(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, Region="JP")
res = amazon.ItemLookup(ItemId=ASIN, ResponseGroup="Offers")

# xml parse
soup = BeautifulStoneSoup(res)
price = soup.find("item").find("offer").find("price").find("amount").text

# push button
if int(price) < PURCHASE_PRICE:
    ser = serial.Serial('/dev/ttyACM0', 9600)
    time.sleep(3)
    ser.write(b'A')
    ser.close()

I was able to purchase it. IMG_1108.PNG

Control your purchase with your smartphone

At this rate, we will always buy infinitely regardless of the stock status. Allows you to change the threshold for the amount of products purchased from your smartphone.

For example --Be sure to purchase (set to 10,000 yen) --Purchase if less than 1200 yen (set to 1200 yen) --Do not purchase (set to 0 yen)

If you create an API on AWS Lambda, update this amount from the iOS app, and make it readable from the Raspberry Pi, you will not have to worry about continuous purchases. When the button is pressed, just return to 0 yen specification from Raspberry Pi.

Track the price of each item and buy it when it's the cheapest in the last month! You can also do something like **

Set to cron and run regularly

This time I checked once every 30 minutes.

$ crontab -e

In the case of raspbian, the editor selection screen is displayed at the first startup, If you press enter properly without reading, nano is selected and it is about to get stuck You can reselect with vim by hitting the select-editor command

crontab


*/30 * * * * sudo python dashpush.py > /dev/null 2>&1

The rest is left ...

end

Advent Calendar was just in time ... Until now, there was no purchase API, but I'm happy that I can finally purchase it.

It was an application before Tweaking Pokemon GO Plus, but it became a completely neglected device.

Amazon has been focusing on AWS Greengrass and AWS IoT Button. I have Finally, the Raspberry Pi Zero will be sold in Japan next year, I want Japan to come to the official version as soon as possible.

Recommended Posts

Home hack to buy automatically when it gets cheaper with Amazon Dash Button and Raspberry Pi
Easy introduction to home hack with Raspberry Pi and discord.py
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Easy IoT to start with Raspberry Pi and MESH
I automatically collected my Qiita post data with Raspberry Pi and notified it regularly with Slack
I hacked the Amazon Dash Button and registered with Salesforce
Measure and compare temperature with Raspberry Pi and automatically generate graph
Measure temperature, humidity, etc. with SensorTag and send it to Ambient via Raspberry Pi 3 to graph it Part 2
A memorandum when I tried to get it automatically with selenium
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
Get GrovePi + sensor value with Raspberry Pi and store it in kintone
Pet monitoring with Rekognition and Raspberry pi
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
I tried to create a button for Slack with Raspberry Pi + Tact Switch