arduino / cloud-examples

Examples for Arduino Cloud
11 stars 4 forks source link

Can't connect to personal broker, Azure IoT Hub or Arduino from MKR1000 #3

Closed AdamMiltonBarker closed 8 years ago

AdamMiltonBarker commented 8 years ago

Hi guys I am having issues connecting to my personal broker, Azure IoT Hub and Arduino. With Azure and my personal broker it was failing after about 5 minutes until I imported the SSL certs (on a MKR1000), now the certs are there it just hangs forever and crashes the serial monitor with exception to Arduino which just fails after about 5 mins. Any ideas please. Just using this but taken out the pin code etc.

https://github.com/arduino/cloud-examples/blob/master/mqtt/mqtt_send_digital/mqtt_send_digital.ino

AdamMiltonBarker commented 8 years ago

This issue is still happening guys any ideas? I can connect via HTTP using SSL just not the MQTT library.

sandeepmistry commented 8 years ago

@AdamMiltonBarker I just tried this out with the Arduino broker and a MKR1000 left it running for 10 minutes, everything seems ok.

More info on my environment:

it just hangs forever and crashes the serial monitor with exception to Arduino which just fails after about 5 mins.

Could you please provide more details on this?

AdamMiltonBarker commented 8 years ago

Hi thank you for reply as soon as it loads up it just freezes, the same connections work fine from the MKR1000 using HTTP methods I was thinking maybe at one point there was an issue with the Wifi101 but I resolved the issues with connecting via HTTP, basically I load it up it connects to the wifi then it does nothing for about 5 minutes freezes and I have to force shut the serial monitor.

sandeepmistry commented 8 years ago

as it loads up it just freezes,

This is still quite vague, please provide more details. For example, sketch you are running and serial monitor output. Also, provide the library versions you are using.

Have you re-flashed the arduino.cc certificate using the WiFi101-FirmwareUpdate tool?

Are you saying HTTPS is fine, but MQTT + SSL doesn't work?

AdamMiltonBarker commented 8 years ago

i can't really say anymore as that is exactly what happens, when I start the serial monitor it connects to the wifi and displays the connection info as it is suppose to the next thing that is printed out is "connecting" and then it is either meant to say "Connected"or "Connection refused" which it does neither it just freezes and crashes the serial monitor.

Yes I have re-flashed and double checked the SSL is imported by reuploading the SSL cert to the MKR100. Yes it works fine with HTTPS, I have actually completed that side of the project using HTTPS, but MQTT and SSL doesn't work, would still like to figure out what is going as would like to offer users the option of using HTTP or MQTT.

AdamMiltonBarker commented 8 years ago

I still have this issue it totally freezes up and will not connect to Azure, again this is working with HTTPS connection on the Arduino can you offer any advice?

/*
 This example connects to a WPA encrypted WiFi network,
 using the WiFi Shield 101.
 Then connects to the Arduino Cloud MQTT broker using 
 SSL, and sends updates on the status of an LED which
 is controlled by a button.
 Circuit:
 * WiFi shield attached
 * LED
 * button
 */

#include <SPI.h>
#include <WiFi101.h>
#include <WiFiSSLClient.h>

// include PubSubClient library
// https://github.com/knolleary/pubsubclient
#include <PubSubClient.h>

char ssid[] = "WIFISSIF"; //  your network SSID (name)
char password[] = "WIFIPASS";  

// MQTT settings
char mqttServer[]     = "AZUREHOSTNAME";
int  mqttPort         = 8883;
char mqttClientName[] = "AZUREDEVICEDID";
char mqttUsername[]   = "HOSTNAME/DEVICEID";       // your MQTT username
char mqttPassword[]   = "SHAREDACCESSSIGNATURE";       // your MQTT password
char mqttTopic[]      = "DEVICEBOUNDTOPIC";  // your MQTT topic /<username>/topic

// You can also access the MQTT broker service
// using the same set of credentials using
// WebSockets via the following URI:
//
//   wss://mqtt.arduino.cc:9002/
//
// This would allow you to subscribe and publish
// to MQTT topics using a web browser.

// constants won't change. They're used here to

// variables will change:
int wifiStatus        = WL_IDLE_STATUS;  // the Wifi radio's status

// Initialize the WiFi SSL client library
WiFiSSLClient wifiSSLClient;

// Initialize the PubSubClient
PubSubClient mqttClient(mqttServer, mqttPort, wifiSSLClient);

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  setupWiFi();

  Serial.println(F("Connecting to MQTT broker ..."));
  if (mqttClient.connect(mqttClientName, mqttUsername, mqttPassword)) {
    Serial.println(F("Connected :D"));
  } else {
    Serial.println(F("Connection failed :("));
    // don't continue:
    while (true);
  }

}

void setupWiFi() {
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println(F("WiFi shield not present"));
    // don't continue:
    while (true);
  }

  Serial.print("Firmware version is ");
  Serial.println(WiFi.firmwareVersion());

  // attempt to connect to Wifi network:
  while (wifiStatus != WL_CONNECTED) {
    Serial.print(F("Attempting to connect to WPA SSID: "));
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    wifiStatus = WiFi.begin(ssid, password);

    if (wifiStatus != WL_CONNECTED) {
      // wait 10 seconds for next connection attempt
      delay(10000);
    }
  }

  Serial.println(F("Connected to wifi"));

  Serial.print(F("SSID: "));
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print(F("IP Address: "));
  Serial.println(ip);

  Serial.print(F("signal strength (RSSI):"));
  Serial.print(WiFi.RSSI());
  Serial.println(F(" dBm"));
}

void loop() {
  // let the MQTT client process events
  mqttClient.loop();
  mqttClient.publish(mqttTopic, "yes");
}

Gets stuck atL

  if (mqttClient.connect(mqttClientName, mqttUsername, mqttPassword)) {
    Serial.println(F("Connected :D"));
  } else {
    Serial.println(F("Connection failed :("));
    // don't continue:
    while (true);
  }

does not print out connected or fails just jams up the serial monitor you have to click many times to close to the serial monitor.

sandeepmistry commented 8 years ago

@AdamMiltonBarker I suggest you re-flash the cloud.arduino.cc root certificate on the shield, and make sure that is working as a baseline. Note: we have modified the credentials needed, please see https://cloud.arduino.cc/ to create new ones.

Once you confirm this is working, we'll need to narrow down the line in PubSubClient library that triggers the "hanging". It could be related to https://github.com/arduino-libraries/WiFi101/issues/48.

AdamMiltonBarker commented 8 years ago

Hi I have moved on from this project now and I am now attempting to connect to my own broker, I have uploaded the SSL multiple times and I am still getting connection failed. On my broker this is the info provided:

1460342331: New connection from ##.##.##.### on port 8883. 1460342332: OpenSSL Error: error:140940E5:SSL routines:SSL3_READ_BYTES:ssl handshake failure 1460342332: Socket error on client <unknown>, disconnecting.

sandeepmistry commented 8 years ago

@AdamMiltonBarker I've spent sometime figuring out what the issue with Azure was.

If you look inside PubSubClient.h you can see the default buffer size is #define MQTT_MAX_PACKET_SIZE 128.

My SHAREDACCESSSIGNATURE had a size of 138 bytes, so the library will overflow the buffer. I changed this to #define MQTT_MAX_PACKET_SIZE 1280 just to be on the safe side and made a successful connection.

cc/ @akash73

sandeepmistry commented 8 years ago

@AdamMiltonBarker regarding connection to your personal broker.

I'm wondering if your broker has a cipher setup that the WINC1500 does not support as mentioned in https://github.com/arduino-libraries/WiFi101/issues/48.

AdamMiltonBarker commented 8 years ago

Web sockets are not enabled on the MQTT broker so it will not work, I have to rebuild the broker with websockets enabled, for now I am just working on connecting with MQTT, Arduino libraries I have found including this one is the only ones that struggle. Python paho / Node JS all connect perfectly.

The broker is mosquitto. steps to reproduce are exactly as the code above. Changing the size of packet allowed me to access Azure but moved away from that project a while ago, now I am creating samples for developers using my developer program but struggling to get Arduino and actually ESP8266 connecting to the broker. Arduino only tested on MKR1000 at this point.

AdamMiltonBarker commented 8 years ago

Ok info from the broker:

1460928836: OpenSSL Error: error:140940E5:SSL routines:SSL3_READ_BYTES:ssl handshake failure 1460928836: Socket error on client , disconnecting.

Which suggests issue with the SSL I had this issue on the Intel Galileo and updating the date and time of the board solved it but have not found how to update this on the MKR1000 to see if this is causing the issue which reminds me I posted about it on Arduino forum will see if anyone replied.

AdamMiltonBarker commented 8 years ago

No just going round in circles, that topic points back to here, so as this is working with Python and Node JS it is something related to the MKR1000 or the libraries.

AdamMiltonBarker commented 8 years ago

Have added websockets now and nothing on either files or either ports 8883 or 9001 I have now got applications running from the web, Mac / PC / Node JS / Python / Intel Galileo / Raspbery Pi all able to connect with no issues to either port 8883 or 9001 for websockets, I have uploaded the ca file again which is a chain from Comodo and still nothing. Error from broker:

OpenSSL Error: error:140940E5:SSL routines:SSL3_READ_BYTES:ssl handshake failure

sandeepmistry commented 8 years ago

I have uploaded the ca file again which is a chain from Comodo and still nothing. Error from broker:

This is definitely related to https://github.com/arduino-libraries/WiFi101/issues/48 then. The Comodo chain uses SHA-384, which is not supported by the WINC1500 at this time.

I'm closing this, please follow https://github.com/arduino-libraries/WiFi101/issues/48 for updates on this topic.