//Libararies
#include "DHTesp.h" // temperature/humidity sensor
#include <WiFi.h> //wifi
#include <PubSubClient.h> // mqtt
#include <AccelStepper.h> // motor driver
//MQTT
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
char *mqttServer = "broker.hivemq.com";
int mqttPort = 1883;
//Sensor
DHTesp dhtSensor;
const int DHT_PIN = 15;
String temp = "", hum = "", payload="";
//Stepper Motor
AccelStepper stepper(AccelStepper::DRIVER, 0, 2); // Initialize the stepper motor (Assuming STEP is connected to GPIO 26, DIR to GPIO 27)
//Timing
unsigned long previousMillis = 0;
const long interval = 2000; // Read temperature every 2 seconds
//Setting up the Program
void setup() {
//Setup Serial
Serial.begin(115200);
//Setup Sensor
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
//Setup Stepper Motor
stepper.setMaxSpeed(1000); //number of steps that can be run per second
stepper.setSpeed(500); // initial speed of the motor
stepper.enableOutputs(); // enable the use of the motor
//Setup WiFi
setupWiFi();
//Setup MQTT
setupMQTT();
}
//Main Program runs all the time
void loop() {
//Make sure we connected to MQTT server
if (!mqttClient.connected())
reconnect();
mqttClient.loop();
//Stepper Motor
stepper.runSpeed(); //asking our mottor to run based on the values given before
// Check if it's time to read the temperature
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
//Update the initial time for comparison
previousMillis = currentMillis;
// Read Sensor Data
TempAndHumidity data = dhtSensor.getTempAndHumidity();
temp = String(data.temperature, 2);
hum = String(data.humidity, 1);
// Get motor status and speed
int motorStatus = stepper.distanceToGo() == 0 ? 0 : 1; // 0 for stopped, 1 for moving
float motorSpeed = stepper.speed();
// Preparing payload for sensor data, motor status, and speed
payload =
"{\"Temp\":" + temp +
",\"Humidity\":" + hum +
",\"MotorStatus\":" + motorStatus +
",\"MotorSpeed\":" + motorSpeed +
"} ";
// Send sensor data to the server
mqttClient.publish("IIoT_SxU/GGtelemetryMuqMine", payload.c_str());
}
}
void setupWiFi(){
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void setupMQTT() {
mqttClient.setServer(mqttServer, mqttPort);
mqttClient.setCallback(callback);
}
void reconnect() {
Serial.println("Connecting to MQTT Broker...");
while (!mqttClient.connected()) {
Serial.println("Reconnecting to MQTT Broker..");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str())) {
Serial.println("Connected.");
// subscribe to topic
mqttClient.subscribe("IIoT_SxU/PJ");
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
message[length] = '\0'; // Null terminator used to terminate the char array
String result = (char*)message;
if(result == "y"){
Serial.println("Received YES");
}
if(result == "n"){
Serial.println("Received NO");
}
}