0% found this document useful (0 votes)
75 views

Control DC Motor Using MQTT Protoco

This Arduino code controls a DC motor using the MQTT protocol. It connects an ESP32 to WiFi, sets up an MQTT client connection, and defines callbacks to publish and subscribe to messages. When it receives "FF", "RR", or "STOP" messages, it controls the motor pins to spin forward, backward, or stop the motor respectively and publishes acknowledgments.

Uploaded by

yuvaraj C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Control DC Motor Using MQTT Protoco

This Arduino code controls a DC motor using the MQTT protocol. It connects an ESP32 to WiFi, sets up an MQTT client connection, and defines callbacks to publish and subscribe to messages. When it receives "FF", "RR", or "STOP" messages, it controls the motor pins to spin forward, backward, or stop the motor respectively and publishes acknowledgments.

Uploaded by

yuvaraj C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Control DC motor using MQTT protocol

#include <WiFi.h>
#include <PubSubClient.h>

WiFiClient espClient;
PubSubClient client(espClient);
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* mqtt_server = "broker.mqtt-dashboard.com";
const int motorpin1 = 2;
const int motorpin2 = 4;

long lastMsg = 0;
char msg[50];
int value = 0;
String message = "Hi";
char val[50];
String top;

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(".");
}

randomSeed(micros());

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {


top = topic;
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
message += '\0';
Serial.println(message);
Serial.println();
}

void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// client.publish("outTopic", "hello world");
client.subscribe("ack");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}

void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(motorpin1,OUTPUT);
pinMode(motorpin2,OUTPUT);
}

void loop() {
client.setCallback(callback);
if (!client.connected()) {
reconnect();
}
client.loop();
if (message == "FF") {
Serial.println(top);
// message.toCharArray(c,20);
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
digitalWrite(LED_BUILTIN, HIGH);
client.publish("ack", "Forward Direction");
Serial.println("Forward Direction");
}
if (message == "RR") {
// message.toCharArray(c,20);
Serial.println(top);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
client.publish("ack", "Backward Direction");
Serial.println("Backward Direction");
}
if (message == "STOP") {
// message.toCharArray(c,20);
Serial.println(top);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
client.publish("ack", "Motor Stopped");
Serial.println("Motor Stopped");
}
message = "";
}

You might also like