3
5

More than 5 years have passed since last update.

esp8266からSlackにメッセージを送る

Last updated at Posted at 2017-12-31

IMG_3634.jpg

ボタンが押されたら、Slackにメッセージを送るesp8266用のスケッチです。ボタンの配線は、Arduinoのチュートリアルを真似ています(違いはinputピンが2 -> 12になったところくらい)。

esp8266用の設定は switch-science の記事を参考にしました。flushボタンを押さないとスケッチを書き込めなくなったりして、このあたりの扱いが普通のArduinoよりちょっとコツがいる感じです。

こんな感じでメッセージが届きます。

Screen Shot 2018-01-01 at 8.32.27.png

ちょっと心配なのが、SlackのAPIリファレンスのどこを探しても、以下のスケッチで使用している形式のURLが見つからないこと…。あらゆるチャネルに応用できるので、便利に使っているんですが、ひょっとして既にdeprecated(非推奨)になってたりして?

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>

const int buttonPin = 12;     // the number of the pushbutton pin
const int ledPin =  14;      // the number of the LED pin

int buttonState = 0;         // variable for reading the pushbutton status

const char* ssid     = "yourssid";  // WifiのSSID
const char* password = "yourpsk";  // Wifiのパスワード

const char* host = "lumbermill.slack.com";
const int httpsPort = 443;
const char* path = "/services/hooks/slackbot?token=yourtoken&channel=%23yourchannel";

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);

  // *** Wifi Setup ***
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

}

void send_data() {
  // Use WiFiClientSecure class to create TLS connection
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("requesting URL: ");
  Serial.println(path);

  String message = "Hello, iot butoon!";

  client.print(String("POST ") + path + " HTTP/1.1\r\n" +
      "Host: " + host + "\r\n" +
      "User-Agent: MukoyamaClientESP8266\r\n" +
      "Content-Type: application/x-www-form-urlencoded\r\n" +
      "Content-Length: "+ String(message.length()) +"\r\n\r\n" +
      message+"\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readString();
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}


void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    Serial.print("Send");
    send_data();
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
3
5
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
5