I buyed my cats this toy that drives around with a laserpointer on top. It was a banger on first sight so I decided to add a remote controlling ESP-32 that can be reached over HTTP to control it. I built a simpe web page with a button to start and stop it.

smart cat toy html page

Wiring diagram:

smart cat toy wiring

smart cat toy transistor

Arduino source code for this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <WiFi.h>
#include <ESPAsyncWebSrv.h>

constexpr int GPIO_TOY_TOGGLE = 32;

static const char* ssid = "...";
static const char* password = "...";
static const char* mainPageContent = R"__(<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  <title>Cat Toy Control</title>
  <style>
    html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; }
    body { margin-top: 50px; }
    h1 { color: #444444; margin: 50px auto 30px; }
    .button {
      display: block; width: 160px; background-color: #34495e; border: none; color: white;
      padding: 13px 30px; text-decoration: none; font-size: 25px; margin: 0px auto 35px;
      cursor: pointer; border-radius: 4px;
    }
    .button:active { background-color: #2c3e50; }
  </style>
</head>
<body>
  <h1>Cat Toy Controller</h1>
  <a class="button" href="/toggle">Start/Stop</a>
</body>
</html>)__";

static AsyncWebServer server(80);

void setup() {
  pinMode(GPIO_TOY_TOGGLE, OUTPUT);
  digitalWrite(GPIO_TOY_TOGGLE, LOW);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  // power saving
  WiFi.setSleep(true);
  setCpuFrequencyMhz(10);

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(200, "text/html", mainPageContent);
  });

  server.on("/toggle", HTTP_GET, [](AsyncWebServerRequest *request) {
    digitalWrite(GPIO_TOY_TOGGLE, HIGH);
    delay(100);
    digitalWrite(GPIO_TOY_TOGGLE, LOW);
    request->redirect("/");
  });

  server.begin();
}

void loop() {
  delay(1);
}