Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
suk_
Published © GPL3+

Phone Controlled Robot Car using Wi-Fi Module

Remotely controlled car - IoT based project using Wi-Fi Module and MIT App Inventor with intuitive control. A step-by-step guide!

BeginnerFull instructions provided10,230
Phone Controlled Robot Car using Wi-Fi Module

Things used in this project

Story

Read more

Schematics

Interfacing L298n motor driver with Arduino and Motors

We direct the power to the Arduino via the L298n motor driver.

Speed and Direction Control Circuit

Interfacing NodeMCU with Arduino

To power the NodeMCU, just connect the 3V3 pin on the Wi-Fi module to 3.3V port on Arduino.

Code

NodeMCU Code

Arduino
Code for taking input data from your Joystick application on the phone and sending the data to Arduino via I2C communication protocol.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Wire.h>

const char* ssid = "Wi-Fi name";
const char* password = "password";

WiFiServer server(80);

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

  Wire.begin(D1, D2); 
  
  Serial.println();
  Serial.print("Connecting to ");
  Serial.print(ssid);

  WiFi.begin(ssid, password);

  while(WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  server.begin(); 
  Serial.println("Server started");

  Serial.println(WiFi.localIP());
}

void loop() {
  WiFiClient client = server.available();
  if(!client){
    return;
  }

  Serial.println("New Client");
  while(!client.available()){
    delay(1);
  }

  String req = client.readStringUntil('\r');
  req.replace("+", " ");          // Spaces without +
  req.replace(" HTTP/1.1", "");   // this delete HTTP/1.1
  req.replace("GET /", "");   
  int val = req.toInt();

  byte buffer[10];
  buffer[0] = lowByte(val);
  buffer[1] = highByte(val);

  Serial.println(val);
  Wire.beginTransmission(8);
  Wire.write(buffer, 2);
  Wire.endTransmission();
  
  
  client.flush();

}

Arduino Uno Code

Arduino
Code for receiving incoming data from NodeMCU and giving input to the motor drivers to vary the speed and direction of spin of motors.
#include <Wire.h>

//motors
int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 4;
int motor2pin2 = 5;

int speed9 = 40;
int speed10 = 40;

int slider;
int valx;
int valy;

void setup() {
 Wire.begin(8);                /* join i2c bus with address 8 */
 Wire.onReceive(receiveEvent); /* register receive event */
 Wire.onRequest(requestEvent); /* register request event */
 Serial.begin(9600);           /* start serial for debug */

  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);

  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

  analogWrite(9,speed9); //left
  analogWrite(10, speed10);//Right

  //anticlockwise
  digitalWrite(motor1pin1, HIGH); //right motor going front
  digitalWrite(motor1pin2, LOW); //right motor going front
  digitalWrite(motor2pin1, LOW); //left motor going front
  digitalWrite(motor2pin2, HIGH); //left motor going front

  /*temporary setup code*/
  analogWrite(9, 60);
  analogWrite(10, 60);
}

void loop() {

}

// function that executes whenever data is received from master
void receiveEvent(int howMany) {
 while (0 <Wire.available()) {
    byte low = Wire.read();
    byte high = Wire.read();
    int val = word(high, low);
    
    
    if(val>200 && val<=400){
      //Serial.print("Y: ");
      valy = 400 - val;
      //Serial.println(valy);
      if(valy<=100){
        digitalWrite(motor1pin1, HIGH); //right motor going front
        digitalWrite(motor1pin2, LOW); //right motor going front
        digitalWrite(motor2pin1, LOW); //left motor going front
        digitalWrite(motor2pin2, HIGH); //left motor going front
        Serial.print("Moving Forward");
       
      }
      else if(valy>100){
        digitalWrite(motor1pin1, LOW); //right motor going front
        digitalWrite(motor1pin2, HIGH); //right motor going front
        digitalWrite(motor2pin1, HIGH); //left motor going front
        digitalWrite(motor2pin2, LOW); //left motor going front
        Serial.print("Moving Backward");
      }
    }
    if(val>1000){
      //Serial.print("Speed: ");
      slider = val - 1000;
      speed10 = slider/5;
      speed9 = slider/5;
      //Serial.println(slider);
      analogWrite(10, speed10);//right
      analogWrite(9, speed9);//left
      Serial.print("Left speed: ");
      Serial.println(speed9);
      Serial.print("Right speed: ");
      Serial.println(speed10);
    }
    if(val<=200){
      //Serial.print("X: ");
      valx = 200-val;
      //Serial.println(valx);
      if(valx<100){
        speed9 = speed9 - ((100-valx)/10);
        speed10 = speed10 + ((100-valx)/10);
        analogWrite(9, speed9);
        analogWrite(10, speed10);
        if(speed9<0){
          speed9 = 0;
          speed10 = 100;
          analogWrite(9, speed9);
          analogWrite(10, speed10);
        }
        Serial.print("Left speed: ");
        Serial.println(speed9);
        Serial.print("Right speed: ");
        Serial.println(speed10);
      }
      else if(valx>100){
        speed9 = speed9 + ((valx-100)/10);
        speed10 = speed10 - ((valx-100)/10);
        analogWrite(9, speed9);
        analogWrite(10, speed10);
        if(speed10<0){
          speed9 = 100;
          speed10 = 0;
          analogWrite(9, speed9);
          analogWrite(10, speed10);
        }
        Serial.print("Left speed: ");
        Serial.println(speed9);
        Serial.print("Right speed: ");
        Serial.println(speed10);
      }
      else if(valx==100){
        speed9 = 40;
        speed10 = 40;
        analogWrite(9, speed9);
        analogWrite(10, speed10);
        Serial.print("Left speed: ");
        Serial.println(speed9);
        Serial.print("Right speed: ");
        Serial.println(speed10);
      }
    }
 Serial.println();             /* to newline */
}
}

Remote_Controll_Joystick.aia

Java
You can open this file by going to https://appinventor.mit.edu/. Download the .aia file and then import project on the App Inventor.
No preview (download only).

Credits

suk_
0 projects • 4 followers

Comments