Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToggleLED.ino doesn't work #5

Open
Bamamou opened this issue Jan 8, 2025 · 1 comment
Open

ToggleLED.ino doesn't work #5

Bamamou opened this issue Jan 8, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@Bamamou
Copy link

Bamamou commented Jan 8, 2025

Board

ESP32 DEV Board in plaformio

Device Description

The ToggleLED.ino example doesn't work both in Arduino IDE and Platformio.
It simple because there is no explanation on the examples given. I recommend adding comment on each line of the code.
The code is fairly simple but no explanation or comment, so the Bytebeam.begin() function doesn't work and I don't to pass as argument.

Arduino-ESP32 Version

ESP32 WROOM 32E

Bytebeam Arduino SDK Version

bytebeamio/BytebeamArduino@^1.0.1

IDE Name

Both Arduino IDE and Platformio

Operating System

Win10

Problem Description

No explanation on how to use the code.

Sketch

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

// on board led pin number
#define BOARD_LED 15

// led state variable
int ledState = 0;

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

// sntp credentials
const long  gmtOffset_sec = 28800;      // 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();
}

// function to setup the predefined led
void setupLED() {
  pinMode(BOARD_LED, OUTPUT);
  digitalWrite(BOARD_LED, ledState);
}

// function to toggle the predefined led
void toggleLED() {
  ledState = !ledState;
  digitalWrite(BOARD_LED, ledState);
}

// function to get the time 
unsigned long long getEpochMillis() {
  time_t now;
  struct tm timeinfo;

  // get the current time i.e make sure the device is in sync with the ntp server
  if (!getLocalTime(&timeinfo)) {
    Serial.println("failed to obtain time");
    return 0;
  }

  // get the epoch time
  time(&now);

  // generate the epoch millis
  unsigned long long timeMillis = ((unsigned long long)now * 1000) + (millis() % 1000);

  return timeMillis;
}

// function to publish payload to device shadow
bool publishToDeviceShadow() {
  static int sequence = 0;
  unsigned long long milliseconds = 0;
  char ledStatus[200] = "";
  char deviceShadowStream[] = "device_shadow";

  const char* payload = "";
  String deviceShadowStr = "";
  StaticJsonDocument<1024> doc;

  // get the current epoch millis
  milliseconds = getEpochMillis();

  // make sure you got the millis
  if(milliseconds == 0) {
    Serial.println("failed to get epoch millis");
    return false;
  }

  // increment the sequence counter
  sequence++;

  // generate the led status message string
  sprintf(ledStatus, "LED is %s !", ledState == true? "ON" : "OFF");

  JsonArray deviceShadowJsonArray = doc.to<JsonArray>();
  JsonObject deviceShadowJsonObj_1 = deviceShadowJsonArray.createNestedObject();

  deviceShadowJsonObj_1["timestamp"] = milliseconds;
  deviceShadowJsonObj_1["sequence"]  = sequence;
  deviceShadowJsonObj_1["Status"]    = ledStatus;
  
  serializeJson(deviceShadowJsonArray, deviceShadowStr);
  payload = deviceShadowStr.c_str();

  Serial.printf("publishing %s to %s\n", payload, deviceShadowStream);

  return Bytebeam.publishToStream(deviceShadowStream, payload);
}

// handler for ToggleLED action
int ToggleLED_Hanlder(char* args, char* actionId) {
  Serial.println("ToggleLED Action Received !");
  Serial.printf("<--- args : %s, actionId : %s --->\n", args, actionId);

  // toggle the led
  toggleLED();

  // publish led state to device shadow
  if(!publishToDeviceShadow()) {
    // publish action failed status
    if(!Bytebeam.publishActionFailed(actionId, "Publish led state to device shadow Failed")) {
      Serial.println("Failed to publish action failed response for Toggle LED action");
    }

    Serial.println("Failed to publish led state to device shadow");
    return -1;
  }

  // publish action completed status
  if(!Bytebeam.publishActionCompleted(actionId)) {
    Serial.println("Failed to publish action completed response for Toggle LED action");
    return -1;
  }

  return 0;
}

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

  setupWifi();
  syncTimeFromNtp();

  // setup the gpio led
  setupLED();

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

  // begin the bytebeam client
  if(!Bytebeam.begin()) {
    Serial.println("Bytebeam Client Initialization Failed.");
  } else {
    Serial.println("Bytebeam Client is Initialized Successfully.");
  }

  // add the handler for toggle led action
  Bytebeam.addActionHandler(ToggleLED_Hanlder, "ToggleLED");
}

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

  // bytebeam client loop
  Bytebeam.loop();

  // hold on the execution for some time
  delay(5000);
}

Debug Message

--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.
BytebeamLogger::LOG_ERROR (BytebeamArduino) loop : Bytebeam Client is not Initialized.

More Information

Give a short comment on each function and their parameters in the example.
Before even someone try to use the code, it should have a description of the what the code does and how to use it.

@Bamamou Bamamou added the bug Something isn't working label Jan 8, 2025
@narangmayank
Copy link
Contributor

@Bamamou Is device provisioning done? Wondering if you have gone through the docs https://bytebeam.io/docs/arduino

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants