//****************************************************
// Android/fruitPhone Controlled WiFi Car
//****************************************************
// ESP 8266 NodeMcu V1.0 PWM Servo/ LM298 H-bridge Control Via WiFi UDP
//*************************************************
//*************************************************
// TEST SKETCH
// USING LM298's ONLY ONE CHANNEL
//*************************************************
//*************************************************
//****************************************************
// Using Free Android or iOS App "RoboRemo"
//****************************************************
//***************************************************************
// PLEASE VISIT
//****************************************************************
// My Blog : https://amkDiyProjects.BlogSpot.com
// My youtube Channel: https://youtube.com/CrazyGuyOfficial
//****************************************************************
//************************************************************
// Libraries Required for this to work
//************************************************************
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Servo.h>
//************************************************************
// Settings for the WIFI:
//************************************************************
const char* ssid = "myWifi"; // Set WiFi Name of WiFi Router/AP/HotSpot AND Esp8266 will connect TO it
const char* password = "12345678"; // Set PassWord For WiFi
unsigned int localPort = 9876; // Set port number
//************************************************************
// Settings for Servo using Servo library
//************************************************************
const int chCount = 3;
Servo servoCh[chCount];
int chPin[] = {5, 4, 14};
int chVal[] = {1600, 1600, 1600}; // default value in microSeconds for PWM signals (middle)
int usMin = 700; // min pulse micro seconds
int usMax = 2600; // max pulse micro seconds
//***************************************************************
// LM298 H-bridge Motor Driver Pins and Settings for one channel
//***************************************************************
int enA = 12 ; // Pwm signal Pin used to control Motor Speed (Lm298)
int IN1 = 13 ; // IN1 IN2 pins on lm298 to control Motor's direction of rotation
int IN2 = 15 ;
int mid = 1024 ; // Default PWM value for enA pin
//***************************************************************
char cmd[40];
// Make a character array/C string to store the Contents Read from the received UDP Packet
// Cmd is the name of the UDP packet Buffer in which the command which is received is stored
unsigned long lastCmdTime = 60000;
unsigned long aliveSentTime = 0;
//***************************************************************
WiFiUDP port;
//************************************************************
// SETUP BEGINS
//************************************************************
void setup() {
pinMode(enA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
digitalWrite(enA, LOW);
digitalWrite(IN1, LOW);
digitalWrite (IN2, LOW);
delay(500);
Serial.begin(115200);
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA); //Set Esp8266 in Station mode
//it can work without this line but necessary for preventing Ap mode (happened in my case )
WiFi.begin(ssid, password); // Connect to WiFi network
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("trying to connect");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
port.begin(localPort);
Serial.print("Use this Ip and Port in RoboRemoApp to connect: "); //Print the IP address and port number
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.print(localPort);
}
//*************************************************
// SETUP END
//*************************************************
//************************************************************
// LOOP BEGINS
//************************************************************
void loop() {
//*************************************************
if (millis() - lastCmdTime > 500) {
//*************************************************
analogWrite(enA, 0); // Write 0 or turn off the pwm signal to the lm298's enA pin so motor stops
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
//*************************************************
for (int i = 0; i <= 2; ) {
servoCh[i].detach(); // Turns OFF all the Pins declared in servo library settings above
i++;
}
}
//*************************************************
int packetSize = port.parsePacket(); // Declaring an int for getting Packet Size
if (packetSize > 0) {
port.read(cmd, 40);
//Serial.print("CMD");
//Serial.println(cmd); // Print the recieved cmd
//*************************************************
if (cmd[0] == 'f' && cmd[1] == 'b') { // if the recieved command string contains the channel id "fb" (forward/backwards)
exeFB(); // execute the function "FB" so all the pins required to control lm298 can operate
}
if (cmd[0] == 'c' && cmd[1] == 'h') { // if the recieved command string contains the channel id "ch"
exeServo(); // execute the servo function
//so Pwm signals according to the channel number can be sent to servos
// via the GPIO pins declared above in the servo library settings
}
//*************************************************
if (millis() - aliveSentTime > 1000) {
// an "alive" signal is sent periodically to the phone to know the connection state
// in the RoboRemo app from "edit UI" option add an "led" or a "text log box" set the ID
// to "alive" or can be changed but for the text box if left empty the received text will be shown
exeReply(); // Execute exeReply function At the end of the loop
}
}
}
//*************************************************
// LOOP ENDS
//*************************************************
//************************************************************************************
// FUNCTIONS
//************************************************************************************
void exeFB() {
lastCmdTime = millis();
int fb = cmd[2] - '0' ;
int fbVl = 0;
int i = 4;
while ( isdigit( cmd[i] ) ) { // if the 4th character in the Array is a Number
//get the number and Calculate all the digits in to a single 4 digit number
fbVl = (fbVl * 10) + (cmd[i] - '0');
i++;
}
if (fb == 3 && cmd[3] == ' ' && fbVl > mid) { // forward motor direction
digitalWrite(IN1, HIGH); // set LM298 channel one motor direction
digitalWrite(IN2, LOW);
int FWD = map(fbVl, 1025, 2048, 0, 1024); // map the channel value number to a number between 0-1024
// for writing Pwm to pin enA
Serial.print("FWD :");
Serial.println(FWD);
analogWrite(enA, FWD); // write pwm
}
if (fb == 3 && cmd[3] == ' ' && fbVl < mid) { // backwards motor direction
digitalWrite(IN1, LOW); // set pins for reverse direction
digitalWrite(IN2, HIGH);
int BKW = map(fbVl, 1024, 0, 0, 1024); //map value
Serial.print("BKW :");
Serial.println(BKW);
analogWrite(enA, BKW); //write PWM
}
Serial.print ("fb Number : ");
Serial.println (fb);
Serial.print ("fbVl : ");
Serial.println (fbVl);
}
//******************************************************************************************
void exeServo() { // exeCmd function when called from the loop Reads the channel number and value and sends PWM signals
lastCmdTime = millis();
int ch = cmd[2] - '0' ; // Channel Number
int chVl = 0; // Channel Value temporarily store it in this and then move to Array chVal[] which is already
// declared at the top
int i = 4;
while ( isdigit( cmd[i] ) ) { // if the 4th character in the Array is a Number
//get the number and Calculate all the digits in to a single 4 digit number
chVl = (chVl * 10) + (cmd[i] - '0');
chVal[ch] = chVl;
i++;
}
if (ch >= 0 && ch <= 2 && cmd[3] == ' ') { //if channel value is between 0 and 9 send PWM Signals using Channel Value
if (!servoCh[ch].attached()) {
servoCh[ch].attach(chPin[ch], usMin, usMax);
}
servoCh[ch].writeMicroseconds(chVal[ch]);
}
Serial.print ("channel Number : ");
Serial.println (ch);
Serial.print ("chVl : ");
Serial.println (chVl);
}
//*********************************************************************************************
void exeReply() {
port.beginPacket(port.remoteIP(), localPort); // Send "Alive 1" back to the App to light up the "led" in the App GUI
// Alive is the "id" for "LED" and "1" is the On command /n" will be ignored
port.write("alive 1\n");
port.endPacket();
aliveSentTime = millis();
Serial.println("alive sent");
}
Do-it-yourself,DIY,How-To,Electronics,Arduino,Esp,Raspberry-Pi,Programming,Hacking Stuff,Gardening,Photography,Hobby,Fun
Showing posts with label lm298. Show all posts
Showing posts with label lm298. Show all posts
ESP8266 Node MCU (12E) Internet/WiFi Controlled robot/rover using Lm298 H-bridge Motor driver TEST-01
Labels:
android,
arduino,
code,
controller,
diy,
esp8266internetrobot,
h-bridge,
internet,
ios,
lm298,
micro-controller,
motor-driver,
programming,
relays,
roboremo,
robot,
servo,
sketch,
udp,
wifi
Control motors,Leds,Relays or Motor Driver like lm298 using ESP8266 NodeMcu WiFi UDP with RoboRemo App
//**************************************************** // Android/fruitPhone Controlled WiFi Car //**************************************************** // ESP 8266 NodeMcu V1.0 PWM Servo Control Via WiFi UDP // Using Free Android or iOS App "RoboRemo" //****************************************************
// for Controlling LM298 module with 3 pins (one channel) //I have uploaded another sketch after this one see this link with LM298 pins included
//****************************************************
//*************************************************************** // PLEASE VISIT //**************************************************************** // My Blog : https://amkDiyProjects.BlogSpot.com // My youtube Channel: https://youtube.com/CrazyGuyOfficial //**************************************************************** //************************************************************ // Libraries Required for this to work //************************************************************ #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <Servo.h> //************************************************************ //************************************************************ // Settings for the WIFI: //************************************************************ const char* ssid = "mywifi"; // Set WiFi Name of WiFi Router/AP/HotSpot AND Esp8266 will connect TO it const char* password = "12345678"; // Set PassWord For WiFi unsigned int localPort = 9876; // Set port number // if it's not working first ensure the following things // to use it in Local network i.e. in the same wifi "client Isolation" should be turned off in the Wifi Router's settings // otherwise even if both the phone and the esp8266 are connected to the wifi they will not be able to "talk to each-other" // to use it from the Internet the port should be forwarded in the WiFi router's settings //************************************************************ // Settings for Servo using Servo library //************************************************************ const int chCount = 4; // 4 channels,total number of channels is 4 and 4 GPIOs will be used Servo servoCh[chCount]; int chPin[] = {5, 4, 14, 12}; // ESP8266 12E NODE MCU DevKit V1.0 pins: GPIO 5, 4, 14, 12 // on my NODE MCU board they are marked D1 D2 D5 D6 respectively // IN THE App first channel "ID" is Ch 0 = 5 ch 1 = 4 ch 2 = 14 ch 3 = 12 int chVal[] = {1600, 1600, 1600, 1600}; // default value in micro-seconds for PWM signals (middle) int usMin = 700; // min pulse micro seconds int usMax = 2600; // max pulse micro seconds //************************************************************ char cmd[40]; // Make a character array/C string to store the Contents Read from the received UDP Packet // Cmd is the name of the UDP packet Buffer in which the command which is received is stored unsigned long lastCmdTime = 60000; unsigned long aliveSentTime = 0; //************************************************************ WiFiUDP port; //************************************************************ // SETUP BEGINS //************************************************************ void setup() { delay(1000); Serial.begin(115200); Serial.println("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); //Set Esp8266 in Station mode //it can work without this line but necessary for preventing Ap mode (happened in my case ) WiFi.begin(ssid, password); // Connect to WiFi network while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.println("trying to connect"); delay(500); // IF UNABLE TO CONNECT OR IF IT'S TAKING TOO LONG //first CHECK the SSID,PASSWORD in the SKETCH it should be your WIFI name/ssid and PASSWORD //second every-time after uploading a sketch reset esp8266 once or disconnect from power and connect again //and in the second case to view the serial data again //select the device port in Arduino IDE again and turn off/on Serial monitor } Serial.println(""); Serial.println("WiFi connected"); port.begin(localPort); Serial.print("Use this Ip and Port in RoboRemoApp to connect: "); //Print the IP address and port number Serial.print(WiFi.localIP()); Serial.print(":"); Serial.print(localPort); } void loop() { if (millis() - lastCmdTime > 500) { // All PWM values will Return to Default Values 500 milli seconds after the time of Last Received Command // So all PWM signals will return to default values if there is No New Command for (int i = 1; i <= 3; ) { // set i to 0,1,2 or 3 (if connected to servo and want it to stay at the last commanded position) // and rest of the pins PWM signals will return to default values if there is No New Command // Comment out these two lines if you just want to turn the Pwm signals off immediately // and Uncomment if you also want the servo to return to mid but then servo.detach below should be commented // because servo.detach turns the pins off immediately and does'nt give the servo enough time to return to mid // servoCh[i].attach(chPin[i]); // servoCh[i].writeMicroseconds(1600 ); servoCh[i].detach(); // Turns OFF the Pins // comment it out if want the servo to Return to it's mid Position Quickly // because this loop runs over and over again quickly and the servos need some time to //physically move and PWM should be ON during that period // because this line turns off the pin before moving to i++ and changing the pin number i++; } } int packetSize = port.parsePacket(); // Declaring an int for getting Packet Size if (packetSize > 0) { // if Packet Size is more than zero or if there is some command received then port.read(cmd, 40); // Read the Received UDP Packet //and Put the Contents,the channel number and value in the Cmd (array/C string) // Serial.println(cmd); // Print the recieved cmd if (cmd[0] == 'c' && cmd[1] == 'h') { // if the command starts with the caracter c and the second character is h exeCmd(); // Execute the execmd function which is at the end of the loop } if (millis() - aliveSentTime > 1000) { // an "alive" signal is sent periodically to the phone to know the connection state // in the RoboRemo app from "edit UI" option add an "led" or a "text log box" set the ID // to "alive" or can be changed but for the text box if left empty the received text will be shown exeReply(); // Execute exeReply function At the end of the loop } } } void exeCmd() { // exeCmd function when called from the loop Reads the channel number and value and sends PWM signals lastCmdTime = millis(); int ch = cmd[2] - '0' ; // Channel Number int chVl = 0; // Channel Value temporarily store it in this and then move to Array chVal[] which is already declared at the top int i = 4; while ( isdigit( cmd[i] ) ) { // if the 4th character in the Array is a Number //get the number and Calculate all the digits in to a single 4 digit number chVl = (chVl * 10) + (cmd[i] - '0'); chVal[ch] = chVl; i++; } if (ch >= 0 && ch <= 9 && cmd[3] == ' ') { //if channel value is between 0 and 9 send PWM Signals using Channel Value if (!servoCh[ch].attached()) { servoCh[ch].attach(chPin[ch], usMin, usMax); } servoCh[ch].writeMicroseconds(chVal[ch]); } Serial.print ("channel Number : "); Serial.println (ch); Serial.print ("channel Value : "); Serial.println (chVal[ch]); } void exeReply() { port.beginPacket(port.remoteIP(),localPort); // Send "Alive 1" back to the App to light up the "led" in the App GUI // Alive is the "id" for "LED" and "1" is the On command /n" will be ignored port.write("alive 1\n"); port.endPacket(); aliveSentTime = millis(); Serial.print("alive sent"); }
Labels:
android,
arduino,
automation,
code,
controller,
humidity,
internet,
ios,
lights,
lm298,
motion sensor,
motor driver,
programming,
relays,
roboremo,
servo,
sketch,
temperature,
udp,
wifi
Subscribe to:
Posts (Atom)