aws / aws-iot-device-sdk-arduino-yun

SDK for connecting to AWS IoT from an Arduino Yún.
Apache License 2.0
163 stars 77 forks source link

Bridge.begin() - I get Setup failed! -5 #61

Closed flycast closed 6 years ago

flycast commented 6 years ago

If I use the following code I can connect every time:

#include <aws_iot_mqtt.h>
#include <aws_iot_version.h>
#include "aws_iot_config.h"
#include <Process.h>

aws_iot_mqtt_client myClient; // init iot_mqtt_client
int rc = -100; // return value placeholder

void setup() {
  Serial.begin(250000);
  while(!Serial);
  //
  char curr_version[80];
  snprintf_P(curr_version, 80, PSTR("AWS IoT SDK Version(dev) %d.%d.%d-%s\n"), VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
  Serial.println(curr_version);
}

void loop() {
      // restart the date process:
    connect();
    delay(4000);
}

bool connect(){

  Serial.println("Attempting to setup...");

  // Set up the client
  if((rc = myClient.setup(AWS_IOT_CLIENT_ID)) == 0) {
    Serial.println("Set up success...");

    // Load user configuration
    Serial.println("Attempting to connect...");
    if((rc = myClient.config(AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, AWS_IOT_ROOT_CA_PATH, AWS_IOT_PRIVATE_KEY_PATH, AWS_IOT_CERTIFICATE_PATH)) == 0) {
      if((rc = myClient.connect()) == 0) {
        Serial.println("Connected!");
        Serial.println(rc);
        return true;
      }else{
        Serial.println(F("Connect failed!"));
        Serial.println(rc);
        return false;
      }
    }else{
      Serial.println(F("Config failed!"));
      Serial.println(rc);
      return false;
    }
  }else{
    Serial.println(F("Setup failed!"));
    Serial.println(rc);
    return false;
  }

}

Once I use Bridge.begin(); in my setup():

void setup() {
  Bridge.begin();
  Serial.begin(250000);
  while(!Serial);
  //
  char curr_version[80];
  snprintf_P(curr_version, 80, PSTR("AWS IoT SDK Version(dev) %d.%d.%d-%s\n"), VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
  Serial.println(curr_version);
}

I start getting

Setup failed! -5

This behavior persists until I run YunSerialTerminal and issue the ~~ command.

What may be happening here?

hedeshianaws commented 6 years ago

Can you clarify what you are trying to accomplish with the YunBridgeLibrary? YBL will conflict with the SDK's usage of the serial port.

flycast commented 6 years ago

Yes. Get the date since Linux epoch in seconds, seconds + milliseconds if possible. I need a time stamp.

hedeshianaws commented 6 years ago

Ah yes, this will be tricky. The IoT SDK takes control of the serial link and expects that it is in the default reset state. By using YBL, the serial link is no longer in the default power-on state. Unfortunately, this is one of the reasons I mentioned in your last ticket (#58) that you'll want to use the Python IoT SDK directly on the AR9331 and write your own arbiter for the inter-processor serial link. You'll likely need to write some custom code to multiplex the SDK traffic and your Linux commands over the serial link.

If you only need the time-stamp once at program start, you could maybe do something like this:

void setup() {
  Bridge.begin();
  Bridge.whatever();
  Bridge.end();
  Serial.begin(250000);
  while(!Serial);
  //
  char curr_version[80];
  snprintf_P(curr_version, 80, PSTR("AWS IoT SDK Version(dev) %d.%d.%d-%s\n"), VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
  Serial.println(curr_version);
}

That said, I don't know if it will work. You may still need to kick the serial port into a clean state which can be used by the SDK.

flycast commented 6 years ago

As yes, this will be tricky. The IoT SDK takes control of the serial link and expects that it is in the default reset state. By using YBL, the serial link is no longer in the default power-on state. Unfortunately, this is one of the reasons I mentioned in your last ticket (#58) that you'll want to use the Python IoT SDK directly on the AR9331 and write your own arbiter for the inter-processor serial link. You'll likely need to write some custom code to multiplex the SDK traffic and your Linux commands over the serial link.

Sounds WAY over my head!

There is no Bridge.end();

hedeshianaws commented 6 years ago

Closing due to inactivity. Please feel free to start a new thread/issue if you have any additional questions.

mastrolinux commented 6 years ago

Just to explain a bit more. Here at Arduino we implemented the mentioned Bridge.end(); as suggested by @hedeshianaws It is now merged on master and available to everyone at https://github.com/arduino-libraries/Bridge