LoginSignup
3
1

More than 3 years have passed since last update.

ESP8266(ESPrDeveloper)+Google-Home-Notiferで 自由発声マシン!?

Last updated at Posted at 2020-03-29

ESPrDeveloperで、GoogleHomeを自由に喋らせてみました

@horihiroさん素晴らしすぎます。
まさに、これです。そのまんまです。
PCとかラズパイとか不要で、arduinoだけで出来るなんて素晴らしすぎです。

https://qiita.com/horihiro/items/4ab0edf415916a2cd542

上記に書いてあるソースも利用させていただきました・・・。

使ったもの

ESPrDeveloper(スイッチサイエンス製)
・Arduino IDE
 ・ESP8266ライブラリ
 ・Google Home Notiferライブラリ

mDNSでWEBに接続

せっかくなんで、mDNSを使って、hogehoge.local:8080 でWEBに接続できるようにしています。
ソースは、サンプルそのままです。

外部インターネットからの接続

serveo.netをやろうかと思ったんですが、現在停止中でした。
そこで、GCPの無料枠を使って、httpsで接続するためのリバプロを作りました。
別記事にします。
https://qiita.com/ABK28/items/c1683364edd9894beb65

使い方(ローカル版)

ローカル環境から、例えば、hogehoge.local:8080 でつなぐだけです。
iPhoneやMacですとそのまま使えます。
Windowsだと、Bonjourのインストール必要らしいですが、Windowsを使っていないので詳細不明です・・。

サンプルソース

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <esp8266-google-home-notifier.h>


// Google Home
GoogleHomeNotifier ghn;
const char displayName[] = "あなたのデバイスネーム";
String mes;

// WiFi
const char* ssid="あなたのSSID";
const char* password="あなたのパスフレーズ";

// Web Server 8080ポートで接続
ESP8266WebServer server(8080);
static const char* cpResponse200 = "<HTML>"
 "<BODY style='background-color:#ffffde;font-family:sans-serif;font-size:32px;'>"
 "WEB Server Title<br/><br/><br/>"
 "<input type=\"text\" style=\"font-family:sans-serif;font-size:30px;width:400px;height:80px;\">"
 "&nbsp;<button style=\"font-family:sans-serif;font-size:24px;\">speech</button>"
 "<script>"
 "var d = document;"
 "d.querySelector('button').addEventListener('click',function(){xhr = new XMLHttpRequest();"
 "xhr.open('GET','/speech?phrase='+encodeURIComponent(d.querySelector('input').value));"
 "xhr.send();});"
 "</script>"
 "</BODY></HTML>\r\n";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("");

  //WiFi
  Serial.println("");
  Serial.print("connecting to Wi-Fi");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("connected.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //Print the local IP

  //Google Home speak IP Address.
  mes = "フリースピーキングマシーンの IPアドレスは、";
  mes += ipToString(WiFi.localIP());
  mes += "です。";
  googleHomeConnection( "ja", mes.c_str() );

  // Set up mDNS responder
  if (!MDNS.begin("hogehoge")) { //hogehoge.local
    Serial.println("Error setting up MDNS responder!");
    while (1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");

  // Web Server 
  server.on("/speech", handleSpeechPath);
  server.on("/", handleRootPath);
  server.begin();

  // Add service to MDNS-SD
  MDNS.addService("http", "tcp", 8080);
}

void loop() {
  // put your main code here, to run repeatedly:
  MDNS.update();
  server.handleClient();
}

//Google Home Neotifer で喋らせる
void googleHomeConnection( String lang_str1, String talk_str1 ){
  Serial.println("connecting to Google Home...");
  if (ghn.device( displayName, lang_str1.c_str() ) != true) {
    Serial.println(ghn.getLastError());
    return;
  }
  Serial.print("found Google Home(");
  Serial.print(ghn.getIPAddress());
  Serial.print(":");
  Serial.print(ghn.getPort());
  Serial.println(")");

  if (ghn.notify( talk_str1.c_str() ) != true) {
    Serial.println(ghn.getLastError());
    return;
  }
  Serial.println("");
}

// IPadressをString型に・・
String ipToString(uint32_t ip){
    String result = "";
    result += String((ip & 0xFF), 10);
    result += ".";
    result += String((ip & 0xFF00) >> 8, 10);
    result += ".";
    result += String((ip & 0xFF0000) >> 16, 10);
    result += ".";
    result += String((ip & 0xFF000000) >> 24, 10);
    return result;
}

// WEB -> 発声
void handleSpeechPath() {
  String phrase = server.arg("phrase");
  if (phrase == "") {
    server.send(401, "text / plain", "query 'phrase' is not found");
    return;
  }
  googleHomeConnection( "ja", phrase.c_str() );
  server.send(200, "text / plain", "OK");
}

// WEB表示
void handleRootPath() {
  server.send(200, "text/html", cpResponse200);
}
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1