0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MQTT を受信して ボードの LED の点灯色を変える

Last updated at Posted at 2021-08-11

IMG_20210811_102621.jpg

こちらのプログラムを改造しました。
Grove IoT スターターキット for SORACOM で MQTT クライアントを使う

フォルダー構造

$ tree mqtt_led/
mqtt_led/
├── mqtt_led.ino
├── setupLTE.ino
└── setup_mqtt.ino

プログラム

mqtt_led.ino
// ---------------------------------------------------------------
/*
	mqtt_led.ino

					Aug/20/2021
*/
// ---------------------------------------------------------------
#include <WioLTEforArduino.h>
#include <WioLTEClient.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <stdio.h>

#define MQTT_SERVER_HOST  "example.com"
#define MQTT_SERVER_PORT  (1883)

#define ID                "WioLTE"
#define OUT_TOPIC         "sample/imageTopic"
#define IN_TOPIC          "sample/imageTopic"

#define INTERVAL          (60000)

WioLTE Wio;
WioLTEClient WioClient(&Wio);
PubSubClient MqttClient;

DynamicJsonDocument doc(1024);

int icount = 0;
// ---------------------------------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
	SerialUSB.println("");
	payload[length] = '\0';
	String msg = String((char*) payload);
	SerialUSB.println(msg);
	deserializeJson(doc, msg);	
	const char* led = doc["led"];
	SerialUSB.println(led);
	
	if (strstr(led,"green")) {	
		SerialUSB.println("*** green ***");
		Wio.LedSetRGB(0, 1, 0); 
		}
	else if (strstr(led,"yellow")) {	
		SerialUSB.println("*** yellow ***");
		Wio.LedSetRGB(1, 1, 0); 
		}
	else if (strstr(led,"red")) {	
		SerialUSB.println("*** red ***");
		Wio.LedSetRGB(1, 0, 0);
		}
	else if (strstr(led,"white")) { 
		SerialUSB.println("*** white ***");
		Wio.LedSetRGB(1, 1, 1);
		}
}

// ---------------------------------------------------------------
void setup() {
	setupLTE();

	setup_mqtt_proc();

	Wio.LedSetRGB(1, 1, 1); 
	SerialUSB.println("*** Setup completed *** Aug/20/2021 AM 09:54 ***");
}


// ---------------------------------------------------------------
void loop()
{
	DynamicJsonDocument doc(512);
	char data_json[128];

	if ((icount % 1000) == 0)
		{
		doc["icount"] = icount;
		doc["uptime"] = millis() / 1000;
		serializeJson(doc, data_json);

		SerialUSB.println(data_json);
		}

	MqttClient.loop();

	icount++;
}

// ---------------------------------------------------------------

setupLTE.ino はこちら
UDP で温度と湿度を Harvest に送る

setup_mqtt.ino はこちら
[ブラウザーから MQTT Publish でブザーを鳴らす](setupLTE.ino はこちら)

緑を点灯させるコマンド

go_green.sh
BROKER="broker.emqx.io"
#
echo $BROKER
message='{"led": "green"}'
mosquitto_pub -d -t orz -m "${message}" \
	-h  $BROKER --topic example/testTopic

黄色を点灯させるコマンド

go_yellow.sh
BROKER="broker.emqx.io"
#
echo $BROKER
message='{"led": "yellow"}'
mosquitto_pub -d -t orz -m "${message}" \
    -h  $BROKER --topic example/testTopic

赤を点灯させるコマンド

go_red.sh
BROKER="broker.emqx.io"
#
echo $BROKER
message='{"led": "red"}'
mosquitto_pub -d -t orz -m "${message}" \
    -h  $BROKER --topic example/testTopic

Arduino IDE

image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?