#include "DHTesp.h"
#include <ESP32Servo.h>
#include <Stepper.h>
#include <WiFi.h>
#include <PubSubClient.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 4, 5, 17, 16);
uint16_t steptarget2 = 0;
uint16_t steptarget2_old = 0;
const int servoPin = 18;
uint8_t duty_cycle = 0;
Servo servo;
const int DHT_PIN = 15;
DHTesp dhtSensor;
//============================================================
//Variables de autenticación y Comunicación
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org"; // MQTT broker
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//============================================================
//Declaración de Funciones Adicionales ===================
//========================================================
//Setup Wifi
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
}
//Callback subscriber
void callback(char* topic, byte* payload, unsigned int length)
{
String data_str;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
data_str+=((char)payload[i]);
}
Serial.print(data_str);
// if (topic = "/Motor/Servo-nodered") /// esp32 subscribe topic
if (strcmp(topic, "/Motor/Servo123456/") == 0)
{
Serial.print(" ");
int status = data_str.toInt();
duty_cycle = map(status, 1, 100, 0, 180);
Serial.println("PWM received: "+String(duty_cycle));
}
if(strcmp(topic,"/Motor/Stepper/") == 0)
{
Serial.print(" ");
int status = data_str.toInt();
steptarget2 = status;
Serial.println("Steps received: "+String(duty_cycle));
}
}
//=================================================================
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESPClient"))
{
Serial.println("connected");
client.subscribe("/Motor/Servo123456/");
client.subscribe("/Motor/Stepper/");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
//================================================================
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
servo.attach(servoPin, 500, 2400);
myStepper.setSpeed(60);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.println("Hello, ESP32!");
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
// put your main code here, to run repeatedly:
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
String temp = String(data.temperature, 2);
client.publish("/Readings/Tmp/", temp.c_str());
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
String hum = String(data.humidity, 1);
client.publish("/Readings/Hm/", hum.c_str());
Serial.println("---");
delay(2000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
Serial.println("Humidity: " + String(duty_cycle) + " Steptarget: " + String(steptarget2) + " Old: " + String(steptarget2_old));
myStepper.step(steptarget2 - steptarget2_old);
steptarget2_old = steptarget2;
servo.write(duty_cycle);
}
//