Rahul Rajan
Published © GPL3+

Web-Based Voice-Controlled Robot!

Add voice control to your Arduino robot!

IntermediateFull instructions provided2 hours1,019
Web-Based Voice-Controlled Robot!

Things used in this project

Hardware components

Servos (Tower Pro MG996R)
Should be Arduino compatible.
×2
Romeo BLE - Arduino Robot Control Board with Bluetooth 4.0
DFRobot Romeo BLE - Arduino Robot Control Board with Bluetooth 4.0
×1
Adafruit Feather HUZZAH with ESP8266 WiFi
Adafruit Feather HUZZAH with ESP8266 WiFi
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Project circuit diagram. Make sure to not switch SCL and SDA!

Code

ESP8266 Server Code

Arduino
This should be uploaded on the Adafruit Feather. Make sure to fill in lines 7 & 8 with your network's SSID and Password.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Wire.h>

const char* ssid = "[SSID]";
const char* password = "[PASSWORD]";

ESP8266WebServer server(80);

const int led = 0;

void handleRoot() {

  String html = "";
  html="<script src=\"https://cdnjs.cloudflare.com/ajax/libs/annyang/2.6.0/annyang.min.js\"></script>\
<script>\
var xhttp = new XMLHttpRequest();\
if (annyang) {\
  var commands = {\
      'up': function() {\
        xhttp.open(\"GET\", \"up\", true);\
        xhttp.send();\
    },\
      'down': function()\
      {\
        xhttp.open(\"GET\", \"down\", true);\
        xhttp.send();\
      },\
      'left': function() {\
        xhttp.open(\"GET\", \"left\", true);\
        xhttp.send();\
    },\
      'right': function() {\
        xhttp.open(\"GET\", \"right\", true);\
        xhttp.send();\
    },\
      'stop': function() {\
        xhttp.open(\"GET\", \"stop\", true);\
        xhttp.send();\
    }\
  };\
  annyang.addCommands(commands);\
  annyang.start();\
}\
</script>\
";
  

  server.send(200, "text/html", html);
  digitalWrite(led, 0);
}

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){
  Wire.begin();
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);
  server.on("/up",[](){
    Wire.beginTransmission(8);
    Wire.write("u");
    Wire.endTransmission();
//    digitalWrite(0,1);
//    delay(1000);
//    digitalWrite(0,0);
    Serial.println("Received up");
  });
    server.on("/down",[](){
    Wire.beginTransmission(8);
    Wire.write("d");
    Wire.endTransmission();
//    digitalWrite(0,1);
//    delay(1000);
//    digitalWrite(0,0);
    Serial.println("Received down");
  });
      server.on("/left",[](){
    Wire.beginTransmission(8);
    Wire.write("l");
    Wire.endTransmission();
//    digitalWrite(0,1);
//    delay(1000);
//    digitalWrite(0,0);
    Serial.println("Received left");
  });
  server.on("/right",[](){
    Wire.beginTransmission(8);
    Wire.write("r");
    Wire.endTransmission();
//    digitalWrite(0,1);
//    delay(1000);
//    digitalWrite(0,0);
    Serial.println("Received right");
  });
    server.on("/stop",[](){
    Wire.beginTransmission(8);
    Wire.write("s");
    Wire.endTransmission();
//    digitalWrite(0,1);
//    delay(1000);
//    digitalWrite(0,0);
    Serial.println("Received stop");
  });
  

  server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
}

Romeo/Arduino Servo Control Code

Arduino
This Code receives commands from the Adafruit Feather via the Wire library. The program interprets these commands and instructs the servos to move.
#include <Wire.h>
#include <Servo.h>
Servo left;
Servo right;
void setup() {
  // put your setup code here, to run once:
  left.attach(4);
  right.attach(7);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(100);

}
void receiveEvent(int howMany) {
  char c = Wire.read();
  Serial.println(c);
  if(c == 'u')
  {
    Serial.println("moving up");
    left.write(120);
    right.write(75);

  }
  else if(c=='d')
  {
    Serial.println("moving down");
    left.write(75);
    right.write(120);

 
  }
  else if(c=='r')
  {
    Serial.println("moving left");
    left.write(120);
    right.write(120);
 

  }
  else if(c=='l')
  {
    Serial.println("moving right");
    left.write(75);
    right.write(75);
 
  }
  else if(c=='s')
  {
    Serial.println("stopping");
    left.write(90);
    right.write(90);
    
  }
  
  Serial.println();
       // print the integer
}

Credits

Rahul Rajan

Rahul Rajan

0 projects • 0 followers

Comments