Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
roboattic Lab
Published © GPL3+

How to Make a Google Assistant Control Car Using Node MCU

This a Google Assistant Control Car that works on the voice Commands that are given by you to Google Assistant.

IntermediateFull instructions provided6 hours991
How to Make a Google Assistant Control Car Using Node MCU

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Google Assistance control car

Code

Google Assistance control car 2.0.ino

C/C++
//Google Assistance control Robot
//Created By roboattic Lab
//Contact me here https://www.instagram.com/diy_burner/
//You need to include "ESP8266WiFi.h,Adafruit_MQTT.h,Adafruit_MQTT_Client.h" library before uploading the sketch, otherwise you'll get compilation error message.

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define ENA 14   // Enable/speed motors Right        GPIO14(D5)
#define IN1 15   // L298N in1 motors Right           GPIO15(D8)
#define IN2 13   // L298N in2 motors Right           GPIO13(D7)
#define IN3 2    // L298N in3 motors Left            GPIO2(D4)
#define IN4 0    // L298N in4 motors Left            GPIO0(D3)
#define ENB 12   // Enable/speed motors Left         GPIO12(D6)

//Your WIFI name and password

#define WLAN_SSID       "WIFI NAME"             // Your SSID
#define WLAN_PASS       "PASSWORD"        // Your password

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com" //Adafruit Server
#define AIO_SERVERPORT  1883                   
#define AIO_USERNAME    "USERNAME"            // Username
#define AIO_KEY         "KEY"   // Auth Key

//WIFI CLIENT
WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

Adafruit_MQTT_Subscribe Forward = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME"/feeds/forward"); // Feeds name should be same everywhere
Adafruit_MQTT_Subscribe Left = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/left");
Adafruit_MQTT_Subscribe Right = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/right");
Adafruit_MQTT_Subscribe Backward = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Backward");


void MQTT_connect();

void setup() {
  Serial.begin(115200);

  //Set the motor pins as output pins
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);
  
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP());
 
  mqtt.subscribe(&Forward);
  mqtt.subscribe(&Left);
  mqtt.subscribe(&Right);
  mqtt.subscribe(&Backward);

}

void loop() {
 
  MQTT_connect();
  

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(20000))) {
    if (subscription == &Forward) {
     
      Serial.print(F("Got: "));
      Serial.println((char *)Forward.lastread);
      int Forward_State = atoi((char *)Forward.lastread);
      if(Forward_State ==1)
      {
         goAhead();
         delay(1600);
         carStop();
      }
     }
      
    
      if (subscription == &Left) {
      Serial.print(F("Got: "));
      Serial.println((char *)Left.lastread);
      int Left_State = atoi((char *)Left.lastread);
      if(Left_State == 1)
      {
        
          goLeft();
          delay(1500);
          carStop();
      }
    }


    if (subscription == &Right) {
      Serial.print(F("Got: "));
      Serial.println((char *)Right.lastread);
      int Right_State = atoi((char *)Right.lastread);
      if(Right_State == 1)
      {
        
          goRight();
          delay(1500);
          carStop();
      }
    }
    
    if (subscription == &Backward) {
      Serial.print(F("Got: "));
      Serial.println((char *)Backward.lastread);
      int Backward_State = atoi((char *)Backward.lastread);
      if(Backward_State == 1)
      {
        
          goBack();
          delay(1600);
          carStop();
      }
    }

    
  }

}

void MQTT_connect() {
  int8_t ret;

  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  
  while ((ret = mqtt.connect()) != 0) {
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000); 
    retries--;
    if (retries == 0) {
      while (1);
    }
  }
  Serial.println("MQTT Connected!");
  
}

void goAhead(){ 

      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 200);

      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
      analogWrite(ENB, 200);
      
  }

void goBack(){ 
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      analogWrite(ENA, 200);

      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      analogWrite(ENB, 200);
      
  }

void goRight(){ 

      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
      analogWrite(ENA, 200);

      digitalWrite(IN3, HIGH);
      digitalWrite(IN4, LOW);
      analogWrite(ENB, 200);
      
  }

void goLeft(){

      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 200);

      digitalWrite(IN3, LOW);
      digitalWrite(IN4, HIGH);
      analogWrite(ENB, 200);
     
  }
void carStop() {
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
      analogWrite(ENA, 0);

      digitalWrite(IN3, LOW);
      digitalWrite(IN4, LOW);
      analogWrite(ENB, 0);
}

Credits

roboattic Lab
13 projects • 9 followers
YouTube Content Creator Robotics Enthusiast
Contact

Comments

Please log in or sign up to comment.