ESP8266にMQTTを喋らせてIoTサーバと通信するには、esp_mqttライブラリを使うのが良いと思われる。
https://github.com/tuanpmt/esp_mqtt
wget https://github.com/tuanpmt/esp_mqtt/archive/master.zip
include/user_config.hに各種ID/PWDや接続先サーバの定義行があるので環境に合わせて修正、ビルドしてファームを焼いて走らせてみた結果、一応Exception等は出ずに動いた。けどまだIoTサーバとTCPレベルでは接続が成功していない。。ソースを確認すると、ログに出ているSTATION_IDLEとはStation modeでWifiのAPと接続待ち状態に出るメッセージのようであった。なぜつながらないのだろうか。。
SDK ver: 1.4.0 compiled @ Sep 18 2015 20:55:49 phy ver: 484, pp ver: 9.5 load ... MQTT_InitConnection MQTT_InitClient call WIFI_Connect System started ... mode : sta(18:fe:34:fa:8d:9e) add if0 STATION_IDLE STATION_IDLE STATION_IDLE
■追記(160124)
esp_mqttライブラリを使ったサンプルコードはそのままではうまく走らずいろいろ手を加えてなんとかAWS上に走らせているMQTTサーバ(Mosquittoで実装)と通信できた。以下は通信した時のログ。ご参考にAWS側のIPもそのまま掲載してますが、お金をけちって普段はインスタンス落としてます(ですので繋がりません)。
今後はサンプルコードを理解して、IBMのBlueMixのIoTサーバと繋いでみる予定
TCP: Connect to ip 52.68.192.205:1883 MQTT: Connected to broker 52.68.192.205:1883 MQTT: Sending, type: 1, id: 0000 TCP: Sent TCP: data received 4 bytes MQTT: Connected to 52.68.192.205:1883 MQTT: Connected MQTT: queue subscribe, topic"/mqtt/topic/0", id: 1 MQTT: queue subscribe, topic"/mqtt/topic/1", id: 2 MQTT: queue subscribe, topic"/mqtt/topic/2", id: 3 MQTT: queuing publish, length: 23, queue size(66/2048) MQTT: queuing publish, length: 25, queue size(91/2048) MQTT: queuing publish, length: 25, queue size(118/2048) MQTT: Sending, type: 8, id: 0001 TCP: Sent TCP: data received 5 bytes MQTT: Subscribe successful MQTT: Sending, type: 8, id: 0002 TCP: Sent TCP: data received 5 bytes MQTT: Subscribe successful MQTT: Sending, type: 8, id: 0003 TCP: Sent TCP: data received 5 bytes MQTT: Subscribe successful MQTT: Sending, type: 3, id: 0000 TCP: Sent MQTT: Published TCP: data received 23 bytes Receive topic: /mqtt/topic/0, data: hello0 MQTT: Sending, type: 3, id: 0004 TCP: Sent MQTT: Published TCP: data received 4 bytes MQTT: received MQTT_MSG_TYPE_PUBACK, finish QoS1 publish MQTT: Sending, type: 3, id: 0005 TCP: Sent MQTT: Published TCP: data received 23 bytes Receive topic: /mqtt/topic/1, data: hello1 TCP: data received 4 bytes MQTT: Sending, type: 6, id: 0005 TCP: Sent TCP: data received 4 bytes TCP: data received 23 bytes Receive topic: /mqtt/topic/2, data: hello2 pm open phy_2,type:2 0 0 MQTT: Send keepalive packet to 52.68.192.205:1883! MQTT: Sending, type: 12, id: 0000 MQTT: Send keepalive packet to 52.68.192.205:1883! MQTT: Sending, type: 12, id: 0000 MQTT: Send keepalive packet to 52.68.192.205:1883! MQTT: Sending, type: 12, id: 0000