Raspberry pi GPIO controll

Please help a novice. I am trying to turn an LED on and off on my Raspberry pi. When I push my Anvil “on” button in my app, the LED on my Rpi turns on, but when I push the off button, I get the following error: I have gone over the code several times, but can’t find the problem.

anvil.server.NoServerFunctionError: No server function matching “turn_off” has been registered at [Form1, line 19](javascript:void(0))

Here is my code on Anvil:

from ._anvil_designer import Form1Template
from anvil import *
import anvil.server

class Form1(Form1Template):

def init(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)

# Any code you write here will run when the form opens.

def button_1_click(self, **event_args):
“”“This method is called when the button is clicked”""
anvil.server.call(“turn_on”)

def button_2_click(self, **event_args):
“”“This method is called when the button is clicked”""
anvil.server.call(“turn_off”)

Here is the code on the Raspberry pi

from gpiozero import LED
from time import sleep
import anvil.server

red = LED(4)

red.on()
sleep(1)
red.off()
sleep(1)

@anvil.server.callable

def turn_on():
red.on()
sleep(1)

def turn_off():
red.off()
sleep(1)

anvil.server.connect(“W6NKWLYPBLRBXSPSFCDC44RD-DBH4NWCQGCFNQ62T”)
anvil.server.wait_forever()

Thanks for any help.

Bill

@anvil.server.callable needs to be applied to every server function, not just once at the top. Right now it’s applying to just the turn_on function, and the turn_off function is not callable from the client.

1 Like

Thank you so much. My code now works.

I have an old stop light that I am going to try to control using Avnil and my Rpi. If I get it working, I will post about it in the “Show and tell” forum.

Kind regards,

Bill

2 Likes

That’s a very cool project!

Did you get to work? I’m really interested now xD

Hi,

Thanks for asking. Yes, I did get it working. I have been slowly making a video documenting all of the steps. I will post a link when I finish the video. I really like the addition of a microbit on the raspberry side of the project because the radio link makes it possible to control things that are not tethered to the computer. The Microbit also has lots of sensors, so next I hope to bring that sensor data back to my anvil app. I will probably be asking the forum for help on that. :slight_smile:

Bill

Bill

If you are interested, Here is a link to the video I made to explain the internet of things stoplight.

Thanks again for your help,

Bill

Thanks again for your help. If you are interested, here is a video about how I made the internet of things stoplight.

Bill,

2 Likes

That’s very cool! I run escape rooms for college students where I teach, and have wanted to do some Internet controlled devices for rooms. I need to get better at the hardware side of things, though.

Running escape rooms sounds like fun. The video is a little long, but if you fast forward about 1/2 way through, I go over some basic circuit theory, the solid state relays and a little introduction to the Microbit. If you have never played with a Microbit, I hope you get a couple. All sorts of ideas come to mind when I think escape room + Microbit. :slight_smile:

My next challenge is to set up a remote robot controller app and to collect sensor data from the robot and send it back to the Anvil app.

Thanks for your encouraging replies.

Bill