#include <WiFi.h>
#include <PubSubClient.h>
#include <TM1637Display.h>
#include <ESP32Servo.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT Server settings
const char* mqtt_server = "192.168.66.53";
const int mqtt_port = 1883;
const char* mqtt_user = "node-red";
const char* mqtt_password = "bacon123";
WiFiClient espClient;
PubSubClient client(espClient);
// GPIO pins for the buttons
const int buttonPins[] = {16, 17, 18};
const int numberOfButtons = 3;
int buttonState[numberOfButtons];
int lastButtonState[numberOfButtons] = {HIGH, HIGH, HIGH}; // assume all buttons start unpressed
// TM1637 Display settings
const int CLK = 22; // Set the CLK pin connection to the display
const int DIO = 23; // Set the DIO pin connection to the display
TM1637Display display(CLK, DIO);
// Servo settings
Servo myServo;
const int servoPin = 25; // Replace with the GPIO pin connected to the servo
// Addressable LED settings
const int ledDataPin = 25; // Data pin for LED strip
const int numLeds = 5; // Number of LEDs in the strip
Adafruit_NeoPixel strip(numLeds, ledDataPin, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
// Initialize buttons
for (int i = 0; i < numberOfButtons; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
lastButtonState[i] = digitalRead(buttonPins[i]);
}
// Initialize the display
display.setBrightness(0x0f);
display.showNumberDec(0);
// Initialize Servo
myServo.attach(servoPin);
// Initialize Addressable LED strip
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Connect to WiFi
setup_wifi();
// Setup MQTT client
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
reconnect();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Check button state
for (int i = 0; i < numberOfButtons; i++) {
int currentState = digitalRead(buttonPins[i]);
if (currentState != lastButtonState[i]) {
if (currentState == LOW) {
// Button is pressed
sendMQTTMessage(buttonPins[i], "-on");
} else {
// Button is released
sendMQTTMessage(buttonPins[i], "-off");
}
lastButtonState[i] = currentState;
delay(50); // Debounce delay
}
}
}
void setup_wifi() {
delay(10);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
if (String(topic) == "esp32display") {
// Display handling
if (messageTemp == "----") {
display.clear();
} else {
display.showNumberDec(messageTemp.toInt(), false);
}
} else if (String(topic) == "esp32servo") {
// Servo handling
int angle = messageTemp.toInt();
angle = constrain(angle, 0, 180);
myServo.write(angle);
} else if (String(topic) == "esp32leds") {
// LED handling
StaticJsonDocument<256> doc;
deserializeJson(doc, message, length);
int ledNumber = doc["led"];
String color = doc["color"];
int brightness = doc["brightness"] | 255; // Default to full brightness if not specified
if (ledNumber < numLeds) { // Check if the LED number is within range
uint32_t rgbColor = 0; // Default to 'off'
if (color == "red") rgbColor = strip.Color(brightness, 0, 0);
else if (color == "green") rgbColor = strip.Color(0, brightness, 0);
else if (color == "blue") rgbColor = strip.Color(0, 0, brightness);
// Add more colors as needed, adjusting for brightness
if (color == "off") {
strip.setPixelColor(ledNumber, strip.Color(0, 0, 0)); // Turn off the LED
} else {
strip.setPixelColor(ledNumber, rgbColor);
}
strip.show(); // Update the strip to apply changes
}
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
// Subscribe to topics
client.subscribe("esp32display");
client.subscribe("esp32servo");
client.subscribe("esp32leds");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void sendMQTTMessage(int pin, const char* messageSuffix) {
String msg = String(pin) + messageSuffix;
client.publish("esp32button", msg.c_str());
}