Published © CC BY-SA

How to Use Voice to Control and Integrate ESP8266 & Android

This article describes how to integrate ESP8266 and Android using an app that uses voice commands to interact with an IoT device.

BeginnerShowcase (no instructions)1 hour1,895
How to Use Voice to Control and Integrate ESP8266 & Android

Things used in this project

Story

Read more

Schematics

ESP8266 + Neopixel Ring

Code

Code snippet #1

Plain text
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.survivingwithandroid.voice.MainActivity" 
    android:id="@+id/mainView" 
    android:background="@drawable/bg_gradient">

   <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        app:layout_constraintLeft_toRightOf="parent" 
        app:layout_constraintRight_toLeftOf="parent" 
        android:text="Send command" 
        android:id="@+id/btnCommand" 
        app:layout_constraintBottom_toBottomOf="parent" 
        android:layout_marginBottom="15dp"/>

</android.support.constraint.ConstraintLayout>

Code snippet #2

Plain text
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btn = (Button) findViewById(R.id.btnCommand);
  btn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    startVoiceCommand();
   }
  });
}

Code snippet #3

Plain text
private void startVoiceCommand() {
  Log.d(TAG, "Starting Voice intent...");
  Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
             RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
  i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Tell me, I'm ready!");
  try {
   startActivityForResult(i, REQ_SPEECH_RESULT);
  }
  catch (Exception e) {
   Snackbar.make(v, "Speech to text not supported", Snackbar.LENGTH_LONG).show();
  }
}

Code snippet #4

Plain text
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

   // Check the Request code
   if (requestCode ==  REQ_SPEECH_RESULT) {
     Log.d(TAG, "Request speech result..");
     ArrayList&lt;String&gt; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
     String command = results.get(0);
     Log.d(TAG, "Current command ["+command+"]");
     // Now we send commands to the IoT device
   }
}

Code snippet #5

Plain text
public class IoTConnectionHandler {
    private static IoTConnectionHandler me;
    private OkHttpClient client;
    private static final String TAG = IoTConnectionHandler.class.getName();

    private static final String IOT_URL = 
          "http://192.168.1.9:8080/ring?param=0";

    private IoTConnectionHandler() {
        client = new OkHttpClient();
    }

    public static IoTConnectionHandler getInstance() {
        if (me == null)
            me = new IoTConnectionHandler();

        return me;
    }

    public void sendData(String data) {
        Request req = new Request.Builder()
                      .url(IOT_URL + data)
                      .build();

        client.newCall(req).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "Connection error", e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i(TAG, "Command sent");
            }
        });
    }
}

Code snippet #6

Plain text
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <aREST.h>

#define PIN D2
#define NUMS 12
#define SERVER_PORT 8080

// Neopixel rings
Adafruit_NeoPixel pixels = 
   Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);

aREST rest = aREST();

char *ssid = "xxxxx";
char *pwd = "xxx";

// Let us create the server
WiFiServer server(SERVER_PORT);

void setup() {
  Serial.begin(9600);
  pixels.begin();
  pixels.setBrightness(85);

  // Register the function
  rest.function("ring", setColor);
  WiFi.begin(ssid, pwd);
  Serial.println("Connecting to WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
   delay(1000);
   Serial.println("Try again....");
  }

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

  // let us start the server
  server.begin();
}

void loop() {

  WiFiClient client = server.available();
  if (!client) {
    return ;
  }

  while (!client.available()) {
   delay(1);
  }

  rest.handle(client);
}

int setColor(String color) {
  Serial.println("Hex color [" + color + "]");
  long tmpColor = strtol( &("#" + color)[1], NULL, 16);

  Serial.println("Int ["+String(tmpColor)+"]");

  int r = tmpColor << 16;
  int g = tmpColor << 8 & 0xFF;
  int b = tmpColor & 0xFF;

  Serial.print("Red [" + String(r) + "]");
  Serial.print("Green [" + String(g) + "]");
  Serial.println("Blue [" + String(b) + "]");

  for (int i = 0; i &lt; 12; i++)
    pixels.setPixelColor(i, pixels.Color(r,g,b));

  pixels.show();

  return 1;
}

Github

https://github.com/marcoschwartz/aREST

Github

https://github.com/square/okhttp

Github

https://github.com/adafruit/Adafruit_NeoPixel

Credits

Comments