1.. _showcase-sense-tutorial-prod: 2 3=================================== 414. Run the air quality monitor app 5=================================== 6Your tour of Pigweed is almost done. Before you go, let's get you 7familiar with the application described at 8:ref:`showcase-sense-product-concept`. Within the Sense codebase this app 9is called ``production``. The purpose of the ``production`` app is to 10demonstrate what a medium-complexity application built on top of Pigweed's 11software abstractions looks like. We're still perfecting the codebase 12structure, but this application can begin to give you an idea of how the 13Pigweed team thinks Pigweed-based projects should be structured. 14 15.. important:: 16 17 This section requires the :ref:`full hardware 18 setup <showcase-sense-tutorial-hardware>`. 19 20First, let's get the app running on your Pico. Then we'll provide 21an overview of the code. 22 23.. warning:: 24 25 This is just a sample application. It is not suitable for real 26 air quality monitoring. 27 28.. _showcase-sense-tutorial-prod-hardware: 29 30-------------------- 31Set up your hardware 32-------------------- 33This part of the tutorial requires the 34:ref:`full setup <showcase-sense-tutorial-hardware>`. 35 36.. _showcase-sense-tutorial-prod-flash: 37 38-------------- 39Flash the Pico 40-------------- 41Flash the ``production`` app to your Pico: 42 43.. tab-set:: 44 45 .. tab-item:: VS Code 46 :sync: vsc 47 48 .. tab-set:: 49 50 .. tab-item:: Pico 1 & 1W (RP2040) 51 :sync: rp2040 52 53 In **Bazel Targets** expand **//apps/production**, then right-click 54 **:flash_rp2040 (native binary)**, then select **Run target**. 55 56 .. tab-item:: Pico 2 & 2W (RP2350) 57 :sync: rp2350 58 59 In **Bazel Targets** expand **//apps/production**, then right-click 60 **:flash_rp2350 (native binary)**, then select **Run target**. 61 62 .. tab-item:: CLI 63 :sync: cli 64 65 .. tab-set:: 66 67 .. tab-item:: Pico 1 & 1W (RP2040) 68 69 .. code-block:: console 70 71 bazelisk run //apps/production:flash_rp2040 72 73 .. tab-item:: Pico 2 & 2W (RP2350) 74 75 .. code-block:: console 76 77 bazelisk run //apps/production:flash_rp2350 78 79.. _showcase-sense-tutorial-prod-logs: 80 81---------------------- 82Monitor the app's logs 83---------------------- 84The app prints out a lot of informational logs. These logs can 85help you grok how the app works. Fire up ``pw_console`` again now: 86 87.. tab-set:: 88 89 .. tab-item:: VS Code 90 :sync: vsc 91 92 .. tab-set:: 93 94 .. tab-item:: Pico 1 & 1W (RP2040) 95 :sync: rp2040 96 97 In **Bazel Targets** right-click the **:rp2040_console (native binary)** 98 target (under **//apps/production**) then select **Run target**. 99 100 .. tab-item:: Pico 2 & 2W (RP2350) 101 :sync: rp2350 102 103 In **Bazel Targets** right-click the **:rp2350_console (native binary)** 104 target (under **//apps/production**) then select **Run target**. 105 106 .. tab-item:: CLI 107 :sync: cli 108 109 Run the terminal-based console: 110 111 .. tab-set:: 112 113 .. tab-item:: Pico 1 & 1W (RP2040) 114 115 .. code-block:: console 116 117 bazelisk run //apps/production:rp2040_console 118 119 .. tab-item:: Pico 2 & 2W (RP2350) 120 121 .. code-block:: console 122 123 bazelisk run //apps/production:rp2350_console 124 125See :ref:`showcase-sense-tutorial-sim` if you need a refresher 126on how to use ``pw_console``. 127 128.. _showcase-sense-tutorial-prod-alarm: 129 130---------------------------- 131Trigger an air quality alarm 132---------------------------- 133The default mode of the app is to continuously monitor air quality. 134You should see the LED on your Enviro+ in one of the following 135states: 136 137* Blue/green: Excellent air quality 138* Green: Good air quality 139* Orange: Meh air quality 140* Red: Bad air quality 141 142.. admonition:: Troubleshooting 143 144 **The LCD screen is blank**. This is expected because we haven't 145 implemented display support in the app yet. Stay tuned! 146 147Try triggering an air quality alarm now: 148 149#. Hold a strong chemical such as rubbing alcohol close to the 150 **BME688** sensor on your Enviro+ Pack. 151 152 The LED on the Enviro+ Pack should change to orange (meh air quality) or 153 red (bad air quality). 154 155The next video is an example of what you should see. 156 157.. raw:: html 158 159 <video preload="metadata" style="width: 100%; height: auto;" controls muted> 160 <source type="video/webm" 161 src="https://storage.googleapis.com/pigweed-media/sense/20240802/production.mp4#t=0.5"/> 162 </video> 163 164.. _showcase-sense-tutorial-prod-thresholds: 165 166---------------------------- 167Adjust the alarm sensitivity 168---------------------------- 169You can adjust the sensitivity i.e. thresholds of the alarm with 170the **A** and **B** buttons on your Enviro+ Pack: 171 172* Press the **A** button repeatedly to increase the sensitivity 173 of the alarm. In other words, with only a slight change in 174 air quality the LED will shift to orange (meh air quality) or 175 red (bad air quality). 176* Press the **B** button repeatedly to decrease the sensitivity 177 of the alarm. In other words, it takes a bigger change in 178 air quality for the LED to shift to orange or red. 179 180.. note:: 181 182 The "threshold adjustment" mode that you enter after pressing 183 **A** or **B** will automatically exit after 3 seconds of 184 inactivity. 185 186In the **Device Logs** of ``pw_console`` you should see the 187air quality thresholds change as you press **A** and **B**. 188For example, if you quickly press **A** twice (i.e. in less 189than a second) you should see a log like this: 190 191.. code-block:: text 192 193 19:38:23 INF 00:00:25.758 STATE Air quality thresholds set: alarm at 384, silence at 512 194 195That log is telling you that the LED will change to red and start 196blinking when the air quality value is less than ``384``. 197 198.. _showcase-sense-tutorial-prod-morse: 199 200---------------------------------------- 201Print air quality messages in Morse code 202---------------------------------------- 203.. _Morse code: https://en.wikipedia.org/wiki/Morse_code 204 205Press the **Y** button to put the app in `Morse code`_ mode. 206In this mode, the LED on the Enviro+ prints out air quality 207messages like ``AQ EXCELLENT 872`` as Morse code messages, 208in addition to changing color as previously described. 209 210------------------------------------------- 211View more information in the custom web app 212------------------------------------------- 213Now that your Pico is running the full ``production`` app, 214the custom web app that was demonstrated in :ref:`showcase-sense-tutorial-webapp` 215will show you more information if you fire it up again. 216 217.. _showcase-sense-tutorial-prod-code: 218 219------------- 220Code overview 221------------- 222.. _Sense codebase: https://cs.opensource.google/pigweed/showcase/sense 223 224As mentioned in the intro of this page, the ``production`` app 225provides a good start for figuring out how to structure your 226Pigweed-based project. It's not perfect yet, but it's a solid 227start. We'll leave it up to you to study the code in-depth, but 228here are some pointers on the relevant parts of the `Sense codebase`_: 229 230* ``//apps/production/*``: The app's entrypoint code. 231* ``//modules/*``: Portable business logic, algorithms, state handling, etc. 232 Look at the header includes in ``//apps/production/main.cc`` to figure out 233 what modules to study. 234* ``//system/*``: System global accesors. Gives access to pre-created instances 235 of portable system interfaces. For example, ``am::system::RpcServer()`` 236 returns the RPC server instance. 237 238.. _showcase-sense-tutorial-prod-summary: 239 240------- 241Summary 242------- 243You now have a rudimentary but working air quality monitor. More 244importantly, the code that powers your new air quality monitor is 245a solid (but not perfect) starting point for learning how to structure 246your own Pigweed-powered products. 247 248Next, head over to :ref:`showcase-sense-tutorial-crash-handler` to learn about 249the pigweed crash handler and crash snapshots. 250