Display Pico W PIN/LED status

What I’m trying to do:
I just started to experiment with the Pico W and Anvil. I have installed 3 pairs of LEDs and can toggle these in my Anvil Web app. So far, everything is working fine.

Now I would like to display the status of the LED/PIN in the Anvil App in order to see, if said LED is on or not.

What I’ve tried and what’s not working:
I have searched for tutorials, but couldn’t find any that do this. Do I have to save the data in a database, or is there another possibility? How do i read the status of the pin?
I have to add that I have almost no experience with Python.

Code Sample:
This is the code I use to toggle one of the LEDs on the pico.

@anvil.pico.callable(is_async=True)
async def green_01(n):
    led = Pin(5, Pin.OUT)
    led.toggle()

You could save it on the front end as a property on the form, or as a variable in the pico function using whatever the property on the pin is for whether its toggled or not. Even if there’s no property on the Pin object for that, you can still keep track by knowing the original state is off (False) and every toggle goes from False to True/True to False.

Thank you very much for the hints. Would you have any example code that shows how to do this, or could you maybe point me in the direction where I could find this information.

Like I said, I am unfortunately not very versed in Python.

I don’t know anything about pico and couldn’t get it easily setup, but the gist would be to

  1. return a value from the pico function that tells your web frontend whether it’s toggled or not.

  2. use the self object (which is just the form), and set a property on it to the return value from the pico function like this:

def whatever_calls_pico(self, **event_args):
  self.light_on = anvil.server.call("green01")

note: I don’t know if anvil.server.call is the right way to call pico, so replace that with whatever that method is.

Again thank you very much for your answer and your patience. Unfortunately, I am still lost on hot to achieve this.
I tried to enter your code into the form, but only got errors. Here is the code of my form. Where should I insert the object?

class Form1(Form1Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

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

  def outlined_button_1_click(self, **event_args):
    """This method is called when the button is clicked"""
    anvil.server.call('green_01',1)

  def outlined_button_2_click(self, **event_args):
    """This method is called when the button is clicked"""
    anvil.server.call('red_01',1)

Additionally, could you tell me how to return a value from the pico?

P.S. as far as I know, anvil.server.call is the right way to call the pico. At least it allows me to toggle the LEDs.

you return a value by using the keyword return (literally that word) a space and then whatever variable or value you want to return.

What error are you getting? If I don’t know the error I can’t debug it.

I have tried to insert the code you provided me into the form as follows:

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)
  def anvil.server.call(self, **event_args):
    self.light_on = anvil.server.call("green01")

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

  def outlined_button_1_click(self, **event_args):
    """This method is called when the button is clicked"""
    anvil.server.call('green_01',1)

  def outlined_button_2_click(self, **event_args):
    """This method is called when the button is clicked"""
    anvil.server.call('red_01',1)

Unfortunately, I get the error

SyntaxError: bad input

  • at Form1, line 10

as soon as I try to run the App. It seems that anvil.server.call isn’t the right syntax after all. But like I said, it allows me to toggle the LEDs, so I don’t know.

the problem is that you should remove the line with def anvil.server.call, that method is already defined. Also, you can never define a method with dots in the name.

I changed the code as follows:

class Form1(Form1Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    self.light_on = anvil.server.call("green01")

As far as my interpretation goes, the code wants to get the variable green01 from the pico.
I tried to define the variable on the pico with a value of 42 for test purposes.

@anvil.pico.callable(is_async=True)
def green01():
    return 42

This triggers a new error for me:
Error: 'int' object isn't iterable

  • at Form1, line 10

can you give me a clone link? it’s hard to make out the error from just the snippets.

Sure. Here is the Link:

I will keep the pico connected in case that that is necessary.

Additionally, here is the content of my main.py

import anvil.pico
import uasyncio as a
from machine import Pin
import time



# This is an example Anvil Uplink script for the Pico W.
# See https://anvil.works/pico for more information

UPLINK_KEY = "server_BU4TIFCEBMV6A2YC6T7TA5Q5-V2E6XYWC3JOAZCSN"

# We use the LED to indicate server calls and responses.
#led = Pin("LED", Pin.OUT, value=1)
led = Pin(5, Pin.OUT)


# ------------------------- Gruppe 1 -------------------------
#LED Gruppe 1 - Grün
@anvil.pico.callable(is_async=True)
async def green_01(n):
    led = Pin(5, Pin.OUT)
    led.toggle()
    
@anvil.pico.callable(is_async=True)
def green01():
    return 42

#LED Gruppe 1 - Rot
@anvil.pico.callable(is_async=True)
async def red_01(n):
    led = Pin(28, Pin.OUT)
    led.toggle()

Hmm, nothing really seems wrong with the syntax. Unfortunately I can’t use your Uplink code, so I can’t really run it. Maybe someone with a pico can help.

Thanks again. I will continue to search, maybe I will find a tutorial or something that can help me here.

If there is anyone else with an idea, it would be much appreciated.