QuantumPixie & Grimjoy
Hey QuantumPixie, fancy turning your next gadget into a chaotic prank? I can make a smart bulb play hide‑and‑seek with your living room.
Sounds wicked fun! Let’s make that bulb pop up behind the couch cushions, flash when the cat pounces, and disappear into the closet. I’ll need an ESP‑32, some relays, and a touch of chaos – you got any spare code to share?
Sure thing, here’s a quick sketch to get you started. Just wire the ESP‑32’s GPIO to a relay that’s connected to your bulb, and stick a photoresistor or a simple motion sensor where the cat can trigger it.
```cpp
#include <WiFi.h>
#include <PubSubClient.h>
// Wi‑Fi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASS";
// MQTT broker
const char* mqtt_server = "YOUR_BROKER_IP";
WiFiClient espClient;
PubSubClient client(espClient);
const int bulbRelay = 5; // GPIO5 controls the bulb
const int catSensor = 34; // ADC pin for a simple photoresistor
void setup() {
Serial.begin(115200);
pinMode(bulbRelay, OUTPUT);
pinMode(catSensor, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
int sensorVal = analogRead(catSensor);
// Cat pounce detected if sensor value goes above threshold
if (sensorVal > 800) {
toggleBulb();
delay(500); // debounce
}
}
void toggleBulb() {
digitalWrite(bulbRelay, !digitalRead(bulbRelay));
// Publish state for tracking
client.publish("bulb/state", digitalRead(bulbRelay) ? "ON" : "OFF");
}
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP32Client")) {
client.subscribe("bulb/control");
} else {
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String msg;
for (unsigned int i = 0; i < length; i++) msg += (char)payload[i];
if (msg == "ON") digitalWrite(bulbRelay, HIGH);
if (msg == "OFF") digitalWrite(bulbRelay, LOW);
}
```
Just tweak the sensor threshold for your cat’s pounce speed, flip the GPIOs if you wired differently, and you’ve got a little light‑show that hates order. Happy chaos!
Cool code, thanks! I’ll hit the relay pin with a tiny buzzer so the bulb buzzes when it flips. Maybe throw in a random timer so the cat never knows when it’s hiding. Let’s crank up the fun!
Here’s a quick tweak – wire a small piezo or buzzer to a free GPIO (say GPIO 18) and add a little randomness so the cat never knows when the light will blink.
```cpp
const int buzzer = 18; // GPIO18 controls the buzzer
...
pinMode(buzzer, OUTPUT);
pinMode(bulbRelay, OUTPUT);
pinMode(catSensor, INPUT);
void toggleBulb() {
digitalWrite(bulbRelay, !digitalRead(bulbRelay));
// buzz a quick beep each toggle
tone(buzzer, 1000, 100); // 1kHz for 100ms
client.publish("bulb/state", digitalRead(bulbRelay) ? "ON" : "OFF");
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
// random timer between 5 and 20 seconds
static unsigned long nextFlip = millis() + random(5000, 20000);
if (millis() > nextFlip) {
toggleBulb();
nextFlip = millis() + random(5000, 20000);
}
int sensorVal = analogRead(catSensor);
if (sensorVal > 800) {
toggleBulb();
delay(500);
nextFlip = millis() + random(5000, 20000);
}
}
```