xref: /aosp_15_r20/external/pigweed/docs/showcases/sense/tutorial/rpc.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1.. _showcase-sense-tutorial-pico-rpc:
2
3======================================
49. Communicate with your Pico over RPC
5======================================
6Now, let's revisit ``pw_console`` and ``pw_rpc``. This time, we'll send commands
7to and view logs from the real Pico device.
8
9.. _showcase-sense-tutorial-pico-rpc-interact:
10
11----------------------
12Interact with the Pico
13----------------------
14#. Fire up a ``pw_console`` instance that's connected to the Pico:
15
16   .. tab-set::
17
18      .. tab-item:: VS Code
19         :sync: vsc
20
21         In **Bazel Build Targets** right-click
22         **:rp2040_console (native_binary)** (under **//apps/blinky**)
23         and then select **Run target**.
24
25      .. tab-item:: CLI
26         :sync: cli
27
28         .. code-block:: console
29
30            $ bazelisk run //apps/blinky:rp2040_console
31
32#. Toggle the Pico's LED by typing the following into **Python Repl** and then
33   pressing :kbd:`Enter`:
34
35   .. code-block:: pycon
36
37      >>> device.rpcs.blinky.Blinky.ToggleLed()
38
39   You should see your Raspberry Pi Pico's LED turn either on or
40   off. If you run the command again you should see the LED switch
41   to its opposite state.
42
43   (The next few commands should be executed the same way.)
44
45#. Blink the LED 10 times:
46
47   .. code-block:: pycon
48
49      >>> device.rpcs.blinky.Blinky.Blink(interval_ms=1000, blink_count=10)
50
51#. Write some custom automation in the Python REPL to achieve the same
52   blinking effect:
53
54   .. code-block:: pycon
55
56      >>> def my_blinky(count, delay):
57      ...     from time import sleep
58      ...     toggle = device.rpcs.blinky.Blinky.ToggleLed
59      ...     for _ in range(count):
60      ...         toggle()
61      ...         sleep(delay)
62      ...
63      >>> my_blinky(20, 1)
64
65
66   .. note::
67
68      The REPL doesn't currently support top-level execution of multiple
69      statements. You can workaround this by wrapping your multi-statement
70      logic in a function and then invoking the function, as seen in
71      ``my_blinky()``.
72
73#. View the RP2040's onboard temperature:
74
75   .. code-block:: pycon
76
77      >>> device.rpcs.board.Board.OnboardTemp()
78
79   In **Python Results** you should see output like this:
80
81   .. code-block:: pycon
82
83      >>> device.rpcs.board.Board.OnboardTemp()
84      (Status.OK, board.rpc.OnboardTempResponse(temp=23.861492156982422))
85
86   Put your finger on the RP2040 chip in the middle of your Raspberry Pi
87   Pico for a few seconds and then run the temperature command again and
88   you should see the temperature increase.
89
90   .. admonition:: Exercise
91
92      Can you figure out the code to read the temperature 10 times
93      with a 1-second interval between readings, and then output
94      the average temperature? See
95      :ref:`showcase-sense-tutorial-appendix-temp-solution` for
96      one option.
97
98#. Leave the console open and proceed to the next section.
99
100.. _showcase-sense-tutorial-search-filter:
101
102----------------------
103Search and filter logs
104----------------------
105You can search and filter your device's logs. Try it now:
106
107#. Click any row in the **Device Logs** table to focus that part of the UI.
108#. Press :kbd:`/` to search the logs.
109#. Type ``Stopped blinking`` and press :kbd:`Enter`. A log that matches
110   that string should be highlighted.
111
112   .. admonition:: Troubleshooting
113
114      **No logs are shown**. There probably has just not been any
115      logs that match the filter you entered. Try filtering by
116      other values, such as ``00:00`` to only show logs that occurred
117      during the first 60 seconds of logs.
118
119#. Press :kbd:`n` to go to next match and :kbd:`N` to go to previous match.
120   (If there are 0 matches or only 1 match then this naturally won't work.)
121#. Press :kbd:`Ctrl+Alt+F` to filter out logs that don't match your query.
122#. Press :kbd:`Ctrl+Alt+R` or click **Clear Filters** to clear your filter
123   and return to the original table view.
124
125.. _showcase-sense-tutorial-pico-rpc-summary:
126
127-------
128Summary
129-------
130On this page we revisited our old friends ``pw_console`` and ``pw_rpc``,
131except this time we used them to communicate with a real embedded
132device rather than a simulated device running on our development host.
133In other words, when it's time to switch from simulated devices to
134real ones, you don't need to learn new tools.
135
136Next, head over to :ref:`showcase-sense-tutorial-automate` to
137learn how to package up common development tasks into small scripts
138so that your whole team can benefit from them.
139
140--------
141Appendix
142--------
143
144.. _showcase-sense-tutorial-appendix-temp-solution:
145
146Temperature averaging solution
147==============================
148Here's one possible solution to the temperature averaging exercise
149at the bottom of :ref:`showcase-sense-tutorial-pico-rpc-interact`.
150
151.. code-block:: py
152
153   def average(count, delay):
154       from time import sleep
155       total = 0
156       sample = device.rpcs.board.Board.OnboardTemp
157       for _ in range(count):
158           status, data = sample()
159           total += data.temp
160           sleep(delay)
161       return total / count
162
163   average(10, 1)
164