{"id":29348,"date":"2022-09-06T16:26:07","date_gmt":"2022-09-06T23:26:07","guid":{"rendered":"https:\/\/digilent.com\/blog\/?p=29348"},"modified":"2022-09-06T16:26:07","modified_gmt":"2022-09-06T23:26:07","slug":"build-a-smart-lamp-using-an-analog-discovery-pro-adp3450-part-4","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/build-a-smart-lamp-using-an-analog-discovery-pro-adp3450-part-4\/","title":{"rendered":"Build a Smart Lamp Using an Analog Discovery Pro ADP3450 \u2013 Part 4"},"content":{"rendered":"<p>In this last installment of our four-part blog series about creating a smart lamp, we will bring everything together. The <a href=\"https:\/\/digilent.com\/reference\/software\/waveforms\/waveforms-sdk\/start\">HAL for the WaveForms SDK<\/a> we developed in the <a href=\"https:\/\/digilent.com\/blog\/build-a-smart-lamp-using-an-analog-discovery-pro-adp3450-part-1\/\">Part 1<\/a>, the software functions for controlling our two Pmods, the <a href=\"https:\/\/digilent.com\/shop\/pmod-ble-bluetooth-low-energy-interface\/\">Pmod BLE<\/a> and the <a href=\"https:\/\/digilent.com\/shop\/pmod-als-ambient-light-sensor\/\">Pmod ALS<\/a>, we wrote in <a href=\"https:\/\/digilent.com\/blog\/build-a-smart-lamp-using-an-analog-discovery-pro-adp3450-part-2\/\">Part 2<\/a>, and the Android app we designed in the <a href=\"https:\/\/digilent.com\/blog\/build-a-smart-lamp-using-an-analog-discovery-pro-adp3450-part-3\/\">Part 3<\/a>.<\/p>\n<p>Now we are going to create the main script containing the application running on the <a href=\"https:\/\/digilent.com\/shop\/analog-discovery-pro-3000-series-portable-high-resolution-mixed-signal-oscilloscopes\/\">Analog Discovery Pro ADP3450<\/a> from Digilent, working in Linux Mode. Once everything is complete, you will be able to adjust luminosity and color of the LED through your phone\u2019s app.<\/p>\n<h2>Bringing the Software Together<\/h2>\n<p>As before, you can either write the application from scratch following the guidelines below, or you can <a href=\"https:\/\/digilent.com\/reference\/_media\/test-and-measurement\/analog-discovery-pro-3x50\/lamp_controller.zip\">download it from here<\/a>.<\/p>\n<p>First, import all the modules you created during the previous parts of this blog series. Then define the constants needed and develop some helper functions like one for decoding the incoming Bluetooth data. These functions make your life easier later on.<\/p>\n<p>Once done, write the body of your program. For our solution, we used a \u201ctry-except structure\u201d, but how you implement it is really up to you. Start with the initialization of the WaveForms instruments and the Pmods. Then move on and create an endless loop through a \u201cwhile True\u201d statement. Inside the loop, look for data packets received by the Pmod BLE and decode them. Query the ambient light brightness measured by the Pmod ALS as well. Eventually, you might want to read the voltage level of the battery and transmit it via Bluetooth to your smartphone.<\/p>\n<p>At the end, you will need to provide code which allows you to exit your program gracefully. Define an event you want to react to, e.g., pressing \u201cCtrl+C\u201d, and include statements that turn off the lamp, and close and reset the WaveForms instruments.<\/p>\n<h2>Putting the ADP3450 in Linux Mode<\/h2>\n<p>Before you can run your script, you will need to put the Analog Discovery Pro ADP3450 in Linux Mode. For this, follow the steps detailed in the guide <a href=\"https:\/\/digilent.com\/reference\/test-and-measurement\/analog-discovery-pro-3x50\/linux-mode\">\u201cGetting Started in Linux Mode with the Analog Discovery Pro (ADP3450\/ADP3250)\u201d<\/a>. When finished, follow the instructions in the guide <a href=\"https:\/\/digilent.com\/reference\/test-and-measurement\/analog-discovery-pro-3x50\/connect-to-the-internet\">\u201cConnecting the Analog Discovery Pro (ADP3450\/ADP3250) to the Internet\u201d<\/a>. This will allow you to install the Python package installer (pip) directly on the mixed signal oscilloscope (MSO) using the command below:<\/p>\n<p><a href=\"#_ftnref1\" name=\"_ftn1\"><span><\/span><\/a><span><a href=\"https:\/\/digilent.com\/reference\/test-and-measurement\/analog-discovery-pro-3x50\/linux-mode#-1\"><\/a><\/span><\/p>\n<blockquote><p><span>sudo apt install python3-pip<\/span><\/p><\/blockquote>\n<p>Next, copy the HAL files you created for the WaveForms instruments in Part 1 to your directory on the ADP3450. Before you can run the script, you have to execute the commands below from your terminal:<\/p>\n<blockquote><p><span>sudo su<\/span><\/p>\n<p><span>cd \/etc\/systemd\/system<\/span><\/p>\n<p><span>echo -n &#8220;&#8221; &gt; lamp.service<\/span><\/p>\n<p><span>nano lamp.service<\/span><\/p><\/blockquote>\n<p class=\"iE-Fliesstext\">Now type the following text in your editor but change the path according to your setup. Then save the file.<o:p><\/o:p><\/p>\n<blockquote><p><span>[Unit]<\/span><\/p>\n<p><span>Description=Smart Lamp Controller<\/span><\/p>\n<p><span>[Service]<\/span><\/p>\n<p><span>ExecStart=nohup python3 \/home\/digilent\/Smart-Lamp-Controller\/Python\/Lamp_Controller.py &amp;<\/span><\/p>\n<p><span>[Install]<\/span><\/p>\n<p><span>WantedBy=multi-user.target<\/span><\/p><\/blockquote>\n<p class=\"iE-Fliesstext\">And finally, enable the new service and reboot the MSO:<o:p><\/o:p><\/p>\n<blockquote><p><span>systemctl start lamp<\/span><\/p>\n<p><span>systemctl enable lamp<\/span><\/p>\n<p><span>reboot<\/span><\/p><\/blockquote>\n<h2>Running the Script<\/h2>\n<p>Once the ADP3450 has booted, start the script and as well as the app you created on your smartphone. Wait until the phone detects the Pmod BLE, connect to it and start playing with your sliders and switches in your app.<\/p>\n<p>If you want to make any changes to your script, you will need to stop it by pressing your key combination, make your changes, and restart it. Enjoy your smart lamp!<\/p>\n<p>A more detailed description of this project is<a href=\"https:\/\/digilent.com\/reference\/test-and-measurement\/analog-discovery-pro-3x50\/smart-lamp\"> available here<\/a>.<\/p>\n<p><a href=\"#_ftnref2\" name=\"_ftn2\"><span><\/span><\/a><span><a href=\"https:\/\/digilent.com\/shop\/analog-discovery-pro-3000-series-portable-high-resolution-mixed-signal-oscilloscopes\/#-1\"><\/a><\/span><\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-29348 jlk' data-task='like' data-post_id='29348' data-nonce='0f678f749c' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-29348 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-29348 jlk' data-task='unlike' data-post_id='29348' data-nonce='0f678f749c' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-29348 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-29348 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>In this last installment of our four-part blog series about creating a smart lamp, we will bring everything together. The HAL for the WaveForms SDK we developed in the Part &hellip; <\/p>\n","protected":false},"author":61,"featured_media":29229,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[38,4327,4312,4267,1563],"tags":[4352,4353],"ppma_author":[4506],"class_list":["post-29348","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-expansion-modules","category-projects","category-usb-scopes-analyzers-generators","category-featured","category-guide","tag-adp3450","tag-linux"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2022\/06\/smartlamp1.jpg","jetpack_sharing_enabled":true,"authors":[{"term_id":4506,"user_id":61,"is_guest":0,"slug":"richardoed","display_name":"Richard Oed","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/12de1ad12c6f52d1668873102baa76018ec3d4f2078318739209f4b75051c424?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/29348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/61"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=29348"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/29348\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/29229"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=29348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=29348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=29348"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=29348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}