WeMosを使ってみる

RaspberryとのMQTT通信(Raspberry→WeMos)

mosquitto-clients編


これまでは、
WeMos(Publisher)--->Broker--->Raspberry(Subscriber)
を紹介しましたが、ここからは
Raspberry(Publisher)--->Broker--->WeMos(Subscriber)
を紹介します。

必要なライブラリなどはこちらのページを参照してください。

WeMos側のスケッチは以下の通りです。
MQTTクライアントライブラリに含まれている「pubsubclient\examples\mqtt_esp8266」を元に作りました。
「nopnop2002」で始まるトピックだけを読み込みます。
SSIDとPASSWORDは自分の環境に合わせて変更してください。

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define SSID            "APのSSID"
#define PASSWORD        "APのパスワード"
//#define MQTT_SERVER     "192.168.10.40"
//#define MQTT_SERVER     "broker.mqtt-dashboard.com"
#define MQTT_SERVER     "broker.hivemq.com"
#define MQTT_PORT       1883
#define MQTT_TOPIC      "nopnop2002/#" // You can change

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {

  delay(10);

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Wait for WiFi...");
  WiFi.begin(SSID, PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }
}

void errorDisplay(char* buff) {
  Serial.print("Error:");
  Serial.println(buff);
  int led = 0;
  while(1) {
    digitalWrite(BUILTIN_LED,led);
    led = ~led;
    delay(200);
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(BUILTIN_LED, OUTPUT);
  setup_wifi();
  client.setServer(MQTT_SERVER, MQTT_PORT);
  client.setCallback(callback);

  char clientid[20];
  sprintf(clientid,"ESP8266-%06x",ESP.getChipId());
  Serial.print("clientid=");
  Serial.println(clientid);
  Serial.print("Attempting MQTT connection...");
  // Attempt to connect
  if (!client.connect(clientid)) {
    errorDisplay("connect Fail");
  }
  Serial.println("connected");

  if (!client.subscribe(MQTT_TOPIC)) {
    errorDisplay("subscribe Fail");
  }

}

void loop() {
  client.loop(); // Receive MQTT Event
}


このスケッチを実行するとArduino-IDEのシリアルモニターはこのように表示されます。


ここでRaspberry側から以下のコマンドを実行します。
$ mosquitto_pub -h broker.hivemq.com -p 1883 -t "nopnop2002" -m "1"
$ mosquitto_pub -h broker.hivemq.com -p 1883 -t "nopnop2002" -m "0"

WeMos側に以下が表示され、オンボードのLEDがLチカします。


ちなみに、WeMosのスケッチを以下のように変更すると、
このサーバーにPublishされる全てのトピックを見ることができます。
(前略)

//#define MQTT_TOPIC      "nopnop2002/#" // You can change
#define MQTT_TOPIC      "#" // You can change

(後略)



世界中の人が色々Publishしています。

次は、Raspberry側のPublisherをPythonで作ってみます。

続く....