#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接收數據。 我已經使用Arduino Uno R3,ESP8266-01設備和自由來連接和發送/接收數據WiFiEsp.h,WiFiEspClient.h,WiFiEspUdp.h,SoftwareSerial.h,PubSubClient.h client.subscribe(「topic」) ;返回1 也實現了回調函數,但無法撥打電話。無法接收Arduino Uno R3中PubSubClient.h的訂閱消息
那麼,任何人都可以幫助我爲什麼我沒有在Arduino中獲得訂閱的主題消息嗎?
這是什麼意思? 「我能夠從Arduino接收所有其他客戶端的數據,但我無法接收Arduino中訂閱主題的數據。」另外請將您的代碼與您的問題一起發佈,以便您可以更輕鬆地獲得幫助。 –
我所見過的所有mqtt客戶端esp8266都需要esp代碼,而不僅僅是AT命令 – dandavis
嗨,我已附加Arduino溫度傳感器,它讀取溫度並延遲5秒,併發布到Mosquitto 代理商和Mosquitto代理商 將溫度數據發送給訂購了 溫度數據的MySQL,Raspberry Pi和web客戶端。如果溫度升高超過60度,我必須對樹莓派保持警惕。 這一切工作正常。 – user3256309