hrbrmstr / mqtt

:telephone_receiver: Interoperate with 'MQTT' Message Brokers with R
21 stars 7 forks source link

Stop mqtt_run from executing #3

Open lefec opened 6 years ago

lefec commented 6 years ago

Hi, thanks for your making this package. I am playing around with MQTT for the first time today. I am trying to use shiny for displaying live sensor data.

This is the example you gave on the frontpage:

library(mqtt)

sensor <- function(id, topic, payload, qos, retain, con) {
  if (topic == "bbc/subtitles/bbc_two_england/raw") {
    cat(crayon::cyan(topic), crayon::blue(readBin(payload, "character")), "\n", sep=" ")
  }
}

# NOTE: Use a unique name vs `hrbrunique`
mqtt_broker("hrbrnique", "test.mosquitto.org", 1883L) %>%
  mqtt_silence(c("error", "log")) %>% 
  mqtt_subscribe(
    "bbc/subtitles/bbc_one_london/raw", 
    function(id, topic, payload, qos, retain, con) { # regular anonymous function
      if (topic == "bbc/subtitles/bbc_one_london/raw")
        cat(crayon::yellow(topic), crayon::green(readBin(payload, "character")), "\n", sep=" ")
    }) %>%
  mqtt_subscribe("bbc/subtitles/bbc_news24/raw", ~{ # tilde shortcut function (passing in named, pre-known params)
    if (topic == "bbc/subtitles/bbc_news24/raw")
      cat(crayon::yellow(topic), crayon::red(readBin(payload, "character")), "\n", sep=" ")
  }) %>%
  mqtt_subscribe("bbc/subtitles/bbc_two_england/raw", sensor) %>% # named function
  mqtt_run() -> res

I can't figure out how to stop it from executing (for me it is printing out the subtitles without an end). I set the 'times' argument in mqtt_run but it does not seem to have any effect.

I am not really interested in printing the results to the console but to save them in a vector to make some quick analysis.

jorditruji commented 6 years ago

Hello Lefec,

I had the same problem as you, sincerely the only thing I can recommend you is switching to python and use paho mqtt.

JeremyBesson commented 6 years ago

You can use the timeout parameter Example:

 mqtt_run(timeout = 1) 

But be careful this timeout is an interval in minutes contrary to what you can read in documentation. Also it is not really usable because the process is not really close, you only don't have new data coming after the timeout.