bytebeamio / bytebeam-arduino-sdk

A small Arduino Library for Bytebeam Cloud that just works
https://bytebeam.io/docs/arduino
Apache License 2.0
17 stars 1 forks source link

initSDK : Initialization failed, error while connecting to cloud. #3

Closed reusables-official closed 2 months ago

reusables-official commented 2 months ago

Board

esp32-s3

Device Description

DevKitC

Arduino-ESP32 Version

Latest

Bytebeam Arduino SDK Version

1.1.0

IDE Name

ArduinoIDE and PlatformIO

Operating System

macOs 13.6.6

Problem Description

From ArduinoIDE, I have provisioned the device using your example sketch with a json file I downloaded from my dashboard and it completes without errors. Using SetupClient.ino with my WiFi and SSID, produces a "failed to connect" error without much more information.

Sketch

#include <time.h>
#include <WiFi.h>
#include <BytebeamArduino.h>      
#include "arduino_secrets.h"

//
// By including the above header you got the access to the gloabl object named Bytebeam
// Use Bytebeam global object to perform the required operations
//

// wifi credentials
const char* WIFI_SSID     = SECRET_SSID;
const char* WIFI_PASSWORD = SECRET_PASS;

// sntp credentials
const long  gmtOffset_sec = 19800;      // GMT + 5:30h
const int   daylightOffset_sec = 0;
const char* ntpServer = "pool.ntp.org";

// function to setup the wifi with predefined credentials
void setupWifi() {
  // set the wifi to station mode to connect to a access point
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID , WIFI_PASSWORD);

  Serial.println();
  Serial.print("Connecting to " + String(WIFI_SSID));

  // wait till chip is being connected to wifi  (Blocking Mode)
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(250);
  }

  // now it is connected to the access point just print the ip assigned to chip
  Serial.println();
  Serial.print("Connected to " + String(WIFI_SSID) + ", Got IP address : ");
  Serial.println(WiFi.localIP());
}

// function to sync time from ntp server with predefined credentials
void syncTimeFromNtp() {
  // sync the time from ntp server
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

  struct tm timeinfo;

  // get the current time
  if(!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }

  // log the time info to serial :)
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
  Serial.println();
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();

  setupWifi();
  syncTimeFromNtp();

  //
  //  Your other application setup stuff goes here
  //

  // setting up the device info i.e to be seen in the device shadow
  Bytebeam.status          = "Device is Up!";
  Bytebeam.softwareType    = "setup-wifi-client-ino";
  Bytebeam.softwareVersion = "1.0.0";
  Bytebeam.hardwareType    = "ESP32 Dev Module";
  Bytebeam.hardwareVersion = "rev1";

  // This method will initialize and start the bytebeam client 
  // You can over-ride the default file system, file name and library logging options, if needed
  // eg. Bytebeam.begin(BytebeamArduino::LITTLEFS_FILE_SYSTEM, "/my_device_config.json", BytebeamLogger::LOG_INFO)
  if(!Bytebeam.begin(BytebeamArduino::SPIFFS_FILE_SYSTEM, "/device_config.json", BytebeamLogger::LOG_DEBUG)) {
    Serial.println("Bytebeam Client Initialization Failed.");
  } else {
    Serial.println("Bytebeam Client is Initialized Successfully.");
  }

  //
  // If above call is successfull then the bytebeam client is now configured for the use
  // You can always check for the logs in serial monitor for the status of the above call
  //

  // check if bytebeam client is connected or disconnected
  bool connectionStatus = Bytebeam.isConnected();

  if(!connectionStatus) {
    Serial.println("Bytebeam Client is Disconnected.");
  } else {
    Serial.println("Bytebeam Client is Connected.");
  }

  // Call the end method to stop and de-initialize the bytebeam client at any point of time in the code
  // Bytebeam.end();
}

void loop() {
  // put your main code here, to run repeatedly:

  //
  //  Your application regular stuff goes here
  //

  // This method will let you maintain the connection with the bytebeam cloud, In case
  // the connection is lost, it will attempt to reconnect to the bytebeam cloud
  Bytebeam.loop();

  // software delay, you can customize it as per your application needs
  delay(5000);
}

### Debug Message

```plain
BytebeamLogger::LOG_DEBUG (BytebeamArduino) readDeviceConfigFile : deviceConfigStr : 
{
  "project_id": "srbv3",
  "broker": "cloud.bytebeam.io",
  "port": 8883,
  "device_id": "1",
  "authentication": {
... etc

BytebeamLogger::LOG_INFO (BytebeamArduino) parseDeviceConfigFile : deserializeJson() success
BytebeamLogger::LOG_INFO (BytebeamArduino) parseDeviceConfigFile : Obtaining device config file variables
BytebeamLogger::LOG_INFO (BytebeamArduino) parseDeviceConfigFile : - obtain device config file variables
BytebeamLogger::LOG_DEBUG (BytebeamArduino) parseDeviceConfigFile : mqttPort : 8883
BytebeamLogger::LOG_DEBUG (BytebeamArduino) parseDeviceConfigFile : mqttBrokerUrl : cloud.bytebeam.io
BytebeamLogger::LOG_DEBUG (BytebeamArduino) parseDeviceConfigFile : deviceId : 1
BytebeamLogger::LOG_DEBUG (BytebeamArduino) parseDeviceConfigFile : projectId : srbv3
BytebeamLogger::LOG_DEBUG (BytebeamArduino) parseDeviceConfigFile : caCertPem : 

... prints out certs

BytebeamLogger::LOG_DEBUG (BytebeamArduino) parseDeviceConfigFile : clientId : BytebeamClient
BytebeamLogger::LOG_INFO (BytebeamArduino) parseDeviceConfigFile : Project Id : srbv3 and Device Id : 1
BytebeamLogger::LOG_INFO (BytebeamOTA) retrieveOTAInfo : NVS: Retrieving OTA Information
BytebeamLogger::LOG_INFO (BytebeamArduino) initSDK : RESTART: Normal Reboot !
BytebeamLogger::LOG_INFO (BytebeamArduino) setupBytebeamClient : Connecting To Bytebeam Cloud ... 
BytebeamLogger::LOG_ERROR (BytebeamArduino) setupBytebeamClient : ERROR
BytebeamLogger::LOG_ERROR (BytebeamArduino) initSDK : Initialization failed, error while connecting to cloud.

Bytebeam Client Initialization Failed.
BytebeamLogger::LOG_ERROR (BytebeamArduino) isConnected : Bytebeam Client is not Initialized.
Bytebeam Client is Disconnected.
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.


### More Information

Additionally, I was not able to use this library with PlatformIO as it raised NUMEROUS exceptions. The sketches compile in ArduinoIDE. I installed the dependencies manually in both cases.
reusables-official commented 2 months ago

@narangmayank I have just ran the provisioning sketch again, no issues, then tried flashing another example "ActionsHandling.ino" but the same exact same error is present.

I will add that I opened the device config json file, and formatted it in vscode before pasting it into the sketch as per the instructions. Just to see, I provisioned the device with the json file as is (without formatting) and again same error.

Are you able to look at any logs on your side, is my device hitting your server and trying to authenticate but failing?

My latest attempt timestamps:

Tuesday, April 09 2024 09:38:38

BytebeamLogger::LOG_DEBUG (BytebeamTime) getEpochMillis : prevMillis : 0
BytebeamLogger::LOG_DEBUG (BytebeamTime) getEpochMillis : nowMillis : 1712635719007
BytebeamLogger::LOG_DEBUG (BytebeamTime) begin : beginMillis : 1712635719007
BytebeamLogger::LOG_INFO (BytebeamTime) begin : TIME : Client begin! 
BytebeamLogger::LOG_INFO (BytebeamArduino) printArchitectureInfo : * ****************************************************************** *
BytebeamLogger::LOG_INFO (BytebeamArduino) printArchitectureInfo : *             Architecture : ESP32               *
BytebeamLogger::LOG_INFO (BytebeamArduino) printArchitectureInfo : *             Chip Model   : ESP32-S3            *
BytebeamLogger::LOG_INFO (BytebeamArduino) printArchitectureInfo : *             CPU Freq     : 240 MHz             *
BytebeamLogger::LOG_INFO (BytebeamArduino) printArchitectureInfo : *             Flash Size   : 8 MB                *
BytebeamLogger::LOG_INFO (BytebeamArduino) printArchitectureInfo : *             Free Heap    : 269 KB              *
BytebeamLogger::LOG_INFO (BytebeamArduino) printArchitectureInfo : *             SDK Version  : v4.4.6-dirty                *
BytebeamLogger::LOG_INFO (BytebeamArduino) printArchitectureInfo : * ****************************************************************** *
BytebeamLogger::LOG_INFO (BytebeamArduino) readDeviceConfigFile : SPIFFS file system detected !
BytebeamLogger::LOG_INFO (BytebeamArduino) readDeviceConfigFile : Reading file : /device_config.json
BytebeamLogger::LOG_INFO (BytebeamArduino) readDeviceConfigFile : - read from file
reusables-official commented 2 months ago

Another question I have is can you let me know exact versions of dependencies I should have installed? I believe I am currently running v7 of ArduinoJSON which has some breaking changes from v6, for example.

reusables-official commented 2 months ago

@narangmayank ok, I was able to initialize successfully after I downgraded ArduinoJson to v6.21.5 🎉

narangmayank commented 2 months ago

@reusables-official Its good to hear that you have solved the issue.