{"id":3305,"date":"2022-11-15T21:21:23","date_gmt":"2022-11-15T21:21:23","guid":{"rendered":"https:\/\/picockpit.com\/raspberry-pi\/?p=3305"},"modified":"2023-08-16T15:43:48","modified_gmt":"2023-08-16T15:43:48","slug":"raspberry-pi-pico-w-beginners-components-tutorial","status":"publish","type":"post","link":"https:\/\/picockpit.com\/raspberry-pi\/nl\/raspberry-pi-pico-w-beginners-componenten-tutorial\/","title":{"rendered":"Raspberry Pi Pico W tutorial voor beginners"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2-1024x576.jpg\" alt=\"raspberry pi pico w components tutorial\" class=\"wp-image-3374\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2-1024x576.jpg 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2-300x169.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2-768x432.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2-1536x864.jpg 1536w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2-18x10.jpg 18w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2.jpg 1778w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This Raspberry Pi Pico W Beginners Components Tutorial will teach you how to interact with the microcontroller to interact with simple components such as LEDs, ultrasonic sensors and many other items found in a beginner&#8217;s kit.<\/p>\n\n\n\n<p>If you are a complete beginner, this tutorial will help you understand MicroPython so that you can read, write and modify code. With this knowledge, you can combine code and components to create something that has a real-world use case.<\/p>\n\n\n\n<p>If you are coming from a Pico, this tutorial will teach you how to control the Pico W wirelessly. Previously, you could only interact with the Pico through a switch, button or some physical interaction device. Not anymore! You can now control the components with your phone or desktop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tutorial flow<\/h2>\n\n\n\n<p><strong>Introduction<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#Soldering_header_pins\">Soldering header pins<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#Use_Thonny_as_your_code_editor\">Using Thonny<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#Update_your_firmware_by_uploading_the_latest_UF2\">Updating your firmware<\/a><\/li>\n<\/ol>\n\n\n\n<p><strong>The Hello World tutorial for the Pico W<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#1_Serve_a_web_page_that_says_Hello_World_on_the_Pico\">Serve a web page that says \u201cHello World\u201d on the Pico<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#2_Controlling_a_LED_wirelessly\">Controlling a LED wirelessly<\/a><\/li>\n<\/ol>\n\n\n\n<p><strong>One step up<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#3_RGB_LED_on_Pico_W\">Control RGB LED wirelessly<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#4_Buzzer_operation_on_the_Pico_W\">Buzzer operation on the Pico W<\/a><\/li>\n<\/ol>\n\n\n\n<p><strong>Basic ways to transmit data from sensor<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#5_Pico_W_and_HC-SR04_ultrasonic_sensor\">Pico W and HC-SR04 ultrasonic sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#Getting_data_sent_to_your_browser\">Transmit a web page with sensor data<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#6_Ultrasonic_data_but_lets_use_AJAX_to_reduce_payload\">Reducing payload with AJAX<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#7_Pimoroni_Phew_for_cleaner_code\">Pimoroni Phew to streamline endpoint coding<\/a><\/li>\n<\/ol>\n\n\n\n<p><strong>Connecting to web utilities<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#8_Setting_up_the_DHT22_temperature_and_humidity_sensor_to_the_Pico_W\">Log DHT22 sensor climate data to Google Sheets with IFTTT<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#9_Build_a_physical_Spotify_remote_control_with_Raspberry_Pi_Pico_W\">Build a Spotify remote with play\/pause\/skip functions<\/a><\/li>\n<\/ol>\n\n\n\n<p><strong>No-code GPIO control with PiCockpit<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#10_Simple_LED_control_with_PiCockpit_and_Pico_W\">Super simple PiCockpit installation on Pico W<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#10_Simple_LED_control_with_PiCockpit_and_Pico_W\">Simple LED control with PiCockpit and Pico W<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#11_Pico_W_5V_fan_and_a_transistor_controlled_by_PiCockpit\">Pico W, 5V fan and a transistor, controlled by PiCockpit<\/a><\/li>\n<\/ol>\n\n\n\n<p><strong>MQTT<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#Display_photoresistor_using_MQTT_and_Node-RED_with_Pico_W\">Display photoresistor using MQTT and Node-RED with Pico W<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Table of contents<\/h2>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Tutorial goals in summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Learn how to interact with fundamental components that make up larger projects<\/li>\n\n\n\n<li>Control all of these wirelessly &#8212; no switches, buttons or other interaction devices.<\/li>\n\n\n\n<li>Gain a better understanding of the Pico W&#8217;s strengths<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Important links<\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/xuyunzeng\/pico-w-mega-tutorial\">Github repo<\/a> for tutorial code (except secrets.py)<\/p>\n\n\n\n<p><a href=\"https:\/\/docs.micropython.org\/en\/latest\/rp2\/quickref.html\">MicroPython documentation<\/a><\/p>\n\n\n\n<p>Errors, suggestions, comments? Leave a comment in the comment box below, <a href=\"xyz@xyzcreativeworks.com\">email me<\/a> or <a href=\"https:\/\/twitter.com\/xuyunzeng\">Tweet me<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">OSError: [Errno 98] EADDRINUSE<\/h2>\n\n\n\n<p>If you get this error, just unplug and plug in your Raspberry Pi Pico.<\/p>\n\n\n\n<p>You can also do this by typing these commands into Thonny&#8217;s Shell:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import machine\nmachine.reset()<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"364\" height=\"161\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-4.png\" alt=\"\" class=\"wp-image-3474\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-4.png 364w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-4-300x133.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-4-18x8.png 18w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Soldering header pins<\/h2>\n\n\n\n<p>When you buy a Raspberry Pi Pico W, it might not come with headers that will allow you to connect components to your Pico.<\/p>\n\n\n\n<p>At the time of writing, the Pico WH (H for headers) hasn&#8217;t been released. <a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Raspberry_Pi_Pico_Variants\">Our Pico W mega-article is keeping track of its release.<\/a> <\/p>\n\n\n\n<p>If, however, you can find a Pico W with pre-soldered headers, I&#8217;d advise you to buy that.<\/p>\n\n\n\n<p>Nonetheless, for the rest of us, soldering headers onto the Pico W is a simple thing to do. You will need:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Breadboard<\/li>\n\n\n\n<li>Soldering iron and solder<\/li>\n\n\n\n<li>Headers<\/li>\n<\/ul>\n\n\n\n<p>When you buy your headers, make sure to buy one that&#8217;s meant for the Pico W. Unlike headers for the Raspberry Pi Zero-series, the headers on the Pico W aren&#8217;t side-by-side. They are on opposite ends of the board.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"667\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9418-1000px.jpg\" alt=\"\" class=\"wp-image-3310\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9418-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9418-1000px-300x200.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9418-1000px-768x512.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9418-1000px-18x12.jpg 18w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption class=\"wp-element-caption\">A breadboard, the Pico W and 2&#215;20 pin headers<\/figcaption><\/figure>\n\n\n\n<p>The pins have a long side and a short side. You&#8217;ll want the longer pins to be on the side where you see the GPIO labels (GP0, GP1, GND, VBUS, VSYS, etc.)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"667\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9422-1000px.jpg\" alt=\"\" class=\"wp-image-3311\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9422-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9422-1000px-300x200.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9422-1000px-768x512.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9422-1000px-18x12.jpg 18w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption class=\"wp-element-caption\">You will want the longer pins to be on the side opposite where the USB connector is.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"667\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9424-1000px.jpg\" alt=\"\" class=\"wp-image-3312\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9424-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9424-1000px-300x200.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9424-1000px-768x512.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9424-1000px-18x12.jpg 18w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Therefore, insert the longer pins into the breadboard. You will need four holes&#8217; and a gutter worth of spacing between them. If unsure, test with your Pico W.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"667\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9425-1000px.jpg\" alt=\"\" class=\"wp-image-3313\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9425-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9425-1000px-300x200.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9425-1000px-768x512.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/IMG_9425-1000px-18x12.jpg 18w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Soldering is easy. Make sure your soldering iron is hot, and make sure to use the tip of the soldering iron.<\/p>\n\n\n\n<p>What I found most effective was to get the soldering iron&#8217;s tip close to the pin, get the solder touching the tip and see it flow down the pin and create a connection.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"768\" style=\"aspect-ratio: 1024 \/ 768;\" width=\"1024\" controls src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-header-soldering-1.mp4\"><\/video><\/figure>\n\n\n\n<p>After soldering, make sure to check and ensure there isn&#8217;t any extraneous solder that could be connecting two GPIO pins together or some leftover waste solder that&#8217;s on your board.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use Thonny as your code editor<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"593\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-10.png\" alt=\"\" class=\"wp-image-3326\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-10.png 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-10-300x174.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-10-768x445.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-10-18x10.png 18w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Thonny programming a Raspberry Pi Pico W<\/figcaption><\/figure>\n\n\n\n<p>Thonny remains the easiest way to program your Raspberry Pi Pico W.<\/p>\n\n\n\n<p>If you are using the Raspberry Pi OS, you would already have it installed.<\/p>\n\n\n\n<p>However, if you are using Windows or Mac, you will need to download it and configure it.<\/p>\n\n\n\n<p><a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Installing_Thonny_IDE\">Here&#8217;s a guide that will walk you through the steps.<\/a><\/p>\n\n\n\n<p>Also make sure to <a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Uploading_files_to_your_Raspberry_Pi_Pico_W\">refer to the guide on how to upload files onto your Pico W<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Update your firmware by uploading the latest UF2<\/h2>\n\n\n\n<p>When you buy your Pico W, you might have already have the outdated firmware.<\/p>\n\n\n\n<p>Many changes are coming to the Pico W, so it&#8217;s ideal to update your firmware now.<\/p>\n\n\n\n<p>Some changes that I have seen since the day of release are improvements in the WLAN&#8217;s access point function, and future updates could unlock the Bluetooth function on the board.<\/p>\n\n\n\n<p><a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Flashing_the_MicroPython_UF2\">Update now! Here&#8217;s a guide in our mega-article.<\/a><\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-bf8d926f-dd10-4839-9d87-c0098a0e6028\" href=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/rp2-pico-w-20221109-unstable-v1.19.1-642-geefd946e6.zip\">These tutorials uses the 20221109-unstable-v1.19.1 firmware <\/a><a href=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/rp2-pico-w-20221109-unstable-v1.19.1-642-geefd946e6.zip\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-bf8d926f-dd10-4839-9d87-c0098a0e6028\">Download<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Serve a web page that says &#8220;Hello World&#8221; on the Pico<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"950\" height=\"530\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-9.png\" alt=\"\" class=\"wp-image-3325\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-9.png 950w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-9-300x167.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-9-768x428.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-9-18x10.png 18w\" sizes=\"auto, (max-width: 950px) 100vw, 950px\" \/><figcaption class=\"wp-element-caption\">The &#8220;Hello World&#8221; project of the Raspberry Pi Pico W<\/figcaption><\/figure>\n\n\n\n<p>One of the most fundamental projects in all programming tutorials is the &#8220;Hello World&#8221; project.<\/p>\n\n\n\n<p>A microcontroller&#8217;s &#8220;Hello World&#8221; usually involves blinking an LED. <a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Blink_onboard_LED\">It&#8217;s super easy. Here&#8217;s how.<\/a><\/p>\n\n\n\n<p>However, since the Pico W can serve a web page, let&#8217;s start by learning how to serve a web page that has a &#8220;Hello World&#8221; message.<\/p>\n\n\n\n<p>The setup used here will form the most fundamental building block for the rest of the tutorials.<\/p>\n\n\n\n<p>There are two ways you can connect to the Pico W. You can make it join a WiFi network or you can <a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Broadcasting_a_WiFi_network_SoftAP_access_point\">broadcast a SoftAP hotspot similar to what your smartphone does<\/a>. If you want to try the latter, <a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Broadcasting_a_WiFi_network_SoftAP_access_point\">follow this link<\/a>. However, for consistency throughout this tutorial, we will always connect to a WiFi network rather than broadcast one from the Pico W.<\/p>\n\n\n\n<p>So, fundamentally, the steps for serving a web page involves:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Connecting to WiFi<\/li>\n\n\n\n<li>Writing code to serve index.html to anyone who connects to the Pico W&#8217;s IP address<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s set up a few files. Save these and upload them to your Raspberry Pi Pico W. <a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Uploading_files_to_your_Raspberry_Pi_Pico_W\">Here&#8217;s how to upload files, in case you missed it.<\/a><\/p>\n\n\n\n<p><strong>wifi.py<\/strong><\/p>\n\n\n\n<p>wifi.py is a boilerplate meant to help you to connect to your WiFi network. Creating a separate file and importing it into <strong>main.py<\/strong> file later will help reduce cluttered code.<\/p>\n\n\n\n<p>Note that you should change your country&#8217;s code in the line <strong>rp2.country(&#8216;DE&#8217;) <\/strong>if your country is not Germany.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import rp2\nimport network\nimport ubinascii\nimport machine\nimport urequests as requests\nimport time\nfrom secrets import secrets\n\n\ndef init_wifi():\n    # Set country to avoid possible errors\n    rp2.country('DE')\n\n    wlan = network.WLAN(network.STA_IF)\n    wlan.active(True)\n\n    # Load login data from different file for safety reasons\n    ssid = secrets&#091;'ssid']\n    pw = secrets&#091;'pw']\n\n    wlan.connect(ssid, pw)\n\n    # Wait for connection with 10 second timeout\n    timeout = 10\n    while timeout &gt; 0:\n        if wlan.status() &lt; 0 or wlan.status() &gt;= 3:\n            break\n        timeout -= 1\n        print('Waiting for connection...')\n        time.sleep(1)\n\n    # Define blinking function for onboard LED to indicate error codes    \n    def blink_onboard_led(num_blinks):\n        led = machine.Pin('LED', machine.Pin.OUT)\n        for i in range(num_blinks):\n            led.on()\n            time.sleep(.2)\n            led.off()\n            time.sleep(.2)\n\n    wlan_status = wlan.status()\n    blink_onboard_led(wlan_status)\n\n    if wlan_status != 3:\n        raise RuntimeError('Wi-Fi connection failed')\n    else:\n        print('Connected')\n        status = wlan.ifconfig()\n        print('ip = ' + status&#091;0])\n<\/code><\/pre>\n\n\n\n<p><strong>secrets.py<\/strong><\/p>\n\n\n\n<p><strong>wifi.py<\/strong> imports <strong>secrets.py<\/strong>, where you store your WiFi network&#8217;s info. <\/p>\n\n\n\n<p><strong>secrets.py<\/strong> is a simple JSON file which contains your WiFi SSID and password.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>secrets = {\n    'ssid': 'SM-A520W9371',\n    'pw': 'starting',\n    }\n<\/code><\/pre>\n\n\n\n<p><strong>serve_webpage.py<\/strong><\/p>\n\n\n\n<p>As the name suggests, this page serves websites to users who connect to the Pico W.<\/p>\n\n\n\n<p>Upon receiving a connection, the Pico W finds a file called <strong>index.html<\/strong> and sends it to the connected client, as seen in the line <strong>response = get_html(&#8216;index.html&#8217;)<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import socket\n\ndef serve_webpage():\n    #Function to load in html page    \n    def get_html(html_name):\n        # open html_name (index.html), 'r' = read-only as variable 'file'\n        with open(html_name, 'r') as file:\n            html = file.read()\n            \n        return html\n\n    # HTTP server with socket\n    addr = socket.getaddrinfo('0.0.0.0', 80)&#091;0]&#091;-1]\n\n    s = socket.socket()\n    s.bind(addr)\n    s.listen(1)\n\n    print('Listening on', addr)\n\n    # Listen for connections\n    while True:\n        try:\n            cl, addr = s.accept()\n            print('Client connected from', addr)\n            response = get_html('index.html')\n            cl.send('HTTP\/1.0 200 OK\\r\\nContent-type: text\/html\\r\\n\\r\\n')\n            cl.send(response)\n            cl.close()\n            \n        except OSError as e:\n            cl.close()\n            print('Connection closed')\n\n<\/code><\/pre>\n\n\n\n<p>Finally, we need to create the <strong>index.html<\/strong> file that will be sent to a connected client.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Pico W&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt;Hello World&lt;\/h1&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>This is a simple HTML boilerplate with two changes: one to the &lt;title&gt; tag which outputs &#8220;Pico W&#8221; as the title and the &lt;h1&gt; tag which says &#8220;Hello World&#8221;.<\/p>\n\n\n\n<p><strong>main.py<\/strong><\/p>\n\n\n\n<p>Since we have placed all the code elsewhere, our <strong>main.py<\/strong> file just has to import and call these functions in order to serve the Hello World web page. <\/p>\n\n\n\n<p>As you can see, we initialize WiFi first before serving the web page.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from wifi import init_wifi\nfrom serve_webpage import serve_webpage\n\ninit_wifi()\nserve_webpage()<\/code><\/pre>\n\n\n\n<p>You&#8217;re almost there!<\/p>\n\n\n\n<p>Referring to the screenshot below, make sure:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You have uploaded five files into your Raspberry Pi Pico W (see bottom left red box)<\/li>\n\n\n\n<li>Make sure your interpreter is set to <strong>MicroPython (Raspberry Pi Pico)<\/strong> (see bottom right box)<\/li>\n\n\n\n<li>Then, highlight the <strong>main.py<\/strong> in your code editor, and click the green run button (top left red box)<\/li>\n\n\n\n<li>Once you run this, you will see your IP address in the Shell. Go to your browser and type in this address and you will see the Hello World web page.<\/li>\n\n\n\n<li>If your Shell is not open, go to View -&gt; Shell.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"972\" height=\"753\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-13.png\" alt=\"\" class=\"wp-image-3337\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-13.png 972w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-13-300x232.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-13-768x595.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-13-15x12.png 15w\" sizes=\"auto, (max-width: 972px) 100vw, 972px\" \/><\/figure>\n\n\n\n<p>If all is successful, you will see the page below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"928\" height=\"435\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-11.png\" alt=\"\" class=\"wp-image-3335\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-11.png 928w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-11-300x141.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-11-768x360.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-11-18x8.png 18w\" sizes=\"auto, (max-width: 928px) 100vw, 928px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Controlling a LED wirelessly<\/h2>\n\n\n\n<p>Now that you have the basics set up, let&#8217;s go one step forward.<\/p>\n\n\n\n<p>One of the most fundamental Raspberry Pi projects involves blinking an LED.<\/p>\n\n\n\n<p>Let&#8217;s up that a notch by controlling the LED wirelessly. We want to be able to blink, turn on and off the LED.<\/p>\n\n\n\n<p>In order to do this, you will need to set up a circuit and a web server with three buttons &#8212; ON, OFF, BLINK.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"252\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-14.png\" alt=\"\" class=\"wp-image-3339\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-14.png 452w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-14-300x167.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-14-18x10.png 18w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><\/figure>\n<\/div>\n\n\n<p>For this project, you will need an LED, a 330 ohm resistor, one jumper wire and a breadboard. <\/p>\n\n\n\n<p>We will use a red LED because most kits will have it. Do note that if you use an LED of any other colour, you will need to adjust the resistor.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"750\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-led-tutorial-1000px.jpg\" alt=\"\" class=\"wp-image-3340\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-led-tutorial-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-led-tutorial-1000px-300x225.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-led-tutorial-1000px-768x576.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-led-tutorial-1000px-16x12.jpg 16w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Here&#8217;s how to connect the components<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GPIO 2 -&gt; LED&#8217;s long leg (anode\/positive)<\/li>\n\n\n\n<li>GND -&gt; 330 ohm resistor -&gt; LED&#8217;s short leg (cathode\/negative)<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"946\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/blinking_led_schematic-1.jpg\" alt=\"\" class=\"wp-image-3348\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/blinking_led_schematic-1.jpg 662w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/blinking_led_schematic-1-210x300.jpg 210w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/blinking_led_schematic-1-8x12.jpg 8w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">Code to control LED on Pico W<\/h3>\n\n\n\n<p>Let&#8217;s build upon what we did in the previous tutorial. The only two files we will need to modify are <strong>index.html <\/strong>to add buttons and <strong>main.py<\/strong> to interact with the LED based on the input from <strong>index.html<\/strong>.<\/p>\n\n\n\n<p>The parts that are bold indicate the new lines that were added to <strong>index.html. <\/strong>They add three buttons and a paragraph that says &#8220;Control the LED&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Pico W&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt;Pico W&lt;\/h1&gt;\n<strong>        &lt;p&gt;Control the LED&lt;\/p&gt;\n        &lt;a href=\\\"?led=on\\\"&gt;&lt;button&gt;ON&lt;\/button&gt;&lt;\/a&gt;&amp;nbsp;\n        &lt;a href=\\\"?led=off\\\"&gt;&lt;button&gt;OFF&lt;\/button&gt;&lt;\/a&gt;\n        &lt;a href=\\\"?led=blink\\\"&gt;&lt;button&gt;BLINK&lt;\/button&gt;&lt;\/a&gt;<\/strong>\n    &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Notice when you press the buttons, you will see a parameter added to your Pico W&#8217;s IP address (e.g. <strong>http:\/\/192.168.43.134\/%22?led=blink\\<\/strong>). These parameters are captured by the Pico W&#8217;s backend and controls the LED.<\/p>\n\n\n\n<p>We are going to move <strong>serve_webpage.py<\/strong>&#8216;s code into the <strong>main.py<\/strong> file.<\/p>\n\n\n\n<p>Here&#8217;s <strong>main.py<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from wifi import init_wifi\nimport socket\nimport machine\nimport time\n\ninit_wifi()\n\n\n#LED controls\nled = machine.Pin(2, machine.Pin.OUT)\n      \ndef blink_led():\n    led.on()\n    time.sleep(0.2)\n    led.off()\n    time.sleep(0.2)\n\n#Function to load in html page    \ndef get_html(html_name):\n    # open html_name (index.html), 'r' = read-only as variable 'file'\n    with open(html_name, 'r') as file:\n        html = file.read()\n        \n    return html\n\n# HTTP server with socket\naddr = socket.getaddrinfo('0.0.0.0', 80)&#091;0]&#091;-1]\n\ns = socket.socket()\ns.bind(addr)\ns.listen(1)\n\nprint('Listening on', addr)\n\n# Listen for connections\nwhile True:\n    try:\n        cl, addr = s.accept()\n        print('Client connected from', addr)\n        request = cl.recv(1024)\n        print(request)\n        \n        request = str(request)\n        led_on = request.find('?led=on')\n        led_off = request.find('?led=off')\n        led_blink = request.find('?led=blink')\n        print('led_on = ', led_on)\n        print('led_off = ', led_off)\n        print('led_blink = ', led_blink)\n        if led_on &gt; -1:\n            print('LED ON')\n            led.on()\n            \n        if led_off &gt; -1:\n            print('LED OFF')\n            led.off()\n            \n        if led_blink &gt; -1:\n            print('LED BLINK')\n            blink_led()\n            \n        response = get_html('index.html')\n        cl.send('HTTP\/1.0 200 OK\\r\\nContent-type: text\/html\\r\\n\\r\\n')\n        cl.send(response)\n        cl.close()\n        \n    except OSError as e:\n        cl.close()\n        print('Connection closed')<\/code><\/pre>\n\n\n\n<p>The first, and key, segment is below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>request = cl.recv(1024)\n        print(request)\n        \n        request = str(request)\n        led_on = request.find('?led=on')\n        led_off = request.find('?led=off')\n        led_blink = request.find('?led=blink')\n        print('led_on = ', led_on)\n        print('led_off = ', led_off)\n        print('led_blink = ', led_blink)\n        if led_on &gt; -1:\n            print('LED ON')\n            led.on()\n            \n        if led_off &gt; -1:\n            print('LED OFF')\n            led.off()\n            \n        if led_blink &gt; -1:\n            print('LED BLINK')\n            blink_led()<\/code><\/pre>\n\n\n\n<p>The variable &#8220;request&#8221;, when printed, outputs the first block of text below. The last three lines are the print statements that check for LED on, off or blink: <\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>b'GET \/%22?led=blink\\\\%22 HTTP\/1.1\\r\\nHost: 192.168.43.134\\r\\nConnection: keep-alive\\r\\nCache-Control: max-age=0\\r\\nUpgrade-Insecure-Requests: 1\\r\\nUser-Agent: Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/107.0.0.0 Safari\/537.36\\r\\nAccept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/avif,image\/webp,image\/apng,*\/*;q=0.8,application\/signed-exchange;v=b3;q=0.9\\r\\nReferer: http:\/\/192.168.43.134\/%22?led=on\\\\%22\\r\\nAccept-Encoding: gzip, deflate\\r\\nAccept-Language: en-US,en;q=0.9,es;q=0.8,zh-TW;q=0.7,zh-CN;q=0.6,zh;q=0.5\\r\\n\\r\\n'\n<strong>led_on =  456\nled_off =  -1\nled_blink =  10<\/strong><\/code><\/pre>\n\n\n\n<p>The code above will try to search out specific strings such as &#8220;<strong>led=on<\/strong>&#8220;. If it exists, the value will be more than -1, therefore triggering the relevant <strong>if<\/strong> statement.<\/p>\n\n\n\n<p>For example, if led=on exists in the parameters, then the variable led_on will be more than -1, therefore the <strong>if led_on &gt; -1<\/strong> statement triggers and runs <strong>led.on()<\/strong>;<\/p>\n\n\n\n<p>The only complicated bit here is the <strong>led_blink<\/strong> function which will trigger the function:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>    def blink_led():\n        led.on()\n        time.sleep(0.2)\n        led.off()\n        time.sleep(0.2)<\/code><\/pre>\n\n\n\n<p>Finally, here&#8217;s how you initialize GPIO 2 to operate the LED:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><strong>import machine\nimport time<\/strong>\n    \n#LED controls\n<strong>led = machine.Pin(2, machine.Pin.OUT)<\/strong><\/code><\/pre>\n\n\n\n<p>We import machine to interact with the GPIO pin. As you can see in the variable <strong>led<\/strong>, we want the Pico W to power GPIO 2. <\/p>\n\n\n\n<p>We import <strong>time<\/strong> so that we can have a pause of 0.2 seconds in <strong>blink_led().<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. RGB LED on Pico W<\/h2>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-2 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"750\" data-id=\"3358\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-3-1000px.jpg\" alt=\"\" class=\"wp-image-3358\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-3-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-3-1000px-300x225.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-3-1000px-768x576.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-3-1000px-16x12.jpg 16w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"750\" data-id=\"3360\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-2-1000px.jpg\" alt=\"\" class=\"wp-image-3360\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-2-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-2-1000px-300x225.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-2-1000px-768x576.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-2-1000px-16x12.jpg 16w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"750\" data-id=\"3362\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-1-1000px.jpg\" alt=\"\" class=\"wp-image-3362\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-1-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-1-1000px-300x225.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-1-1000px-768x576.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-1-1000px-16x12.jpg 16w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"750\" data-id=\"3361\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-4-1000px.jpg\" alt=\"\" class=\"wp-image-3361\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-4-1000px.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-4-1000px-300x225.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-4-1000px-768x576.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-rgb-led-4-1000px-16x12.jpg 16w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>To light up your RGB LED, you need:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Three 330-ohm resistors<\/li>\n\n\n\n<li>One RGB LED<\/li>\n\n\n\n<li>One jumper wire<\/li>\n<\/ul>\n\n\n\n<p>On the RGB LED, you will find four legs. One leg will be the longest. That is either a cathode (negative) or anode (positive). My RGB LED had a shared cathode so here&#8217;s the connection:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"392\" height=\"306\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-16.png\" alt=\"\" class=\"wp-image-3349\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-16.png 392w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-16-300x234.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-16-15x12.png 15w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><figcaption class=\"wp-element-caption\">Image shows R, G, B legs on a common cathode RGB LED. (Image from Raspberry Pi Foundation CC-BY-SA)<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GPIO 15 -&gt; 330-ohm resistor -&gt; red LED<\/li>\n\n\n\n<li>GPIO 17 -&gt; resistor -&gt; green LED<\/li>\n\n\n\n<li>GPIO 16 -&gt; resistor -&gt; blue LED<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"946\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/03-rgb-led_bb.jpg\" alt=\"\" class=\"wp-image-3356\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/03-rgb-led_bb.jpg 709w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/03-rgb-led_bb-225x300.jpg 225w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/03-rgb-led_bb-9x12.jpg 9w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p>Here&#8217;s some code to see if you wired it up right:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import machine\n\nred_led = machine.PWM(machine.Pin(15))\ngreen_led = machine.PWM(machine.Pin(17))\nblue_led = machine.PWM(machine.Pin(16))\n\nred_led.duty_u16(65534)\ngreen_led.duty_u16(65534)\nblue_led.duty_u16(65534)<\/code><\/pre>\n\n\n\n<p>In this example, we are using PWM, which allows you to vary the brightness of the R, G, B LEDs.<\/p>\n\n\n\n<p>You can change the values passed to [colour].duty_u16 to a value between 0 and 65534. Theoretically, you should be able to pass 65535, but somehow that doesn&#8217;t seem to work for me.<\/p>\n\n\n\n<p>Think of passing &#8220;0&#8221; as saying you want zero per cent brightness. If you pass it 65534, you want 100 per cent brightness.<\/p>\n\n\n\n<p>If you do 65534 for one colour and zero for the rest, you will be able to tell whether you have connected the right GPIO pins to the right LED colour.<\/p>\n\n\n\n<p>So why are we using PWM? Because it will help you get more colours. If you just use an &#8220;on-and-off&#8221; method, you can get red, green, blue, white-ish and no light. With PWM, you can vary the intensity of the R, G, B LED and create as many colours as you can imagine.<\/p>\n\n\n\n<p>Now, let&#8217;s create something that you can control wirelessly!<\/p>\n\n\n\n<p><strong>index.html<\/strong><\/p>\n\n\n\n<p>The main change here is to have a <strong>&lt;form&gt;<\/strong> which has three sliders. These sliders have a value between zero and 100.<\/p>\n\n\n\n<p>Using values zero to 100 helps you visualize brightness as a percentage. It also reduces the amount of code needed to parse out the value from the parameters (seen later in <strong>main.py<\/strong>)<\/p>\n\n\n\n<p>When you have set the values for the R, G, B LEDs, you would press submit, and this data is captured by the Pico W.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Pico W&lt;\/title&gt;\n    &lt;\/head&gt;\n      &lt;body&gt;\n        &lt;h1&gt;Pico W RGB LED&lt;\/h1&gt;\n        &lt;form id=\"form\"&gt;\n            \n            &lt;input type=\"range\" min=\"0\" max=\"100\" value=\"slider_value\" name=\"red\" id=\"red\"&gt;\n            &lt;label for=\"red\"&gt;R&lt;\/p&gt;\n            \n            &lt;input type=\"range\" min=\"0\" max=\"100\" value=\"slider_value\" name=\"green\" id=\"green\"&gt;\n             &lt;label for=\"green\"&gt;G&lt;\/p&gt;\n            \n            &lt;input type=\"range\" min=\"0\" max=\"100\" value=\"slider_value\" name=\"blue\" id=\"blue\"&gt;\n            &lt;label for=\"blue\"&gt;B&lt;\/p&gt;\n            \n            &lt;input type=\"submit\" id=\"submit\"&gt;\n        &lt;\/form&gt;\n        &lt;script&gt;\n        \n            submit.addEventListener('click', function ()  {\n                form.submit();\n            }, false);\n        \n        &lt;\/script&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>main.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from wifi import init_wifi\nimport socket\nimport machine\nimport time\n\ninit_wifi()\n\n#LED controls\nred_led = machine.PWM(machine.Pin(15))\ngreen_led = machine.PWM(machine.Pin(17))\nblue_led = machine.PWM(machine.Pin(16))\n\n#Function to load in html page    \ndef get_html(html_name):\n    # open html_name (index.html), 'r' = read-only as variable 'file'\n    with open(html_name, 'r') as file:\n        html = file.read()\n    return html\n\ndef find_intensity(color, request_str):\n    index = request_str.find(color) + len(color)\n    offset = 0\n    if request_str&#091;index].isdigit():\n        offset = 1\n        if request_str&#091;index+1].isdigit():\n            offset = 2\n            if request_str&#091;index+2].isdigit():\n                offset = 3\n\n    intensity = int(request_str&#091;index:index+offset])\n    return intensity\n    \n    \n# HTTP server with socket\naddr = socket.getaddrinfo('0.0.0.0', 80)&#091;0]&#091;-1]\n\ns = socket.socket()\ns.bind(addr)\ns.listen(1)\n\nprint('Listening on', addr)\n\n# Listen for connections\nwhile True:\n    try:\n        cl, addr = s.accept()\n        print('Client connected from', addr)\n        request = cl.recv(1024)\n        \n        request_str = str(request)\n       \n<strong>        #find intensity ONLY if the params exist in request\n        if request_str.find('red') &gt; -1 :\n            #int = get rid of decimal\n            #\/100*65534 = find_intensity returns something 0 to 100,\n            # so x\/100 = proportion of 65534 that you want to send to LED\n            # 65534 = max number you can use for PWM\n            red_intensity = int(find_intensity('red=', request_str) \/100 * 65534)\n            green_intensity = int(find_intensity('green=', request_str) \/100 * 65534)\n            blue_intensity = int(find_intensity('blue=', request_str) \/100 * 65534)\n            \n            #print('r=' + str(red_intensity))\n            #print('g=' + str(green_intensity))\n            #print('b=' + str(blue_intensity))\n            \n            red_led.duty_u16(red_intensity)\n            green_led.duty_u16(green_intensity)\n            blue_led.duty_u16(blue_intensity)<\/strong>\n                        \n        response = get_html('index.html')\n        cl.send('HTTP\/1.0 200 OK\\r\\nContent-type: text\/html\\r\\n\\r\\n')\n        cl.send(response)\n        cl.close()\n        \n    except OSError as e:\n        cl.close()\n        print('Connection closed')\n<\/code><\/pre>\n\n\n\n<p>As you press submit on the <strong>index.html<\/strong> web page, the Pico W will take the parameters and process it.<\/p>\n\n\n\n<p>Let&#8217;s look at some key points in the code, bolded in the code block above.<\/p>\n\n\n\n<p><strong>find_intensity function<\/strong><\/p>\n\n\n\n<p>This function takes two params: <strong>color <\/strong>and <strong>request_str<\/strong>. <strong>color<\/strong> takes in &#8220;red&#8221;, &#8220;green&#8221; or &#8220;blue&#8221; and finds the value after the equals (=) sign.<\/p>\n\n\n\n<p>For example, your URL after you submit the form is &#8220;http:\/\/192.168.1.142\/?red=89&amp;green=50&amp;blue=50&#8221;.<\/p>\n\n\n\n<p>If you pass &#8220;red&#8221; to find_intensity, it will return 89.<\/p>\n\n\n\n<p><strong>intensity parser<\/strong><\/p>\n\n\n\n<p>The second bolded block of code represents the code that tells the Pico W&#8217;s GPIO how much brightness you want from each LED.<\/p>\n\n\n\n<p>First up, we must ensure that the params exist in the URL. This is done by the <strong>if<\/strong> statement <strong>request_str.find(&#8216;red&#8217;) &gt; -1<\/strong>. I just used <strong>&#8216;red&#8217;<\/strong> because the params will 100% contain the string &#8216;red&#8217; if you use the form.<\/p>\n\n\n\n<p>If you are visiting your Pico W&#8217;s IP address (e.g. http:\/\/192.168.1.142\/) for the first time, you won&#8217;t have the params, so the program will crash if you ran <strong>find_intensity<\/strong>.<\/p>\n\n\n\n<p>If params exist, we find the intensity for each LED according to the values submitted. Let&#8217;s take a look at <strong>red_intensity.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><strong>red_intensity = int(find_intensity('red=', request_str) \/100 * 65534)<\/strong>\n...\n<strong>red_led.duty_u16(red_intensity)<\/strong><\/code><\/pre>\n\n\n\n<p><strong>find_intensity<\/strong> returns an integer zero to 100. We divide this by 100 so you can get a percentage. This percentage divides the maximum value that the <strong>duty_u16 <\/strong>method can take. <\/p>\n\n\n\n<p>However, we need an <strong>int<\/strong> function to wrap this because you can get a float sometimes and <strong>duty_u16 <\/strong>needs an <strong>int<\/strong>. For example, say you wanted 41% brightness &#8212; 41% of 65534 is 26,868.94. You can&#8217;t pass this float as the program will crash.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Buzzer operation on the Pico W<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"750\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/raspberry-pi-pico-w-buzzer.jpg\" alt=\"\" class=\"wp-image-3372\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/raspberry-pi-pico-w-buzzer.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/raspberry-pi-pico-w-buzzer-300x225.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/raspberry-pi-pico-w-buzzer-768x576.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/raspberry-pi-pico-w-buzzer-16x12.jpg 16w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>A buzzer is a pretty important component for basic notifications, just like how an LED can tell you about the state of something, a buzzer give you an auditory notification.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/04-buzzer_bb.jpg\" alt=\"\" class=\"wp-image-3371\" style=\"width:292px;height:474px\" width=\"292\" height=\"474\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/04-buzzer_bb.jpg 583w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/04-buzzer_bb-184x300.jpg 184w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/04-buzzer_bb-7x12.jpg 7w\" sizes=\"auto, (max-width: 292px) 100vw, 292px\" \/><figcaption class=\"wp-element-caption\">Connect the buzzer&#8217;s positive terminal to GPIO 16 and ground to a GND pin on the Pico W.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Rather than implementing my own code, I&#8217;ve chosen to use <a href=\"https:\/\/peppe8o.com\/passive-buzzer-with-raspberry-pi-pico-and-micropython\/\" rel=\"nofollow\">Giuseppe Cassibba&#8217;s implementation<\/a>. If you ran his code on the Pico W, you will hear beeps on your buzzer.<\/p>\n\n\n\n<p>But since this code is meant for the Pico, it won&#8217;t have any wireless interactivity features. <\/p>\n\n\n\n<p>So, let&#8217;s get the fork out!<\/p>\n\n\n\n<p>First, let&#8217;s modify the <strong>index.html<\/strong> to implement the buttons. Let&#8217;s have a button for &#8220;ON&#8221;, &#8220;OFF&#8221;, &#8220;SCALE&#8221;, &#8220;MUSIC&#8221;.<\/p>\n\n\n\n<p>The first two buttons are self-explanatory. The third button plays a C-scale and the last one plays a piece of music.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Pico W&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt;Pico W Buzzer&lt;\/h1&gt;\n        &lt;p&gt;Control the buzzer&lt;\/p&gt;\n        &lt;a href=\\\"?buzzer=on\\\"&gt;&lt;button&gt;ON&lt;\/button&gt;&lt;\/a&gt;&amp;nbsp;\n        &lt;a href=\\\"?buzzer=off\\\"&gt;&lt;button&gt;OFF&lt;\/button&gt;&lt;\/a&gt;\n        &lt;a href=\\\"?buzzer=scale\\\"&gt;&lt;button&gt;SCALE&lt;\/button&gt;&lt;\/a&gt;\n        &lt;a href=\\\"?buzzer=music\\\"&gt;&lt;button&gt;MUSIC&lt;\/button&gt;&lt;\/a&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Just like before, we create a few &lt;button&gt; tags with &lt;a&gt; tags surrounding them. Upon clicking the buttons, the URL will have a parameter such as <strong>\/buzzer=on<\/strong>. The Pico W reads this and turns the buzzer on.<\/p>\n\n\n\n<p>Now, let&#8217;s look at Giuseppe&#8217;s code for the Pico:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from machine import Pin, PWM\nfrom time import sleep\n\nbuzzerPIN=16\nBuzzerObj=PWM(Pin(buzzerPIN))\n\ndef buzzer(buzzerPinObject,frequency,sound_duration,silence_duration):\n    # Set duty cycle to a positive value to emit sound from buzzer\n    buzzerPinObject.duty_u16(int(65536*0.2))\n    # Set frequency\n    buzzerPinObject.freq(frequency)\n    # wait for sound duration\n    sleep(sound_duration)\n    # Set duty cycle to zero to stop sound\n    buzzerPinObject.duty_u16(int(65536*0))\n    # Wait for sound interrumption, if needed \n    sleep(silence_duration)\n\n\n# Play following notes by changing frequency:\n#C (DO)\nbuzzer(BuzzerObj,523,0.5,0.1)\n\n#D (RE)\nbuzzer(BuzzerObj,587,0.5,0.1)\n\n#E (MI)\nbuzzer(BuzzerObj,659,0.5,0.1)\n\n#F (FA)\nbuzzer(BuzzerObj,698,0.5,0.1)\n\n#G (SOL)\nbuzzer(BuzzerObj,784,0.5,0.1)\n\n#A (LA)\nbuzzer(BuzzerObj,880,0.5,0.1)\n\n#B (SI)\nbuzzer(BuzzerObj,987,0.5,0.1)\n\n#Deactivates the buzzer\nBuzzerObj.deinit()<\/code><\/pre>\n\n\n\n<p>His code plays a C-scale when run using the function <strong>buzzer()<\/strong>, which takes in four parameters: Buzzer object, frequency in Hertz, sound duration and pause duration before playing the next sound.<\/p>\n\n\n\n<p>Let&#8217;s modify the code so that we can activate the <strong>ON, OFF, SCALE <\/strong>and <strong>MUSIC<\/strong>.<\/p>\n\n\n\n<p>Here&#8217;s how we integrate Giuseppe&#8217;s code into our <strong>main.py<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from wifi import init_wifi\nimport socket\nimport machine\nimport time\n\ninit_wifi()\n\n# Buzzer\nbuzzerPIN = 16\nBuzzerObj = machine.PWM(machine.Pin(buzzerPIN))\n\n\ndef buzzer(buzzerPinObject, frequency, sound_duration, silence_duration):\n    # Set duty cycle to a positive value to emit sound from buzzer\n    buzzerPinObject.duty_u16(int(65536*0.2))\n    # Set frequency\n    buzzerPinObject.freq(frequency)\n    # wait for sound duration\n    time.sleep(sound_duration)\n    # Set duty cycle to zero to stop sound\n    buzzerPinObject.duty_u16(int(65536*0))\n    # Wait for sound interrumption, if needed\n    time.sleep(silence_duration)\n\n# Function to load in html page\n\n\ndef get_html(html_name):\n    # open html_name (index.html), 'r' = read-only as variable 'file'\n    with open(html_name, 'r') as file:\n        html = file.read()\n\n    return html\n\n\n# HTTP server with socket\naddr = socket.getaddrinfo('0.0.0.0', 80)&#091;0]&#091;-1]\n\ns = socket.socket()\ns.bind(addr)\ns.listen(1)\n\nprint('Listening on', addr)\n\n# Listen for connections\nwhile True:\n    try:\n        cl, addr = s.accept()\n        print('Client connected from', addr)\n        request = cl.recv(1024)\n\n        request = str(request)\n        buzzer_on = request.find('?buzzer=on')\n        buzzer_off = request.find('?buzzer=off')\n        buzzer_scale = request.find('?buzzer=scale')\n        buzzer_music = request.find('?buzzer=music')\n\n        if buzzer_on &gt; -1:\n            BuzzerObj.duty_u16(int(65536*0.2))\n            BuzzerObj.freq(440)\n\n        if buzzer_off &gt; -1:\n            BuzzerObj.duty_u16(0)\n\n        if buzzer_scale &gt; -1:\n            #C (DO)\n            buzzer(BuzzerObj, 523, 0.5, 0.1)\n\n            #D (RE)\n            buzzer(BuzzerObj, 587, 0.5, 0.1)\n\n            #E (MI)\n            buzzer(BuzzerObj, 659, 0.5, 0.1)\n\n            #F (FA)\n            buzzer(BuzzerObj, 698, 0.5, 0.1)\n\n            #G (SOL)\n            buzzer(BuzzerObj, 784, 0.5, 0.1)\n\n            #A (LA)\n            buzzer(BuzzerObj, 880, 0.5, 0.1)\n\n            #B (SI)\n            buzzer(BuzzerObj, 987, 0.5, 0.1)\n\n            BuzzerObj.deinit()\n\n        if buzzer_music &gt; -1:\n            pass\n\n        response = get_html('index.html')\n        cl.send('HTTP\/1.0 200 OK\\r\\nContent-type: text\/html\\r\\n\\r\\n')\n        cl.send(response)\n        cl.close()\n\n    except OSError as e:\n        cl.close()\n        print('Connection closed')\n<\/code><\/pre>\n\n\n\n<p>In the above implementation, we haven&#8217;t written the code for <strong>MUSIC<\/strong>.<\/p>\n\n\n\n<p>The code is very similar to the red LED tutorial above, where the Pico W captures the parameters after the Pico W&#8217;s IP address. If the param <strong>buzzer=ON,<\/strong> it will play a 440Hz sound. If <strong>buzzer=scale<\/strong>, it will play the scale that&#8217;s taken from Giuseppe&#8217;s code.<\/p>\n\n\n\n<p>What about implementing music? Implementing music is a bit more complicated so we should create a new file called <strong>constants.py<\/strong> and add a few lines into our <strong>main.py<\/strong>.<\/p>\n\n\n\n<p>This code is a fork from <a href=\"https:\/\/create.arduino.cc\/projecthub\/410027\/rickroll-piezo-buzzer-a1cd11\" rel=\"nofollow\">Rowan Packard&#8217;s Arduino code.<\/a><\/p>\n\n\n\n<p><strong>constants.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>A3F = 208  # 208 Hz\nB3F = 233  # 233 Hz\nB3 = 247  # 247 Hz\nC4 = 261  # 261 Hz MIDDLE C\nC4S = 277  # 277 Hz\nE4F = 311  # 311 Hz\nF4 = 349  # 349 Hz\nA4F = 415  # 415 Hz\nB4F = 466  # 466 Hz\nB4 = 493  # 493 Hz\nC5 = 523  # 523 Hz\nC5S = 554  # 554 Hz\nE5F = 622  # 622 Hz\nF5 = 698  # 698 Hz\nF5S = 740  # 740 Hz\nA5F = 831  # 831 Hz\n<\/code><\/pre>\n\n\n\n<p><strong>main.py<\/strong> (additions bolded)<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from wifi import init_wifi\nimport socket\nimport machine\nimport time\n<strong>from constants import *<\/strong>\n\ninit_wifi()\n\n# Buzzer\nbuzzerPIN = 16\nBuzzerObj = machine.PWM(machine.Pin(buzzerPIN))\n\n\ndef buzzer(buzzerPinObject, frequency, sound_duration, silence_duration):\n    # Set duty cycle to a positive value to emit sound from buzzer\n    buzzerPinObject.duty_u16(int(65536*0.2))\n    # Set frequency\n    buzzerPinObject.freq(frequency)\n    # wait for sound duration\n    time.sleep(sound_duration)\n    # Set duty cycle to zero to stop sound\n    buzzerPinObject.duty_u16(int(65536*0))\n    # Wait for sound interrumption, if needed\n    time.sleep(silence_duration)\n\n# Function to load in html page\n\n\ndef get_html(html_name):\n    # open html_name (index.html), 'r' = read-only as variable 'file'\n    with open(html_name, 'r') as file:\n        html = file.read()\n\n    return html\n\n\n# HTTP server with socket\naddr = socket.getaddrinfo('0.0.0.0', 80)&#091;0]&#091;-1]\n\ns = socket.socket()\ns.bind(addr)\ns.listen(1)\n\nprint('Listening on', addr)\n\n# Listen for connections\nwhile True:\n    try:\n        cl, addr = s.accept()\n        print('Client connected from', addr)\n        request = cl.recv(1024)\n\n        request = str(request)\n        buzzer_on = request.find('?buzzer=on')\n        buzzer_off = request.find('?buzzer=off')\n        buzzer_scale = request.find('?buzzer=scale')\n        buzzer_music = request.find('?buzzer=music')\n\n        if buzzer_on &gt; -1:\n            BuzzerObj.duty_u16(int(65536*0.2))\n            BuzzerObj.freq(440)\n\n        if buzzer_off &gt; -1:\n            BuzzerObj.duty_u16(0)\n\n        if buzzer_scale &gt; -1:\n            #C (DO)\n            buzzer(BuzzerObj, 523, 0.5, 0.1)\n\n            #D (RE)\n            buzzer(BuzzerObj, 587, 0.5, 0.1)\n\n            #E (MI)\n            buzzer(BuzzerObj, 659, 0.5, 0.1)\n\n            #F (FA)\n            buzzer(BuzzerObj, 698, 0.5, 0.1)\n\n            #G (SOL)\n            buzzer(BuzzerObj, 784, 0.5, 0.1)\n\n            #A (LA)\n            buzzer(BuzzerObj, 880, 0.5, 0.1)\n\n            #B (SI)\n            buzzer(BuzzerObj, 987, 0.5, 0.1)\n\n            BuzzerObj.deinit()\n\n        if buzzer_music &gt; -1:\n           <strong> pause = 0.0<\/strong>5<strong>\n            # pauses between notes\n            t = 0.125\n            # time that music note plays\n\n            music_notes = &#091;B4F, B4F, A4F, A4F,\n                           F5, F5, E5F, B4F, B4F, A4F, A4F, E5F, E5F, C5S, C5, B4F,\n                           C5S, C5S, C5S, C5S,\n                           C5S, E5F, C5, B4F, A4F, A4F, A4F, E5F, C5S,\n                           B4F, B4F, A4F, A4F,\n                           F5, F5, E5F, B4F, B4F, A4F, A4F, A5F, C5, C5S, C5, B4F,\n                           C5S, C5S, C5S, C5S,\n                           C5S, E5F, C5, B4F, A4F, A4F, A4F, E5F, C5S, C5S]\n\n            rhythm = &#091;1, 1, 1, 1,\n                      3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2,\n                      1, 1, 1, 1,\n                      3, 3, 3, 1, 2, 2, 2, 4, 8,\n                      1, 1, 1, 1,\n                      3, 3, 6, 1, 1, 1, 1, 3, 3, 3, 1, 2,\n                      1, 1, 1, 1,\n                      3, 3, 3, 1, 2, 2, 2, 4, 8, 4]\n\n            for i in range(len(music_notes)):\n                buzzer(BuzzerObj, music_notes&#091;i], rhythm&#091;i]*t, pause)<\/strong>\n\n        response = get_html('index.html')\n        cl.send('HTTP\/1.0 200 OK\\r\\nContent-type: text\/html\\r\\n\\r\\n')\n        cl.send(response)\n        cl.close()\n\n    except OSError as e:\n        cl.close()\n        print('Connection closed')\n<\/code><\/pre>\n\n\n\n<p>As you can see, there are two arrays, <strong>music_notes<\/strong> and <strong>rhythm<\/strong>. You would then run a for loop to put these values into the <strong>buzzer()<\/strong> function. Also, at the top of the code, we import all variables from <strong>constants.py<\/strong>.<\/p>\n\n\n\n<p>The <strong>buzzer() <\/strong>function also uses two new variables &#8212; <strong>t<\/strong> and <strong>pause<\/strong>. These two variables help you tune tempo of the music. <strong>t <\/strong>defines how long each note should be played and <strong>pause<\/strong> defines how long of a silence there should be between notes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Pico W and HC-SR04 ultrasonic sensor<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"667\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-ultrasonic-sensor.jpg\" alt=\"pico-w-ultrasonic-sensor\" class=\"wp-image-3422\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-ultrasonic-sensor.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-ultrasonic-sensor-300x200.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-ultrasonic-sensor-768x512.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/pico-w-ultrasonic-sensor-18x12.jpg 18w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption class=\"wp-element-caption\">`<\/figcaption><\/figure>\n\n\n\n<p>For the previous tutorials, we&#8217;ve been using the Pico W to send commands to the LEDs and buzzers.<\/p>\n\n\n\n<p>But what if we&#8217;re using the Pico W to receive information?<\/p>\n\n\n\n<p>In this case, we&#8217;re talking about the HC-SR04 ultrasonic distance sensor. <\/p>\n\n\n\n<p>When you plug in your ultrasonic sensor, be sure to know if it&#8217;s a 5V or 3.3V component. I plugged my 5V version into the 3.3V pin and got no response from the program, which I will share below. Once I moved the power source to the 5V pin, I immediately got a response.<\/p>\n\n\n\n<p>Apparently, some newer versions of the HC-SR04 can take both 3.3V to 5V. Perhaps it&#8217;s best to just try the 3.3V pin first and see if you get a result. If not, try the 5V.<\/p>\n\n\n\n<p>Here&#8217;s the schematics and the program you can run in Thonny. If you get a response, that means you have plugged everything in the right way, including the voltage.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Wiring<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"943\" height=\"1024\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/05-ultrasonic_bb-943x1024.jpg\" alt=\"\" class=\"wp-image-3413\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/05-ultrasonic_bb-943x1024.jpg 943w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/05-ultrasonic_bb-276x300.jpg 276w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/05-ultrasonic_bb-768x834.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/05-ultrasonic_bb-11x12.jpg 11w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/05-ultrasonic_bb.jpg 981w\" sizes=\"auto, (max-width: 943px) 100vw, 943px\" \/><\/figure>\n\n\n\n<p>The wiring from the labelled pins of the HC-SR04 sensors are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>VCC to 3.3V or 5V pin<\/strong> (if in doubt, try 3.3V first)<\/li>\n\n\n\n<li><strong>TRIG to GPIO 16<\/strong><\/li>\n\n\n\n<li><strong>ECHO to GPIO 15<\/strong><\/li>\n\n\n\n<li><strong>GND to GND<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Test program for HC-SR04 <\/h3>\n\n\n\n<p>In order to test if your wiring is correct, try this code that will print out the distance on the Thonny Shell.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from machine import Pin\nimport time\ntrigger = Pin(16, Pin.OUT)\necho = Pin(15, Pin.IN)\ndef ultrasonic():\n   trigger.low()\n   time.sleep_us(1)\n   trigger.high()\n   time.sleep_us(10)\n   trigger.low()\n   while echo.value() == 0:\n       signaloff = time.ticks_us()\n   while echo.value() == 1:\n       signalon = time.ticks_us()\n   timepassed = signalon - signaloff\n   distance = (timepassed * 0.0340) \/ 2\n   print(\"Distance = \",distance,\"cm\")\nwhile True:\n   ultrasonic()\n   time.sleep(1)\n<\/code><\/pre>\n\n\n\n<p>Your output should be:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>Distance =  9.707001 cm\nDistance =  9.707001 cm\nDistance =  8.619 cm\nDistance =  8.415001 cm\nDistance =  8.551001 cm\nDistance =  8.551001 cm\nDistance =  8.619 cm<\/code><\/pre>\n\n\n\n<p>If the program runs and quits without any output, you probably wired something wrongly. When I plugged the sensor to a 3.3V pin instead of a 5V, the program quit with no response.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Getting data sent to your browser<\/h3>\n\n\n\n<p>Let&#8217;s whip up the files that we used to serve a web page and modify it so that we can see the distance values.<\/p>\n\n\n\n<p>Here&#8217;s the final code:<\/p>\n\n\n\n<p><strong>main.py<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from wifi import init_wifi\nimport socket\nfrom machine import Pin\nimport time\n\ninit_wifi()\n\n# ultrasonic sensor pins and functions\ntrigger = Pin(16, Pin.OUT)\necho = Pin(15, Pin.IN)\n\n\ndef ultrasonic():\n    trigger.low()\n    time.sleep_us(1)\n    trigger.high()\n    time.sleep_us(10)\n    trigger.low()\n    while echo.value() == 0:\n        signaloff = time.ticks_us()\n    while echo.value() == 1:\n        signalon = time.ticks_us()\n    timepassed = signalon - signaloff\n    distance = (timepassed * 0.0340) \/ 2\n    return distance\n\n# Function to load in html page\n\ndef get_html(html_name, distance):\n    # open html_name (index.html), 'r' = read-only as variable 'file'\n    with open(html_name, 'r') as file:\n        html = file.read()\n    content = html.replace(\n        \"&lt;h2 id=\\\"ultrasonic\\\"&gt;&lt;\/h2&gt;\", f\"&lt;h2 id=\\\"ultrasonic\\\"&gt;{distance}cm&lt;\/h2&gt;\")\n\n    return content\n\n\n# HTTP server with socket\naddr = socket.getaddrinfo('0.0.0.0', 80)&#091;0]&#091;-1]\n\ns = socket.socket()\ns.bind(addr)\ns.listen(1)\n\nprint('Listening on', addr)\n\n# Listen for connections\nwhile True:\n    try:\n        cl, addr = s.accept()\n        print('Client connected from', addr)\n        request = cl.recv(1024)\n        print(ultrasonic())\n        response = get_html('index.html', ultrasonic())\n        cl.send('HTTP\/1.0 200 OK\\r\\nContent-type: text\/html\\r\\n\\r\\n')\n        cl.send(response)\n        cl.close()\n\n    except OSError as e:\n        cl.close()\n        print('Connection closed')\n<\/code><\/pre>\n\n\n\n<p><strong>index.html<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Pico W&lt;\/title&gt;\n    &lt;\/head&gt;\n      &lt;body&gt;\n        &lt;h1&gt;Pico W Ultrasonic Distance&lt;\/h1&gt;\n        &lt;h2 id=\"ultrasonic\"&gt;&lt;\/h2&gt;\n        &lt;script&gt;\n            setInterval(() =&gt; location.reload(), 500)\n        &lt;\/script&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>Let&#8217;s look at some snippets of the code to understand what&#8217;s happening.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code to get ultrasonic distance<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>def ultrasonic():\n    trigger.low()\n    time.sleep_us(1)\n    trigger.high()\n    time.sleep_us(10)\n    trigger.low()\n    while echo.value() == 0:\n        signaloff = time.ticks_us()\n    while echo.value() == 1:\n        signalon = time.ticks_us()\n    timepassed = signalon - signaloff\n    distance = (timepassed * 0.0340) \/ 2\n    return distance<\/code><\/pre>\n\n\n\n<p>The code block above triggers the emission of an ultrasonic wave. The <strong>time.sleep<\/strong> between the lows and highs are necessary for the functioning of the ultrasonic sensor.<\/p>\n\n\n\n<p>To measure how much time it takes between the emission of the ultrasonic wave and the time for the sound wave to return, we use <strong>time.ticks_us<\/strong> to measure the time of emission and the time for the echo to be detected.<\/p>\n\n\n\n<p><strong>time.ticks_us <\/strong>is an arbitrary number, so we will have to subtract <strong>signalon <\/strong>from <strong>signaloff<\/strong> to get the time passed. <\/p>\n\n\n\n<p>To get the distance, we use the formula <strong>distance = (timepassed * speedofsound<\/strong>) <strong>\/ 2.<\/strong> Speed of sound is 340m\/s, which therefore is 0.0340.<\/p>\n\n\n\n<p>The reason why we have to divide it by two is because the sound wave travels to the object and returns back.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code to serve web page with distance value<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>def get_html(html_name, distance):\n    # open html_name (index.html), 'r' = read-only as variable 'file'\n    with open(html_name, 'r') as file:\n        html = file.read()\n    content = html.replace(\n        \"&lt;h2 id=\\\"ultrasonic\\\"&gt;&lt;\/h2&gt;\", f\"&lt;h2 id=\\\"ultrasonic\\\"&gt;{distance}cm&lt;\/h2&gt;\")\n\n    return content\n\n...\n\nresponse = get_html('index.html', ultrasonic())<\/code><\/pre>\n\n\n\n<p>This function has been modified a little from the previous tutorials, taking in an additional parameter <strong>distance<\/strong>, which runs <strong>ultrasonic().<\/strong><\/p>\n\n\n\n<p>The function opens the <strong>index.html<\/strong> file normally but uses the <strong>replace<\/strong> method to find the <strong>&lt;h2<\/strong>&gt; and inserts the <strong>distance<\/strong> variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">index.html pings server every 500ms to get new value<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"424\" height=\"121\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-20.png\" alt=\"\" class=\"wp-image-3423\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-20.png 424w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-20-300x86.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/image-20-18x5.png 18w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><figcaption class=\"wp-element-caption\"><strong>index.html<\/strong><\/figcaption><\/figure>\n\n\n\n<p>The main web page receives a function that reloads the web page every 500ms. Every reload pings the Pico W to get the new ultrasonic distance value.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>    &lt;script&gt;\n            setInterval(() =&gt; location.reload(), 500)\n        &lt;\/script&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. Ultrasonic data, but let&#8217;s use AJAX to reduce payload<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"400\" style=\"aspect-ratio: 600 \/ 400;\" width=\"600\" controls src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/ultrasonic-distance.mp4\"><\/video><\/figure>\n\n\n\n<p>The previous tutorial works by having the Raspberry Pi Pico W send a whole new <strong>index.html<\/strong> file every time there&#8217;s a connection.<\/p>\n\n\n\n<p>This works but it&#8217;s incredibly inefficient because the whole <strong>index.html<\/strong> is being resent when you only need to update the ultrasonic distance data.<\/p>\n\n\n\n<p>We want the same results as the previous tutorial, but without the heavy payload of the previous method.<\/p>\n\n\n\n<p>So, we need to make the client (your phone or PC) ping an endpoint that will respond only with the ultrasonic data.<\/p>\n\n\n\n<p>The new methodology is this: if people visit the root URL, say 192.168.1.119, they will be served the <strong>index.html<\/strong>. <\/p>\n\n\n\n<p><strong>index.html <\/strong>will have JavaScript that pings the <strong>\/data<\/strong> endpoint, which triggers the Pico W to get ultrasonic distance data, and respond with it.<\/p>\n\n\n\n<p>Then, <strong>index.html<\/strong> will receive this data and update the web page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Splicing the request from client<\/h3>\n\n\n\n<p>Remember that in the <strong>main.py<\/strong>, there&#8217;s always a line saying <strong>requests = cl.recv(1024)<\/strong>. The request object looks a bit like this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>b'GET \/data HTTP\/1.1\nHost: 192.168.1.119\nConnection: keep-alive\nCache-Control: max-age=0\nUpgrade-Insecure-Requests: 1\nUser-Agent: Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/107.0.0.0 Safari\/537.36\nAccept: text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/avif,image\/webp,image\/apng,*\/*;q=0.8,application\/signed-exchange;v=b3;q=0.9\nReferer: http:\/\/192.168.1.119\/data\nAccept-Encoding: gzip, deflate\nAccept-Language: en-US,en;q=0.9,es;q=0.8,zh-TW;q=0.7,zh-CN;q=0.6,zh;q=0.5\n<\/code><\/pre>\n\n\n\n<p>First up, we need to filter this wall of text out.<\/p>\n\n\n\n<p>We use the <strong>find()<\/strong> method to find whether the <strong>request<\/strong> variable has &#8220;\/data&#8221;. If it does, respond with the ultrasonic distance data. If there&#8217;s no &#8220;\/data&#8221;, then respond with <strong>index.html<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<p><strong>Before<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>request = cl.recv(1024)\n        print(ultrasonic())\n        response = get_html('index.html', ultrasonic())<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<p><strong>After<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code> request = cl.recv(1024)\n        request = str(request)\n        print (request.find('\/data'))\n        if (request.find('\/data') &gt; -1):\n            #if string is found, respond with data.\n            response = str(ultrasonic())\n        else:\n            response = get_html('index.html', ultrasonic())<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>If you visited \/data in your browser, you will see the distance value printed, as below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"229\" height=\"128\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-3.png\" alt=\"\" class=\"wp-image-3465\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-3.png 229w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-3-18x10.png 18w\" sizes=\"auto, (max-width: 229px) 100vw, 229px\" \/><\/figure>\n<\/div>\n\n\n<p>Previously, the backend handled almost everything. The ultrasonic distance was modified before it was served as index.html. <\/p>\n\n\n\n<p>Now, <strong>index.html<\/strong> is fetching the sensor data from Pico W, so we&#8217;d have to make some changes to the JavaScript that&#8217;s inside the HTML file. Changes are highlighted in bold.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<pre class=\"wp-block-code has-small-font-size\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n    &lt;head&gt;\n        &lt;title&gt;Pico W&lt;\/title&gt;\n    &lt;\/head&gt;\n      &lt;body&gt;\n        &lt;h1&gt;Pico W Ultrasonic Distance&lt;\/h1&gt;\n        &lt;h2 id=\"ultrasonic\"&gt;&lt;\/h2&gt;\n        &lt;script&gt;\n<strong>            \/\/instead of hardcoding, use address which browser accessed Pico W.\n            const PicoAddress = window.location.href\n\n            const getData = async () =&gt; {\n                \/\/there is a slash at the end of PicoAddress, so don't use \"\/data\". use \"data\"\n                const data = await fetch(`${PicoAddress}data`);\n                \/\/response is just a string, so use data.text() to decode the Response's body\n                const distance = await data.text();\n                \/\/target ultrasonic id and change its inner HTML\n                ultrasonic.innerHTML = `${distance} cm`;\n                \/\/run getData() again.\n                setInterval(getData,100)\n                \/\/you can run getData() but it seems more optimized if you give it some time to breathe\n            };\n\n            getData();<\/strong>\n           \n        &lt;\/script&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>Central to the code here is the <strong>Fetch API<\/strong> and the latest <strong>async\/await<\/strong> syntax here.<\/p>\n\n\n\n<p>The function <strong>getData()<\/strong> pings the Pico W&#8217;s \/data endpoint, and the Pico W responds. We ensure the program doesn&#8217;t skip ahead before we get a full response asking it to <strong>await<\/strong>.<\/p>\n\n\n\n<p>Fetch becomes a Response object which you&#8217;ll need to use the Response.text() method to get to the body of the response. Hence the lines <strong>const distance = await data.text();<\/strong><\/p>\n\n\n\n<p>Once we have the ultrasonic data, we change the <strong>&lt;h2 id=&#8221;ultrasonic&#8221;&gt;<\/strong> element&#8217;s inner HTML, which starts empty, with the distance data. You can target an element with an <strong>id<\/strong> directly without use the <strong>getElementById()<\/strong> or <strong>querySelector()<\/strong> methods.<\/p>\n\n\n\n<p>Once all the code is done, we want to run getData() again. Why did I use a <strong>setInterval<\/strong> to set a 100ms interval?<\/p>\n\n\n\n<p>What I found is that if you called getData() without an interval, you will get more pauses between outputs. You&#8217;ll get a sequence of fast updates, then a pause. With the 100ms breathing room, the Pico W runs a bit better and updates seem more or less real-time still.<\/p>\n\n\n\n<p>If you want to understand what&#8217;s happening under the hood, refer to the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Fetch_API\">Fetch API documentation<\/a> and the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/async_function\">async\/await<\/a> documentation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Pimoroni Phew for cleaner code<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-5-1024x585.png\" alt=\"\" class=\"wp-image-3480\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-5-1024x585.png 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-5-300x171.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-5-768x439.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-5-1536x877.png 1536w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-5-18x10.png 18w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-5.png 1667w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Pimoroni Phew being used to deliver ultrasonic data<\/figcaption><\/figure>\n\n\n\n<p>In the last section, we covered the AJAX method of updating the ultrasonic sensor data.<\/p>\n\n\n\n<p>But is there an even better way to do this?<\/p>\n\n\n\n<p>Pimoroni&#8217;s Phew is a library that helps you do a lot of networking easily because you can avoid a lot of drugery.<\/p>\n\n\n\n<p>The goals are the same:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Get ultrasonic data and display it on <strong>index.html<\/strong><\/li>\n\n\n\n<li>Keep payload as low as possible.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s how <strong>main.py<\/strong> looks like.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from wifi import init_wifi\nfrom phew import server\nfrom phew.template import render_template\nfrom machine import Pin\nimport time\n\ninit_wifi()\n\n# ultrasonic sensor pins and functions\ntrigger = Pin(16, Pin.OUT)\necho = Pin(15, Pin.IN)\n\n\n# Function to load in html page\n\n@server.route(\"\/data\", methods=&#091;\"GET\"])\ndef ultrasonic(request):\n    trigger.low()\n    time.sleep_us(1)\n    trigger.high()\n    time.sleep_us(10)\n    trigger.low()\n    while echo.value() == 0:\n        signaloff = time.ticks_us()\n    while echo.value() == 1:\n        signalon = time.ticks_us()\n    timepassed = signalon - signaloff\n    distance = (timepassed * 0.0340) \/ 2\n    return str(distance), 200\n\n\n@server.catchall()\ndef catchall(request):\n    return render_template(\"index.html\")\n\n\nserver.run()\n<\/code><\/pre>\n\n\n\n<p>As you can see, the code is a lot more readable as you can set endpoints and link them up with functions that return specific data.<\/p>\n\n\n\n<p>Instead of fumbling with code to run the sockets, you&#8217;d just call a function from Phew&#8217;s library. <\/p>\n\n\n\n<p>Some things that are like about Phew are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No more using of <strong>request.find()<\/strong> to create an endpoint<\/li>\n\n\n\n<li>No more <strong>get_html()<\/strong> function to open the <strong>index.html<\/strong> file, just use Phew&#8217;s <strong>render_template()<\/strong><\/li>\n\n\n\n<li><strong>main.py<\/strong> is a lot easier to read, with minimal additional effort!<\/li>\n\n\n\n<li>Phew also adds in logging functionality that will allow you to debug your code more easily.<\/li>\n<\/ul>\n\n\n\n<p>The only downside is that Phew is still &#8220;a very new project and should be considered, at best, alpha stage.&#8221; So, you might find unexpected roadblocks sometimes.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/pimoroni\/phew#response\">Check it out.<\/a><\/p>\n\n\n\n<p>The previous tutorials talked about how to interact with your Pico W with your PC\/mobile phone.<\/p>\n\n\n\n<p>Let&#8217;s take a step out of this and get the Pico W to interact with a cloud service.<\/p>\n\n\n\n<p>For this, let&#8217;s make a temperature-and-humidity logger with a DHT22 sensor that sends data to Google Sheets via IFTTT.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. Setting up the DHT22 temperature and humidity sensor to the Pico W<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"667\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/featured-image-1.jpg\" alt=\"\" class=\"wp-image-3510\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/featured-image-1.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/featured-image-1-300x200.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/featured-image-1-768x512.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/featured-image-1-18x12.jpg 18w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><figcaption class=\"wp-element-caption\">Raspberry Pi Pico W paired with a DHT22 temperature and humidity logger.<\/figcaption><\/figure>\n\n\n\n<p>All these steps work for the DHT11 and DHT22. The main difference between the two is the accuracy of the readings. DHT11 is blue in colour while DHT22 is white.<\/p>\n\n\n\n<p>If you have a DHT11, note that references to the DHT22 in the code should be changed to DHT11. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>import dht\n#dht22 = dht.DHT22(Pin(16))\n#should be\ndht11 = dht.DHT11(Pin(16))<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"336\" height=\"564\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-6.png\" alt=\"\" class=\"wp-image-3488\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-6.png 336w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-6-179x300.png 179w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-6-7x12.png 7w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><figcaption class=\"wp-element-caption\">DHT22 pins<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Looking at the diagram below, from top to bottom of the DHT22, you&#8217;d connect 3.3V to the first pin.<\/p>\n\n\n\n<p>The second pin is the data pin. You&#8217;d need to power it. So, connect a 10K ohm resistor from the power rail to it, and then connect another jumper wire to GPIO 16 on the Raspberry Pi Pico.<\/p>\n\n\n\n<p>GND goes to GND.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/08-dht22.jpg\" alt=\"\" class=\"wp-image-3489\" style=\"width:363px;height:473px\" width=\"363\" height=\"473\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/08-dht22.jpg 725w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/08-dht22-230x300.jpg 230w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/08-dht22-9x12.jpg 9w\" sizes=\"auto, (max-width: 363px) 100vw, 363px\" \/><\/figure>\n<\/div>\n\n\n<p>To test the sensor, run this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from machine import Pin\nimport dht\n\n# ultrasonic sensor pins and functions\ndht22 = dht.DHT22(Pin(16))\n\nprint(dht22.measure())\nprint(dht22.temperature())\nprint(dht22.humidity())\n<\/code><\/pre>\n\n\n\n<p>If you get <strong>OSError: [Errno 110] ETIMEDOUT<\/strong> means you are running the script too soon. The DHT22 requires a two-second break before it can return another value. DHT11 requires one second.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Connecting IFTTT and Google Sheets<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"273\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-7.png\" alt=\"\" class=\"wp-image-3490\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-7.png 572w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-7-300x143.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-7-18x9.png 18w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><figcaption class=\"wp-element-caption\">Pico W logging time, temperature and humidity data into Google Sheets<\/figcaption><\/figure>\n<\/div>\n\n\n<p>The easiest way to get data from the Pico W to Google Sheets is via IFTTT.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/ifttt.com\/explore\">sign up for IFTTT account<\/a>.<\/p>\n\n\n\n<p>Then, click the &#8220;Create&#8221; button to create the applet.<\/p>\n\n\n\n<p>You will see this screen:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"502\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-8-1024x502.png\" alt=\"\" class=\"wp-image-3491\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-8-1024x502.png 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-8-300x147.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-8-768x377.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-8-1536x753.png 1536w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-8-18x9.png 18w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-8.png 1907w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Click on &#8220;If This&#8221; and search for Webhooks. Click on &#8220;Receive a web request&#8221;. Do not pick the JSON one. Name the event name &#8220;dht22&#8221;. The event name is critical because this is how IFTTT knows which applet to trigger.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"748\" height=\"510\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-9.png\" alt=\"\" class=\"wp-image-3492\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-9.png 748w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-9-300x205.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-9-18x12.png 18w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><figcaption class=\"wp-element-caption\">Pick this, not the JSON payload option.<\/figcaption><\/figure>\n<\/div>\n\n\n<p>What you&#8217;re doing here is creating an endpoint which you can ping with the DHT22&#8217;s sensor data.<\/p>\n\n\n\n<p>Then, click &#8220;Then That&#8221;. Pick &#8220;Google Sheets&#8221;. Then pick &#8220;Add row to spreadsheet&#8221;. You&#8217;d have to link your Google Sheets account. <\/p>\n\n\n\n<p>Change the &#8220;Spreadsheet name&#8221; and &#8220;Drive folder path&#8221; to whatever you want. The only thing critical here is the &#8220;Formatted row&#8221;, where you&#8217;ll want it to be <\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>{{OccurredAt}} ||| {{Value1}} ||| {{Value2}}<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"816\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-10.png\" alt=\"\" class=\"wp-image-3494\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-10.png 723w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-10-266x300.png 266w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-10-11x12.png 11w\" sizes=\"auto, (max-width: 723px) 100vw, 723px\" \/><\/figure>\n<\/div>\n\n\n<p>Now, you will need to go get the endpoint where the Pico W can send sensor data to.<\/p>\n\n\n\n<p>Head to <a href=\"https:\/\/ifttt.com\/maker_webhooks\">https:\/\/ifttt.com\/maker_webhooks<\/a> and click on &#8220;Documentation&#8221;. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"878\" height=\"489\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-11.png\" alt=\"\" class=\"wp-image-3495\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-11.png 878w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-11-300x167.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-11-768x428.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-11-18x10.png 18w\" sizes=\"auto, (max-width: 878px) 100vw, 878px\" \/><\/figure>\n\n\n\n<p>The documentation will tell you your key, the endpoint you need to send data to and how to structure the JSON body.<\/p>\n\n\n\n<p>Since you used &#8220;dht22&#8221; as your event name, your endpoint is:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>ttps:\/\/maker.ifttt.com\/trigger\/dht22\/with\/key\/&#091;your_key_here]<\/code><\/pre>\n\n\n\n<p>You&#8217;ll also want to structure your JSON body like this:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>{'value1': *** temperature data *** , value2': *** humidity data *** }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Coding the Pico W to get DHT22 data and sending it to IFTTT<\/h3>\n\n\n\n<p>Here&#8217;s how.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from machine import Pin\nfrom wifi import init_wifi\nimport urequests\nimport ujson\nimport dht\nimport time\n\ninit_wifi()\n\ndht22 = dht.DHT22(Pin(16))\n\n\ndef send_data():\n    #tell dht22 to take a reading\n    dht22.measure()\n    #find temp &amp; humidity data and then jsonify data.\n    dht_data = {'value1': str(dht22.temperature()),\n                'value2': str(dht22.humidity())}\n    post_data = ujson.dumps(dht_data)\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/dht22\/with\/key\/<strong>&#091;your_key_here]<\/strong>'\n    res = urequests.post(request_url, headers={\n                         'content-type': 'application\/json'}, data=post_data)\n    #log response from IFTTT.\n    print(res.text)\n    #sleep for a minute before running send_data again\n    time.sleep(1*60)\n    send_data()\n\n\nsend_data()<\/code><\/pre>\n\n\n\n<p>If you are going to copy this code, make sure to replace <strong>[your_key_here]<\/strong> inn the <strong>request_url<\/strong> variable.<\/p>\n\n\n\n<p>You should get a new reading every minute that gets logged to Google Sheet, as below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"273\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-7.png\" alt=\"\" class=\"wp-image-3490\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-7.png 572w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-7-300x143.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-7-18x9.png 18w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><figcaption class=\"wp-element-caption\">Pico W logging time, temperature and humidity data into Google Sheets<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Note that the temperature logged is in Celcius. If you want it in Fahrenheit, here&#8217;s the formula:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fahrenheit = dht22.temperature() * 1.8000 + 32.00<\/code><\/pre>\n\n\n\n<p>If you want data to be logged more (or less) frequently, change the value in <strong>time.sleep<\/strong>().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use IFTTT to notify you if error occurs<\/h3>\n\n\n\n<p>Of course, this code is still quite fragile. <\/p>\n\n\n\n<p>Let&#8217;s say something doesn&#8217;t go right. For example, your cat pulled the 3.3V jumper wire while you placed on the floor to log the temperature.<\/p>\n\n\n\n<p>The program will die, but you won&#8217;t get any notification until you notice that your data isn&#8217;t being logged. <\/p>\n\n\n\n<p>How do you resolve this?<\/p>\n\n\n\n<p>Well, get IFTTT to send you a notification when that happens!<\/p>\n\n\n\n<p>You would need the IFTTT app on your phone. So download it.<\/p>\n\n\n\n<p>Then, you&#8217;d create a new applet. <\/p>\n\n\n\n<p>For the <strong>&#8220;If This&#8221; <\/strong>part, Choose <strong>Webhooks -&gt; Receive a web request<\/strong>. Name your event &#8220;<strong>error<\/strong>&#8220;.<\/p>\n\n\n\n<p>For the &#8220;<strong>Then This&#8221;<\/strong> part, choose &#8220;<strong>Notifications<\/strong>&#8221; and <strong>&#8220;Send a notification from the IFTTT app&#8221;<\/strong>.<\/p>\n\n\n\n<p>I made the message simple<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>Error: {{Value1}}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"690\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-12-1024x690.png\" alt=\"\" class=\"wp-image-3497\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-12-1024x690.png 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-12-300x202.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-12-768x517.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-12-18x12.png 18w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-12.png 1214w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, let&#8217;s build the try-error blocks.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from machine import Pin\nfrom wifi import init_wifi\nimport urequests\nimport ujson\nimport dht\nimport time\n\ninit_wifi()\n\ndht22 = dht.DHT22(Pin(16))\n\n\ndef send_data():\n    # tell dht22 to take a reading\n    dht22.measure()\n    # find temp &amp; humidity data and then jsonify data.\n    dht_data = {'value1': str(dht22.temperature()),\n                'value2': str(dht22.humidity())}\n    post_data = ujson.dumps(dht_data)\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/dht22\/with\/key\/&#091;your-key-here]'\n    res = urequests.post(request_url, headers={\n                         'content-type': 'application\/json'}, data=post_data)\n    # log response from IFTTT.\n    print(res.text)\n    # sleep for a minute before running send_data again\n    time.sleep(1*60)\n    send_data()\n\n\ntry:\n    send_data()\nexcept Exception as e:\n    print(e)\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/error\/with\/key\/&#091;your-key-here]'\n    post_data = ujson.dumps({\"value1\": str(e)})\n    urequests.post(request_url, headers={\n        'content-type': 'application\/json'}, data=post_data)\n<\/code><\/pre>\n\n\n\n<p>Try it. Unplug the 3.3V wire from your DHT22 and you will get a notification as such:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"520\" height=\"561\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-13.png\" alt=\"\" class=\"wp-image-3498\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-13.png 520w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-13-278x300.png 278w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-13-11x12.png 11w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. Build a physical Spotify remote control with Raspberry Pi Pico W<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"649\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-spotify-control.jpg\" alt=\"\" class=\"wp-image-3517\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-spotify-control.jpg 1000w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-spotify-control-300x195.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-spotify-control-768x498.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-spotify-control-18x12.jpg 18w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>Building on the previous project, we&#8217;ll use IFTTT to control Spotify.<\/p>\n\n\n\n<p>We&#8217;ll create a device that has a play, pause and skip track button.<\/p>\n\n\n\n<p>The chief benefit of using IFTTT to control your Spotify is how easy it is. <\/p>\n\n\n\n<p>The downside is that you need a paid Spotify account and responses are slow. Meaning that if you pressed the skip button, you&#8217;d have to wait a while before you see the result.<\/p>\n\n\n\n<p>The beauty of having a dedicated Spotify remote is that you can change your music without carrying your phone around or having to open up the Spotify app. <\/p>\n\n\n\n<p>If you have ever driven a modern car, you&#8217;ll know how nice having steering wheel controls are.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"400\" style=\"aspect-ratio: 600 \/ 400;\" width=\"600\" controls src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/spotify-remote-control-pico-w.mp4\"><\/video><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Wiring up a remote Spotify controller<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"946\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/spotify-control_bb.jpg\" alt=\"Spotify Raspberry Pi Pico W controller\" class=\"wp-image-3518\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/spotify-control_bb.jpg 680w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/spotify-control_bb-216x300.jpg 216w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/spotify-control_bb-9x12.jpg 9w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><figcaption class=\"wp-element-caption\">Spotify Raspberry Pi Pico W controller. From top to bottom, the buttons are pause, play, skip track.<\/figcaption><\/figure>\n\n\n\n<p>You&#8217;ll need&#8230;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>7x jumper wires<\/li>\n\n\n\n<li>3x buttons<\/li>\n\n\n\n<li>Raspberry Pi Pico W<\/li>\n\n\n\n<li>Spotify paid account<\/li>\n<\/ul>\n\n\n\n<p>There&#8217;s a lot of wires crisscrossing up there in the image, so here&#8217;s a textual explanation.<\/p>\n\n\n\n<p>The push buttons are like switches. You need to connect the 3V3 pin to each of them, which means using the positive column on the breadboard. Therefore:<\/p>\n\n\n\n<p><strong>3V3 -&gt; Breadboard positive column -&gt; Push button (on one side of the breadboard gutter) -&gt; GPIO (on the other side)<\/strong><\/p>\n\n\n\n<p>My code will use GPIO 16 for the play button, GPIO 2 for the pause button and GPIO 15 for the skip track button.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up IFTTT to control Spotify<\/h3>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"391\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/Screenshot_3-1024x391.jpg\" alt=\"\" class=\"wp-image-3520\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/Screenshot_3-1024x391.jpg 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/Screenshot_3-300x115.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/Screenshot_3-768x293.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/Screenshot_3-18x7.jpg 18w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/Screenshot_3.jpg 1126w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">We&#8217;ll create three IFTTT applets to control Spotify.<\/figcaption><\/figure>\n\n\n\n<p>We have to create three applets, one each for the play, pause and skip function.<\/p>\n\n\n\n<p>If you have the premium version of IFTTT, you probably could use the JavaScript filters to contain it all into one app. But since we don&#8217;t, we need an applet each.<\/p>\n\n\n\n<p>Once logged in, click on &#8220;Create&#8221; on the top right of the main menu.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"225\" height=\"105\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-14.png\" alt=\"\" class=\"wp-image-3521\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-14.png 225w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-14-18x8.png 18w\" sizes=\"auto, (max-width: 225px) 100vw, 225px\" \/><\/figure>\n<\/div>\n\n\n<p>On the &#8220;If This&#8221; bar, click on it and search for Webhooks.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-17.png\" alt=\"\" class=\"wp-image-3531\" style=\"width:773px;height:485px\" width=\"773\" height=\"485\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-17.png 773w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-17-300x188.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-17-768x482.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-17-18x12.png 18w\" sizes=\"auto, (max-width: 773px) 100vw, 773px\" \/><figcaption class=\"wp-element-caption\">Click on the &#8220;If This&#8221; or &#8220;Then That&#8221; and choose Webhooks and Spotify respectively.<\/figcaption><\/figure>\n\n\n\n<p>Then, click on &#8220;Receive a web request&#8221; (not the other option that has &#8220;with a JSON payload&#8221;).<\/p>\n\n\n\n<p>For event name, type <strong>spotify_skip<\/strong>.<\/p>\n\n\n\n<p>You will have to repeat this step two times more for <strong>spotify_pause<\/strong> and <strong>spotify_play<\/strong> once you&#8217;re done creating this applet.<\/p>\n\n\n\n<p>Once that&#8217;s done, go to the &#8220;<strong>Then That<\/strong>&#8221; bar and click on it. Search for &#8220;<strong>Spotify<\/strong>&#8220;.<\/p>\n\n\n\n<p>You will have to authorize IFTTT to connect to Spotify once.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"690\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-15-1024x690.png\" alt=\"\" class=\"wp-image-3522\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-15-1024x690.png 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-15-300x202.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-15-768x518.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-15-18x12.png 18w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-15.png 1147w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If you are doing the <strong>spotify_skip<\/strong> action, you will want to click on &#8220;Skip track&#8221;. But if you are doing an applet to do some other action, the image above will show you which one to use.<\/p>\n\n\n\n<p>Once you have created all three applets, it&#8217;s time to code!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Coding the Spotify remote for the Pico W<\/h3>\n\n\n\n<p>First up, you will need to know the endpoint you need to hit.<\/p>\n\n\n\n<p><a href=\"https:\/\/ifttt.com\/maker_webhooks\">Go to this page<\/a> and click on the <strong>Documentation<\/strong>.<\/p>\n\n\n\n<p>You will see your key there. If you have followed all the steps above, the difference between your endpoint and mine is your key. Hence,<\/p>\n\n\n\n<p><strong>Play endpoint<\/strong>: https:\/\/maker.ifttt.com\/trigger\/spotify_play\/with\/key\/[your_key_here]<\/p>\n\n\n\n<p><strong>Pause<\/strong>: https:\/\/maker.ifttt.com\/trigger\/spotify_pause\/with\/key\/[your_key_here]<\/p>\n\n\n\n<p><strong>Skip<\/strong>: https:\/\/maker.ifttt.com\/trigger\/spotify_skip\/with\/key\/[your_key_here]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from machine import Pin\nfrom wifi import init_wifi\nimport urequests\nimport time\n\ninit_wifi()\n\nplay_btn = Pin(16, Pin.IN, Pin.PULL_DOWN)\npause_btn = Pin(2, Pin.IN, Pin.PULL_DOWN)\nskip_btn = Pin(15, Pin.IN, Pin.PULL_DOWN)\n\n\ndef play():\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/spotify_play\/with\/key\/&#091;your_key_here]'\n    res = urequests.post(request_url)\n    # print response from IFTTT.\n    print(res.text)\n\n\ndef pause():\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/spotify_pause\/with\/key\/&#091;your_key_here]'\n    res = urequests.post(request_url)\n    # print response from IFTTT.\n    print(res.text)\n\n\ndef skip():\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/spotify_skip\/with\/key\/&#091;your_key_here]'\n    res = urequests.post(request_url)\n    # print response from IFTTT.\n    print(res.text)\n\n\ntry:\n    while True:\n        if play_btn():\n            print('play btn')\n            play()\n            time.sleep(0.25)\n        if pause_btn():\n            print('pause btn')\n            pause()\n            time.sleep(0.25)\n        if skip_btn():\n            skip()\n            print('skip')\n            time.sleep(0.25)\n\nexcept Exception as e:\n    print(e)\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/error\/with\/key\/&#091;your_key_here]'\n    post_data = ujson.dumps({\"value1\": str(e)})\n    urequests.post(request_url, headers={\n        'content-type': 'application\/json'}, data=post_data)\n<\/code><\/pre>\n\n\n\n<p>Let&#8217;s go through the code. <\/p>\n\n\n\n<p>Note that you will need to replace <strong>[your_key_here]<\/strong> with your real key, as obtained through the <strong>Documentation<\/strong> link.<\/p>\n\n\n\n<p>First, we declare variables for the push buttons.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>play_btn = Pin(16, Pin.IN, Pin.PULL_DOWN)\npause_btn = Pin(2, Pin.IN, Pin.PULL_DOWN)\nskip_btn = Pin(15, Pin.IN, Pin.PULL_DOWN)<\/code><\/pre>\n\n\n\n<p>Currently, if you do not press the button, your variable will have a value of <strong>0.<\/strong> If you press it, it becomes <strong>1<\/strong>. This is what we&#8217;ll use to trigger the <strong>play()<\/strong> , <strong>pause()<\/strong> and <strong>skip() <\/strong>functions.<\/p>\n\n\n\n<p>Then, we create functions for the play, pause and skip endpoints. The general template is as such:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>def play():\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/spotify_play\/with\/key\/&#091;your_key_here]'\n    res = urequests.post(request_url)\n    # print response from IFTTT.\n    print(res.text)<\/code><\/pre>\n\n\n\n<p>It&#8217;s pretty simple. If this function is run, it will send a POST request to IFTTT. Sending a GET request will not work.<\/p>\n\n\n\n<p>Then, we have the try\/except block.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>try:\n    while True:\n        if play_btn():\n            print('play btn')\n            play()\n            time.sleep(0.25)\n        if pause_btn():\n            print('pause btn')\n            pause()\n            time.sleep(0.25)\n        if skip_btn():\n            skip()\n            print('skip')\n            time.sleep(0.25)\n\nexcept Exception as e:\n    print(e)\n    request_url = 'https:\/\/maker.ifttt.com\/trigger\/error\/with\/key\/&#091;your_key_here]'\n    post_data = ujson.dumps({\"value1\": str(e)})\n    urequests.post(request_url, headers={\n        'content-type': 'application\/json'}, data=post_data)<\/code><\/pre>\n\n\n\n<p>If a button is pressed, the code will run the relevant function. For example, pressing the skip button will run the function <strong>skip()<\/strong>.<\/p>\n\n\n\n<p>A <strong>time.sleep(0.25)<\/strong> will pause the function for 250ms. Without this, even a short press can overload and crash the Pico W.<\/p>\n\n\n\n<p>The <strong>except<\/strong> block is optional, but I did it because I already had an &#8220;error&#8221; applet on IFTTT. If you have followed the previous tutorial, you might have used it.<\/p>\n\n\n\n<p>Basically, it sends the error message, <strong>e<\/strong>, to IFTTT, so that you get the error message as a phone app notification.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why is it not working?<\/h3>\n\n\n\n<p>Using IFTTT as a medium to control your Spotify is easy, but it does come with some downsides.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">You must start the music the regular way first<\/h4>\n\n\n\n<p>If you tried pressing the play button on your Pico W and expected music to start playing&#8230; well, nothing happens. <\/p>\n\n\n\n<p>The solution is to start the music on your computer or phone normally. You need to go to your app and press play.<\/p>\n\n\n\n<p>I think this declares which device is the active device. Once you have done this, you will be able to use your Pico W Spotify remote.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Slow responses<\/h4>\n\n\n\n<p>It takes a few seconds between button press and response. Unfortunately, this is the way it is. <\/p>\n\n\n\n<p>You could pay for an IFTTT upgrade to get faster response speeds. At least, that&#8217;s what they promise for your money.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Is there a direct way to connect Spotify?<\/h4>\n\n\n\n<p>Yes! Spotify has an API which you can connect to.<\/p>\n\n\n\n<p>It gives you significantly more control. You could add a rotary encoder to control volume. You could add add a LCD screen to show what&#8217;s playing. <a href=\"https:\/\/developer.spotify.com\/console\">Check out Spotify&#8217;s console here.<\/a><\/p>\n\n\n\n<p>Amazing, but also much more difficult to program, especially on a Pico W. <\/p>\n\n\n\n<p>IFTTT makes it all easy because they do all the heavy lifting. If you want to be the heavy lifter, check out the authentication flow.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"930\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-16-1024x930.png\" alt=\"\" class=\"wp-image-3527\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-16-1024x930.png 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-16-300x272.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-16-768x698.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-16-13x12.png 13w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-16.png 1068w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Authorization Code Flow diagram by Spotify<\/figcaption><\/figure>\n\n\n\n<p>Of course, we&#8217;re Raspberry Pi enthusiasts. Someone out there will do it. Should it be you? Or me? Comment below.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\">Control your Pico W wirelessly with PiCockpit!<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/picockpit.com\/raspberry-pi\/picockpit-v2-6-5-support-for-the-pico-w-part-2\/\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/four.png\" alt=\"\" class=\"wp-image-3610\" width=\"755\" height=\"142\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/four.png 895w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/four-300x57.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/four-768x145.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/four-18x3.png 18w\" sizes=\"auto, (max-width: 755px) 100vw, 755px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>You can control and get data from your Pico W wirelessly by using PiCockpit.<\/p>\n\n\n\n<p>PiCockpit allows you to get values, control and use PWM through a GUI via its GPIO applet.<\/p>\n\n\n\n<p>You can also see your Pico W&#8217;s stats through the PiStats applet.<\/p>\n\n\n\n<p>Integrating PiCockpit into your Pico W is super easy.<\/p>\n\n\n\n<p><a href=\"https:\/\/picockpit.com\/raspberry-pi\/picockpit-v2-6-5-support-for-the-pico-w-part-2\/\">Follow this tutorial.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Write even less code with PiCockpit and the Pico W<\/h2>\n\n\n\n<p>PiCockpit makes it easy for you to control your GPIO pins without having to write any code.<\/p>\n\n\n\n<p>If you look at <a href=\"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#Code_to_control_LED_on_Pico_W\">tutorial number 2<\/a>, notice how much code is necessary just to toggle an LED.<\/p>\n\n\n\n<p>With our new Pico W integration, PiCockpit makes it so much easier as you don&#8217;t have to program anything at all. Not even the WiFi configuration &#8212; that&#8217;s done with our setup wizard.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. Simple LED control with PiCockpit and Pico W<\/h2>\n\n\n\n<p>If you have your LED configured exactly the way I&#8217;ve done it in tutorial no. 2, then all that&#8217;s left is to set it up on PiCockpit.<\/p>\n\n\n\n<p>If you&#8217;re coding it out, you&#8217;ll declare which pin your LED is on using <code>led = machine.Pin(2, machine.Pin.OUT)<\/code><\/p>\n\n\n\n<p>On PiCockpit, you&#8217;ll go to your GPIO applet, and the scroll to &#8220;GPIO Output (On\/Off)&#8221;.<\/p>\n\n\n\n<p>Choose BCM02 from the dropdown menu because your LED is on GPIO 2.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-38.png\" alt=\"\" class=\"wp-image-3664\" width=\"752\" height=\"427\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-38.png 906w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-38-300x171.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-38-768x437.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-38-18x10.png 18w\" sizes=\"auto, (max-width: 752px) 100vw, 752px\" \/><\/figure>\n<\/div>\n\n\n<p>Then, on the &#8220;Control&#8221; column, toggle the switch to turn the LED on.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"105\" height=\"125\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-39.png\" alt=\"\" class=\"wp-image-3665\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-39.png 105w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-39-10x12.png 10w\" sizes=\"auto, (max-width: 105px) 100vw, 105px\" \/><\/figure>\n<\/div>\n\n\n<p>You can also easily use the Software PWM section below to control the brightness of your LED.<\/p>\n\n\n\n<p>Note that you will need to remove the previous setting because you cannot have two outputs on the same GPIO.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-40.png\" alt=\"\" class=\"wp-image-3666\" width=\"716\" height=\"261\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-40.png 908w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-40-300x109.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-40-768x280.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-40-18x7.png 18w\" sizes=\"auto, (max-width: 716px) 100vw, 716px\" \/><\/figure>\n<\/div>\n\n\n<p>As you toggle the &#8220;Control&#8221; slider, you&#8217;ll notice the brightness of the LED changing.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"203\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/05\/picockpit-gpio-pwm.gif\" alt=\"picockpit gpio pwm remote control\" class=\"wp-image-2285\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">11. Pico W, 5V fan and a transistor, controlled by PiCockpit<\/h2>\n\n\n\n<p>Let&#8217;s try something a little bit more comprehensive but using the same GPIO Output toggle.<\/p>\n\n\n\n<p>To illustrate some real-world use cases, I will power a 5V fan using PiCockpit.<\/p>\n\n\n\n<p>This is a low-power 5V fan taken from my Raspberry Pi 4, so it&#8217;s well within the output capabilities of the Raspberry Pi Pico W.<\/p>\n\n\n\n<p>That said, because it&#8217;s a 5V fan, I can&#8217;t use a GPIO pin. In less power-intensive components, like an LED, you can have the GPIO do double duty of supplying power to the component and being the &#8220;switch&#8221; that turns it on and off.<\/p>\n\n\n\n<p>But the 5V fan would require too high a voltage. So, the next best way is to put a transistor in the middle.<\/p>\n\n\n\n<p>This allows me to supply 5V to the fan, while ensuring that I can turn it on and off.<\/p>\n\n\n\n<p>Once again, because of PiCockpit, I did zero programming. I only did the hardware, which is wired as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-transistor-and-picockpit_bb-1024x813.jpg\" alt=\"\" class=\"wp-image-3668\" width=\"627\" height=\"497\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-transistor-and-picockpit_bb-1024x813.jpg 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-transistor-and-picockpit_bb-300x238.jpg 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-transistor-and-picockpit_bb-768x610.jpg 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-transistor-and-picockpit_bb-15x12.jpg 15w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-transistor-and-picockpit_bb.jpg 1192w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/figure>\n<\/div>\n\n\n<p>The fan is a 5V\/0.12A fan, connected to 5V on the positive end (red wire), and the negative wire goes to the emitter leg of the transistor.<\/p>\n\n\n\n<p>The transistor is a PN2222 (NPN) transistor, which means that it switches ON when it receives a high signal.<\/p>\n\n\n\n<p>From left to right, with the semi-circled part facing away from you, the legs are the Emitter, Base and Collector.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"282\" height=\"267\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-41.png\" alt=\"\" class=\"wp-image-3670\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-41.png 282w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-41-13x12.png 13w\" sizes=\"auto, (max-width: 282px) 100vw, 282px\" \/><figcaption class=\"wp-element-caption\">PN2222 diagram from <a href=\"https:\/\/alltransistors.com\/adv\/pdfdatasheet_fairchild_semi\/pn2222.pdf\">Fairchild Semiconductor&#8217;s datasheet.<\/a><\/figcaption><\/figure>\n<\/div>\n\n\n<p>The Base leg is connected to a 1K resistor, then connected to GPIO 15.<\/p>\n\n\n\n<p>The Collector leg is connected to ground.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Configuring PiCockpit to work with transistor<\/h3>\n\n\n\n<p>Once again, super easy.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-42.png\" alt=\"\" class=\"wp-image-3671\" width=\"695\" height=\"431\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-42.png 906w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-42-300x186.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-42-768x477.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-42-18x12.png 18w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n<\/div>\n\n\n<p>Go to the dropdown menu in the GPIO Output section and add BCM15.<\/p>\n\n\n\n<p>Once it&#8217;s in, you can click the downward arrow and change the State Names to &#8220;fan off&#8221; and &#8220;fan on&#8221;.<\/p>\n\n\n\n<p>Toggle the control switch and you should see the fan turning on.<\/p>\n\n\n\n<p>You can also use PiStats to see the drop in temperatures on your board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-43.png\" alt=\"\" class=\"wp-image-3673\" width=\"704\" height=\"472\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-43.png 877w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-43-300x201.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-43-768x515.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-43-18x12.png 18w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/figure>\n<\/div><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Display photoresistor using MQTT and Node-RED with Pico W.<\/h2>\n\n\n\n<p>The main goal of this tutorial is to introduce MQTT.<\/p>\n\n\n\n<p>In the previous tutorials, I&#8217;ve shown you how you can use your Pico W to deliver data, but what if you want a central repository of data in the cloud?<\/p>\n\n\n\n<p>HiveMQ Cloud is a free service that we can use to achieve this goal. By using someone else&#8217;s computers, we can also lighten the load on the Pico W.<\/p>\n\n\n\n<p>To add to that, MQTT has major advantages compared to the previous methods used. For one, it&#8217;s a lot more efficient in sending small data. MQTT protocol headers are 2 bytes in size. HTTP is about 4000 times larger.<\/p>\n\n\n\n<p>Reducing local processing load and network load means longer battery life for your Pico W, which is perfect for battery or solar-powered projects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Photoresistor connection with the Pico W<\/h3>\n\n\n\n<p>A photoresistor (photocell) is super easy to connect. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-photoresistor_bb.jpg\" alt=\"\" class=\"wp-image-3679\" style=\"width:334px;height:473px\" width=\"334\" height=\"473\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-photoresistor_bb.jpg 668w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-photoresistor_bb-212x300.jpg 212w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/pico-w-photoresistor_bb-8x12.jpg 8w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><\/figure>\n<\/div>\n\n\n<p>Place the photoresistor across the central gutter of the breadboard.<\/p>\n\n\n\n<p>Then, connect the 3V3 pin to one side of the photoresistor.<\/p>\n\n\n\n<p>You&#8217;ll want to connect an ADC pin to the other side of the photoresistor, so connect GPIO 26.<\/p>\n\n\n\n<p>Finally, connect a 10K ohm resistor from ground to the photocell.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">HiveMQ Cloud and coding the Pico W<\/h3>\n\n\n\n<p>First up, <a href=\"https:\/\/www.hivemq.com\/mqtt-cloud-broker\/\">sign up for HiveMQ Cloud here.<\/a><\/p>\n\n\n\n<p>Go through the setup and create a cluster. It will ask you to choose AWS or Azure. For our purposes, there&#8217;s no difference. <\/p>\n\n\n\n<p>Then, click &#8220;Manage Cluster&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"514\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-44-1024x514.png\" alt=\"\" class=\"wp-image-3680\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-44-1024x514.png 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-44-300x150.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-44-768x385.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-44-1536x770.png 1536w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-44-18x9.png 18w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-44.png 1884w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Cluster main menu. Take note of the two orange rectangles.<\/figcaption><\/figure>\n\n\n\n<p>Take note of your Cluster URL, and click on Access Management to create a new user. Go through the steps and create a new user.<\/p>\n\n\n\n<p>With these details, you can now program your Pico W to send data there.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Coding the Pico W to receive photocell data and MQTTClient<\/h3>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from machine import Pin, ADC\nfrom wifi import init_wifi\nimport time\nfrom umqtt.simple import MQTTClient\n\ninit_wifi()\n\nphotoresistor = ADC(Pin(26))\n\n\ndef readLight():\n    light = photoresistor.read_u16()\n    return light\n\n\n# Connect MQTT\n\ndef connectMQTT():\n    client = MQTTClient(client_id=b\"&#091;your_client_id]\",\n                        server=b\"&#091;your-host-name]\",\n                        port=0,\n                        user=b\"&#091;your-user]\",\n                        password=b\"&#091;your-pw]\",\n                        keepalive=7200,\n                        ssl=True,\n                        ssl_params={\n                            'server_hostname': '&#091;your-host-name]'}\n                        )\n\n    client.connect()\n    return client\n\n\nclient = connectMQTT()\n\n\ndef publish(topic, value):\n    print(topic)\n    print(value)\n    client.publish(topic, value)\n    print(\"data published\")\n\n\nwhile True:\n    brightness = str(readLight()) #to publish, must send string\n\n    print(brightness)\n\n    publish('picow\/brightness', brightness)\n\n    time.sleep(0.1)\n<\/code><\/pre>\n\n\n\n<p>First, let&#8217;s get our imports in order.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>from machine import Pin, ADC\nfrom wifi import init_wifi\nimport time\nfrom umqtt.simple import MQTTClient<\/code><\/pre>\n\n\n\n<p>The <code>wifi<\/code> import comes from the previous tutorials.<\/p>\n\n\n\n<p>You will need the umqtt.simple library, which <a href=\"https:\/\/github.com\/micropython\/micropython-lib\/tree\/master\/micropython\/umqtt.simple\">can be downloaded here<\/a>.<\/p>\n\n\n\n<p>Once that&#8217;s downloaded, you can upload it to your board (<a href=\"https:\/\/picockpit.com\/raspberry-pi\/everything-about-the-raspberry-pi-pico-w\/#Uploading_files_to_your_Raspberry_Pi_Pico_W\">guide here<\/a>).<\/p>\n\n\n\n<p>You should have these files on your Pico W.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"222\" height=\"121\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-45.png\" alt=\"\" class=\"wp-image-3682\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-45.png 222w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-45-18x10.png 18w\" sizes=\"auto, (max-width: 222px) 100vw, 222px\" \/><\/figure>\n<\/div>\n\n\n<p>Then, create a function to get a reading from the photoresistor:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>photoresistor = ADC(Pin(26))\n\ndef readLight():\n    light = photoresistor.read_u16()\n    return light\n<\/code><\/pre>\n\n\n\n<p>This returns a value up to 65535. The brighter it is, the higher the value.<\/p>\n\n\n\n<p><strong>Connecting to HiveMQ<\/strong><\/p>\n\n\n\n<p>In order to connect to HiveMQ, you will need to send some parameters to the MQTTClient class.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code># Connect MQTT\n\ndef connectMQTT():\n    client = MQTTClient(client_id=b\"&#091;your_client_id]\",\n                        server=b\"&#091;your-host-name]\",\n                        port=0,\n                        user=b\"&#091;your-user]\",\n                        password=b\"&#091;your-pw]\",\n                        keepalive=7200,\n                        ssl=True,\n                        ssl_params={\n                            'server_hostname': '&#091;your-host-name]'}\n                        )\n\n    client.connect()\n    return client\n\n\nclient = connectMQTT()<\/code><\/pre>\n\n\n\n<p>Replace [your-host-name] with the address found on your dashboard. You will have to do this twice, once for <code>server<\/code> and another for <code>server_hostname<\/code>. Also replace [your_client_id] with a name for your device, such as &#8220;your_picow&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"955\" height=\"349\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-46.png\" alt=\"\" class=\"wp-image-3683\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-46.png 955w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-46-300x110.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-46-768x281.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-46-18x7.png 18w\" sizes=\"auto, (max-width: 955px) 100vw, 955px\" \/><\/figure>\n\n\n\n<p>Then, replace <code>[your-user]<\/code> and <code>[your-pw]<\/code> with the user you created in the Access Management page (screenshot of Access Management page below).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"753\" height=\"615\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-47.png\" alt=\"\" class=\"wp-image-3684\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-47.png 753w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-47-300x245.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-47-15x12.png 15w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/figure>\n\n\n\n<p>For reference, this function sends data to the HiveMQ:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>def publish(topic, value):\n    print(topic)\n    print(value)\n    client.publish(topic, value)\n    print(\"data published\")<\/code><\/pre>\n\n\n\n<p>Let&#8217;s call it in our <code>while <\/code>loop:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>while True:\n    brightness = str(readLight()) #to publish, must send string\n\n    print(brightness)\n\n    publish('picow\/brightness', brightness)\n\n    time.sleep(0.1)<\/code><\/pre>\n\n\n\n<p>When publishing, you must send strings, which is why <code>brightness = str(readLight())<\/code> is there.<\/p>\n\n\n\n<p>If you send integers or floats, the program will die.<\/p>\n\n\n\n<p>In the publish function, give your topic a name. Say, <code>picow\/brightness<\/code>, then add the value you want to send. In this case, we want to send the stringified light reading, <code>brightness<\/code>.<\/p>\n\n\n\n<p>You should be able to see the data being published when you log into the Web Client tab.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"541\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-48-1024x541.png\" alt=\"\" class=\"wp-image-3686\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-48-1024x541.png 1024w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-48-300x158.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-48-768x406.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-48-18x10.png 18w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-48.png 1437w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">HiveMQ&#8217;s web client, on the left, showing you the data that has been published.<\/figcaption><\/figure>\n\n\n\n<p><strong>Node-RED<\/strong><\/p>\n\n\n\n<p>Those are just numbers online that might seem like gibberish. What if you wanted to access the data on HiveMQ Cloud and present it graphically?<\/p>\n\n\n\n<p>Rather than rolling your own, you could just use Node-RED.<\/p>\n\n\n\n<p>Node-RED makes it really easy for you to take data from HiveMQ and then present it using graphical representations.<\/p>\n\n\n\n<p>We&#8217;re going to make a gauge using Node-RED.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"388\" height=\"316\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-49.png\" alt=\"\" class=\"wp-image-3688\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-49.png 388w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-49-300x244.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-49-15x12.png 15w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/figure>\n\n\n\n<p>To start, you will need to have <a href=\"https:\/\/nodejs.org\/en\/\">nodejs<\/a>. Check HiveMQ&#8217;s documentation <a href=\"https:\/\/nodered.org\/docs\/faq\/node-versions\">to see which version is recommended.<\/a><\/p>\n\n\n\n<p>Once you have Node installed, you will need to open up a command prompt\/Terminal and run this commands (exclude sudo if you&#8217;re on Windows):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo npm install -g --unsafe-perm node-red<\/code><\/pre>\n\n\n\n<p>This will use node package manager (npm) to install Node-RED globally.<\/p>\n\n\n\n<p>Then, run Node-RED by typing <code>node-red<\/code> into the Terminal\/command prompt.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"568\" height=\"169\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-50.png\" alt=\"\" class=\"wp-image-3689\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-50.png 568w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-50-300x89.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-50-18x5.png 18w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/figure>\n\n\n\n<p>Open your browser and head to <strong>http:\/\/127.0.0.1:1880<\/strong> or whatever address is listed in your Terminal.<\/p>\n\n\n\n<p>Let&#8217;s build the flow. Drag an &#8220;mqtt in&#8221; onto the canvas. You can find it under the &#8220;network&#8221; tab on the left sidebar.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"916\" height=\"367\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-51.png\" alt=\"\" class=\"wp-image-3690\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-51.png 916w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-51-300x120.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-51-768x308.png 768w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-51-18x7.png 18w\" sizes=\"auto, (max-width: 916px) 100vw, 916px\" \/><\/figure>\n\n\n\n<p>We&#8217;ll need to configure the tab, so double click on the rectangle and do the following:<\/p>\n\n\n\n<p>In the &#8220;topic&#8221; field, make sure to add <strong>picow\/brightness<\/strong>, since that&#8217;s what you published from the Pico W.<\/p>\n\n\n\n<p>In &#8220;server&#8221;, add a new one by clicking the pencil icon and you will be brought to the next menu.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"557\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-52.png\" alt=\"\" class=\"wp-image-3691\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-52.png 649w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-52-300x257.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-52-14x12.png 14w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/figure>\n\n\n\n<p>Put a new server address in and change the port to 8883. Tick &#8220;Use TLS&#8221; but don&#8217;t bother adding new tls-config.<\/p>\n\n\n\n<p>Then, go to the security tab and add in your login credentials.<\/p>\n\n\n\n<p>All of these details can be found in your code when you initialize MQTTClient.<\/p>\n\n\n\n<p><strong>Adding a gauge<\/strong><\/p>\n\n\n\n<p>To add a gauge, you must have the <strong>node-red-dashboard<\/strong>.<\/p>\n\n\n\n<p>On the left sidebar, if you do not see these:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"157\" height=\"429\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-53.png\" alt=\"\" class=\"wp-image-3692\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-53.png 157w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-53-110x300.png 110w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-53-4x12.png 4w\" sizes=\"auto, (max-width: 157px) 100vw, 157px\" \/><\/figure>\n\n\n\n<p>Then go to the menu (top right button) -&gt; Manage Palette. Then go to the Install tab and search for node-red-dashboard. Click on &#8220;Install&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"694\" height=\"307\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-54.png\" alt=\"\" class=\"wp-image-3693\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-54.png 694w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-54-300x133.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-54-18x8.png 18w\" sizes=\"auto, (max-width: 694px) 100vw, 694px\" \/><\/figure>\n\n\n\n<p>Drag and drop a &#8220;gauge&#8221; to the right of the <strong>mqtt in<\/strong> rectangle, and connect them by dragging a line from <strong>mqtt in<\/strong> to the gauge.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"461\" height=\"199\" src=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-55.png\" alt=\"\" class=\"wp-image-3694\" srcset=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-55.png 461w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-55-300x130.png 300w, https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/12\/image-55-18x8.png 18w\" sizes=\"auto, (max-width: 461px) 100vw, 461px\" \/><\/figure>\n\n\n\n<p>Double click the gauge rectangle and change the label to &#8220;brightness&#8221; and the &#8220;range&#8221; max to 65535.<\/p>\n\n\n\n<p>Great. Now let&#8217;s press &#8220;Deploy&#8221;.<\/p>\n\n\n\n<p>If your settings were correct, you will see a green circle and &#8220;connected&#8221; under the rectangle. If not, your terminal will give you more details as to why there&#8217;s an error.<\/p>\n\n\n\n<p>With your Pico W delivering data to HiveMQ Cloud, it&#8217;s now time to check out the dashboard. Visit <strong>http:\/\/127.0.0.1:1880\/ui<\/strong> and you should see the gauge being updated frequently.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Your suggestions are welcome. <\/p>\n\n\n\n<p>Leave a comment in the comment box below!<\/p>\n<div class=\"shariff shariff-align-center shariff-widget-align-left shariff-buttonstretch\"><ul class=\"shariff-buttons theme-white orientation-horizontal buttonsize-medium\"><li class=\"shariff-button twitter shariff-nocustomcolor\" style=\"background-color:#595959\"><a href=\"https:\/\/twitter.com\/share?url=https%3A%2F%2Fpicockpit.com%2Fraspberry-pi%2Fnl%2Fraspberry-pi-pico-w-beginners-componenten-tutorial%2F&text=Raspberry%20Pi%20Pico%20W%20beginners%E2%80%99%20components%20tutorial\" title=\"Delen op X\" aria-label=\"Delen op X\" role=\"button\" rel=\"noopener nofollow\" class=\"shariff-link\" style=\"; background-color:#000; color:#000\" target=\"_blank\"><span class=\"shariff-icon\" style=\"fill:#000\"><svg width=\"32px\" height=\"20px\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 24 24\"><path fill=\"#000\" d=\"M14.258 10.152L23.176 0h-2.113l-7.747 8.813L7.133 0H0l9.352 13.328L0 23.973h2.113l8.176-9.309 6.531 9.309h7.133zm-2.895 3.293l-.949-1.328L2.875 1.56h3.246l6.086 8.523.945 1.328 7.91 11.078h-3.246zm0 0\"\/><\/svg><\/span><span class=\"shariff-text\" style=\"color:#000\">delen<\/span>&nbsp;<\/a><\/li><li class=\"shariff-button facebook shariff-nocustomcolor\" style=\"background-color:#4273c8\"><a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fpicockpit.com%2Fraspberry-pi%2Fnl%2Fraspberry-pi-pico-w-beginners-componenten-tutorial%2F\" title=\"Delen op Facebook\" aria-label=\"Delen op Facebook\" role=\"button\" rel=\"nofollow\" class=\"shariff-link\" style=\"; background-color:#3b5998; color:#3b5998\" target=\"_blank\"><span class=\"shariff-icon\" style=\"fill:#3b5998\"><svg width=\"32px\" height=\"20px\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 18 32\"><path fill=\"#3b5998\" d=\"M17.1 0.2v4.7h-2.8q-1.5 0-2.1 0.6t-0.5 1.9v3.4h5.2l-0.7 5.3h-4.5v13.6h-5.5v-13.6h-4.5v-5.3h4.5v-3.9q0-3.3 1.9-5.2t5-1.8q2.6 0 4.1 0.2z\"\/><\/svg><\/span><span class=\"shariff-text\" style=\"color:#3b5998\">delen<\/span>&nbsp;<\/a><\/li><li class=\"shariff-button telegram shariff-nocustomcolor\" style=\"background-color:#4084A6\"><a href=\"https:\/\/telegram.me\/share\/url?url=https%3A%2F%2Fpicockpit.com%2Fraspberry-pi%2Fnl%2Fraspberry-pi-pico-w-beginners-componenten-tutorial%2F&text=Raspberry%20Pi%20Pico%20W%20beginners%E2%80%99%20components%20tutorial\" title=\"Delen op Telegram\" aria-label=\"Delen op Telegram\" role=\"button\" rel=\"noopener nofollow\" class=\"shariff-link\" style=\"; background-color:#0088cc; color:#0088cc\" target=\"_blank\"><span class=\"shariff-icon\" style=\"fill:#0088cc\"><svg width=\"32px\" height=\"20px\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 32 32\"><path fill=\"#0088cc\" d=\"M30.8 6.5l-4.5 21.4c-.3 1.5-1.2 1.9-2.5 1.2L16.9 24l-3.3 3.2c-.4.4-.7.7-1.4.7l.5-7L25.5 9.2c.6-.5-.1-.8-.9-.3l-15.8 10L2 16.7c-1.5-.5-1.5-1.5.3-2.2L28.9 4.3c1.3-.5 2.3.3 1.9 2.2z\"\/><\/svg><\/span><span class=\"shariff-text\" style=\"color:#0088cc\">delen<\/span>&nbsp;<\/a><\/li><li class=\"shariff-button reddit shariff-nocustomcolor\" style=\"background-color:#ff5700\"><a href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fpicockpit.com%2Fraspberry-pi%2Fnl%2Fraspberry-pi-pico-w-beginners-componenten-tutorial%2F\" title=\"Delen op Reddit\" aria-label=\"Delen op Reddit\" role=\"button\" rel=\"noopener nofollow\" class=\"shariff-link\" style=\"; background-color:#ff4500; color:#ff4500\" target=\"_blank\"><span class=\"shariff-icon\" style=\"fill:#ff4500\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path fill=\"#ff4500\" d=\"M440.3 203.5c-15 0-28.2 6.2-37.9 15.9-35.7-24.7-83.8-40.6-137.1-42.3L293 52.3l88.2 19.8c0 21.6 17.6 39.2 39.2 39.2 22 0 39.7-18.1 39.7-39.7s-17.6-39.7-39.7-39.7c-15.4 0-28.7 9.3-35.3 22l-97.4-21.6c-4.9-1.3-9.7 2.2-11 7.1L246.3 177c-52.9 2.2-100.5 18.1-136.3 42.8-9.7-10.1-23.4-16.3-38.4-16.3-55.6 0-73.8 74.6-22.9 100.1-1.8 7.9-2.6 16.3-2.6 24.7 0 83.8 94.4 151.7 210.3 151.7 116.4 0 210.8-67.9 210.8-151.7 0-8.4-.9-17.2-3.1-25.1 49.9-25.6 31.5-99.7-23.8-99.7zM129.4 308.9c0-22 17.6-39.7 39.7-39.7 21.6 0 39.2 17.6 39.2 39.7 0 21.6-17.6 39.2-39.2 39.2-22 .1-39.7-17.6-39.7-39.2zm214.3 93.5c-36.4 36.4-139.1 36.4-175.5 0-4-3.5-4-9.7 0-13.7 3.5-3.5 9.7-3.5 13.2 0 27.8 28.5 120 29 149 0 3.5-3.5 9.7-3.5 13.2 0 4.1 4 4.1 10.2.1 13.7zm-.8-54.2c-21.6 0-39.2-17.6-39.2-39.2 0-22 17.6-39.7 39.2-39.7 22 0 39.7 17.6 39.7 39.7-.1 21.5-17.7 39.2-39.7 39.2z\"\/><\/svg><\/span><span class=\"shariff-text\" style=\"color:#ff4500\">delen<\/span>&nbsp;<\/a><\/li><\/ul><\/div>","protected":false},"excerpt":{"rendered":"<p>In deze Raspberry Pi Pico W Beginnerstutorial leer je hoe je met de microcontroller kunt communiceren met eenvoudige componenten zoals LED's, ultrasone sensoren en vele andere dingen die je in een beginnerskit vindt. Als je een complete beginner bent, zal deze tutorial je helpen MicroPython te begrijpen zodat je kunt lezen, schrijven...<\/p>","protected":false},"author":2,"featured_media":3374,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[463,466,462,467,189,447,187,461,465,464],"class_list":["post-3305","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","tag-beginner","tag-components","tag-newbie","tag-parts","tag-pi","tag-projects","tag-raspberry-pi","tag-starter","tag-tutor","tag-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Raspberry Pi Pico W beginners&#039; components tutorial | PiCockpit<\/title>\n<meta name=\"description\" content=\"A Raspberry Pi Pico W Tutorial that covers how to interact with components wirelessly on the Raspberry Pi Pico W.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/picockpit.com\/raspberry-pi\/nl\/raspberry-pi-pico-w-beginners-componenten-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"nl_NL\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Raspberry Pi Pico W beginners&#039; components tutorial | PiCockpit\" \/>\n<meta property=\"og:description\" content=\"A Raspberry Pi Pico W Tutorial that covers how to interact with components wirelessly on the Raspberry Pi Pico W.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/picockpit.com\/raspberry-pi\/nl\/raspberry-pi-pico-w-beginners-componenten-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"PiCockpit\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/pi3gshop\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-15T21:21:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-16T15:43:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1778\" \/>\n\t<meta property=\"og:image:height\" content=\"1000\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"raspi berry\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pi3g\" \/>\n<meta name=\"twitter:site\" content=\"@pi3g\" \/>\n<meta name=\"twitter:label1\" content=\"Geschreven door\" \/>\n\t<meta name=\"twitter:data1\" content=\"raspi berry\" \/>\n\t<meta name=\"twitter:label2\" content=\"Verwachte leestijd\" \/>\n\t<meta name=\"twitter:data2\" content=\"41 minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/\"},\"author\":{\"name\":\"raspi berry\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#\\\/schema\\\/person\\\/7af3a7a8c999bc722029ef9ca9d78eee\"},\"headline\":\"Raspberry Pi Pico W beginners&#8217; components tutorial\",\"datePublished\":\"2022-11-15T21:21:23+00:00\",\"dateModified\":\"2023-08-16T15:43:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/\"},\"wordCount\":7039,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/featured-image-2.jpg\",\"keywords\":[\"Beginner\",\"components\",\"Newbie\",\"parts\",\"Pi\",\"Projects\",\"Raspberry Pi\",\"Starter\",\"Tutor\",\"Tutorial\"],\"articleSection\":[\"Raspberry Pi Pico\"],\"inLanguage\":\"nl-NL\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/\",\"url\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/\",\"name\":\"Raspberry Pi Pico W beginners' components tutorial | PiCockpit\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/featured-image-2.jpg\",\"datePublished\":\"2022-11-15T21:21:23+00:00\",\"dateModified\":\"2023-08-16T15:43:48+00:00\",\"description\":\"A Raspberry Pi Pico W Tutorial that covers how to interact with components wirelessly on the Raspberry Pi Pico W.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/#breadcrumb\"},\"inLanguage\":\"nl-NL\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"nl-NL\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/#primaryimage\",\"url\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/featured-image-2.jpg\",\"contentUrl\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/featured-image-2.jpg\",\"width\":1778,\"height\":1000,\"caption\":\"raspberry pi pico w components tutorial\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/raspberry-pi-pico-w-beginners-components-tutorial\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Raspberry Pi Pico\",\"item\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/category\\\/raspberry-pi-pico\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Raspberry Pi Pico W beginners&#8217; components tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#website\",\"url\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/\",\"name\":\"PiCockpit\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"nl-NL\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#organization\",\"name\":\"PiCockpit.com\",\"url\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"nl-NL\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/Screenshot-from-2023-07-24-15-01-24.png\",\"contentUrl\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/Screenshot-from-2023-07-24-15-01-24.png\",\"width\":1165,\"height\":283,\"caption\":\"PiCockpit.com\"},\"image\":{\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/pi3gshop\",\"https:\\\/\\\/x.com\\\/pi3g\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/#\\\/schema\\\/person\\\/7af3a7a8c999bc722029ef9ca9d78eee\",\"name\":\"raspi berry\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"nl-NL\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c10c423ceeff34163dd54a5472d6da17d71c0e360e0cf8cdb8847006372f216b?s=96&d=blank&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c10c423ceeff34163dd54a5472d6da17d71c0e360e0cf8cdb8847006372f216b?s=96&d=blank&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c10c423ceeff34163dd54a5472d6da17d71c0e360e0cf8cdb8847006372f216b?s=96&d=blank&r=g\",\"caption\":\"raspi berry\"},\"description\":\"I'm here to help you with all your Raspberry Pi related questions &amp; love to share about new project ideas for the Raspberry Pi, and give quick tips. Of course I'll also help you with PiCockpit and how to make your Raspberry Pi even easier to use with this raspberry pi monitoring solution :-)\",\"sameAs\":[\"https:\\\/\\\/picockpit.com\"],\"url\":\"https:\\\/\\\/picockpit.com\\\/raspberry-pi\\\/nl\\\/author\\\/raspi-berry\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tips voor beginners over Raspberry Pi Pico W onderdelen | PiCockpit","description":"Een Raspberry Pi Pico W tutorial die laat zien hoe je draadloos kunt communiceren met componenten op de Raspberry Pi Pico W.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/picockpit.com\/raspberry-pi\/nl\/raspberry-pi-pico-w-beginners-componenten-tutorial\/","og_locale":"nl_NL","og_type":"article","og_title":"Raspberry Pi Pico W beginners' components tutorial | PiCockpit","og_description":"A Raspberry Pi Pico W Tutorial that covers how to interact with components wirelessly on the Raspberry Pi Pico W.","og_url":"https:\/\/picockpit.com\/raspberry-pi\/nl\/raspberry-pi-pico-w-beginners-componenten-tutorial\/","og_site_name":"PiCockpit","article_publisher":"https:\/\/www.facebook.com\/pi3gshop","article_published_time":"2022-11-15T21:21:23+00:00","article_modified_time":"2023-08-16T15:43:48+00:00","og_image":[{"width":1778,"height":1000,"url":"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2.jpg","type":"image\/jpeg"}],"author":"raspi berry","twitter_card":"summary_large_image","twitter_creator":"@pi3g","twitter_site":"@pi3g","twitter_misc":{"Geschreven door":"raspi berry","Verwachte leestijd":"41 minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#article","isPartOf":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/"},"author":{"name":"raspi berry","@id":"https:\/\/picockpit.com\/raspberry-pi\/#\/schema\/person\/7af3a7a8c999bc722029ef9ca9d78eee"},"headline":"Raspberry Pi Pico W beginners&#8217; components tutorial","datePublished":"2022-11-15T21:21:23+00:00","dateModified":"2023-08-16T15:43:48+00:00","mainEntityOfPage":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/"},"wordCount":7039,"commentCount":6,"publisher":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/#organization"},"image":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2.jpg","keywords":["Beginner","components","Newbie","parts","Pi","Projects","Raspberry Pi","Starter","Tutor","Tutorial"],"articleSection":["Raspberry Pi Pico"],"inLanguage":"nl-NL","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/","url":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/","name":"Tips voor beginners over Raspberry Pi Pico W onderdelen | PiCockpit","isPartOf":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/#website"},"primaryImageOfPage":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2.jpg","datePublished":"2022-11-15T21:21:23+00:00","dateModified":"2023-08-16T15:43:48+00:00","description":"Een Raspberry Pi Pico W tutorial die laat zien hoe je draadloos kunt communiceren met componenten op de Raspberry Pi Pico W.","breadcrumb":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#breadcrumb"},"inLanguage":"nl-NL","potentialAction":[{"@type":"ReadAction","target":["https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"nl-NL","@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#primaryimage","url":"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2.jpg","contentUrl":"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2022\/11\/featured-image-2.jpg","width":1778,"height":1000,"caption":"raspberry pi pico w components tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/picockpit.com\/raspberry-pi\/raspberry-pi-pico-w-beginners-components-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/picockpit.com\/raspberry-pi\/"},{"@type":"ListItem","position":2,"name":"Raspberry Pi Pico","item":"https:\/\/picockpit.com\/raspberry-pi\/category\/raspberry-pi-pico\/"},{"@type":"ListItem","position":3,"name":"Raspberry Pi Pico W beginners&#8217; components tutorial"}]},{"@type":"WebSite","@id":"https:\/\/picockpit.com\/raspberry-pi\/#website","url":"https:\/\/picockpit.com\/raspberry-pi\/","name":"PiCockpit","description":"","publisher":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/picockpit.com\/raspberry-pi\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"nl-NL"},{"@type":"Organization","@id":"https:\/\/picockpit.com\/raspberry-pi\/#organization","name":"PiCockpit.com","url":"https:\/\/picockpit.com\/raspberry-pi\/","logo":{"@type":"ImageObject","inLanguage":"nl-NL","@id":"https:\/\/picockpit.com\/raspberry-pi\/#\/schema\/logo\/image\/","url":"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2023\/07\/Screenshot-from-2023-07-24-15-01-24.png","contentUrl":"https:\/\/picockpit.com\/raspberry-pi\/wp-content\/uploads\/2023\/07\/Screenshot-from-2023-07-24-15-01-24.png","width":1165,"height":283,"caption":"PiCockpit.com"},"image":{"@id":"https:\/\/picockpit.com\/raspberry-pi\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/pi3gshop","https:\/\/x.com\/pi3g"]},{"@type":"Person","@id":"https:\/\/picockpit.com\/raspberry-pi\/#\/schema\/person\/7af3a7a8c999bc722029ef9ca9d78eee","name":"framboos","image":{"@type":"ImageObject","inLanguage":"nl-NL","@id":"https:\/\/secure.gravatar.com\/avatar\/c10c423ceeff34163dd54a5472d6da17d71c0e360e0cf8cdb8847006372f216b?s=96&d=blank&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c10c423ceeff34163dd54a5472d6da17d71c0e360e0cf8cdb8847006372f216b?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c10c423ceeff34163dd54a5472d6da17d71c0e360e0cf8cdb8847006372f216b?s=96&d=blank&r=g","caption":"raspi berry"},"description":"Ik ben hier om u te helpen met al uw Raspberry Pi gerelateerde vragen &amp; deel graag over nieuwe projectidee\u00ebn voor de Raspberry Pi, en geef snelle tips. Natuurlijk help ik u ook met PiCockpit en hoe u uw Raspberry Pi nog makkelijker in gebruik kunt maken met deze raspberry pi monitoring oplossing :-)","sameAs":["https:\/\/picockpit.com"],"url":"https:\/\/picockpit.com\/raspberry-pi\/nl\/author\/raspi-berry\/"}]}},"_links":{"self":[{"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/posts\/3305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/comments?post=3305"}],"version-history":[{"count":61,"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/posts\/3305\/revisions"}],"predecessor-version":[{"id":4807,"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/posts\/3305\/revisions\/4807"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/media\/3374"}],"wp:attachment":[{"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/media?parent=3305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/categories?post=3305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/picockpit.com\/raspberry-pi\/nl\/wp-json\/wp\/v2\/tags?post=3305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}