Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Amaan Javed
Published

M5Stack ROBOT with NodeMCU and MPU6050

M5Stack robot car which we can control with mpu6050 module through WiFi with NodeMCU

IntermediateFull instructions provided1 hour2,252
M5Stack ROBOT with NodeMCU and MPU6050

Things used in this project

Hardware components

ESP32 Basic Core IoT Development Kit
M5Stack ESP32 Basic Core IoT Development Kit
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
SparkFun Full-Bridge Motor Driver Breakout - L298N
SparkFun Full-Bridge Motor Driver Breakout - L298N
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Breadboard (generic)
Breadboard (generic)
×1
6 DOF Sensor - MPU6050
DFRobot 6 DOF Sensor - MPU6050
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
9V battery (generic)
9V battery (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering Station, 110 V
Soldering Station, 110 V
Multitool, Screwdriver
Multitool, Screwdriver
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Code

M5STACK ROBOT

Arduino
#define M5STACK_MPU6050 
#include <M5Stack.h>
#include <WiFi.h>
#include <HTTPClient.h>
float pitch = 0.0F;
float roll  = 0.0F;
float yaw   = 0.0F;
const char* ssid ="ESPcar";
const char* password ="12345678";
void setup(){
  M5.begin();  
  M5.Power.begin();   
  M5.IMU.Init();
  for(uint8_t t = 4; t > 0; t--) {
        Serial.printf("[SETUP] WAIT %d...\n", t);
        Serial.flush();
        delay(1000);
    }
      WiFi.mode(WIFI_STA);
      WiFi.disconnect(true);
    WiFi.begin(ssid,password);
    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    M5.Lcd.print(".");
 
    delay(15000);
    M5.Lcd.println("Connected");
  }
}
void loop() {
   
  M5.IMU.getAhrsData(&pitch,&roll,&yaw);
  if((WiFi.status() == WL_CONNECTED)) {

        HTTPClient http;      
  
  if(roll >= 40){
  M5.Lcd.print("[HTTP] begin...\n");
        
        http.begin("http://192.168.4.1/right"); 
        M5.Lcd.fillScreen(RED);        
        int httpCode = http.GET();

        http.end();
    
    }
    else if(roll <= -40){
      M5.Lcd.print("[HTTP] begin...\n");
        
        http.begin("http://192.168.4.1/left"); //HTTP
        M5.Lcd.fillScreen(BLUE);        
        int httpCode = http.GET();

        http.end();
    }
       else if(pitch >= 40){
   M5.Lcd.print("[HTTP] begin...\n");
       
        http.begin("http://192.168.4.1/forward"); //HTTP
        M5.Lcd.fillScreen(YELLOW);        
        int httpCode = http.GET();

        http.end();
    }
    else if(pitch <= -40){
       M5.Lcd.print("[HTTP] begin...\n");
         
        http.begin("http://192.168.4.1/back"); //HTTP
        M5.Lcd.fillScreen(WHITE);        
        int httpCode = http.GET();

        http.end();
    }
    else{
       M5.Lcd.fillScreen(GREEN);
      }
  }
    
  delay(1);
}//////AMAAN JAVED/////////////

NodeMCU (on board com)

Arduino
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char *ssid = "ESPcar";
const char *password = "12345678";

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/html", "<h1>You are connected</h1>");
}

void forward(){
  digitalWrite(5, 1);
  digitalWrite(0, 1);
  delay(2000);
  digitalWrite(5,0);
  digitalWrite(0,0);
  delay(1000);  
}
void back()
{
   digitalWrite(4, 1);
   digitalWrite(2,1);
  delay(2000);
  digitalWrite(2,0);
  digitalWrite(4,0);
  delay(1000);   
}

void right()
{
    digitalWrite(2, 1);
    digitalWrite(5,1);
  delay(2000);
  digitalWrite(2,0);
  digitalWrite(5,0);
  delay(1000);   
}

void left()
{
    digitalWrite(0,1);
    digitalWrite(4,1);
  delay(2000);
  digitalWrite(0,0);
  digitalWrite(4,0);
  delay(1000);   
}
void setup() {
  delay(1000);
  Serial.begin(115200);
  Serial.println();
  Serial.print("Configuring access point...");
  WiFi.softAP(ssid, password);
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(4, OUTPUT);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.on("/", handleRoot);
  server.on("/forward", forward);
  server.on("/", handleRoot);
  server.on("/back", back);
  server.on("/", handleRoot);
  server.on("/right", right);
  server.on("/", handleRoot);
  server.on("/left", left);
  server.begin();
  Serial.println("HTTP server started");
}
void loop() {
  Serial.println("Waiting for the link");
  server.handleClient();
}
//////////////////AMAAN JAVED////////////////////

Credits

Amaan Javed

Amaan Javed

19 projects • 4 followers
Student

Comments