Car WiFi UDP NodeMCU Accelerometer Android & Speaker

Car WiFi UDP NodeMCU Accelerometer Android & Speaker © GPL3+

The control of a robot car using the accelerometer of an Android device via WiFi using the UDP protocol.

  • 41 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project



Code

Car_Accel_nodeMCU_UDP.inoArduino
#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>

const char *ssid = "ESP-Car1";      //change ssid
const char *password = "12345678"; //change Password
const byte pinBuiltinLed PROGMEM = D4;

unsigned int localPort = 8888;  //Port from which we receive UDP packets
const int NTP_PACKET_SIZE = 48; // Buffer size for received packets
char packetBuffer[ NTP_PACKET_SIZE]; //Packet storage buffer

//connecting the engine driver L298N
#define L9110_A_IA 14 // Pin D12 --> output int1 to motor "A"
#define L9110_A_IB 12 // Pin D11 --> output int2 to motor "A"
#define L9110_B_IA 13 // Pin D10 --> output  int3 to motor "B"
#define L9110_B_IB 15 // Pin D9 --> output  int4 to motor "B"
 
// which contacts are responsible for what
#define MOTOR_A_PWM L9110_A_IA // control the speed of motors "A"
#define MOTOR_A_DIR L9110_A_IB // motor power
#define MOTOR_B_PWM L9110_B_IA // control the speed of motors ""
#define MOTOR_B_DIR L9110_B_IB // motor power


WiFiUDP udp;

int t = 0;
int rnd;
int sc = 1; //operating mode 1-joystick 0-accelerometer
int xx = 0; //speed coefficient

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  pinMode(pinBuiltinLed, OUTPUT);
  // We start operations with WIFI
  Serial.print("Configuring access point...");
  Serial.println(ssid);
WiFi.mode(WIFI_OFF); //turn off WIFI
WiFi.mode(WIFI_AP);  //Put WIFI In Access Point Mode
WiFi.softAP(ssid, password,11,0); //Run the access point with the given ssid and password on channel 11 (do not interfere with others)

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  Serial.println("HTTP server started");
  Serial.println("Starting UDP");
  udp.begin(localPort); // initialize UDP
  Serial.print("Local port: ");
  Serial.println(udp.localPort());

 pinMode( MOTOR_A_DIR, OUTPUT ); //-------------------------------------//
 pinMode( MOTOR_A_PWM, OUTPUT ); //                                     //
 pinMode( MOTOR_B_DIR, OUTPUT ); //    pins to EXIT mode and stop motors//
 pinMode( MOTOR_B_PWM, OUTPUT ); //                                     //
 fstop(10);                      //------------------------------------//
}

void loop()
{
    int cb = udp.parsePacket(); 
  if (!cb) {}
  else {
    udp.read(packetBuffer, NTP_PACKET_SIZE);
    Serial.println(packetBuffer);
    String  req=(packetBuffer);

//---------------------------------------------------------------------//
//                                                                     //
//                   This is the main cycle of our program !!!         //
//                                                                    //
//---------------------------------------------------------------------//    

// If the robot is in joystick control then ...
if (req.indexOf("fforward") != -1){fforward(1023); sc=1;}
if (req.indexOf("fback") != -1){fback(1023);  sc=1;}
if (req.indexOf("fleft") != -1){fleft(1023);  sc=1;}
if (req.indexOf("fright") != -1){fright(1023);  sc=1;}
if (req.indexOf("stop") != -1){fstop(10);  sc=1;}
if (req.indexOf("gy") != -1){sc=0;}

if (sc==0) {  //If the robot is in accelerometer control mode, then ..
  if (req.indexOf("bm") != -1) xx = 3; // high speed
  if (req.indexOf("sm") != -1) xx = 2; //average speed
  if (req.indexOf("tm") != -1) xx = 1; //slow speed
  if (req.indexOf("m") == -1)  {fstop(10); xx = 0;} //Stop



  if (req.indexOf("zm") != -1) { fback(400); } // reverse

                                        
  if (req.indexOf("iforward-90") != -1)fleft(341*xx);
  if (req.indexOf("iforward90") != -1)fright(341*xx);
  if (req.indexOf("iforward-80") != -1)fleft(200*xx);
  if (req.indexOf("iforward80") != -1)fright(200*xx);

  if (req.indexOf("iforward0") != -1)iforward(341*xx,341*xx);
  if (req.indexOf("iforward-10") != -1)iforward(341*xx,341*xx);
  if (req.indexOf("iforward-20") != -1)iforward(300*xx,341*xx);
  if (req.indexOf("iforward-30") != -1)iforward(266*xx,341*xx);
  if (req.indexOf("iforward-40") != -1)iforward(200*xx,341*xx);
  if (req.indexOf("iforward-50") != -1)iforward(133*xx,341*xx);
  if (req.indexOf("iforward-60") != -1)iforward(66*xx,341*xx);
  if (req.indexOf("iforward-70") != -1)iforward(0,341*xx);

  if (req.indexOf("iforward10") != -1)iforward(341*xx,341*xx);
  if (req.indexOf("iforward20") != -1)iforward(341*xx,300*xx);
  if (req.indexOf("iforward30") != -1)iforward(341*xx,266*xx);
  if (req.indexOf("iforward40") != -1)iforward(341*xx,200*xx);
  if (req.indexOf("iforward50") != -1)iforward(341*xx,133*xx);
  if (req.indexOf("iforward60") != -1)iforward(341*xx,66*xx);
  if (req.indexOf("iforward70") != -1)iforward(341*xx,0);


  xx = 0; 
}

memset(packetBuffer, 0, NTP_PACKET_SIZE);  //clear the buffer to receive the next command
  xx = 0; 
}
//---------------------------------------------------------------------//
//                                                                    //
//                        End our main loop !!!                       //
//                                                                    //
//--------------------------------------------------------------------//
}


//-----Below we create simplified commands for controlling the robot----//


//***************************************************
void move(int motor, int speed, int rotate){
//create a motion function with
//three parameters (motors, speed, direction of rotation)
// motors - 1 or 0
// speed - from 0 to 255
// direction of rotation - 1 or 0

  boolean inPin1 = LOW;
  
  if(rotate == 1){
    inPin1 = HIGH;
    
  if(motor == 1){
    digitalWrite(MOTOR_A_DIR, inPin1);
    analogWrite(MOTOR_A_PWM, 1023-speed);
          }else{
            digitalWrite(MOTOR_B_DIR, inPin1);
            analogWrite(MOTOR_B_PWM, 1023-speed);
          }    
    }
    
  else{
    inPin1 = LOW;
  
  if(motor == 1){
    digitalWrite(MOTOR_A_DIR, inPin1);
    analogWrite(MOTOR_A_PWM, 1023-(1023-speed));
          }else{
            digitalWrite(MOTOR_B_DIR, inPin1);
            analogWrite(MOTOR_B_PWM, 1023-(1023-speed));
          }  
 }
 
}
//***************************************************
void fstop(int t){  //create the stop function

  move(1,0,0);
  move(2,0,0);
 delay(t);
}

//***************************************************
void forward(int s, int time){//create the "forward" function
  move(1,s,0);
  move(2,s,0);
  delay(time);
}
//***************************************************
void fforward(int s){  //create the function "move forward without a time limit"
  move(1,s,0);
  move(2,s,0);
}
//***************************************************
void back(int s, int time){ //create the "backward" function
  move(1,s,1);
  move(2,s,1);
  delay(time);
}
//***************************************************
void fback(int s){ //create the function "move backward without a time limit"
  move(1,s,1);
  move(2,s,1);
}
//***************************************************
void left(int s, int time){ //create the function "turn right"
  move(1,s,1);
  move(2,s,0);
  delay(time);
}
//***************************************************
void fleft(int s){ //create the function "turn right without time limit"
  move(1,s,1);
  move(2,s,0);
}
//***************************************************
void right(int s, int time){ //create the function "turn left"
  move(1,s,0);
  move(2,s,1);
  delay(time);
}
//***************************************************
void fright(int s){ //create the function "turn left without time limit"
  move(1,s,0);
  move(2,s,1);
}
//***************************************************
void iforward(int sl, int sr){//create the function "move forward without a time limit"
  move(1,sl,0);
  move(2,sr,0);
}
SD_Card_Wav_speaker_nano.inoArduino
#include <SPI.h>
#include <SD.h>
#define SDPIN 10
#include <TMRpcm.h>

File myFile;
TMRpcm tmrpcm;

void setup() {
  tmrpcm.speakerPin = 9; // Speaker is connected to pin 9
  tmrpcm.setVolume(7); // volume setting from 0 to 7
  tmrpcm.quality(1);

  // Initializing SD Memory Card
  if (!SD.begin(SDPIN)) {
    return;
  }
}

void loop() {
  delay(5000); //play time
  tmrpcm.play("sounds/son8bit"); // file playback "son8bit.wav"
}

void playSound(char *file) {
  tmrpcm.play(file);
  return;
}

Custom parts and enclosures

Android Code
Mobile application made in MIT App Inventor 2
car_accel_nodemcu_udp_vuZBi65ZAW.apk
Source Code (MIT App Inventor 2)
The free UDP client extension (UrsAI2UDPv3) has been added to the MIT App Inventor app.
car_accel_nodemcu_udp_XWviVTYWeE.aia

Schematics

car-nodemcu-l298n-arduino-nano_jalJLq3PcK.jpg
Car nodemcu l298n arduino nano jaljlq3pck

Comments

Similar projects you might like

Jrobot - Android and Arduino-Based Global Control Robot Car

Project showcase by joechen

  • 7,791 views
  • 4 comments
  • 27 respects

Play with Fire Over Wi-Fi (ESP8266, NeoPixels & Android App)

Project tutorial by ElectroPeak

  • 7,894 views
  • 5 comments
  • 43 respects

RGB Camera Backlight Control With Android Mobile

Project tutorial by DIY Partners

  • 2,513 views
  • 1 comment
  • 8 respects

Arduino WiFi Robot Control Using Mobile Phone

Project tutorial by Jithin Sanal

  • 5,075 views
  • 0 comments
  • 19 respects

Health Band - A Smart Assistant for the Elderly

Project tutorial by Technovation

  • 35,995 views
  • 19 comments
  • 82 respects
Add projectSign up / Login