├── README.md ├── pi.js └── readme_images ├── create_thing_1.png └── create_thing_2.png /README.md: -------------------------------------------------------------------------------- 1 | # How to connect a Raspberry Pi to the AWS IoT Platform 2 | by @sreid. 3 | 4 | ## Intro 5 | This is a simple getting started guide to setting up a Raspberry Pi to connect to the AWS Iot platform. 6 | 7 | ## Pre-reqs: 8 | - Raspberry Pi with a working install of Raspbian 9 | - SSH or command line interfaces to the Pi 10 | 11 | ## How To 12 | 13 | ### Install Node 14 | NodeJS is not currently in the Pi sources, however it can be easily with the following steps: 15 | 16 | ``` 17 | wget http://node-arm.herokuapp.com/node_latest_armhf.deb 18 | sudo dpkg -i node_latest_armhf.deb 19 | node -v # v4.0.0 20 | ``` 21 | 22 | (based on instructions found [here](http://www.andrewconnell.com/blog/setup-node-js-on-raspberry-pi-2-b 23 | )) 24 | 25 | ### Create your working directory 26 | For the code files, as well as for the certificates. 27 | 28 | ``` 29 | mkdir ~/aws-iot && cd ~/aws-iot 30 | mkdir ~/aws-iot/certs 31 | ``` 32 | 33 | ### Install the AWS SDK 34 | 35 | ``` 36 | npm install https://github.com/aws/aws-iot-device-sdk-js 37 | ``` 38 | 39 | 40 | ### IoT Platform Setup 41 | In the console, created a "Thing" called "pi_2". 42 | 43 | ![](./readme_images/create_thing_1.png) 44 | 45 | Then on that thing's detail view, "Connect a Device" 46 | 47 | ![](./readme_images/create_thing_2.png) 48 | 49 | Choose "NodeJS" and click "Generate Certificate & Policy". This will create a keypair, as well as add a policy to the pi_2 thing. 50 | 51 | Be sure to download the certificate, public key, & private key to your computer. 52 | 53 | ###Copy Certificates to the Pi 54 | 55 | Copy the `-certificate.pem.crt` and the `-private.pem.key` files to the `~/aws-iot/certs` directory on your Pi 56 | 57 | Also, grab the AWS CA certificate that's available from Symantec. This is for the MQTT client to verify it is connecting to a valid, Amazon controlled, server. 58 | 59 | ``` 60 | curl https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem > certs/rootCA.pem 61 | ``` 62 | 63 | ### Setup Test Script 64 | The pi.js script in this repository will report the Pi's local IP using the AWS IoT platform. You can grab a copy from this repository here: 65 | 66 | ``` 67 | curl https://raw.githubusercontent.com/sreid/aws-iot-raspberry-pi-how-to/master/pi.js > pi.js 68 | ``` 69 | 70 | Edit the file and update the config block to match your setup: 71 | ``` 72 | var thingShadows = awsIot.thingShadow({ 73 | keyPath: './certs/-private.pem.key', 74 | certPath: './certs/-certificate.pem.crt', 75 | caPath: './certs/rootCA.pem', 76 | clientId: myThingName, 77 | region: 'us-east-1' 78 | }); 79 | ``` 80 | 81 | ### Run It! 82 | In the ~/aws-iot directory, run: 83 | 84 | ``` 85 | node pi.js 86 | 87 | # Sample Output 88 | Connected... 89 | Registering... 90 | Updating my IP address... 91 | Update:pi_1-0 92 | received accepted on pi_1: {"state":{"reported":{"ip":"192.168.2.247"}},"metadata":{"reported":{"ip":{"timestamp":1444691875}}},"timestamp":1444691875} 93 | ``` 94 | 95 | You should be able to then go to the AWS IoT console, and see the updated state information in the thing details. 96 | 97 | ## Common Issues 98 | TBD 99 | 100 | ## Feedback 101 | Questions? Comments? Problems? Don't hesistate to get in touch. GitHub issues and pull requests are welcome. --@sreid 102 | -------------------------------------------------------------------------------- /pi.js: -------------------------------------------------------------------------------- 1 | var awsIot = require('aws-iot-device-sdk'); 2 | 3 | var myThingName = 'pi_2'; 4 | 5 | var thingShadows = awsIot.thingShadow({ 6 | keyPath: './certs/-private.pem.key', 7 | certPath: './certs/-certificate.pem.crt', 8 | caPath: './certs/rootCA.pem', 9 | clientId: myThingName, 10 | region: 'us-east-1' 11 | }); 12 | 13 | mythingstate = { 14 | "state": { 15 | "reported": { 16 | "ip": "unknown" 17 | } 18 | } 19 | } 20 | 21 | var networkInterfaces = require( 'os' ).networkInterfaces( ); 22 | mythingstate["state"]["reported"]["ip"] = networkInterfaces['eth0'][0]['address']; 23 | 24 | thingShadows.on('connect', function() { 25 | console.log("Connected..."); 26 | console.log("Registering..."); 27 | thingShadows.register( myThingName ); 28 | 29 | // An update right away causes a timeout error, so we wait about 2 seconds 30 | setTimeout( function() { 31 | console.log("Updating my IP address..."); 32 | clientTokenIP = thingShadows.update(myThingName, mythingstate); 33 | console.log("Update:" + clientTokenIP); 34 | }, 2500 ); 35 | 36 | 37 | // Code below just logs messages for info/debugging 38 | thingShadows.on('status', 39 | function(thingName, stat, clientToken, stateObject) { 40 | console.log('received '+stat+' on '+thingName+': '+ 41 | JSON.stringify(stateObject)); 42 | }); 43 | 44 | thingShadows.on('update', 45 | function(thingName, stateObject) { 46 | console.log('received update '+' on '+thingName+': '+ 47 | JSON.stringify(stateObject)); 48 | }); 49 | 50 | thingShadows.on('delta', 51 | function(thingName, stateObject) { 52 | console.log('received delta '+' on '+thingName+': '+ 53 | JSON.stringify(stateObject)); 54 | }); 55 | 56 | thingShadows.on('timeout', 57 | function(thingName, clientToken) { 58 | console.log('received timeout for '+ clientToken) 59 | }); 60 | 61 | thingShadows 62 | .on('close', function() { 63 | console.log('close'); 64 | }); 65 | thingShadows 66 | .on('reconnect', function() { 67 | console.log('reconnect'); 68 | }); 69 | thingShadows 70 | .on('offline', function() { 71 | console.log('offline'); 72 | }); 73 | thingShadows 74 | .on('error', function(error) { 75 | console.log('error', error); 76 | }); 77 | 78 | }); 79 | -------------------------------------------------------------------------------- /readme_images/create_thing_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreid/aws-iot-raspberry-pi-how-to/a59d74516de039b99ca4443d237775e1b5d8ed0f/readme_images/create_thing_1.png -------------------------------------------------------------------------------- /readme_images/create_thing_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sreid/aws-iot-raspberry-pi-how-to/a59d74516de039b99ca4443d237775e1b5d8ed0f/readme_images/create_thing_2.png --------------------------------------------------------------------------------