Moheeb ZaraEMQ Technologies
Published

IoT Robot Car using MQTT

Build an Arduino powered robot or car that can be remotely controlled over the web using the EMQX MQTT broker.

BeginnerFull instructions provided1 hour1,727
IoT Robot Car using MQTT

Things used in this project

Hardware components

UNO R4 WiFi
Arduino UNO R4 WiFi
×1
Arduino MotorShield Rev3
Arduino MotorShield Rev3
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Battery Holder, 18650 x 2
Battery Holder, 18650 x 2
×1
4WD Smart Robot Car Chassis Kit
×1

Software apps and online services

EMQX
Arduino IDE
Arduino IDE

Story

Read more

Code

mqtt-car.ino

Arduino
#include "WiFiS3.h"
#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>

char ssid[] = "";    // your network SSID (name)
char pass[] = "";    // your network password 

char mqtt_user[] = "test";
char mqtt_pass[] = "test";

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "emqxpi.local";
int        port     = 1883;
const char subscribe_topic[]  = "car/action";

int rightDir = 12;
int rightPWM = 3;
int rightBrake = 9;

int leftDir = 13;
int leftPWM = 11;
int leftBrake = 8;

void setup() {

  pinMode(rightDir, OUTPUT);
  pinMode(rightPWM, OUTPUT);
  pinMode(rightBrake, OUTPUT);
  pinMode(leftDir, OUTPUT);
  pinMode(leftPWM, OUTPUT);
  pinMode(leftBrake, OUTPUT);

  Serial.begin(9600);
  while (!Serial) {
    ; 
  }

  // Connect to WiFi
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();

  // You can provide a username and password for authentication
  mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);

  Serial.print("Attempting to connect to the MQTT broker.");

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");

  mqttClient.onMessage(onMqttMessage);

  Serial.print("Subscribing to topic: ");
  Serial.println(subscribe_topic);

  // subscribe to a topic
  mqttClient.subscribe(subscribe_topic);

  Serial.print("Waiting for messages on topic: ");
  Serial.println(subscribe_topic);
}



void onMqttMessage(int messageSize) {
  Serial.print("Received a message with topic ");
  Serial.println(mqttClient.messageTopic());
  StaticJsonDocument<256> doc;
  deserializeJson(doc, mqttClient);
  const char* action = doc["action"];
  Serial.print("action: ");
  Serial.println(action);

  String direction(action);   

  if(direction == "forward"){
    Serial.println("forward drive");
    forward(200);
    delay(500);
    stop();
  }

  if(direction == "backward"){
    backward(200);
    delay(500);
    stop();
  }

  if(direction == "left"){
    left();
    delay(500);
    stop();
  }

  if(direction == "right"){
    right();
    delay(500);
    stop();
  }

  if(direction == "stop"){
    stop();
  }

}

void loop() {

  mqttClient.poll();
}


void forward(int speed) {
  digitalWrite(rightDir, HIGH);
  digitalWrite(leftDir, HIGH);

  digitalWrite(rightBrake, LOW);
  digitalWrite(leftBrake, LOW);

  analogWrite(rightPWM, speed);
  analogWrite(leftPWM, speed);
}

void backward(int speed) {
  digitalWrite(rightDir, LOW);
  digitalWrite(leftDir, LOW);

  digitalWrite(rightBrake, LOW);
  digitalWrite(leftBrake, LOW);

  analogWrite(rightPWM, speed);
  analogWrite(leftPWM, speed);
}

void stop() {
  analogWrite(rightPWM, 0);
  analogWrite(leftPWM, 0);

  digitalWrite(rightBrake, HIGH);
  digitalWrite(leftBrake, HIGH);
}

void left() {
  digitalWrite(rightDir, LOW);
  digitalWrite(leftDir, HIGH);

  digitalWrite(rightBrake, LOW);
  digitalWrite(leftBrake, LOW);

  analogWrite(rightPWM, 255);
  analogWrite(leftPWM, 255);
}

void right() {
  digitalWrite(rightDir, HIGH);
  digitalWrite(leftDir, LOW);

  digitalWrite(rightBrake, LOW);
  digitalWrite(leftBrake, LOW);

  analogWrite(rightPWM, 255);
  analogWrite(leftPWM, 255);
}

Credits

Moheeb Zara

Moheeb Zara

39 projects • 136 followers
Developer Advocate at EMQ Technologies - . Co-founder - South West Maker Festival. Hardware hacker, artist, robots, HeatSync Labs
EMQ Technologies

EMQ Technologies

5 projects • 6 followers
World's leading cloud-native MQTT-based IoT messaging platform and streaming database

Comments