-
-
Save sarasantos/b7a84c5572d8ad26616b990e5c259df6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* | |
Rui Santos | |
Complete project details at http://randomnerdtutorials.com | |
*********/ | |
// Load Wi-Fi library | |
#include <WiFi.h> | |
#include<Servo.h> | |
Servo myservo; | |
static const int servoPin = 13; | |
// Replace with your network credentials | |
const char* ssid = "SSID"; | |
const char* password = "password"; | |
// Set web server port number to 80 | |
WiFiServer server(80); | |
// Variable to store the HTTP request | |
String header; | |
// Motor 1 | |
int motor1Pin1 = 27; | |
int motor1Pin2 = 26; | |
int enable1Pin = 14; | |
// Motor 2 | |
int motor2Pin1 = 33; | |
int motor2Pin2 = 25; | |
int enable2Pin = 32; | |
// Setting PWM properties | |
const int freq = 30000; | |
const int pwmChannel = 0; | |
const int resolution = 8; | |
int dutyCycle = 0; | |
// Decode HTTP GET value | |
String valueString = String(5); | |
int pos1 = 0; | |
int pos2 = 0; | |
String valueString1 = String(5); | |
int pos3 = 0; | |
int pos4 = 0; | |
// Current time | |
unsigned long currentTime = millis(); | |
// Previous time | |
unsigned long previousTime = 0; | |
// Define timeout time in milliseconds (example: 2000ms = 2s) | |
const long timeoutTime = 2000; | |
void setup() { | |
Serial.begin(115200); | |
myservo.attach(servoPin); | |
// Set the Motor pins as outputs | |
pinMode(motor1Pin1, OUTPUT); | |
pinMode(motor1Pin2, OUTPUT); | |
pinMode(motor2Pin1, OUTPUT); | |
pinMode(motor2Pin2, OUTPUT); | |
// Configure PWM channel functionalitites | |
ledcSetup(pwmChannel, freq, resolution); | |
// Attach the PWM channel 0 to the enable pins which are the GPIOs to be controlled | |
ledcAttachPin(enable1Pin, pwmChannel); | |
ledcAttachPin(enable2Pin, pwmChannel); | |
// Produce a PWM signal to both enable pins with a duty cycle 0 | |
ledcWrite(pwmChannel, dutyCycle); | |
// Connect to Wi-Fi network with SSID and password | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
// Print local IP address and start web server | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
server.begin(); | |
} | |
void loop(){ | |
WiFiClient client = server.available(); // Listen for incoming clients | |
if (client) { // If a new client connects, | |
currentTime = millis(); | |
previousTime = currentTime; | |
Serial.println("New Client."); // print a message out in the serial port | |
String currentLine = ""; // make a String to hold incoming data from the client | |
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected | |
currentTime = millis(); | |
if (client.available()) { // if there's bytes to read from the client, | |
char c = client.read(); // read a byte, then | |
Serial.write(c); // print it out the serial monitor | |
header += c; | |
if (c == '\n') { // if the byte is a newline character | |
// if the current line is blank, you got two newline characters in a row. | |
// that's the end of the client HTTP request, so send a response: | |
if (currentLine.length() == 0) { | |
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) | |
// and a content-type so the client knows what's coming, then a blank line: | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-type:text/html"); | |
client.println("Connection: close"); | |
client.println(); | |
// Controls the motor pins according to the button pressed | |
if (header.indexOf("GET /forward") >= 0) { | |
Serial.println("Forward"); | |
digitalWrite(motor1Pin1, LOW); | |
digitalWrite(motor1Pin2, HIGH); | |
digitalWrite(motor2Pin1, LOW); | |
digitalWrite(motor2Pin2, HIGH); | |
} else if (header.indexOf("GET /left") >= 0) { | |
Serial.println("Left"); | |
digitalWrite(motor1Pin1, LOW); | |
digitalWrite(motor1Pin2, LOW); | |
digitalWrite(motor2Pin1, LOW); | |
digitalWrite(motor2Pin2, HIGH); | |
} else if (header.indexOf("GET /stop") >= 0) { | |
Serial.println("Stop"); | |
digitalWrite(motor1Pin1, LOW); | |
digitalWrite(motor1Pin2, LOW); | |
digitalWrite(motor2Pin1, LOW); | |
digitalWrite(motor2Pin2, LOW); | |
} else if (header.indexOf("GET /right") >= 0) { | |
Serial.println("Right"); | |
digitalWrite(motor1Pin1, LOW); | |
digitalWrite(motor1Pin2, HIGH); | |
digitalWrite(motor2Pin1, LOW); | |
digitalWrite(motor2Pin2, LOW); | |
} else if (header.indexOf("GET /reverse") >= 0) { | |
Serial.println("Reverse"); | |
digitalWrite(motor1Pin1, HIGH); | |
digitalWrite(motor1Pin2, LOW); | |
digitalWrite(motor2Pin1, HIGH); | |
digitalWrite(motor2Pin2, LOW); | |
} | |
// Display the HTML web page | |
client.println("<!DOCTYPE HTML><html>"); | |
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); | |
client.println("<link rel=\"icon\" href=\"data:,\">"); | |
// CSS to style the buttons | |
// Feel free to change the background-color and font-size attributes to fit your preferences | |
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); | |
client.println(".button { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; background-color: #4CAF50;"); | |
client.println("border: none; color: white; padding: 12px 28px; text-decoration: none; font-size: 26px; margin: 1px; cursor: pointer;}"); | |
client.println(".button2 {background-color: #555555;}</style>"); | |
client.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script></head>"); | |
// Web Page | |
client.println("<p><button class=\"button\" onclick=\"moveForward()\">FORWARD</button></p>"); | |
client.println("<div style=\"clear: both;\"><p><button class=\"button\" onclick=\"moveLeft()\">LEFT </button>"); | |
client.println("<button class=\"button button2\" onclick=\"stopRobot()\">STOP</button>"); | |
client.println("<button class=\"button\" onclick=\"moveRight()\">RIGHT</button></p></div>"); | |
client.println("<p><button class=\"button\" onclick=\"moveReverse()\">REVERSE</button></p>"); | |
client.println("<p>Motor Speed: <span id=\"motorSpeed\"></span></p>"); | |
client.println("<input type=\"range\" min=\"0\" max=\"100\" step=\"25\" id=\"motorSlider\" onchange=\"motorSpeed(this.value)\" value=\"" + valueString + "\"/>"); | |
client.println("<p>Position: <span id=\"servoPos\"></span></p>"); | |
client.println("<input type=\"range\" min=\"0\" max=\"180\" id=\"servoSlider\" onchange=\"servo(this.value)\" value1=\"" + valueString1 + "\"/>"); | |
client.println("<script>$.ajaxSetup({timeout:1000});"); | |
client.println("function moveForward() { $.get(\"/forward\"); {Connection: close};}"); | |
client.println("function moveLeft() { $.get(\"/left\"); {Connection: close};}"); | |
client.println("function stopRobot() {$.get(\"/stop\"); {Connection: close};}"); | |
client.println("function moveRight() { $.get(\"/right\"); {Connection: close};}"); | |
client.println("function moveReverse() { $.get(\"/reverse\"); {Connection: close};}</script>"); | |
client.println("<script>var slider = document.getElementById(\"motorSlider\");"); | |
client.println("var motorP = document.getElementById(\"motorSpeed\"); motorP.innerHTML = slider.value;"); | |
client.println("slider.oninput = function() { slider.value = this.value; motorP.innerHTML = this.value; }"); | |
client.println("function motorSpeed(pos) { $.get(\"/?value=\" + pos + \"&\"); {Connection: close};}</script>"); | |
client.println("<script>var slider1 = document.getElementById(\"servoSlider\");"); | |
client.println("var servoP = document.getElementById(\"servoPos\"); servoP.innerHTML = slider1.value1;"); | |
client.println("slider1.oninput = function() { slider1.value1= this.value; servoP.innerHTML = this.value; }"); | |
client.println(" function servo(pos) {$.get(\"/?value1=\" + pos + \"&\"); {Connection: close};}</script>"); | |
client.println("</html>"); | |
//Request example: GET /?value=100& HTTP/1.1 - sets PWM duty cycle to 100% = 255 | |
if(header.indexOf("GET /?value=")>=0) { | |
pos1 = header.indexOf('='); | |
pos2 = header.indexOf('&'); | |
valueString = header.substring(pos1+1, pos2); | |
//Set motor speed value | |
if (valueString == "0") { | |
ledcWrite(pwmChannel, 0); | |
digitalWrite(motor1Pin1, LOW); | |
digitalWrite(motor1Pin2, LOW); | |
digitalWrite(motor2Pin1, LOW); | |
digitalWrite(motor2Pin2, LOW); | |
} | |
else { | |
dutyCycle = map(valueString.toInt(), 25, 100, 200, 255); | |
ledcWrite(pwmChannel, dutyCycle); | |
Serial.println(valueString); | |
} | |
} | |
//GET /slider | |
if(header.indexOf("GET /?value1=")>=0) { | |
pos3 = header.indexOf('='); | |
pos4 = header.indexOf('&'); | |
valueString1 = header.substring(pos3+1, pos4); | |
//Rotate the servo | |
myservo.write(valueString1.toInt()); | |
Serial.println(valueString1); | |
} | |
// The HTTP response ends with another blank line | |
client.println(); | |
// Break out of the while loop | |
break; | |
} else { // if you got a newline, then clear currentLine | |
currentLine = ""; | |
} | |
} else if (c != '\r') { // if you got anything else but a carriage return character, | |
currentLine += c; // add it to the end of the currentLine | |
} | |
} | |
} | |
// Clear the header variable | |
header = ""; | |
// Close the connection | |
client.stop(); | |
Serial.println("Client disconnected."); | |
Serial.println(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment