Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

How to detect Amazon Dash Button button push with ESP8266

At one time, it was popular to hack the Amazon Dash Button and modify it into an arbitrary IoT button, and while the detection of Amazon Dash Button button pushes can be done on a PC or RaspberryPi, this is a memo on how to do it on an ESP8266.

How the Amazon Dash Button hack works

Let's review how the Amazon Dash Button hack works. You can read more about how it works in Using Amazon Dash Button as just another IoT button.

To reiterate, when the button is pressed, the Amazon Dash Button turns on, connects to WiFi, accesses Amazon's servers via the Internet, and places the actual order.

However, since the device is involved in ordering, it is difficult to really hack it, so we will not do any modifications, etc., but will do an indirect hack, where the Amazon Dash Button detects that it has connected to WiFi and considers the button has been pushed.

In order for the Amazon Dash Button to connect to WiFi when the button is pressed, the Amazon Dash Button must have WiFi access point information registered.

Then, for the registration, proceed with the setup of the Amazon Dash Button until just before selecting the products to order, and stop the setup there.

If the setup has progressed that far, the WiFi access point has been registered, and henceforth, the user will be able to connect to the WiFi access point at the push of a button.

WiFi Connection Detection Method

There are two methods of WiFi connection detection.

one-eyed

Amazon Dash Button broadcasts its MAC address when connecting to WiFi. Using this mechanism, the device that detects the button can watch the MAC address broadcasted to WiFi and detect the button.

Second.

The device that detects the button itself becomes a WiFi access point and registers itself as a registered access point to the Amazon Dash Button. The Amazon Dash Button then detects the connection to its own access point.

Many hacks use the first method because the procedure is simple.

In the first case, if the WiFi access point is working, the Amazon Dash Button will actually connect to Amazon, and since the Amazon Dash Button setup is not complete, it will not cause any orders to be placed. Each time you connect, you will receive a notification from Amazon that the Amazon Dash Button setup is not complete.

We used the second method here because we wanted the communication to take place only in a closed environment between the ESP8266 and the Amazon Dash Button, without any connection to the Internet.

procedure

Register ESP8266 as an access point

Since the ESP8266 can receive WiFi and transmit WiFi, it can be a WiFi-to-WiFi relay point, or NAT, in the specification.

However, as described in Relaying Wi-Fi with NAT (NAPT) on the ESP8266, the ESP8266 library has that functionality turned off, so you need to modify the library once, turn that functionality on, recompile the library, and use that library to implement and move NAT.

The ESP8266 must then be activated as a NAT, the Amazon Dash Button must be set up via that NAT, and the ESP8266 must be registered with the Amazon Dash Button.

Fortunately, A full functional WiFi Repeater has the source and binary to make the ESP8266 NAT, so I installed and used that binary this time.

NAT Installation

More details can be found in "Building and Flashing" in A full functional WiFi Repeater."

  1. Download
    A full functional WiFi Repeater で[Clone or download]-[Download ZIP]して解凍
  2. software installation
    Download and install the binary burning tool "Flash Download Tools" from https://www.espressif.com/en/support/download/other-tools
  3. binary load
    There are binaries "0x00000.bin" and "0x10000.bin" in the "firmware" of the extracted file, so use the "Flash Download Tools" to write them.
    (The ESP8266 FLASH SIZE differs depending on the product, so match it to the ESP8266 you are using.)

NAT setup

  1. ESP8266 setup
    When it starts up, it is set to NAT. For details, follow the "Basic Web Config Interface" in A full functional WiFi Repeater to access and configure it with a browser.
    (Here we used the default settings.)
  2. Amazon Dash Button Setup
    Set up the Amazon Dash Button to access the ESP8266 access point configured above, similar to the one in the Amazon Dash Button hack, right up to the point just before product selection.

Button detection program

Now that the ESP8266 access point (SSID and password) has been registered with Amazon Dash Button, all that remains is to write, transfer and execute the ESP8266 program for button detection.

A sample program is shown below.

  • When the Amazon Dash Button connects to the ESP8266 access point, the onStationConnected() callback function is called.
  • The DNS server is not required, but is included because it seems that Amazon Dash Button gives up retries more quickly (shorter interval before the next button can be detected).
#include <ESP8266WiFi.h>
#include <DNSServer.h>

const char *ssid = "xxxx";
const char *password = "xxxxxxxxxx";
const byte dns_port = 53;
IPAddress apIP(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
DNSServer dnsServer;
WiFiEventHandler stationConnectedHandler;

byte bConnection = 0;
int push_count= 0;
int send_frame = 0;
const int max_send_frame = 200000;
byte pin_a = 16;
byte pin_b = 14;

void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  WiFi.softAPConfig(apIP, apIP, subnet);
  WiFi.softAP(ssid, password);
  stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  dnsServer.start(dns_port, "*", apIP);

  pinMode(pin_a, OUTPUT);
  pinMode(pin_b, OUTPUT);

  digitalWrite(pin_a, LOW);
  digitalWrite(pin_b, LOW);

}

void onStationConnected(const WiFiEventSoftAPModeStationConnected& evt) {
  bConnection = 1;
  send_frame = 0;
  ++push_count;

  Serial.print("Station connected: ");
  Serial.print(macToString(evt.mac) + " -> ");
  Serial.println(push_count);
  
  digitalWrite(pin_a, HIGH); 
  digitalWrite(pin_b, HIGH); 
  Serial.println("pin HIGH");
}

void pinOut(){
  if(bConnection){
    if(send_frame<max_send_frame){
      ++send_frame;
    } else {
      bConnection = 0;
      digitalWrite(pin_a, LOW);
      digitalWrite(pin_b, LOW);
      Serial.println("pin LOW");
    }
  }
}

void loop() {
  dnsServer.processNextRequest();
  pinOut();
}

String macToString(const unsigned char* mac) {
  char buf[20];
  snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  return String(buf);
}

Feelings, etc.

Initially, I thought that the MAC address of the Amazon Dash Button could not be obtained from the ESP8266 unless both the ESP8266 and the Amazon Dash Button were connected to a WiFi access point, but it can be obtained without a WiFi access point.

Without knowing it, we sought a way to make the ESP8266 and Amazon Dash Button work directly, which led us to this approach.

The ESP8266 sample program has an HTTP client and access point, so I thought direct communication would be quick and easy, and tried to implement it myself, but it didn't work, and I finally solved the problem by using NAT software. I took quite a detour, but I learned a lot in the process, so I'm happy about that....

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com