Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

mgos_shadow.h

I'm trying to incorporate the shadow library in my project for ESP32 to connect to my AWS thing. I've been away from it for about a year.
I realize that a lot of changes have happened to the libraries, and I'm mostly up to speed now. However, I do have some problems with the mgos_shadow.

In my main (app_init) I register a call back for the shadow as follows:

  /* Register AWS shadow callback handler   */
  mgos_event_add_group_handler(MGOS_SHADOW_BASE, shadow_state_cb, NULL);

My callback is as follows:

static void shadow_state_cb(int ev, void *ev_data, void *userdata) {
  struct mg_str data = *(struct mg_str *) ev_data;

  dpt_system_t tempSystemData;

  LOG(LL_INFO, ("== Shadow Event: %d (%s)", ev, mgos_shadow_event_name(ev)));

  if (ev == MGOS_SHADOW_CONNECTED) {
    report_state();
    return;
  }

  if (ev != MGOS_SHADOW_GET_ACCEPTED &&
      ev != MGOS_SHADOW_UPDATE_DELTA) {
    return;
  }

  LOG(LL_INFO, ("state : %.*s\n",  (int) data.len, data.p));

  json_scanf(data.p, data.len, JSON_EPOOLTIMER_FMT, &tempSystemData.pump_voltage, &tempSystemData.pump_current,
                                                                        &tempSystemData.pump_power, &tempSystemData.box_temperature,
                                                                                    &tempSystemData.box_pressure, &tempSystemData.manOverride,
                                                                                    &system_data.pumpCmd);

  update_state();
  if (ev == MGOS_SHADOW_UPDATE_DELTA) {
    report_state();
  }
  (void) userdata;                                     
}

However, this does not register an AWS event. And my callback never gets called. I didn't add "aws" in my mos.yml file, initially. However, when I did add it, mgos_aws_events get registered. But my application panics and dumps core.

What is the proper way to implement connect to an AWS thing using the new mgos shadow library?

Here is an excerpt from my mos.yml file:

  # AWS SHADOW
  - ["aws", "o", {title: "AWS settings"}]
  - ["aws.thing_name", "s", "", {title: "Set thing name. If not specified, device.id is used"}]
  - ["shadow.enable", true]
  # - ["shadow.lib", "s", "", {title: "Preferred shadow lib, e.g. aws, dash, gcp"}]

Comments

  • only1chionly1chi Boston

    I did get this working...

    mos.yml should be enable shadow:

      # AWS SHADOW
      - ["aws.thing_name", "s", "", {title: "Set thing name. If not specified, device.id is used"}]
      - ["shadow.enable", true]
      - ["shadow.lib", "s", "", {title: "Preferred shadow lib, e.g. aws, dash, gcp"}]
    

    In the main.c, need to setup a handler as follows:

      /* Register AWS shadow callback handler   */
      mgos_event_add_group_handler(MGOS_SHADOW_BASE, shadow_state_cb, NULL);
    

    An example callback handler:

    static void shadow_state_cb(int ev, void *ev_data, void *userdata) {
      struct mg_str data = *(struct mg_str *) ev_data;
      dpt_system_t tempSystemData;
    
      LOG(LL_INFO, ("== Shadow Event: %d (%s)", ev, mgos_shadow_event_name(ev)));
    
      if (ev == MGOS_SHADOW_CONNECTED) {
        do_Something();
        return;
      }
    
      if (ev != MGOS_SHADOW_GET_ACCEPTED &&
          ev != MGOS_SHADOW_UPDATE_DELTA) {
        return;
      }
    
      if (ev_data == NULL) {
        return;
      }
    
      data = *(struct mg_str *) ev_data;
    
      LOG(LL_DEBUG, ("state : %.*s\n",  (int) data.len, data.p));
    
      json_scanf(data.p, data.len, JSON__FMT, &system_data.ex1, &system_data.ex2, &system_data.3x3);
    
      update_state();
      if (ev == MGOS_SHADOW_UPDATE_DELTA) {
        do_Something();
      }
      (void) userdata;                                     
    }
    
    
Sign In or Register to comment.