Go back
T0B-bot base program Download ino file
int in1 = 14; //Pin for left motor, go forward (counterclockwise)
int in2 = 32; //Pin for left motor, go back (clockwise)
int in3 = 15; //Pin for right motor, go forward (clockwise)
int in4 = 33; //Pin for right motor, go back (counterclockwise)
void setup(){pinMode(in1, OUTPUT); //Setting pin in1 to be output
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void loop() {forward(1000); //Calls function forward with variable 1000 as a delay in milliseconds
delay(500); //Delay for 500 milliseconds, 0.5 seconds
backward(1000);
delay(500);
turnLeft(1000);
delay(500);
turnRight(1000);
delay(500);
}
void forward(int duration){ //Defines function forward with the variable durationdigitalWrite(in1, HIGH); //Turns left motor on, counterclockwise
digitalWrite(in3, HIGH); //Turns right motor on, clockwise
delay(duration); //Uses variable duration as a delay
digitalWrite(in1, LOW); //Turns left motor off
digitalWrite(in3, LOW); //Turns right motor off
}
void backward(int duration){ //Defines function backwards with the variable durationdigitalWrite(in2, HIGH); //Turns left motor on, clockwise
digitalWrite(in4, HIGH); //Turns right motor on, counterclockwise
delay(duration); //Uses variable duration as a delay
digitalWrite(in2, LOW); //Turns left motor off
digitalWrite(in4, LOW); //Turns right motor off
}
void turnLeft(int duration){ //Defines function turnLeft with the variable durationdigitalWrite(in2, HIGH); //Turns left motor on, clockwise
digitalWrite(in3, HIGH); //Turns right motor on, clockwise
delay(duration); //Uses variable duration as a delay
digitalWrite(in2, LOW); //Turns left motor off
digitalWrite(in3, LOW); //Turns right motor off
}
void turnRight(int duration){ //Defines function turnRight with the variable durationdigitalWrite(in1, HIGH); //Turns left motor on, counterclockwise
digitalWrite(in4, HIGH); //Turns right motor on, counterclockwise
delay(duration); //Uses variable duration as a delay
digitalWrite(in1, LOW); //Turns left motor off
digitalWrite(in4, LOW); //Turns right motor off
}
T0B-bot web server Download ino file
/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
*********/
// Load Wi-Fi library
#include
// Replace with your network credentials
const char* ssid = "T0B-bot";
const char* password = "booopbooop";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliary variables to store the current output state
String outputredState = "off";
String outputyellowState = "off";
String outputblueState = "off";
// Assign output variables to GPIO pins
const int outputred = 21;
const int outputyellow = 17;
const int outputblue = 16;
void setup() {Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(outputred, OUTPUT);
pinMode(outputyellow, OUTPUT);
pinMode(outputblue, OUTPUT);
// Set outputs to LOW
digitalWrite(outputred, LOW);
digitalWrite(outputyellow, LOW);
digitalWrite(outputblue, LOW);
// 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());
*/
Serial.print("Setting AP (Access Point)...");
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.begin();
}
void loop(){WiFiClient client = server.available(); // Listen for incoming clients
if (client) {// If a new client connectsSerial.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()) { // loop while the client's connectedif (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();
// turns the GPIOs on and off
if (header.indexOf("GET /red/on") >= 0) {Serial.println("GPIO red on");
outputredState = "on";
digitalWrite(outputred, HIGH);
} else if (header.indexOf("GET /red/off") >= 0) {Serial.println("GPIO red off");
outputredState = "off";
digitalWrite(outputred, LOW);
} else if (header.indexOf("GET /yellow/on") >= 0) {Serial.println("GPIO yellow on");
outputyellowState = "on";
digitalWrite(outputyellow, HIGH);
} else if (header.indexOf("GET /yellow/off") >= 0) {Serial.println("GPIO yellow off");
outputyellowState = "off";
digitalWrite(outputyellow, LOW);
} else if (header.indexOf("GET /blue/on") >= 0) {Serial.println("GPIO blue on");
outputblueState = "on";
digitalWrite(outputblue, HIGH);
} else if (header.indexOf("GET /blue/off") >= 0) {Serial.println("GPIO blue off");
outputblueState = "off";
digitalWrite(outputblue, 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 on/off 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 {background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;
}");
client.println(".button2 {background-color: #555555;
}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 21
client.println("<p>Red - State " + outputredState + "</p>");
// If the outputredState is off, it displays the ON button
if (outputredState=="off") {client.println("<p><a href=\"/red/on\"><button class=\"button\">ON</button></a></p>");
} else {client.println("<p><a href=\"/red/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 17
client.println("<p>Yellow - State " + outputyellowState + "</p>");
// If the outputyellowState is off, it displays the ON button
if (outputyellowState=="off") {client.println("<p><a href=\"/yellow/on\"><button class=\"button\">ON</button></a></p>");
} else {client.println("<p><a href=\"/yellow/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 16
client.println("<p>Blue - State " + outputblueState + "</p>");
// If the outputblueState is off, it displays the ON button
if (outputblueState=="off") {client.println("<p><a href=\"/blue/on\"><button class=\"button\">ON</button></a></p>");
} else {client.println("<p><a href=\"/blue/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// 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("");
}
}
T0B-bot eyes Download ino file
int r = 16;
int g = 21;
int b = 17;
int r1 = 5;
int g1 = 19;
int b1 = 18;
void setup() {pinMode(r, OUTPUT);
pinMode(r1, OUTPUT);
pinMode(g, OUTPUT);
pinMode(g1, OUTPUT);
pinMode(b, OUTPUT);
pinMode(b1, OUTPUT);
}
void loop() {red();
delay(1000);
green();
delay(1000);
blue();
delay(1000);
}
void red() {digitalWrite(r, HIGH);
digitalWrite(r1, HIGH);
digitalWrite(g, LOW);
digitalWrite(g1, LOW);
digitalWrite(b, LOW);
digitalWrite(b1, LOW);
}
void green() {digitalWrite(r, LOW);
digitalWrite(r1, LOW);
digitalWrite(g, HIGH);
digitalWrite(g1, HIGH);
digitalWrite(b, LOW);
digitalWrite(b1, LOW);
}
void blue() {digitalWrite(r, LOW);
digitalWrite(r1, LOW);
digitalWrite(g, LOW);
digitalWrite(g1, LOW);
digitalWrite(b, HIGH);
digitalWrite(b1, HIGH);
}
T0B-bot remote control smart motors Download ino file
// Load Wi-Fi library
#include <WiFi.h >
//Vill ha en indikerande diod
#define LED_PIN 22
//You can customize the SSID name and change the password
const char* ssid = "T0B-bot";
const char* password = "booopbooop";
//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 = 33;
int enable1Pin = 12;
//Motor 2
int motor2Pin1 = 15;
int motor2Pin2 = 32;
int enable2Pin = 14;
//Setting PWM properties
const int freq = 30000;
const int pwmChannel1 = 0;
const int pwmChannel2 = 1;
const int resolution = 8;
int dutyCycle = 0;
float speedFactorMotor1 = 0.85;
float speedFactorMotor2 = 1;
//Decode HTTP GET value
String valueString = "0";
int pos1 = 0;
int pos2 = 0;
//Eyes
int r = 16;
int g = 21;
int b = 17;
int r1 = 5;
int g1 = 19;
int b1 = 18;
void setup() {Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
// Set the Motor pins as outputs
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
// Set all pins to be off
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
// Configure PWM channel functionalitites
ledcSetup(pwmChannel1, freq, resolution);
ledcSetup(pwmChannel2, freq, resolution);
// Attach the PWM channel 0 to the enable pins which are the GPIOs to be controlled
ledcAttachPin(enable1Pin, pwmChannel1);
ledcAttachPin(enable2Pin, pwmChannel2);
// Produce a PWM signal to both enable pins with a duty cycle 0
ledcWrite(pwmChannel1, dutyCycle);
ledcWrite(pwmChannel2, dutyCycle);
// Connect to Wi-Fi network with SSID and password
Serial.print("Setting AP (Access Point)...");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
digitalWrite(LED_PIN, HIGH);
server.begin();
//Eyes
pinMode(r, OUTPUT);
pinMode(r1, OUTPUT);
pinMode(g, OUTPUT);
pinMode(g1, OUTPUT);
pinMode(b, OUTPUT);
pinMode(b1, OUTPUT);
}
void loop() {WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects
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()) { // loop while the client's connected
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();
// turns the GPIOs on and off
// change LOW and HIGH to get the buttons working correctly with your robot
if (header.indexOf("GET /forward") >= 0) {Serial.println("Forward");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
red();
} else if (header.indexOf("GET /left") >= 0) {Serial.println("Left");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
green();
} else if (header.indexOf("GET /stop") >= 0) {Serial.println("Stop");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
none();
} else if (header.indexOf("GET /right") >= 0) {Serial.println("Right");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
blue();
} else if (header.indexOf("GET /reverse") >= 0) {Serial.println("Reverse");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
red();
}
// 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
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 > </head >");
// Web Page
client.println(" <p > <a href=\"/forward\" > <button class=\"button\" onclick=\"moveForward()\" >FORWARD </button > </a > </p >");
client.println(" <div style=\"clear: both;\" > <p > <a href=\"/left\" > <button class=\"button\" onclick=\"moveLeft()\" >LEFT </button > </a >");
client.println(" <a href=\"/stop\" > <button class=\"button button2\" onclick=\"stopRobot()\" >STOP </button > </a >");
client.println(" <a href=\"/right\" > <button class=\"button\" onclick=\"moveRight()\" >RIGHT </button > </a > </p > </div >");
client.println(" <p > <a href=\"/reverse\" > <button class=\"button\" onclick=\"moveReverse()\" >REVERSE </button > </a > </p >");
client.println(" <input type=\"range\" min=\"0\" max=\"100\" step=\"25\" id=\"motorSlider\" onchange=\"motorSpeed(this.value)\" value=\"" + valueString + "\"/ >");
client.println(" <script > function motorSpeed(pos) { ");
client.println("var xhr = new XMLHttpRequest();");
client.println("xhr.open('GET', \"/?value=\" + pos + \"&\", true);");
client.println("xhr.send();
} </script >");
client.println(" </html >");
//Request example: GET /?value=100& HTTP/1.1 - sets PWM duty cycle to 100
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(pwmChannel1, 0);
ledcWrite(pwmChannel2, 0);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
else {int dutyCycle1 = map(valueString.toInt() * speedFactorMotor1, 25, 100, 200, 255);
int dutyCycle2 = map(valueString.toInt() * speedFactorMotor2, 25, 100, 200, 255);
ledcWrite(pwmChannel1, dutyCycle1);
ledcWrite(pwmChannel2, dutyCycle2);
Serial.println(valueString);
}
}
// 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("");
}
}
void red() {digitalWrite(r, HIGH);
digitalWrite(r1, HIGH);
digitalWrite(g, LOW);
digitalWrite(g1, LOW);
digitalWrite(b, LOW);
digitalWrite(b1, LOW);
}
void green() {digitalWrite(r, LOW);
digitalWrite(r1, LOW);
digitalWrite(g, HIGH);
digitalWrite(g1, HIGH);
digitalWrite(b, LOW);
digitalWrite(b1, LOW);
}
void blue() {digitalWrite(r, LOW);
digitalWrite(r1, LOW);
digitalWrite(g, LOW);
digitalWrite(g1, LOW);
digitalWrite(b, HIGH);
digitalWrite(b1, HIGH);
}
void none() {digitalWrite(r, LOW);
digitalWrite(r1, LOW);
digitalWrite(g, LOW);
digitalWrite(g1, LOW);
digitalWrite(b, LOW);
digitalWrite(b1, LOW);
}