#include <DHT.h>
#include <Stepper.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#define NEOPIXEL_PIN 18
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
#define TOTAL_STEPS 200
#define MOTOR_PIN1 2
#define MOTOR_PIN2 4
#define MOTOR_PIN3 5
#define MOTOR_PIN4 15 // Example pin, change it as per your ESP32 setup
#define DHT_PIN 14 // Example pin, change it as per your ESP32 setup
#define TEMP_THRESHOLD_1 23
#define TEMP_THRESHOLD_2 27
#define TEMP_THRESHOLD_3 35
#define MQTT_SERVER "broker.emqx.io"
#define MQTT_PORT 1883
#define MQTT_TOPIC_SPEED "motor_speed"
#define MQTT_TOPIC_LIGHT "neopixel/control"
#define MQTT_TOPIC_TEMPERATURE "temperature"
#define MQTT_TOPIC_HUMIDITY "humidity"
int stepperSpeed = -1;
int lastStepperSpeed = -1;
int lastTemperature = 0;
int lastHumidity = 0;
const char *ssid = "Wokwi-GUEST";
const char *password = "";
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHT_PIN, DHT22);
Stepper stepper(TOTAL_STEPS, MOTOR_PIN1, MOTOR_PIN2, MOTOR_PIN3, MOTOR_PIN4);
int currentSpeed = 500; // Initial speed, you can adjust this as needed
void setup() {
Serial.begin(115200);
// Initialize Neopixel
pixels.begin();
connectToWiFi();
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback);
dht.begin();
stepper.setSpeed(currentSpeed);
}
void loop() {
if (!client.connected()) {
reconnect();
}
float temperature = dht.readTemperature();
float humidity = dht.readHumidity(); // Read temperature from DHT sensor
if((lastTemperature < temperature - 10) || (lastTemperature > (temperature + 10)))
{
lastTemperature = temperature;
client.publish(MQTT_TOPIC_TEMPERATURE, String(temperature).c_str());
client.publish(MQTT_TOPIC_HUMIDITY, String(humidity).c_str());
}
if((humidity<70 && temperature<30) && lastStepperSpeed !=0){
stepperSpeed=0;
lastStepperSpeed=0;
Serial.print("Temp0: ");
Serial.println(temperature);
Serial.println(humidity);
client.publish(MQTT_TOPIC_SPEED, String(0).c_str());
}
else if(((humidity>70 && humidity<=90)||
(temperature >30 && temperature <=35)) && lastStepperSpeed !=-1){
stepperSpeed=-1;
lastStepperSpeed=-1;
Serial.print("Temp1: ");
Serial.println(temperature);
Serial.println(humidity);
client.publish(MQTT_TOPIC_SPEED, String(-1).c_str());
}
else if((humidity>90 || temperature >35) && lastStepperSpeed !=-5){
stepperSpeed=-5;
lastStepperSpeed=-5;
Serial.print("Temp2: ");
Serial.println(temperature);
Serial.println(humidity);
client.publish(MQTT_TOPIC_SPEED, String(-5).c_str());
}
client.loop();
// Move the motor
stepper.step(stepperSpeed);
delay(10); // Adjust the delay as needed for your motor speed
}
void adjustStepperSpeed(int speedCommand) {
// Publish the current speed to MQTT
// client.publish(MQTT_TOPIC_SPEED, String(speedCommand).c_str());
// Set the motor speed based on the received command
stepperSpeed = speedCommand;
}
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void reconnect() {
while (!client.connected()) {
if (client.connect("esp32_neopixel_controller")) {
Serial.println("Connected to MQTT");
subscribeToCommands();
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
}
void subscribeToCommands() {
// Subscribe to the MQTT topic for motor speed control
client.subscribe(MQTT_TOPIC_SPEED);
client.subscribe(MQTT_TOPIC_LIGHT);
}
void callback(char *topic, byte *payload, unsigned int length) {
String message = "";
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.print("Payload: ");
Serial.print("Command Received: ");
Serial.println(message);
// Check the received MQTT topic
if (strcmp(topic, MQTT_TOPIC_SPEED) == 0) {
if (message.equals("0")) {
// Set the motor speed based on the received command
int speedCommand = message.toInt();
adjustStepperSpeed(speedCommand);
}
else if (message.equals("-1")) {
// Set the motor speed based on the received command
int speedCommand = message.toInt();
adjustStepperSpeed(speedCommand);
}
else if (message.equals("-5")) {
// Set the motor speed based on the received command
int speedCommand = message.toInt();
adjustStepperSpeed(speedCommand);
}
}
else if (strcmp(topic, MQTT_TOPIC_LIGHT) == 0) {
if (message.equals("off")) {
updateColor(0, 0, 0);
Serial.println("Neopixel turned OFF");
} else if (message.equals("red")) {
updateColor(255, 0, 0);
Serial.println("Neopixel color set to Red");
} else if (message.equals("green")) {
updateColor(0, 255, 0);
Serial.println("Neopixel color set to Green");
} else if (message.equals("blue")) {
updateColor(0, 0, 255);
Serial.println("Neopixel color set to Blue");
} else if (message.equals("yellow")) {
updateColor(255, 255, 0);
Serial.println("Neopixel color set to Yellow");
} else if (message.equals("on")) {
updateColor(255, 255, 255);
Serial.println("Neopixel color set to on");
}
}
}
void updateColor(int r, int g, int b) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
pixels.show();
}