Home » Control motors from a web page

In this tutorial, you will learn how to control DC motors output with a web page.

The program

#include <ESP8266WiFi.h>
#include <esusBoard.h>
 
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// server
WiFiServer server(80);

// IP address
IPAddress ip(192, 168, 0, 13); 
IPAddress gateway(192,168,0,1);
IPAddress subnet(255,255,255,0); 
 
void setup() 
{

  // init board
  initEsusBoard();

  // init serial port
  Serial.begin(115200);
  delay(10);
 
  // wifi connexion
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet);

  // wait connexion
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");

  server.begin();
  Serial.println("Server started");
 
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
}
 
void loop() 
{
  
  WiFiClient client = server.available();

  while(client != true)
  {
    client = server.available();
    Serial.println("Wait client ...");
    delay(100);
  }
 
  // New client
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  String request = client.readStringUntil('\r');
  
  Serial.println(request);
  client.flush();

  // forward
  if (request.indexOf("/CMD=FOR") != -1) 
  {
    Serial.println("forward");
    motors1_set(1023,DIR_BACK);
    motors2_set(1023, DIR_BACK);
  }

  // back
  if (request.indexOf("/CMD=BAK") != -1) 
  {
    Serial.println("back");
    motors1_set(1023,DIR_FORWARD);
    motors2_set(1023, DIR_FORWARD);
  }

  // turn right
  if (request.indexOf("/CMD=TR") != -1) 
  {
    Serial.println("turn right");
    motors1_set(1023,DIR_BACK);
    motors2_set(1023, DIR_FORWARD);
  }

  // turn left
  if (request.indexOf("/CMD=TL") != -1) 
  {
    Serial.println("turn left");
    motors1_set(1023,DIR_FORWARD);
    motors2_set(1023, DIR_BACK);
  }

  // stop
  if (request.indexOf("/CMD=STP") != -1) 
  {
    Serial.println("turn stop");
    motors1_set(0,DIR_FORWARD);
    motors2_set(0,DIR_BACK);
  }

  Serial.println("Client disonnected");
  Serial.println("");
 
}

  • Upload the program

Control motors

Control motors with a web browser :

  • Forward command with web browser

img1_wifi

  • Back command with web browser

img2_wifi

  • Turn left command with web browser

img3_wifi

  • Turn right command with web browser

img4_wifi