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?

お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

shadowとwillとretainを活用してAWS IoT CoreとPubSubする⑩mosquittoでshadow update

Last updated at Posted at 2024-07-05

この記事について

shadowとwillとretainを活用してAWS IoT CoreとPubSubする⑨mosquittoでpubsubで行ったmosquittoでのmessage pubsubに続いてshadowをupdate/getします。

今回もラズパイに接続された仮想device_0の正常稼働とdevice停止をAWS IoTのdevice shadowに反映させます。


shadowのtopic

paho-mqttAWS IoT Device SDK for Pythonで扱ったと同じく、/getと/updateの操作をします。

shadow update

/update にdesiredとreportedのstateをラズパイからpubすると、AWS IoT Coreより各項目のレスポンスがtopicにpubされます。

名前 アクション MQTT トピック
/update 発行 $aws/things/MQTTServer1/shadow/name/device_0/update 
/update/delta サブスクライブ $aws/things/MQTTServer1/shadow/name/device_0/update/delta 
/update/accepted サブスクライブ $aws/things/MQTTServer1/shadow/name/device_0/update/accepted 
/update/documents サブスクライブ $aws/things/MQTTServer1/shadow/name/device_0/update/documents 
/update/rejected サブスクライブ $aws/things/MQTTServer1/shadow/name/device_0/update/rejected 

shadow get

/get に空のtopicをラズパイからpubすると、AWS IoT Coreより各項目のレスポンスがtopicにpubされます。

名前 アクション MQTT トピック
/get 発行 $aws/things/MQTTServer1/shadow/name/device_0/get 
/get/accepted サブスクライブ $aws/things/MQTTServer1/shadow/name/device_0/get/accepted 
/get/rejected サブスクライブ $aws/things/MQTTServer1/shadow/name/device_0/get/rejected 

/etc/mosquitto/conf.d/bridge.confにtopic追加

shadow update/getに使うtopicを/etc/mosquitto/conf.d/bridge.confに追加します。

/etc/mosquitto/conf.d/bridge.conf
#以下を追加
topic $aws/things/MQTTServer1/shadow/name/# both 1

shadow update

今まで同様にstate、payload、topicを準備します。pubの前にコマンドラインから入力します。

先にsubscriberを立ち上げておきます。topic一つずつ確認します。
payloadに改行が入るのでsub側のスクリプトを対応させておきます。

sub
THING_NAME=$(grep 'THING_NAME' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
PROJECT_NAME=$(grep 'PROJECT_NAME' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
password=$(grep 'MOS_PW' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
username=$(grep 'MOS_USR' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')

#topics
topic="\$aws/things/${THING_NAME}/shadow/name/+/update/accepted"
#topic="\$aws/things/${THING_NAME}/shadow/name/+/update/rejected"
#topic="\$aws/things/${THING_NAME}/shadow/name/+/update/documents"
#topic="\$aws/things/${THING_NAME}/shadow/name/+/update/delta"
echo $topic

# Subscribe to the MQTT topic and decode the payload
mosquitto_sub -h 127.0.0.1 -u "${username}" -P "${password}" -t "$topic" -v | while read -r topic payload; do
  echo "Received message on topic: $topic"
  echo "Payload: $payload"
  
  # Decode the payload (assuming JSON)
  decoded_payload=$(echo "$payload" | jq .)
  echo "Decoded Payload: $decoded_payload"
done

stateは今まで同様に行きますヨシッ👉

pub
desired_state="ヨシッ👉"
state_ok="ヨシッ👉"
state_ng="ダメ🙅"
state=$state_ok

payload="{\"state\": {\"desired\": {\"device_no\": \"device_0\",\"state\": \"${desired_state}\"},\"reported\": {\"device_no\": \"device_0\",\"state\": \"${state}\"}}}"
echo $payload

topic="\$aws/things/${THING_NAME}/shadow/name/device_0/update"
echo $topic

pubします。

pub
mosquitto_pub -h 127.0.0.1 -u ${username} -P ${password} -t $topic -m "$payload"

OK state

各topicへ返されたレスポンスです。
成功しているので/rejectedにはレスポンスなし、OKなので/deltaへもレスポンスがpubされてません。

/update/accepted
Received message on topic: $aws/things/MQTTServer1/shadow/name/device_0/update/accepted
Payload: {"state":{"desired":{"device_no":"device_0","state":"ヨシッ\uD83D\uDC49"},"reported":{"device_no":"device_0","state":"ヨシッ\uD83D\uDC49"}},"metadata":{"desired":{"device_no":{"timestamp":1720176208},"state":{"timestamp":1720176208}},"reported":{"device_no":{"timestamp":1720176208},"state":{"timestamp":1720176208}}},"version":643,"timestamp":1720176208}
Decoded Payload: {
  "state": {
    "desired": {
      "device_no": "device_0",
      "state": "ヨシッ👉"
    },
    "reported": {
      "device_no": "device_0",
      "state": "ヨシッ👉"
    }
  },
  "metadata": {
    "desired": {
      "device_no": {
        "timestamp": 1720176208
      },
      "state": {
        "timestamp": 1720176208
      }
    },
    "reported": {
      "device_no": {
        "timestamp": 1720176208
      },
      "state": {
        "timestamp": 1720176208
      }
    }
  },
  "version": 643,
  "timestamp": 1720176208
}
/update/documents
Received message on topic: $aws/things/MQTTServer1/shadow/name/device_0/update/documents
Payload: {"previous":{"state":{"desired":{"device_no":"device_0","state":"ヨシッ\uD83D\uDC49"},"reported":{"device_no":"device_0","state":"ヨシッ\uD83D\uDC49"}},"metadata":{"desired":{"device_no":{"timestamp":1720176989},"state":{"timestamp":1720176989}},"reported":{"device_no":{"timestamp":1720176989},"state":{"timestamp":1720176989}}},"version":644},"current":{"state":{"desired":{"device_no":"device_0","state":" ヨシッ\uD83D\uDC49"},"reported":{"device_no":"device_0","state":"ヨシッ\uD83D\uDC49"}},"metadata":{"desired":{"device_no":{"timestamp":1720177007},"state":{"timestamp":1720177007}},"reported":{"device_no":{"timestamp":1720177007},"state":{"timestamp":1720177007}}},"version":645},"timestamp":1720177007}
Decoded Payload: {
  "previous": {
    "state": {
      "desired": {
        "device_no": "device_0",
        "state": "ヨシッ👉"
      },
      "reported": {
        "device_no": "device_0",
        "state": "ヨシッ👉"
      }
    },
    "metadata": {
      "desired": {
        "device_no": {
          "timestamp": 1720176989
        },
        "state": {
          "timestamp": 1720176989
        }
      },
      "reported": {
        "device_no": {
          "timestamp": 1720176989
        },
        "state": {
          "timestamp": 1720176989
        }
      }
    },
    "version": 644
  },
  "current": {
    "state": {
      "desired": {
        "device_no": "device_0",
        "state": "ヨシッ👉"
      },
      "reported": {
        "device_no": "device_0",
        "state": "ヨシッ👉"
      }
    },
    "metadata": {
      "desired": {
        "device_no": {
          "timestamp": 1720177007
        },
        "state": {
          "timestamp": 1720177007
        }
      },
      "reported": {
        "device_no": {
          "timestamp": 1720177007
        },
        "state": {
          "timestamp": 1720177007
        }
      }
    },
    "version": 645
  },
  "timestamp": 1720177007
}

NG state

次にNG stateを見ます。

pub state
desired_state="ヨシッ👉"
state_ok="ヨシッ👉"
state_ng="ダメ🙅"
state=$state_ng

pubします。

pub
mosquitto_pub -h 127.0.0.1 -u ${username} -P ${password} -t $topic -m "$payload"

/deltaへのレスポンスが確認できました。

/update/delta
Received message on topic: $aws/things/MQTTServer1/shadow/name/device_0/update/delta
Payload: {"version":647,"timestamp":1720177393,"state":{"state":"ヨシッ\uD83D\uDC49"},"metadata":{"state":{"timestamp":1720177393}}}
Decoded Payload: {
  "version": 647,
  "timestamp": 1720177393,
  "state": {
    "state": "ヨシッ👉"
  },
  "metadata": {
    "state": {
      "timestamp": 1720177393
    }
  }
}

shadow get

こちらは/getに対して/get/acceptedと/get/rejectedのみです。/update同様にpubが成功していると/rejectedはpubされません。

先にsubscriberを立ち上げておきます。

sub
THING_NAME=$(grep 'THING_NAME' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
PROJECT_NAME=$(grep 'PROJECT_NAME' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
password=$(grep 'MOS_PW' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
username=$(grep 'MOS_USR' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')

#topics
topic="\$aws/things/${THING_NAME}/shadow/name/+/get/accepted"
#topic="\$aws/things/${THING_NAME}/shadow/name/+/get/rejected"
echo $topic

# Subscribe to the MQTT topic and decode the payload
mosquitto_sub -h 127.0.0.1 -u "${username}" -P "${password}" -t "$topic" -v | while read -r topic payload; do
  echo "Received message on topic: $topic"
  echo "Payload: $payload"
  
  # Decode the payload (assuming JSON)
  decoded_payload=$(echo "$payload" | jq .)
  echo "Decoded Payload: $decoded_payload"
done

pubします。

pub
THING_NAME=$(grep 'THING_NAME' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
PROJECT_NAME=$(grep 'PROJECT_NAME' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
password=$(grep 'MOS_PW' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')
username=$(grep 'MOS_USR' ./parameters.conf | awk -F'=' '{print $2}' | tr -d '\r\n')

payload=""
echo $payload

topic="\$aws/things/${THING_NAME}/shadow/name/device_0/get"
echo $topic

mosquitto_pub -h 127.0.0.1 -u ${username} -P ${password} -t $topic -m "$payload"

OK state

/get/accepted:OK
Received message on topic: $aws/things/MQTTServer1/shadow/name/device_0/get/accepted
Payload: {"state":{"desired":{"device_no":"device_0","state":"ヨシッ\uD83D\uDC49"},"reported":{"device_no":"device_0","state":"ヨシッ\uD83D\uDC49"}},"metadata":{"desired":{"device_no":{"timestamp":1720177763},"state":{"timestamp":1720177763}},"reported":{"device_no":{"timestamp":1720177763},"state":{"timestamp":1720177763}}},"version":648,"timestamp":1720177774}
Decoded Payload: {
  "state": {
    "desired": {
      "device_no": "device_0",
      "state": "ヨシッ👉"
    },
    "reported": {
      "device_no": "device_0",
      "state": "ヨシッ👉"
    }
  },
  "metadata": {
    "desired": {
      "device_no": {
        "timestamp": 1720177763
      },
      "state": {
        "timestamp": 1720177763
      }
    },
    "reported": {
      "device_no": {
        "timestamp": 1720177763
      },
      "state": {
        "timestamp": 1720177763
      }
    }
  },
  "version": 648,
  "timestamp": 1720177774
}

NG state

deltaが確認できます。

/get/accepted:NG
Received message on topic: $aws/things/MQTTServer1/shadow/name/device_0/get/accepted
Payload: {"state":{"desired":{"device_no":"device_0","state":"ヨシッ\uD83D\uDC49"},"reported":{"device_no":"device_0","state":"ダメ\uD83D\uDE45"},"delta":{"state":"ヨシッ\uD83D\uDC49"}},"metadata":{"desired":{"device_no":{"timestamp":1720177393},"state":{"timestamp":1720177393}},"reported":{"device_no":{"timestamp":1720177393},"state":{"timestamp":1720177393}}},"version":647,"timestamp":1720177706}
Decoded Payload: {
  "state": {
    "desired": {
      "device_no": "device_0",
      "state": "ヨシッ👉"
    },
    "reported": {
      "device_no": "device_0",
      "state": "ダメ🙅"
    },
    "delta": {
      "state": "ヨシッ👉"
    }
  },
  "metadata": {
    "desired": {
      "device_no": {
        "timestamp": 1720177393
      },
      "state": {
        "timestamp": 1720177393
      }
    },
    "reported": {
      "device_no": {
        "timestamp": 1720177393
      },
      "state": {
        "timestamp": 1720177393
      }
    }
  },
  "version": 647,
  "timestamp": 1720177706
}

次回

いよいよシリーズ最終回です。
shadowとwillとretainを活用してAWS IoT CoreとPubSubする⑪mosquittoでretainとwill

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?