# MQTTキープアライブ時間内のping送信

**URL:** https://forum.ficusonline.com/t/mqtt-ping/86
**Category:** ESP
**Created:** [2017 年 3 月 5 日午前 10:35 UTC](https://forum.ficusonline.com/t/mqtt-ping/86 "2017-03-05T10:35:16Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2017 年 3 月 5 日午前 10:35 UTC](https://forum.ficusonline.com/t/mqtt-ping/86/1 "2017-03-05T10:35:16Z")

</div>

MQTTキープアライブについて

ping送信を意図的に送信しなくても、キープアライブ間隔で接続が切断されても、その都度再接続するようにコードを書けば特に問題になることはないと思います。

> **[What Is MQTT Keep Alive and Client Take-Over? – MQTT Essentials Part 10](https://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over/)**
>
> In this blog post we will show you how to make sure the other end is still available and connected. MQTT works perfectly in unreliable networks.

MQTTキープアライブ設定時間内にクライアントからのデータパブリッシュがなければ、ブローカーが接続を自動的に切断します。クライアントとブローカーのどちらかが非アクティブになった際のために必要な設定ですが、クライアントが頻繁にパブリッシュしない場合は設定時間毎に再接続する必要が生じてしまうため、クライアントからのpingをこの設定時間内に送信することで再接続プロセスを回避できます（特別必要な処置ではないようです）。

Arduinoの場合などは下記コマンドを追加して下さい。

// ping the server to keep the mqtt connection alive  
// NOT required if you are publishing once every KEEPALIVE seconds

if(! mqtt.ping()) {  
mqtt.disconnect();  
}
