SignalK / SensESP

Universal Signal K sensor framework for the ESP32 platform
https://signalk.org/SensESP/
Apache License 2.0
146 stars 80 forks source link

SK Listeners doesn't work or crashes ESP32 #605

Closed JohnySeven closed 2 years ago

JohnySeven commented 2 years ago

Hey guys,

today I was trying to make device with SIM800L that will listen to navigation.position and relay X,Y via GPRS to remote server. It looks like that SKListeners are broken as I can see many crashes when update from Signal K server arrives, here is my example:

test_token)(C0) Upgrade Required
(test_token)(C1) Attempting to connect to Signal K Websocket...
(on_connected)(C0) Websocket client connected to URL: /signalk/v1/stream?subscribe=none

(on_connected)(C0) Subscribing to Signal K listeners...
(subscribe_listeners)(C0) Adding navigation.speedOverGround subscription with listen_delay 1000

(subscribe_listeners)(C0) Subscription JSON message:
 {"context":"vessels.self","subscribe":[{"path":"navigation.speedOverGround","period":1000}]}
(on_receive_delta)(C0) Websocket payload received: {"name":"signalk-server","version":"1.43.0","self":"vessels.urn:mrn:signalk:uuid:4affeee5-9ff5-47b4-8af1-2ad4baa7953f","roles":["master","main"],"timestamp":"2022-06-03T15:35:59.071Z"}
(on_receive_delta)(C0) Websocket payload received: {"context":"vessels.urn:mrn:signalk:uuid:4affeee5-9ff5-47b4-8af1-2ad4baa7953f","updates":[{"source":{"sentence":"VTG","talker":"GN","type":"NMEA0183","label":"gps"},"$source":"gps.GN","timestamp":"2022-06-03T15:35:58.857Z","values":[{"path":"navigation.speedOverGround","value":0}]}]}
(on_receive_delta)(C0) Websocket payload received: {"context":"vessels.urn:mrn:signalk:uuid:4affeee5-9ff5-47b4-8af1-2ad4baa7953f","updates":[{"source":{"sentence":"RMC","talker":"GN","type":"NMEA0183","label":"gps"},"$source":"gps.GN","timestamp":"2022-06-03T15:36:01.000Z","values":[{"path":"navigation.speedOverGround","value":0}]}]}

Here is narrowed down code of main.cpp:

#include "sensesp/sensors/analog_input.h"
#include "sensesp/sensors/digital_input.h"
#include "sensesp/sensors/sensor.h"
#include "sensesp/signalk/signalk_output.h"
#include "sensesp/system/lambda_consumer.h"
#include "sensesp_app_builder.h"
#include "sensesp/sensors/sensor.h"
#include "sensesp/signalk/signalk_value_listener.h"
using namespace sensesp;

reactesp::ReactESP app;
// The setup function performs one-time application initialization.
void setup() {
#ifndef SERIAL_DEBUG_DISABLED
  SetupSerialDebug(115200);
#endif
  // Construct the global SensESPApp() object
  SensESPAppBuilder builder;
  sensesp_app = (&builder)
                    ->set_hostname("gsm_gw")
                    ->set_wifi_manager_password("12345678")
                    ->get_app();

  auto speed_listener = new SKValueListener<float>("navigation.speedOverGround", 1000);

  speed_listener->attach([speed_listener]()
  {
    log_i("Speed %f m/s", speed_listener->get());
  });

  // Start networking, SK server connections and other SensESP internals
  sensesp_app->start();
}

void loop() { app.tick(); }

As it's visible from log SK update deltas with navigation.speedOverGround are comming in, but Speed isn't printed in console. I'm using SensESP git hub template.

I've added few log lines into WSClient::process_received_updates() and it looks like it's not called at all.

mairas commented 2 years ago

I have this in one of my test projects:

  auto hdg = new SKValueListener<float>("navigation.headingMagnetic");
  hdg->connect_to(new LambdaConsumer<float>(
      [](float input) { debugD("Heading: %f", input); }));

and it works just fine.

I tried switching to using the low-level observer interface:

  hdg->attach([hdg]() {
    debugD("Heading: %f", hdg->get());
  });

and it still works just fine (although I'd recommend using LambdaConsumer<> instead - there's less room for obscure errors that way).

However, if I switch from debugD to log_i, I don't get any output. Following the log_i symbol in VSCode suggests that ARDUHAL_LOG_LEVEL is defined lower as ARDUHAL_LOG_LEVEL_INFO.

Since the problem would seem to be in the callback function use, I'm closing this issue. If you think there's an actual unrelated SKValueListener issue, feel free to reopen.