2016-11-15 5 views
1
#include "SPI.h" 
#include “WiFiEsp.h” 
#include <WiFiEspClient.h> 
#include “SoftwareSerial.h” 
#include <PubSubClient.h> 
#include <WiFiEspUdp.h> 

float temp=0; 
int tempPin = 0; 
int isClientConnected = 0; 

char data[80]; 
char ssid[] = “SSID”; // your network SSID (name) 
char pass[] = “PASSWORD”; // your network password 

int status = WL_IDLE_STATUS; // the Wifi radio’s status 
char deviceName = “ArduinoClient1”; 

IPAddress server(xxx,xxx,xxx,xxx); //MQTT server IP 
IPAddress ip(192,168,43,200); 

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("-"); 
} 

// Emulate Serial1 on pins 6/7 if not present 
WiFiEspClient espClient; 
PubSubClient client(espClient); 

SoftwareSerial Serial1(6,7); // RX, TX 

void setup(){ 
    Serial.begin(9600); 
    Serial1.begin(9600); 
    WiFi.init(&Serial1); 
    WiFi.config(ip); 
    if (WiFi.status() == WL_NO_SHIELD) { 
     Serial.println("WiFi shield not present"); 
     while (true); 
    } 

    while (status != WL_CONNECTED) { 
     Serial.print("Attemptingonnect to WPA SSID: "); 
     Serial.println(ssid); 
     status = WiFi.begin(ssid, pass); 
     Serial.print("WiFius : "); 
     Serial.println(status); 
    } 

    //connect to MQTT server 
    client.setServer(server, 1883); 
    client.setCallback(callback); 
    isClientConnected = client.connect(deviceName); 
    Serial.println("+++++++"); 
    Serial.print("isClientConnected; 
    Serial.println(isClientConnected); 
    Serial.print("client.state"); 
    Serial.println(client.state()); 

    if (isClientConnected) { 
     Serial.println("Connected….."); 
     client.publish("status","Welcome to ISG"); 
     client.subscribe("isg/demoPublish/rpi/ardTempWarn"); 
     //Not able to recieve for this subscribed topic on Arduino Uno Only if I 
     //print it returns 1 

    } 
} 

void loop() { 
    temp = (5.0 * analogRead(tempPin) * 100.0)/1024; 
    Serial.print(" temp : "); 
    Serial.println(temp); 
    Serial.print("client.connected); 
    Serial.println(client.connected()); 
    if (!client.connected()) { 
      reconnect(); 
    } 
    client.publish("isg/demoPublish/ard1/tempLM35",String(temp).c_str()); 
    // able to receive data at other   
    // clients like RPI,Web using Mosquitto broker 

    client.loop(); 
    delay(5000); 
} 

void reconnect() { 
    Serial.println("Device is trying to connect to server "); 
    while (!client.connected()) { 
     if (client.connect(deviceName)) { 
     } else { 
      delay(5000); 
     } 
    } 
} 

私はArduino Uno R3とESP8266-01をwifiコネクタとして使用しています。 温度データを読んで、Mosquitto、MongoDB、Raspberry Piに送って、Arduinoでトピックを購読している特定の条件に関するデータを受け取る必要があります。 Arduinoから他のすべてのクライアントにデータを受け取ることができますが、Arduinoの購読トピックに関するデータを受け取ることができません。しかし、MongoDBのような他のすべての人は、Raspberry Piからデータを受け取ることができます。 私は、WiFiEsp.h、WiFiEspClient.h、WiFiEspUdp.h、SoftwareSerial.h、PubSubClient.hのデータを接続して送受信するために、Arduino Uno R3、ESP8266-01のデバイスとリベラルを使用しました。 client.subscribe( "topic") ; 1を返す コールバック関数も実装されていますが、呼び出しを取得できません。Arduino Uno R3のPubSubClient.hで購読メッセージを受信できません

Arduinoにサブスクライブされたトピックメッセージが届いていないのはなぜですか?

私は、したがって、私の好みは https://github.com/vshymanskyy/TinyGSM このライブラリは、SIM800(A、のほぼすべての変異体が含まれるだろうあなたが他のライブラリを使用するほうが良いでしょうhttps://sonyarouje.com/2016/03/15/mqtt-communication-with-arduino-using-esp8266-esp-01/#comment-111773

+0

これは正確に何を意味していますか? 「Arduinoから他のすべてのクライアントにデータを受け取ることができますが、ArduinoのSubscribedトピックに関するデータを受け取ることはできません。また、あなたの質問に沿ってあなたのコードを投稿してください。 –

+0

esp8266で見たすべてのmqttクライアントは、ATコマンドだけでなくespにコードが必要です – dandavis

+0

こんにちは、Arduinoで温度センサーを付けて、5秒遅れで温度を読み取り、Mosquittoに公開します ブローカーとMosquittoブローカー 温度データを購読しているMySQL、Raspberry Pi、およびWebクライアントにその温度データを送信します。私より60以上の温度上昇がラズベリーパイに注意を払わなければならない場合。 これはすべて正常に動作します。 – user3256309

答えて

0

使用しているライブラリは、コールバックのバグを持っているに従ってきましたC、L、H、808)、SIM900(A、D、908,968)、ESP8266(arduinoに実装されている)、イーサネットシールドなどの変種です。通信モード(GSM、イーサネット、WiFi)に関係なく、すべての購読メッセージを受信できる

+0

彼はPubSubClientライブラリを指しており、TinyGSMはknolearryのPubSubClientに依存しています。 GitHubにはlmroyの他のバージョンもあります。 – igraczech

関連する問題