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

Any examples of rpigpior with Shiny? #5

Open
dcaud opened this issue Apr 28, 2024 · 3 comments
Open

Any examples of rpigpior with Shiny? #5

dcaud opened this issue Apr 28, 2024 · 3 comments

Comments

@dcaud
Copy link

dcaud commented Apr 28, 2024

I'm wondering whether Pi's GPIO would work well with R's Shiny.

  1. Could you continuously monitor an input from a GPIO and continuously plot it within Shiny? Perhaps something like:
library(shiny)
library(rpigpior)
library(ggplot2)

# Initialize GPIO
rpigpior::setup(pin = 2, mode = "in")

ui <- fluidPage(
  titlePanel("Real-Time GPIO Pin Monitoring"),
  mainPanel(
    plotOutput("pinPlot")
  )
)

server <- function(input, output, session) {
  
  # Function to read pin status
  readPin <- reactivePoll(1000, session,
                          checkFunc = function() {
                            Sys.time()  # Forces reactive to execute every 1 second
                          },
                          valueFunc = function() {
                            rpigpior::read_pin(pin = 2)
                          })
  
  # Generate plot for output
  output$pinPlot <- renderPlot({
    data <- data.frame(
      Time = Sys.time(),
      Value = readPin()
    )
    
    ggplot(data, aes(x = Time, y = Value)) +
      geom_line() +
      labs(title = "GPIO Pin 2 Status", x = "Time", y = "Value") +
      ylim(0, 1)  # Since GPIO pin values are either 0 or 1
  })
}

# Run the application
shinyApp(ui = ui, server = server)

Also, I wonder how this could be adapted to monitor a momentary switch -- something with short pulse that would not span the polling duration set in the Shiny app.

If anyone has examples (or can just say that this generally would work) would be super helpful.

@mnr
Copy link
Owner

mnr commented Apr 28, 2024

This all seems to be possible. It implies you are running shiny on a raspberry pi - or passing data from the raspberry pi to an external data cache.

There is a pretty complete write-up on running shiny on RPi here... . I suspect there are easier ways to do this - certainly start with r4pi

@mnr
Copy link
Owner

mnr commented Apr 28, 2024

regarding monitoring a switch, I haven't implemented generating an interrupt for a switch - so currently that would done by polling the button every so often. Again - ought to work, but I'm not sure how shiny timing would feel about polling.

When you suggest a momentary switch, are you referring to a manual pushbutton? Or are you watching for a signal from some other device?

@dcaud
Copy link
Author

dcaud commented Apr 28, 2024

Great! Thanks for the general confirmation that this seems possible.

I was thinking of a manual pushbutton as I was trying to see the limits of the polling. I presume some kind of proper interrupt would lessen the problems associated with trying to poll events that are short-lived.

In general, I was wondering the feasibility of having Shiny be the front-end (and more!) for a number of possible Pi applications -- including monitoring multiple kinds of inputs and controlling multiple kinds of outputs (PWM, etc.).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants