可以通过以太网屏蔽连接,但不能在我的arduino上使用MQTT时连接

可以通过以太网屏蔽连接,但不能在我的arduino上使用MQTT时连接

问题描述:

我一直试图通过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(); 
} 
+0

是否可以从不同的机器,例如连接到代理亚马逊机器上与mosquitto_pub或mosquitto_sub? – hardillb 2015-03-31 08:26:05

+0

@hardillb我无法用任何东西连接到Amazon Web服务器上的代理。它会自动在1883端口上运行mosquitto,并且我意识到现在我的端口是错误的,但它仍然没有解决任何问题。我甚至试图手动在另一个端口上运行蚊子,我也无法连接到那个。我设置了一个不同的服务器来解决这个问题,我的Arduino能够连接到这个服务器。我认为这可能与防火墙AWS的防火墙设置有关 – 2015-04-06 17:05:43

IIRC AWS只打开了几个特定的​​端口,这可能会帮助你打开一些:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html