6
4

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 3 years have passed since last update.

Node-REDAdvent Calendar 2020

Day 15

Node-REDでM5Stack用赤外線モジュールを操作する

Last updated at Posted at 2020-12-14

Nature Remoをいくつも買うお金がないので、M5StickCを使ってMQTT経由でエアコンを操作できるものを作ってみました。

リモコンの信号を受信してみる

こちらの記事を参考に、エアコンのリモコンの信号を受信してみました。
M5StickC/M5Stack Fireで赤外線リモコンを作ろうと四苦八苦したのでメモ

こんなのが返ってきました。

Protocol  : MULTIBRACKETS
Code      : 0xE0 (8 Bits)
uint16_t rawData[853] = {29990, 49842,  3358, 1710,  382, 1300,  382, 460,  380, 462,  380, 460,  382, 460,  382, 460,  380, 462,  382, 460,  380, 460,  382, 460,  380, 462,  382, 462,  380, 1300,  382, 462,  380, 462,  380, 460,  382, 460,  380, 462,  382, 462,  380, 460,  382, 460,  382, 460,  380, 460,  382, 460,  382, 460,  382, 460,  382, 460,  382, 460,  382, 462,  380, 462,  380, 1300,  382, 462,  380, 1300,  382, 1298,  382, 1300,  382, 1300,  382, 1300,  382, 1302,  382, 460,  382, 1302,  382, 1300,  384, 1300,  382, 1302,  382, 1300,  382, 1300,  382, 1300,  382, 1302,  382, 1300,  382, 460,  380, 462,  382, 460,  380, 462,  382, 486,  354, 462,  380, 462,  380, 460,  382, 460,  380, 462,  380, 1302,  382, 1300,  382, 460,  382, 460,  380, 1300,  380, 1302,  382, 1302,  382, 1302,  384, 460,  380, 462,  380, 1300,  382, 1302,  382, 460,  380, 462,  380, 460,  380, 1300,  380, 462,  378, 462,  382, 1296,  382, 460,  380, 486,  354, 1300,  380, 1302,  382, 460,  380, 1300,  380, 1300,  380, 462,  380, 1300,  380, 1300,  382, 462,  378, 1302,  380, 1300,  382, 462,  378, 486,  356, 1298,  382, 462,  378, 462,  378, 462,  380, 486,  354, 486,  356, 1302,  380, 1302,  382, 462,  378, 1302,  382, 1300,  382, 1300,  384, 460,  380, 464,  378, 486,  354, 1304,  382, 460,  380, 1300,  382, 1300,  380, 462,  380, 1302,  380, 1302,  382, 1300,  380, 464,  378, 1302,  380, 462,  378, 462,  378, 1304,  378, 460,  380, 486,  352, 464,  380, 484,  354, 486,  354, 486,  356, 486,  354, 488,  352, 1304,  380, 1302,  382, 1300,  380, 1302,  380, 1302,  380, 1302,  378, 1302,  380, 1302,  378, 462,  378, 488,  354, 486,  354, 464,  376, 488,  354, 462,  352, 512,  354, 486,  354, 1302,  378, 1300,  382, 1300,  382, 1300,  378, 1304,  380, 1300,  380, 1300,  380, 1300,  380, 462,  378, 486,  354, 486,  352, 488,  354, 486,  352, 488,  354, 486,  352, 462,  378, 1302,  380, 1302,  378, 1302,  380, 1302,  376, 1304,  380, 1302,  380, 1302,  378, 1302,  380, 464,  378, 488,  352, 488,  354, 486,  328, 512,  354, 486,  354, 486,  352, 488,  352, 1306,  378, 1302,  378, 1302,  378, 1302,  380, 1300,  380, 1302,  378, 1302,  378, 1300,  378, 466,  376, 464,  376, 488,  354, 486,  352, 488,  354, 486,  354, 486,  352, 486,  354, 1304,  376, 1302,  376, 1304,  382, 1298,  380, 1302,  378, 1304,  378, 1302,  380, 1302,  378, 488,  354, 1302,  380, 1302,  378, 462,  376, 1302,  380, 486,  356, 1300,  378, 486,  354, 1304,  380, 486,  354, 488,  354, 1302,  378, 488,  354, 1302,  380, 486,  354, 1302,  376, 1304,  378, 486,  354, 486,  354, 486,  354, 486,  354, 1302,  380, 1302,  380, 1302,  378, 488,  354, 1304,  378, 1302,  380, 1302,  380, 1304,  378, 486,  354, 488,  352, 486,  352, 486,  354, 488,  352, 488,  354, 486,  354, 486,  354, 486,  354, 486,  350, 488,  352, 1304,  380, 1300,  378, 1300,  382, 1300,  378, 1302,  380, 1300,  380, 1300,  380, 1302,  378, 486,  354, 488,  354, 486,  354, 488,  354, 486,  354, 486,  352, 488,  354, 488,  354, 1302,  378, 1300,  380, 1302,  378, 1302,  378, 1304,  380, 1300,  380, 1300,  378, 1300,  378, 488,  354, 486,  352, 466,  376, 486,  354, 464,  378, 486,  356, 486,  354, 1302,  378, 1302,  378, 1302,  380, 1302,  380, 1302,  380, 1302,  380, 1302,  378, 1302,  380, 486,  354, 1300,  380, 1302,  378, 488,  354, 488,  352, 488,  354, 486,  352, 488,  352, 464,  376, 488,  354, 488,  354, 1300,  378, 1302,  380, 1302,  378, 1302,  382, 1300,  378, 1302,  382, 1302,  376, 488,  354, 486,  352, 488,  354, 486,  352, 488,  352, 488,  354, 488,  352, 466,  374, 1304,  378, 1302,  378, 1304,  376, 1304,  378, 1304,  378, 1304,  376, 1302,  380, 486,  354, 462,  378, 486,  356, 1300,  378, 488,  352, 486,  352, 488,  352, 1302,  382, 1298,  378, 1302,  380, 1302,  376, 488,  352, 1302,  380, 1302,  378, 1300,  380, 486,  354, 486,  356, 462,  376, 488,  354, 486,  354, 486,  354, 486,  354, 486,  352, 488,  350, 1304,  378, 1302,  380, 1300,  378, 1302,  378, 1302,  378, 1304,  378, 1304,  378, 1302,  380, 486,  354, 488,  352, 488,  354, 486,  354, 488,  352, 488,  354, 486,  354, 486,  354, 1300,  380, 1302,  378, 1302,  378, 1302,  380, 1302,  380, 1304,  378, 1302,  378, 1302,  378, 1302,  378, 1304,  380, 1302,  378, 1302,  378, 1300,  378, 1302,  378, 1326,  354, 1300,  378, 486,  356, 486,  352, 464,  376, 488,  352, 488,  352, 488,  356, 486,  354, 486,  354, 1304,  378, 1302,  380, 1300,  380, 1300,  380, 1300,  380, 1302,  378, 1302,  380, 1302,  378, 486,  354, 486,  354, 486,  354, 486,  352, 466,  378, 486,  354, 486,  354, 486,  352, 1302,  380, 1302,  378, 1302,  378, 1304,  378, 1300,  380, 1300,  380, 1300,  380, 1300,  380, 462,  380, 484,  356, 462,  380, 484,  354, 488,  354, 486,  354, 486,  354, 462,  378, 1304,  376, 1302,  378, 1300,  380, 1302,  378, 1302,  378, 1302,  380, 1300,  380, 1300,  380, 486,  354, 462,  378, 464,  378, 462,  378, 462,  376, 462,  378, 462,  378, 462,  378};  // MULTIBRACKETS E0
uint64_t data = 0xE0;

ON/OFFそれぞれのRawデータをメモしておきます。

記事に載っている下記のコードをコメントアウトし、


irsend.sendSony(0xa90,12);

Rawデータを送る場合をコメントアウトして下記のように書き換えました。

irsend.sendRaw(rawData, 853, 38);  

Node-REDを使って、MQTT経由で操作できるようにする

こちらの記事を参考に、M5stickCでMQTTブローカーを使えるようにします。
ブローカーはshiftr.ioを使いました。

参考記事:M5StackとNode-REDをMQTTで連携するメモ

M5StickCのコード

#include <M5StickC.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

// Wi-FiのSSID
char *ssid = "";
// Wi-Fiのパスワード
char *password = "";
// MQTTの接続先のIP
const char *endpoint = "";
// MQTTのポート
const int port = 1883;
// デバイスID
char *deviceID = "M5Stack";  // デバイスIDは機器ごとにユニークにします
// メッセージを知らせるトピック
char *userName = "";              // MQTTブローカ認証ユーザ名
char *userPass = "";      // MQTTブローカ認証パスワード
char *pubTopic = "/pub/M5Stack";
// メッセージを待つトピック
char *subTopic = "/sub/M5Stack";
 
////////////////////////////////////////////////////////////////////////////////
   
WiFiClient httpsClient;
PubSubClient mqttClient(httpsClient);

//【M5StickC用】
int ir_recv_pin = 33;
//int ir_send_pin = 9;      // M5StickC自体に内蔵されている赤外線送信
int ir_send_pin = 32;        // M5StickCへGrove接続したIRユニットから赤外線送信するパターン

// sendSonyせずにsendRawする場合はこのrawDataを利用する
//off
uint16_t rawData[853] = {29996, 49886,  3326, 1714,  380, 1304,  376, 488,  352, 488,  350, 490,  352, 488,  350, 490,  352, 488,  352, 490,  350, 490,  348, 492,  352, 488,  352, 488,  350, 1330,  352, 490,  352, 490,  350, 490,  352, 492,  350, 490,  350, 490,  350, 490,  350, 490,  352, 490,  350, 490,  350, 490,  350, 490,  350, 492,  350, 490,  350, 490,  350, 490,  350, 492,  350, 1306,  376, 490,  348, 1332,  352, 1332,  348, 1308,  374, 1308,  374, 1308,  374, 1304,  378, 490,  350, 1330,  350, 1332,  350, 1308,  374, 1330,  350, 1330,  350, 1306,  376, 1330,  348, 1334,  374, 1308,  350, 490,  350, 492,  348, 492,  348, 518,  322, 492,  350, 492,  350, 492,  348, 492,  350, 492,  348, 492,  352, 1306,  374, 1306,  376, 490,  350, 490,  350, 1308,  372, 1334,  350, 1332,  352, 1330,  350, 468,  372, 492,  348, 1310,  374, 1330,  348, 492,  324, 518,  348, 492,  348, 1332,  348, 490,  348, 492,  348, 1308,  374, 492,  348, 492,  348, 1334,  346, 1308,  370, 494,  348, 1308,  374, 1332,  348, 468,  372, 1330,  324, 1358,  350, 490,  348, 1334,  348, 1334,  348, 492,  348, 492,  350, 1332,  350, 492,  348, 492,  346, 494,  348, 492,  322, 520,  348, 1334,  348, 1332,  348, 494,  350, 1306,  374, 1308,  372, 1332,  348, 494,  348, 492,  346, 494,  350, 1304,  376, 492,  350, 1330,  348, 1332,  350, 492,  348, 1334,  348, 1308,  376, 1330,  348, 518,  322, 1332,  350, 468,  370, 494,  348, 1332,  350, 490,  348, 492,  348, 492,  348, 518,  322, 492,  350, 492,  352, 490,  350, 490,  350, 1332,  350, 1332,  350, 1308,  370, 1334,  348, 1334,  346, 1310,  374, 1332,  350, 1332,  350, 490,  348, 494,  348, 492,  348, 494,  348, 494,  346, 492,  348, 492,  348, 492,  348, 1334,  350, 1332,  350, 1330,  350, 1332,  350, 1332,  322, 1358,  350, 1308,  376, 1330,  348, 494,  346, 492,  348, 494,  346, 494,  346, 494,  346, 494,  348, 492,  348, 492,  350, 1332,  348, 1330,  350, 1310,  372, 1332,  350, 1332,  350, 1330,  348, 1308,  372, 1332,  348, 492,  350, 490,  350, 490,  348, 494,  348, 490,  350, 492,  348, 492,  348, 492,  350, 1332,  350, 1330,  346, 1310,  372, 1334,  348, 1334,  348, 1332,  350, 1332,  348, 1330,  350, 492,  348, 470,  374, 492,  348, 494,  348, 492,  350, 492,  346, 494,  348, 494,  350, 1332,  350, 1332,  350, 1334,  346, 1312,  372, 1332,  350, 1308,  374, 1330,  348, 1332,  348, 492,  352, 1306,  374, 1332,  350, 492,  350, 1332,  350, 492,  348, 1334,  350, 492,  346, 1332,  350, 492,  348, 492,  348, 1334,  350, 492,  348, 1334,  350, 492,  348, 1310,  374, 1332,  348, 494,  350, 490,  352, 490,  348, 494,  346, 1332,  350, 1332,  350, 1332,  350, 494,  344, 1334,  348, 1332,  350, 1332,  350, 1308,  374, 492,  348, 490,  350, 492,  350, 492,  346, 492,  348, 492,  348, 492,  348, 494,  348, 492,  350, 492,  348, 492,  348, 1308,  374, 1332,  348, 1308,  374, 1332,  348, 1334,  348, 1310,  374, 1330,  350, 1332,  348, 492,  350, 490,  350, 492,  350, 492,  348, 492,  350, 492,  348, 490,  350, 492,  350, 1306,  374, 1332,  350, 1308,  376, 1330,  348, 1332,  348, 1330,  350, 1332,  350, 1330,  348, 492,  348, 492,  348, 468,  372, 492,  350, 490,  348, 492,  348, 492,  350, 1330,  350, 1308,  374, 1332,  350, 1332,  350, 1330,  350, 1334,  348, 1332,  348, 1332,  350, 490,  350, 1330,  352, 1330,  348, 494,  350, 492,  350, 490,  348, 492,  350, 494,  348, 492,  350, 490,  348, 494,  348, 1334,  350, 1332,  350, 1332,  350, 1330,  352, 1330,  350, 1332,  348, 1332,  350, 492,  348, 492,  348, 492,  350, 492,  348, 492,  348, 492,  348, 492,  350, 490,  348, 1332,  348, 1332,  348, 1310,  372, 1332,  348, 1332,  348, 1332,  348, 1332,  350, 492,  348, 492,  346, 494,  348, 1332,  322, 518,  350, 492,  348, 492,  346, 1332,  348, 1332,  348, 1332,  350, 1332,  350, 492,  348, 1332,  350, 1330,  348, 1332,  348, 494,  348, 492,  350, 494,  346, 494,  348, 492,  348, 494,  348, 492,  348, 492,  348, 492,  350, 1332,  348, 1310,  372, 1310,  370, 1332,  348, 1332,  348, 1332,  350, 1332,  350, 1332,  348, 492,  346, 494,  352, 488,  348, 492,  350, 490,  324, 518,  348, 492,  348, 468,  372, 1310,  372, 1332,  350, 1332,  350, 1306,  374, 1332,  350, 1330,  350, 1334,  348, 1332,  348, 1332,  348, 1332,  350, 1332,  348, 1332,  350, 1332,  348, 1308,  374, 1330,  350, 1308,  372, 492,  348, 494,  348, 492,  348, 494,  348, 492,  348, 492,  348, 492,  348, 494,  350, 1330,  350, 1330,  350, 1332,  348, 1334,  350, 1332,  348, 1332,  324, 1358,  324, 1358,  348, 492,  348, 494,  346, 494,  348, 492,  348, 492,  348, 492,  348, 492,  348, 494,  346, 1334,  348, 1334,  350, 1308,  372, 1332,  350, 1310,  370, 1308,  374, 1334,  346, 1334,  350, 494,  346, 494,  346, 470,  374, 492,  350, 492,  346, 494,  348, 492,  350, 492,  348, 1332,  350, 1334,  350, 1332,  350, 1332,  346, 1334,  348, 1332,  348, 1334,  350, 1332,  348, 492,  348, 494,  348, 470,  372, 492,  348, 492,  348, 494,  348, 494,  348, 494,  344}; 
//on
uint16_t rawData2[853] = {29938, 49882,  3322, 1744,  350, 1334,  346, 492,  346, 496,  322, 518,  348, 494,  348, 492,  348, 492,  348, 492,  348, 494,  348, 492,  350, 492,  348, 492,  348, 1334,  348, 490,  352, 492,  348, 492,  348, 494,  348, 494,  348, 520,  322, 492,  348, 492,  350, 490,  350, 492,  348, 494,  348, 494,  348, 494,  346, 494,  348, 492,  348, 494,  346, 492,  350, 1330,  350, 494,  348, 1308,  374, 1332,  350, 1334,  348, 1332,  350, 1332,  348, 1332,  350, 492,  348, 1334,  350, 1332,  348, 1308,  374, 1332,  348, 1332,  348, 1334,  348, 1332,  350, 1332,  350, 1332,  348, 494,  348, 492,  350, 492,  324, 518,  348, 492,  348, 494,  350, 492,  348, 490,  348, 492,  346, 470,  374, 1332,  350, 1334,  352, 490,  348, 492,  348, 1308,  374, 1330,  350, 1332,  348, 1332,  350, 492,  348, 494,  348, 1308,  372, 1332,  348, 494,  350, 490,  348, 492,  352, 1330,  350, 490,  346, 494,  348, 1332,  348, 492,  348, 494,  348, 1330,  348, 1332,  350, 490,  350, 1332,  350, 1306,  374, 492,  348, 1332,  350, 1308,  374, 492,  348, 1308,  372, 1332,  352, 492,  348, 490,  348, 1332,  350, 490,  350, 490,  346, 494,  350, 490,  350, 490,  350, 1304,  374, 1308,  374, 492,  350, 1308,  374, 1330,  352, 1330,  352, 492,  350, 490,  350, 490,  350, 1308,  376, 490,  352, 1304,  376, 1304,  374, 490,  350, 1308,  376, 1306,  376, 1328,  352, 490,  350, 1330,  352, 488,  352, 488,  352, 1306,  374, 490,  352, 490,  350, 490,  350, 490,  350, 466,  376, 490,  352, 488,  352, 488,  352, 1306,  376, 1306,  376, 1306,  376, 1306,  378, 1306,  376, 1306,  376, 1304,  378, 1304,  380, 462,  352, 490,  374, 488,  354, 466,  376, 488,  354, 488,  352, 488,  352, 488,  354, 1304,  378, 1304,  378, 1302,  378, 1306,  376, 1304,  380, 1302,  380, 1300,  382, 1302,  378, 488,  354, 464,  378, 464,  378, 486,  354, 464,  378, 488,  354, 464,  378, 488,  352, 1306,  380, 1302,  378, 1302,  382, 1300,  380, 1302,  380, 1302,  380, 1302,  380, 1302,  380, 462,  380, 462,  380, 462,  380, 462,  380, 462,  378, 462,  380, 464,  378, 462,  378, 1304,  382, 1300,  382, 1300,  380, 1302,  382, 1300,  382, 1302,  380, 1304,  380, 1300,  382, 462,  380, 462,  380, 462,  380, 460,  380, 462,  380, 460,  380, 462,  380, 464,  380, 1302,  382, 1300,  382, 1298,  382, 1300,  382, 1300,  382, 1302,  382, 1300,  382, 1300,  380, 462,  380, 1302,  382, 1300,  382, 460,  380, 1302,  382, 460,  382, 1302,  380, 462,  380, 1300,  382, 460,  382, 460,  380, 1300,  384, 460,  382, 1302,  382, 460,  382, 1300,  382, 1300,  380, 462,  382, 460,  380, 460,  380, 1302,  384, 1298,  384, 1300,  382, 1300,  382, 460,  382, 1300,  380, 1302,  380, 1302,  382, 460,  380, 462,  380, 460,  380, 462,  382, 458,  380, 460,  378, 462,  382, 460,  380, 462,  382, 460,  380, 460,  380, 460,  406, 1274,  382, 1300,  382, 1298,  382, 1300,  382, 1300,  380, 1302,  382, 1298,  380, 1300,  382, 460,  382, 460,  380, 462,  380, 460,  380, 462,  380, 464,  378, 460,  380, 460,  378, 1302,  382, 1300,  382, 1300,  382, 1300,  382, 1300,  382, 1300,  384, 1298,  382, 1298,  384, 460,  380, 460,  380, 460,  380, 460,  380, 462,  380, 460,  380, 460,  380, 1300,  382, 1300,  384, 1298,  382, 1300,  382, 1300,  382, 1300,  382, 1300,  380, 1302,  382, 460,  382, 1300,  382, 1300,  380, 460,  382, 460,  380, 462,  380, 460,  380, 460,  380, 462,  380, 460,  380, 460,  382, 1300,  380, 1300,  382, 1298,  384, 1298,  380, 1302,  382, 1298,  382, 1298,  382, 458,  382, 460,  380, 460,  380, 462,  380, 460,  380, 462,  380, 462,  380, 460,  378, 1302,  382, 1300,  382, 1304,  378, 1300,  384, 1298,  382, 1302,  382, 1300,  382, 460,  382, 458,  382, 460,  380, 1300,  382, 460,  380, 460,  380, 462,  382, 1298,  382, 1300,  382, 1300,  382, 1300,  382, 460,  380, 1300,  382, 1302,  382, 1300,  382, 460,  380, 460,  380, 460,  382, 460,  380, 460,  380, 462,  382, 458,  382, 460,  380, 460,  382, 1300,  382, 1300,  382, 1300,  382, 1298,  382, 1300,  380, 1302,  382, 1300,  382, 1300,  382, 460,  380, 460,  380, 460,  380, 460,  380, 460,  380, 460,  380, 460,  382, 458,  380, 1300,  382, 1300,  382, 1300,  382, 1298,  382, 1300,  382, 1298,  382, 1300,  382, 1298,  382, 1298,  384, 1300,  382, 1298,  382, 1300,  382, 1300,  382, 1300,  382, 1298,  408, 1274,  382, 460,  380, 460,  382, 460,  382, 460,  380, 460,  380, 460,  382, 458,  382, 460,  380, 1300,  380, 1302,  382, 1300,  380, 1300,  382, 1300,  382, 1302,  382, 1300,  382, 1298,  384, 460,  382, 460,  380, 460,  380, 462,  378, 462,  380, 460,  380, 460,  380, 460,  380, 1300,  384, 1298,  382, 1298,  382, 1300,  380, 1302,  382, 1300,  382, 1300,  380, 1300,  382, 462,  378, 462,  382, 460,  382, 460,  380, 460,  380, 462,  378, 462,  378, 462,  380, 1300,  382, 1300,  380, 1300,  382, 1300,  382, 1300,  382, 1300,  382, 1300,  382, 1302,  380, 462,  378, 462,  380, 460,  380, 460,  382, 460,  380, 462,  380, 462,  378, 462,  380}; 

IRsend irsend(ir_send_pin);

void setup() {
  Serial.begin(115200);
  M5.begin();
  pinMode(ir_recv_pin, INPUT);
  pinMode(ir_send_pin, OUTPUT);

  M5.Lcd.setTextSize(2);
  M5.Lcd.setCursor(0, 0);
  M5.Lcd.print("IR SENDER: ");

  // Start WiFi
  Serial.println("Connecting to ");
  Serial.print(ssid);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }

  // WiFi Connected
  Serial.println("\nWiFi Connected.");
  M5.Lcd.setCursor(10, 40);
  M5.Lcd.setTextColor(WHITE);
  M5.Lcd.setTextSize(3);
  M5.Lcd.printf("WiFi Connected.");
   
  mqttClient.setServer(endpoint, port);
  mqttClient.setCallback(mqttCallback);
 
  connectMQTT();
}

void loop() {
  mqttLoop();
  M5.update();

  if ( M5.BtnA.wasPressed() ) {
    for (int i = 0; i < 3; i++) {
      irsend.sendRaw(rawData2, 853, 38);  
    }

    M5.Lcd.setCursor(0, 25);
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.println("send!");

    delay(5000);    // どれだけ待つべき?とりあえず5秒
  }
  if ( M5.BtnB.wasPressed() ) {
    for (int i = 0; i < 3; i++) {
      irsend.sendRaw(rawData, 853, 38);   
    }
    
    M5.Lcd.fillScreen(BLACK);
  }

  delay(100);
}

void mqttCallback (char* topic, byte* payload, unsigned int length) {
 
    String str = "";
    Serial.print("Received. topic=");
    Serial.println(topic);
    for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
        str += (char)payload[i];
    }
    StaticJsonDocument<200> doc;
    DeserializationError error = deserializeJson(doc, str);
    
    if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return;
    }
    
    String sw = doc["switch"];
    
    Serial.print("\n");
    if(sw == "on"){
      irsend.sendRaw(rawData2, 853, 38); 
      Serial.println("onにした");
      }else if(sw == "off"){
       irsend.sendRaw(rawData, 853, 38);
       Serial.println("offにした");
      }
 
    delay(300);
     
}

void connectMQTT() {
    while (!mqttClient.connected()) {
        if (mqttClient.connect(deviceID,userName,userPass)) {
            Serial.println("Connected.");
            int qos = 0;
            mqttClient.subscribe(subTopic, qos);
            Serial.println("Subscribed.");
        } else {
            Serial.print("Failed. Error state=");
            Serial.println(mqttClient.state());
            errorReport();
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}
void mqttLoop() {
    if (!mqttClient.connected()) {
        connectMQTT();
    }
    mqttClient.loop();
}

void errorReport(){
  Serial.print("Failed. Error state = ");

  switch (mqttClient.state()) {
    case MQTT_CONNECT_UNAUTHORIZED:
      Serial.println("MQTT_CONNECT_UNAUTHORIZED");
      break;
    case MQTT_CONNECT_BAD_CREDENTIALS:
      Serial.println("MQTT_CONNECT_BAD_CREDENTIALS");
      break;
    case MQTT_CONNECT_UNAVAILABLE:
      Serial.println("MQTT_CONNECT_UNAVAILABLE");
      break;
    case MQTT_CONNECT_BAD_CLIENT_ID:
      Serial.println("MQTT_CONNECT_BAD_CLIENT_ID");
      break;
    case MQTT_CONNECT_BAD_PROTOCOL:
      Serial.println("MQTT_CONNECT_BAD_PROTOCOL");
      break;
    case MQTT_CONNECTED:
      Serial.println("MQTT_CONNECTED");
      break;
    case MQTT_DISCONNECTED:
      Serial.println("MQTT_DISCONNECTED");
      break;
    case MQTT_CONNECT_FAILED:
      Serial.println("MQTT_CONNECT_FAILED");
      break;
    case MQTT_CONNECTION_LOST:
      Serial.println("MQTT_CONNECTION_LOST");
      break;
    case MQTT_CONNECTION_TIMEOUT:
      Serial.println("MQTT_CONNECTION_TIMEOUT");
      break;
  }

  delay(5000); // Wait 5 seconds before retrying
}

Node-RED

Node-REDからはon/offをJSONで送ります。
スクリーンショット 2020-12-13 19.41.28.png

IMG_7394.JPG

次回は、injectノードをhttp inノードに変更して、LINEやWEBサイトから操作できるようにしたいと思います!

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?