0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ESP8266 / Slack > Slackのチャネルにメッセージを投稿する ( SSL認証失敗版 )

Last updated at Posted at 2016-12-31
使用機材
ESP-WROOM-02 ユニバーサル基板実装

ESP8266からSlackへの投稿をしようとしている。

参考 http://qiita.com/kiyopikko/items/4c525103700f52cb2894
参考 http://www.iotforest.com/2016/3/4/esp8266-arduino-feather-huzzah-using-slack-integration
参考 http://nyanko-omori.hatenablog.com/entry/2016/08/14/144918

v0.1

code > WiFi設定

WifiConfig.h :
自分のアクセスポイントを記載したファイル。
注意: GitHubなどにチェックインしないこと

static const char *kSsid = "pi-31415-g";
static const char *kPass = "47voyager";

code > Slack設定

slackConfig.h :
Slackのチャネル、Incoming WebHooksなどの設定ファイル。

注意: GitHubなどにチェックインしないこと

static const char *kSlackUrl = "/services/PIPIPI/NAPIER/3141592653589793228";
static const String kSlackChannel = "#amazon_dash";
static const String kSlackUsername = "7of9";

kSlackUrl にはIncoming WebHooksのURLの/services/から始まる文字列を設定する。

code > 本体

参考のリンクのコードをもとに以下としました。

esp2866_161230_slack
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include "WifiConfig.h" // for [kSsid],[kPass]
#include "slackConfig.h" // for [kSlackUrl]

/*
 * v0.1 Dec. 31, 2016
 *   Based on:
 *   http://qiita.com/kiyopikko/items/4c525103700f52cb2894
 *   http://www.iotforest.com/2016/3/4/esp8266-arduino-feather-huzzah-using-slack-integration
 * 
 */

static const char *kSlackHost = "hooks.slack.com";
static const int kHttpsPort = 443;

void WiFi_setup()
{
  WiFi.begin(kSsid, kPass);
  while( WiFi.status() != WL_CONNECTED) {
    delay(500); // msec
  }
  Serial.println(WiFi.localIP());
}

void slack_submit()
{
  WiFiClientSecure client;

  // connect
  if (!client.connect(kSlackHost, kHttpsPort)) { 
    Serial.println("slack connection failed");
  } else {
    Serial.println("slack connection: OK");    
  }

  // SSL Certificate finngerprint for the host
  const char* fingerprint = "‎‎‎‎ab f0 5b a9 1a e0 ae 5f ce 32 2e 7c 66 67 49 ec dd 6d 6a 38";
  // verify the signature of the ssl certificate
 if (client.verify(fingerprint, kSlackHost)) {
   Serial.println("ssl cert matches");
 } else {
   Serial.println("ssl cert mismatch");
 }

  // submit
  String message = "test from ESP8266";
  String payload="payload={\"channel\": \"" + kSlackChannel + "\", \"username\": \"" + kSlackUsername 
  + "\", \"text\": \"" + message + "\", \"icon_emoji\": \":ghost:\"}";
  Serial.println(payload.c_str());

  client.print("POST ");
  client.print(kSlackUrl);
  client.println(" HTTP/1.1");
  client.print("Host: ");
  client.println(kSlackHost);
  client.println("User-Agent: ArduinoIoT/1.0");
  client.println("Connection: close");
  client.println("Content-Type: application/x-www-form-urlencoded;");
  client.print("Content-Length: ");
  client.println(payload.length());
  client.println();
  client.println(payload);

  // リクエストを受け取る前に5秒以上は待った方がいいらしい
  delay(7000);

  while(client.available()) {
    String line = client.readStringUntil('\r');
    Serial.println(line);
  }
  client.stop();
}

void setup() {
  Serial.begin(115200);
  Serial.println(""); // to separate line

  WiFi_setup();
  slack_submit();
}

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

ESP8266を起動すると1回だけSlackへ投稿します。

結果

192.168.10.2
slack connection: OK
ssl cert mismatch
payload={"channel": "#amazon_dash", "username": "7of9", "text": "test from ESP8266", "icon_emoji": ":ghost:"}

以下のように、投稿ができた。

qiita.png

ssl cert mismatchとなっている点は来年の課題だ。

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?