私はArduino UnoとWi-Fiシールドを使用しています。私は通信のためのMQTTプロトコルを実装しようとしており、Arduino用の2つのライブラリを試しました。最初はKnollearyのPubSubClient:http://pubsubclient.knolleary.net/です。私はイーサネットの代わりにWiFiモジュールを使用するために、元の例を少し変更しました。送信は動作しますが、毎回ではありません(メッセージが送信され、時には送信されない場合もあります)。しかし、コールバック機能を介して受信することは全く機能しません。あなたが見ることができるようにArduinoのMQTTが動作しない
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
- publishes "hello world" to the desired topic
- subscribes to the desired topic
*/
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "[DELETED]"; // your network SSID (name)
char pass[] = "[DELETED]"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Update these with values suitable for your network.
IPAddress server(85, 119, 83, 194);
WiFiClient WifiClient;
void callbackFunc(char* topic, byte* payload, unsigned int length) {
Serial.println("test message received");
/*Serial.println();
Serial.println("=============== MESSAGE RECEIVED ================================");
Serial.print("Topic: ");
Serial.print(topic);
Serial.println();
Serial.println((const char *) payload);*/
}
PubSubClient client(server, 1883, callbackFunc, WifiClient);
void setup()
{
delay(2000);
Serial.begin(9600);
Serial.println("Starting....");
Serial.println("Initializing Wifi...");
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
}
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if (status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// If you are connected, print out success message
else
Serial.println("Connected to network");
if (client.connect("arduinoClient")) {
Serial.println("Connected to server! Sending message...");
client.publish("randy/test","hello world");
client.subscribe("randy/test");
Serial.println("Sent!");
}
else
{
Serial.println("ERROR: Cannot connect to MQTT server!");
Serial.println(client.state());
}
}
void loop()
{
client.loop();
delay(1000);
if (!client.connected())
{
if(!client.connect("arduinoClient"))
{
Serial.println("ERROR: Cannot connect to MQTT server!");
Serial.println(client.state());
}
else
{
client.subscribe("randy/test");
Serial.println("INFO: reconnected!");
}
}
}
、私はテストのためhttp://test.mosquitto.org/使用しています:
は、ここに私のコードです。私は同じトピックを購読しているUbuntuでmosquittoを使用しています。同じトピックに公開している場所(別の端末ウィンドウ)からです。あなたが見ることができるように、Arduinoのからの「Hello World」のメッセージが(毎回ではなく)正常に受信され
、と私は別のウィンドウから「BLA」のメッセージの公開時:ここでは両方のウィンドウの画像ですそれはモスキート(画像上)で成功裏に受け取られましたが、Arduinoでは成功しませんでした。私のコードに何か問題がありますか?私はここで似たような問題を発見しました:Arduino Knolleary PubSubClient will publish messages but can't receive them
いつも再接続し続けることに気付く価値があります。ご覧のとおり、loop()では、接続が失われた場合に再度接続して購読するコードの一部を入れています。シリアルモニタで私は "情報:再接続!" 2〜3秒ごとにメッセージを送信します。
アダフールライブラリ 次に、私はAdafruitライブラリを試しましたが、まったく接続しません。私はtest.mosquitto.orgとio.adafruit.comを試してみましたが、 "Connection failed!"エラー。私は多くの変更と組み合わせを行ってみましたが、これまでは運がありませんでした。 「接続に失敗しました」の代わりに「購読に失敗しました」というメッセージが表示されたのですが、これは同じコードで一度しか次回には「接続に失敗しました」というメッセージが表示されます。
は、ここに私のコードです:
/*
Basic MQTT example with Authentication
- connects to an MQTT server, providing username
and password
- publishes "hello world" to the topic "outTopic"
- subscribes to the topic "inTopic"
*/
#include <WiFi.h>
#include <PubSubClient.h>
char ssid[] = "[DELETED]"; // your network SSID (name)
char pass[] = "[DELETED]"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Update these with values suitable for your network.
IPAddress server(85, 119, 83, 194);
WiFiClient WifiClient;
void callbackFunc(char* topic, byte* payload, unsigned int length) {
Serial.println("test message received");
/*Serial.println();
Serial.println("=============== MESSAGE RECEIVED ================================");
Serial.print("Topic: ");
Serial.print(topic);
Serial.println();
Serial.println((const char *) payload);*/
}
PubSubClient client(server, 1883, callbackFunc, WifiClient);
void setup()
{
delay(2000);
Serial.begin(9600);
Serial.println("Starting....");
Serial.println("Initializing Wifi...");
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
}
// attempt to connect using WPA2 encryption:
Serial.println("Attempting to connect to WPA network...");
status = WiFi.begin(ssid, pass);
// if you're not connected, stop here:
if (status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// Ff you are connected, print out success message
else
Serial.println("Connected to network");
if (client.connect("arduinoClient")) {
Serial.println("Connected to server! Sending message...");
client.publish("randy/test","hello world");
client.subscribe("randy/test");
Serial.println("Sent!");
}
else
{
Serial.println("ERROR: Cannot connect to MQTT server!");
Serial.println(client.state());
}
}
void loop()
{
client.loop();
delay(1000);
if (!client.connected())
{
if(!client.connect("arduinoClient"))
{
Serial.println("ERROR: Cannot connect to MQTT server!");
Serial.println(client.state());
}
else
{
client.subscribe("randy/test");
Serial.println("INFO: reconnected!");
}
}
}
これらのライブラリまたは私のコードで間違って何任意のアイデア?公共のブローカーにサンプルコードを使用するときは、コメント
で述べたように
可能であれば、できるだけ早く提案してください。そうでない場合は、client_id(現時点では "arduinoClient")をランダムに変更して、これらのパブリックブローカーを使用している可能性がある他の誰とも衝突しないようにしてください。 – hardillb
hardillbによれば、パブリックブローカーのarduinoClient衝突を起こし、切断/再接続のサイクルを引き起こす可能性があります。常に切断/再接続している場合、メッセージが欠落する可能性があります。一意のクライアントIDを選択して、もう一度お試しください。 – knolleary
まあ、私はそんなにばかげています:)私はクライアントのIDにも注意を払っていませんでした。正直言って、私はたぶん別の数十時間を費やしただろうし、おそらくこれについては決してないだろう。ありがとう、今それは魅力のように動作します!他の図書館があるので、私はそこにまだ質問を残します。私は大学のために何らかの研究をしていますので、それらの図書館を比較したいと思います。 PubSubClientライブラリの送信に費やされたメッセージのサイズと時間を確認することができますか? – XploD