-
Notifications
You must be signed in to change notification settings - Fork 16
/
01_guide.html
executable file
·1 lines (1 loc) · 16.8 KB
/
01_guide.html
1
<html><head><meta content="text/html; charset=UTF-8" http-equiv="content-type"><style type="text/css">ol.lst-kix_2xd75vdbhvyp-8.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-8 0}.lst-kix_2xd75vdbhvyp-8>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-8,lower-roman) ". "}ol.lst-kix_2xd75vdbhvyp-1.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-1 0}.lst-kix_2xd75vdbhvyp-2>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-2,lower-roman) ". "}ol.lst-kix_2xd75vdbhvyp-3{list-style-type:none}ol.lst-kix_2xd75vdbhvyp-5.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-5 0}ol.lst-kix_2xd75vdbhvyp-2{list-style-type:none}ol.lst-kix_2xd75vdbhvyp-5{list-style-type:none}.lst-kix_2xd75vdbhvyp-3>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-3,decimal) ". "}ol.lst-kix_2xd75vdbhvyp-4{list-style-type:none}ol.lst-kix_2xd75vdbhvyp-7{list-style-type:none}ol.lst-kix_2xd75vdbhvyp-6{list-style-type:none}ol.lst-kix_2xd75vdbhvyp-6.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-6 0}.lst-kix_2xd75vdbhvyp-6>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-6}.lst-kix_2xd75vdbhvyp-4>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-4,lower-latin) ". "}ol.lst-kix_2xd75vdbhvyp-8{list-style-type:none}.lst-kix_2xd75vdbhvyp-3>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-3}.lst-kix_2xd75vdbhvyp-5>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-5,lower-roman) ". "}.lst-kix_2xd75vdbhvyp-7>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-7,lower-latin) ". "}ol.lst-kix_2xd75vdbhvyp-1{list-style-type:none}ol.lst-kix_2xd75vdbhvyp-3.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-3 0}ol.lst-kix_2xd75vdbhvyp-0{list-style-type:none}.lst-kix_2xd75vdbhvyp-0>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-0}.lst-kix_2xd75vdbhvyp-6>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-6,decimal) ". "}ol.lst-kix_2xd75vdbhvyp-2.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-2 0}.lst-kix_2xd75vdbhvyp-7>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-7}.lst-kix_2xd75vdbhvyp-1>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-1}ol.lst-kix_2xd75vdbhvyp-0.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-0 0}.lst-kix_2xd75vdbhvyp-4>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-4}.lst-kix_2xd75vdbhvyp-1>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-1,lower-latin) ". "}.lst-kix_2xd75vdbhvyp-0>li:before{content:"" counter(lst-ctn-kix_2xd75vdbhvyp-0,decimal) ". "}.lst-kix_2xd75vdbhvyp-2>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-2}ol.lst-kix_2xd75vdbhvyp-4.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-4 0}.lst-kix_2xd75vdbhvyp-5>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-5}ol.lst-kix_2xd75vdbhvyp-7.start{counter-reset:lst-ctn-kix_2xd75vdbhvyp-7 0}.lst-kix_2xd75vdbhvyp-8>li{counter-increment:lst-ctn-kix_2xd75vdbhvyp-8}ol{margin:0;padding:0}table td,table th{padding:0}.c7{margin-left:36pt;padding-top:0pt;padding-left:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c5{margin-left:72pt;padding-top:0pt;padding-left:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c10{margin-left:108pt;padding-top:0pt;padding-left:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c9{margin-left:144pt;padding-top:0pt;padding-left:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c0{padding-top:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left;height:11pt}.c17{padding-top:0pt;padding-bottom:0pt;line-height:1.0;orphans:2;widows:2;text-align:left;height:11pt}.c8{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:14pt;font-family:"Arial";font-style:normal}.c3{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Arial";font-style:normal}.c2{color:#000000;font-weight:700;text-decoration:none;vertical-align:baseline;font-size:11pt;font-family:"Arial";font-style:normal}.c15{color:#000000;font-weight:400;text-decoration:none;vertical-align:baseline;font-size:12pt;font-family:"Arial";font-style:normal}.c12{padding-top:0pt;padding-bottom:0pt;line-height:1.15;orphans:2;widows:2;text-align:left}.c19{color:#000000;text-decoration:none;vertical-align:baseline;font-size:16pt;font-family:"Arial";font-style:normal}.c13{padding-top:18pt;padding-bottom:6pt;line-height:1.15;page-break-after:avoid;text-align:left}.c20{background-color:#ffffff;max-width:468pt;padding:72pt 72pt 72pt 72pt}.c1{padding:0;margin:0}.c16{margin-left:180pt;padding-left:0pt}.c4{color:#1155cc;text-decoration:underline}.c11{color:inherit;text-decoration:inherit}.c14{font-size:14pt}.c6{font-weight:700}.c18{font-size:12pt}.title{padding-top:0pt;color:#000000;font-size:26pt;padding-bottom:3pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}.subtitle{padding-top:0pt;color:#666666;font-size:15pt;padding-bottom:16pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}li{color:#000000;font-size:11pt;font-family:"Arial"}p{margin:0;color:#000000;font-size:11pt;font-family:"Arial"}h1{padding-top:20pt;color:#000000;font-size:20pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h2{padding-top:18pt;color:#000000;font-size:16pt;padding-bottom:6pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h3{padding-top:16pt;color:#434343;font-size:14pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h4{padding-top:14pt;color:#666666;font-size:12pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h5{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;orphans:2;widows:2;text-align:left}h6{padding-top:12pt;color:#666666;font-size:11pt;padding-bottom:4pt;font-family:"Arial";line-height:1.15;page-break-after:avoid;font-style:italic;orphans:2;widows:2;text-align:left}</style></head><body class="c20"><h2 class="c13" id="h.ksepu0yat1sn"><span class="c6 c19">Raspberry Pi and Cloud IoT Core Basics Step-by-step:</span></h2><p class="c0"><span class="c3"></span></p><p class="c12"><span class="c14">Author: Gabe Weiss <</span><span class="c4 c14"><a class="c11" href="mailto:[email protected]">[email protected]</a></span><span class="c8">></span></p><p class="c17"><span class="c8"></span></p><p class="c12"><span class="c15">see also:</span></p><p class="c12"><span class="c18">GitHub: </span><span class="c4 c18"><a class="c11" href="https://www.google.com/url?q=https://github.com/GabeWeiss/IoT_Core_Quick_Starts&sa=D&ust=1512075803326000&usg=AFQjCNH2m2o2ctPlC9F4CN2dcdvSkOTWvQ">https://github.com/GabeWeiss/IoT_Core_Quick_Starts</a></span></p><p class="c12"><span class="c15">Twitter: @GabeWeiss_</span></p><p class="c0"><span class="c8"></span></p><p class="c12"><span class="c3">Note that these instructions double as a self-guide for myself in doing a live demo of this content, so some of the instructions revolve around being sure I have a clean demo environment, or customizing my personal environment to make my life easier. E.g. Changing keyboard layout on the PI obviously can be safely ignored if you’re in the UK.</span></p><p class="c0"><span class="c3"></span></p><p class="c12"><span class="c6">Note:</span><span class="c3"> For this, I flash full Raspbian to my PI because the cryptography Python library requires python3 and the thin install doesn’t include Python 3, only 2. Cryptography requires Python 3, and is now used instead of pycrypto in the jwt library, which is what Cloud IoT Core uses for authentication. You can just do Raspbian Light, and then install Python 3 if that ends up being easier.</span></p><p class="c0"><span class="c3"></span></p><ol class="c1 lst-kix_2xd75vdbhvyp-0 start" start="1"><li class="c7"><span class="c6">Clean laptop to be sure we’re back to </span><span class="c6">zero</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span class="c3">Edit ~/.ssh/known_hosts to remove the entry for the PI. It changes every install.<br></span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-0" start="2"><li class="c7"><span class="c2">Basic device config to make it easier to use (if using monitor)</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span class="c3">Change keyboard layout in preferences to US->English</span></li><li class="c5"><span class="c3">Change font in terminal to Monospace 14<br></span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-0" start="3"><li class="c7"><span class="c6">Basic device config (general)</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span class="c3">Configure wifi</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2 start" start="1"><li class="c10"><span class="c3">Fetch list of wifi networks available</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-3 start" start="1"><li class="c9"><span class="c3">`sudo iwlist wlan0 scan`</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2" start="2"><li class="c10"><span class="c3">Configure one</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-3 start" start="1"><li class="c9"><span class="c3">`sudo vi /etc/wpa_supplicant/wpa_supplicant.conf`</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2" start="3"><li class="c10"><span class="c3">Replace contents of that file with:</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-3 start" start="1"><li class="c9"><span class="c3">If there’s a password:</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-4 start" start="1"><li class="c12 c16"><span class="c3">ctrl_interface=/var/run/wpa_supplicant<br><br>network={<br> ssid=”<ssid>”<br> psk=”<password for network>”<br>}</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-3" start="2"><li class="c9"><span class="c3">If there’s no password:</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-4 start" start="1"><li class="c12 c16"><span class="c3">ctrl_interface=/var/run/wpa_supplicant<br><br>network={<br> ssid=”<ssid>”<br> key_mgmt=NONE<br>}</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2" start="4"><li class="c10"><span class="c3">Restart wpa_supplicant service to engage wlan0</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-3 start" start="1"><li class="c9"><span class="c3">`sudo wpa_cli reconfigure`</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2" start="5"><li class="c10"><span class="c3">Verify wlan0 is now working</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-3 start" start="1"><li class="c9"><span class="c3">`ifconfig wlan0 | grep inet` should have an IP address</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2" start="6"><li class="c10"><span class="c3">If it fails, you can use `wpa_supplicant -iwlan0 -c /etc/wpa_supplicant.conf & dhcpcd wlan0` to restart it without rebooting</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1" start="2"><li class="c5"><span class="c3">`sudo apt-get update`</span></li><li class="c5"><span class="c3">Required modules for cryptography which pyjwt uses now instead of pycrypto</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2 start" start="1"><li class="c10"><span class="c3">`sudo apt-get install build-essential`</span></li><li class="c10"><span class="c3">`sudo apt-get install libssl-dev`</span></li><li class="c10"><span class="c3">`sudo apt-get install python-dev`</span></li><li class="c10"><span class="c3">`sudo apt-get install libffi-dev`<br></span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-0" start="4"><li class="c7"><span class="c2">Prep the Cloud account (only need to do once)</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span class="c3">Enable billing</span></li><li class="c5"><span class="c3">Enable Cloud IoT API (done via clicking onto IoT Core pantheon)</span></li><li class="c5"><span class="c3">Create a Pub/Sub Topic</span></li><li class="c5"><span class="c3">Create a Pub/Sub subscription for the topic</span></li><li class="c5"><span class="c3">Create an IoT Core registry<br></span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-0" start="5"><li class="c7"><span class="c2">Add device to IoT Core</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span class="c3">Create ssl certificate</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2 start" start="1"><li class="c10"><span class="c3">`mkdir ~/.ssh`</span></li><li class="c10"><span class="c3">`cd ~/.ssh`</span></li><li class="c10"><span class="c3">`openssl req -x509 -newkey rsa:2048 -keyout demo_private.pem -nodes -out demo.pub -subj "/CN=unused"`</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1" start="2"><li class="c5"><span class="c3">Add device to IoT Core registry created above using pantheon</span></li><li class="c5"><span class="c3">Grab Google's root certificate</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2 start" start="1"><li class="c10"><span>`wget </span><span class="c4"><a class="c11" href="https://www.google.com/url?q=https://pki.google.com/roots.pem&sa=D&ust=1512075803330000&usg=AFQjCNG1uSXzEDqksv8hojHbzeUqR78OIQ">https://pki.google.com/roots.pem</a></span><span class="c3">`<br></span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-0" start="6"><li class="c7"><span class="c2">Prep the Device</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span>Install </span><span class="c4"><a class="c11" href="https://www.google.com/url?q=https://www.raspberrypi.org/products/sense-hat/%23buy-now-modal&sa=D&ust=1512075803330000&usg=AFQjCNFRYCBzVTLKm1wDjdfUCILMb-qR6w">Sense HAT AP</a></span><span class="c4"><a class="c11" href="https://www.google.com/url?q=https://www.raspberrypi.org/products/sense-hat/%23buy-now-modal&sa=D&ust=1512075803331000&usg=AFQjCNEKtnSoqp6z2We-_SU8N-k9Ba-z4A">I</a></span><span class="c3"> (looks like this is already present on PI model 3)</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2 start" start="1"><li class="c10"><span class="c4"><a class="c11" href="https://www.google.com/url?q=https://www.raspberrypi.org/documentation/hardware/sense-hat/&sa=D&ust=1512075803331000&usg=AFQjCNEheqiUt3VzQhk6O4uLD9a8LE_XPg">https://www.raspberrypi.org/documentation/hardware/sense-hat/</a></span></li><li class="c10"><span class="c3">`sudo pip3 install sense-hat`</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1" start="2"><li class="c5"><span class="c3">install paho mqtt</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2 start" start="1"><li class="c10"><span class="c4"><a class="c11" href="https://www.google.com/url?q=http://www.steves-internet-guide.com/into-mqtt-python-client/&sa=D&ust=1512075803332000&usg=AFQjCNFJkSCNngANA6UgFEE9A5aIW80Ezg">http://www.steves-internet-guide.com/into-mqtt-python-client/</a></span></li><li class="c10"><span class="c3">`sudo pip3 install paho-mqtt`</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1" start="3"><li class="c5"><span class="c3">install json web tokens</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2 start" start="1"><li class="c10"><span class="c4"><a class="c11" href="https://www.google.com/url?q=https://pyjwt.readthedocs.io/en/latest/&sa=D&ust=1512075803332000&usg=AFQjCNEB8-gc_WFSSF9yB-Qi5EA-_4xv4Q">https://pyjwt.readthedocs.io/en/latest/</a></span></li><li class="c10"><span class="c3">`sudo pip3 install pyjwt`</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1" start="4"><li class="c5"><span class="c3">install cryptography (required by encoding in PyJWT)</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-2 start" start="1"><li class="c10"><span class="c4"><a class="c11" href="https://www.google.com/url?q=https://cryptography.io/en/latest/&sa=D&ust=1512075803333000&usg=AFQjCNGx5n3RWyTQLvwVUR5yjc3MTuIthw">https://cryptography.io/en/latest/</a></span></li><li class="c10"><span class="c3">sudo pip3 install cryptography<br></span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-0" start="7"><li class="c7"><span class="c6">Write the code</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span>See 01_basics.py</span><span class="c2"><br></span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-0" start="8"><li class="c7"><span class="c6">Run it</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span>`python3 01_basics.py`</span><span class="c2"><br></span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-0" start="9"><li class="c7"><span class="c2">Verify data in pub/sub topic/subscription</span></li></ol><ol class="c1 lst-kix_2xd75vdbhvyp-1 start" start="1"><li class="c5"><span class="c3">`gcloud beta pubsub subscriptions pull --max-messages=3 <subscription id from Prep the Cloud section>`</span></li></ol><p class="c0"><span class="c3"></span></p></body></html>