-1
我一直試圖通過MQTT從我的Arduino發送消息到我的亞馬遜網絡服務器。以下代碼連接以太網客戶端,但不連接MQTT客戶端。爲什麼我的MQTT客戶端不能連接?可以通過以太網屏蔽連接,但不能在我的arduino上使用MQTT時連接
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
byte mac[] = { 0x12, 0x42, 0x98, 0x85, 0x49, 0x3A }; //MAC address of server
char server[] = "http://52.1.29.117/"; //web address of server
IPAddress ip(172, 31, 51, 13); //IP address of server
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
}
EthernetClient ethClient;
PubSubClient client(server, 80, callback, ethClient);
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
delay(1000);
Serial.println("connecting...");
if (ethClient.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
ethClient.println("GET /search?q=arduino HTTP/1.1");
ethClient.println("Host: www.google.com");
ethClient.println("Connection: close");
ethClient.println();
}
else {
Serial.println("connection failed");
}
// if (client.connect(server)) {
if (client.connect(server, "ubuntu", "")) {
Serial.print("Data sent/n");
client.publish("hello/world","hello world");
client.subscribe("hiWorld");
}
else {
Serial.print("nope");
}
}
void loop()
{
client.loop();
}
是否可以從不同的機器,例如連接到代理亞馬遜機器上與mosquitto_pub或mosquitto_sub? – hardillb 2015-03-31 08:26:05
@hardillb我無法用任何東西連接到Amazon Web服務器上的代理。它會自動在1883端口上運行mosquitto,並且我意識到現在我的端口是錯誤的,但它仍然沒有解決任何問題。我甚至試圖手動在另一個端口上運行蚊子,我也無法連接到那個。我設置了一個不同的服務器來解決這個問題,我的Arduino能夠連接到這個服務器。我認爲這可能與防火牆AWS的防火牆設置有關 – 2015-04-06 17:05:43