//****************************************************
// 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");
}