付録

実験室で赤外リモコン送信テスト

内蔵の赤外LEDを用いて、ビデオカメラ操作する例を リスト 30 に示します。 ライブラリマネージャにて、IRremoteESP8266 をインストールしてください。ちなみに、テストしたバージョンは2.7.15でした。 2.7.15 より新しいバージョンだと、失敗する場合があります。もし最新版を入れてうまくいかない場合はダウングレードしてください。

リスト 30 src/irsend02.ino
 1/* IRremoteESP8266: IRsendDemo - demonstrates sending IR codes with IRsend.
 2
 3   Version 1.1 January, 2019
 4   Based on Ken Shirriff's IrsendDemo Version 0.1 July, 2009,
 5   Copyright 2009 Ken Shirriff, http://arcfn.com
 6
 7   An IR LED circuit *MUST* be connected to the ESP8266 on a pin
 8   as specified by kIrLed below.
 9
10   TL;DR: The IR LED needs to be driven by a transistor for a good result.
11
12   Suggested circuit:
13       https://github.com/crankyoldgit/IRremoteESP8266/wiki#ir-sending
14
15   Common mistakes & tips:
16 *   * Don't just connect the IR LED directly to the pin, it won't
17       have enough current to drive the IR LED effectively.
18 *   * Make sure you have the IR LED polarity correct.
19       See: https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity
20 *   * Typical digital camera/phones can be used to see if the IR LED is flashed.
21       Replace the IR LED with a normal LED if you don't have a digital camera
22       when debugging.
23 *   * Avoid using the following pins unless you really know what you are doing:
24 *     * Pin 0/D3: Can interfere with the boot/program mode & support circuits.
25 *     * Pin 1/TX/TXD0: Any serial transmissions from the ESP8266 will interfere.
26 *     * Pin 3/RX/RXD0: Any serial transmissions to the ESP8266 will interfere.
27 *   * ESP-01 modules are tricky. We suggest you use a module with more GPIOs
28       for your first time. e.g. ESP-12 etc.
29*/
30#include <M5StickCPlus.h>
31#include <IRremoteESP8266.h>
32#include <IRsend.h>
33
34const uint16_t kIrLed = 9;  // 赤外LEDが接続されたピン番号
35
36IRsend irsend(kIrLed);  // Set the GPIO to be used to sending the message.
37
38// Example of data captured by IRrecvDumpV2.ino
39uint16_t photoshot[99] = {3488, 1740,  438, 424,  438, 1302,  440, 422,  438, 424, 440, 422,  440, 422,
40                          440, 422,  438, 424,  438, 424,  438, 424,  438, 424,  438, 426,  438, 424,
41                          438, 1302,  440, 422,  442, 422,  440, 424,  438, 424,  438, 424,
42                          442, 420,  438, 1304,  438, 1304,  440, 1302,  438, 424,  438, 424,  438, 424,
43                          438, 424,  438, 1302,  440, 424,  440, 1302,  440, 422,  438, 424,  440, 422,
44                          440, 1302,  438, 1304,  438, 424,  438, 424,  438, 1304,  438, 1302,  438, 424,
45                          438, 424,  462, 1278,  464, 1278,  464, 1278,  462, 1280,  460, 1280,  464, 398,
46                          464, 398,  438
47                         };  // PANASONIC 40040E14667C
48
49uint16_t recording[99] = {3490, 1738,  440, 424,  438, 1304,  438, 424,  438, 424,  438, 424,  438, 424,
50                      438, 424,  438, 424,  438, 424,  438, 424,  438, 424,  438, 424,  438, 424,
51                      438, 1302,  440, 424,  442, 422,  438, 424,  438, 424,  438, 424,  438, 424,
52                      438, 1302,  442, 1302,  438, 1302,  440, 424,  440, 424,  438, 424,  438, 424,
53                      438, 1302,  442, 420,  442, 1302,  438, 424,  438, 422,  442, 422,  438, 424,
54                      440, 1302,  438, 1302,  438, 424,  438, 424,  438, 1302,  442, 1300,  440, 424,
55                      440, 422,  440, 1302,  440, 424,  462, 1278,  464, 400,  464, 398,  464, 1278,  464
56                     };  // PANASONIC 40040E143329
57
58void setup() {
59  M5.begin();
60  Serial.begin(115200);
61  M5.Lcd.fillScreen(BLUE);
62  irsend.begin();
63}
64
65void loop() {
66  //  Serial.println("NEC");  //  irsend.sendNEC(0x00FFE01FUL);
67  //  Serial.println("Sony"); //  irsend.sendSony(0xa90, 12, 2);  // 12 bits & 2 repeats
68  M5.update();
69  if (M5.BtnA.wasReleased()) {
70    irsend.sendRaw(photoshot, 99, 38);  // Send a raw data capture at 38kHz.
71    M5.Lcd.fillScreen(YELLOW);
72  } else if (M5.BtnB.wasReleased()) {
73    irsend.sendRaw(recording, 99, 38);  // Send a raw data capture at 38kHz.
74    M5.Lcd.fillScreen(RED);
75  }
76  delay(150);
77  M5.Lcd.fillScreen(BLUE);
78  //  Serial.println("a Samsung A/C state from IRrecvDumpV2");
79  //  irsend.sendSamsungAC(samsungState);
80  //  delay(2000);
81}

気温・湿度・気圧 (ENV II Sensor)

M5Stack社ENV II Unit(U001-B) https://lang-ship.com/blog/work/m5stack-env-ii-unit-u001-b/ の、M5StickCでの使い方(GitHub) に従って、以下2つをライブラリマネージャから入れてください。(または、arduino-cli lib install "Adafruit BMP280 Library" などで。)

  • Adafruit BMP280 Library

  • Adafruit SHT31 Library

上のリンクにあったプログラムとほぼ同じですが、リスト 31 が、気温・湿度・気圧をLCDとシリアルコンソールに出力するプログラムです。(すこしだけ改変しています)

リスト 31 src/env2.ino
 1/*
 2 Please install the < Adafruit BMP280 Library > (https://github.com/adafruit/Adafruit_BMP280_Library)
 3                     < Adafruit SHT31 Library > (https://github.com/adafruit/Adafruit_SHT31)
 4   from the library manager before use.
 5  This code will display the temperature, humidity and air pressure information on the screen
 6  https://lang-ship.com/blog/work/m5stack-env-ii-unit-u001-b/
 7  */
 8#include <M5StickCPlus.h>
 9#include <Adafruit_BMP280.h>
10#include <Adafruit_SHT31.h>
11Adafruit_SHT31 sht3x = Adafruit_SHT31(&Wire);
12Adafruit_BMP280 bme = Adafruit_BMP280(&Wire);
13float tmp = 0.0;
14float hum = 0.0;
15float pressure = 0.0;
16void setup() {
17  M5.begin();
18  M5.Lcd.setRotation(3);
19  M5.Lcd.setTextSize(3);
20  Wire.begin(32, 33);
21  Serial.println(F("ENV Unit(SHT30 and BMP280) test..."));
22  while (!bme.begin(0x76)) {
23    Serial.println("Could not find a valid BMP280 sensor, check wiring!");
24    M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!");
25  }
26  while (!sht3x.begin(0x44)) {
27    Serial.println("Could not find a valid SHT3X sensor, check wiring!");
28    M5.Lcd.println("Could not find a valid SHT3X sensor, check wiring!");
29  }
30  M5.Lcd.fillScreen(BLACK);
31  M5.Lcd.println("ENV Unit test...");
32}
33void loop() {
34  pressure = bme.readPressure();
35  tmp = sht3x.readTemperature();
36  hum = sht3x.readHumidity();
37  Serial.printf("Temperature: %2.2f*C  Humidity: %0.2f%%  Pressure: %0.2fhPa\r\n", tmp, hum, pressure / 100);
38  M5.Lcd.setCursor(0, 25);
39  M5.Lcd.setTextColor(WHITE, BLACK);
40  M5.Lcd.printf("Temp:%2.2f*C\n", tmp);
41  M5.Lcd.printf("Humi:%2.2f%%\n", hum);
42  M5.Lcd.printf("Prs :%2.0fhPa\n", pressure / 100);
43  delay(1000);
44}

警告

ENV II HAT用のプログラムではありません。ENV II HAT用のサンプル

Speaker Hatで音をだす

参考:https://nn-hokuson.hatenablog.com/entry/2017/09/01/092945

ボタンを押したときに、2種類の音を出す例を リスト 32 に示します。また、音データのサンプルを 16k.txt8k.txt におきました。 (16kも8kも、Speaker Hatで聴く分には違いはわかりませんでしたので、8kでいいとおもいます。)

リスト 32 のコメント(黄色ハイライト部分)をよく読んで、タブを追加し、16k.txt と 8k.txt の内容を貼り付けてください。

リスト 32 src/speakerhat01.ino
 1#include <M5StickCPlus.h>
 2
 3const int servo_pin = 26;
 4uint8_t ledChannel = 0;
 5uint8_t resolution = 8;
 6uint32_t sampling_rate = 16000; //16kの場合。8kの場合は、8000
 7
 8/*
 9    別ファイル(ファイル名拡張子は.ino)に、音声データを以下の形式で貼っておく。
10    【音1】
11    const uint8_t coin05_8k_raw[] PROGMEM = { 0x80, ... };
12    const uint32_t coin05_8k_raw_len = 7218; // ←配列のサイズ
13    【音2】
14    const uint8_t coin05_16k_raw[] PROGMEM = { 0x80, ... };
15    const uint32_t coin05_16k_raw_len = 14437; // ←配列のサイズ
16    PROGMEM と書くと、フラッシュメモリに配置する
17
18    そのうえで、以下のextern文を、別タブの変数名と合わせて書く。
19    変数名を一致させることが重要です。
20*/
21//    【音1】
22extern const uint8_t coin05_8k_raw[];
23extern const uint32_t coin05_8k_raw_len;
24//    【音2】
25extern const uint8_t coin05_16k_raw[];
26extern const uint32_t coin05_16k_raw_len;
27
28void setup() {
29  // put your setup code here, to run once:
30  M5.begin();
31  M5.Lcd.setRotation(3);
32  M5.Lcd.setCursor(0, 30, 4);
33  M5.Lcd.println(" speaker hat test");
34  ledcSetup(ledChannel, sampling_rate * 32, resolution);
35  ledcAttachPin(servo_pin, ledChannel);
36  ledcWrite(ledChannel, 0);
37}
38void playMusic(const uint8_t* music_data, uint32_t len, uint16_t sample_rate) {
39  uint32_t delay_interval = ((uint32_t)1000000 / sample_rate);
40  for (int i = 0; i < len; i++) {
41    ledcWrite(ledChannel, music_data[i]);
42    delayMicroseconds(delay_interval);
43  }
44  ledcWriteTone(ledChannel, 0);
45}
46void loop() {
47  M5.update();
48  if (M5.BtnA.wasReleased()) {
49    playMusic(coin05_16k_raw, coin05_16k_raw_len, sampling_rate + 800); //なぜか音が低いので +800した
50  } else if (M5.BtnB.wasReleased()) {
51    playMusic(coin05_8k_raw, coin05_8k_raw_len, sampling_rate / 2); // 8kなので、2で割った
52  }
53  ledcWriteTone(ledChannel, 0);
54  delay(100);
55}

LINE Notify (HTTPS POST)

https://notify-bot.line.me/ja/ で、トークンを取得しておく必要があります。

リスト 33 src/line01.ino
 1#include <WiFi.h>
 2#include <WiFiMulti.h>
 3#include <WiFiClient.h>
 4#include <HTTPClient.h>
 5
 6WiFiMulti WiFiMulti;
 7
 8void setup() {
 9  Serial.begin(115200);
10  WiFi.mode(WIFI_STA);
11  WiFiMulti.addAP("ics-ap", "jikkenics");
12
13  // wait for WiFi connection
14  Serial.print("Waiting for WiFi to connect...");
15  while ((WiFiMulti.run() != WL_CONNECTED)) {
16    Serial.print(".");
17    delay(50);
18  }
19  Serial.println(" connected");
20}
21
22void loop() {
23  HTTPClient http;
24
25  if (http.begin("https://notify-api.line.me/api/notify")) {  // HTTPS
26    http.addHeader("Authorization", "Bearer TOKENTOKENTOKEN"); // ここのTOKENTOKEN... の部分に、取得したTokenを指定する
27    http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // format of postdata
28    String postdata = "message=日本語でも大丈夫です";
29    int httpCode = http.POST(postdata);
30
31    // httpCode will be negative on error
32    if (httpCode > 0) {
33      // HTTP header has been send and Server response header has been handled
34      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
35
36      // file found at server
37      if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
38        String payload = http.getString();
39        Serial.println(payload);
40      }
41    } else {
42      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
43    }
44    http.end();
45  } else {
46    Serial.printf("[HTTP] Unable to connect\n");
47  }
48
49  Serial.println();
50  Serial.println("Waiting 2 minutes..."); 
51  delay(60*2*1000); // 120秒まつ
52}