# AWS Native Connected Printer - Connected Device In this section, we'll be setting up a Raspberry Pi to use as a print server. To do this, we will install some prerequisite packages on the device, including Python3.8, the AWS CLI, and the AWS IoT SDK for Python. The AWS IoT SDK for Python is what we will use, specifically, to provision the device as an IoT Thing, and use to connect to MQTT topics to emit device metrics back to the AWS Cloud for tracking device availability. Additionally, we are using the AWS IoT SDK for Python to subscribe to MQTT topics to receive print jobs when they are created by the front end application. ## Prerequisites 1. For this workshop, you'll need the ability to ssh onto your connected device. 2. Additionally, you'll need to install the following on the device: - [Python 3.8](https://www.python.org/downloads/release/python-380/) - [AWS IoT SDK for Python](https://docs.aws.amazon.com/greengrass/v1/developerguide/IoT-SDK.html) - You'll need to copy the contents of `connected_printer_device/src` to your IoT device. The `src` folder contains the scripts we will use to provision our device and connect to IoT Core via MQTT. - [Install the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) - Save the following certificate as /home/pi/.ssh/AmazonRootCA1.pem on the device: [AmazonRootCA1](https://www.amazontrust.com/repository/AmazonRootCA1.pem) ## Registering the Thing with Fleet Provisioning by Claim This workflow for provisioning uses a modified version of the [Python IoT Device SDK Fleet Provisioning Sample](https://github.com/aws/aws-iot-device-sdk-python-v2/tree/main/samples#fleet-provisioning); the modification enables the script to automatically replace your temporary bootstrap certificates with your permanent certificates that are generated by fleetprovisioning.py (without modification, the provision.py file simply prints out cert and pk contents in string form). On the device, run the following commands cd'd into `~/src` ### Get a Claim on Your Provisioning Cert ``` aws iot create-provisioning-claim \ --template-name ConnectedPrinterTemplate \ | python3 /home/pi/src/utils/parse_cert_set_result.py \ --path /tmp \ --filename provision ``` ### Provision the Thing 1. Prior to provisioning the device, you'll need to run an AWS CLI command to describe the regional IoT Endpoint for your account, and paste it in the examples below. - Execute: ```aws iot describe-endpoint --profile ``` The output will look something like this: ``` { "endpointAddress": "ab8n7nxxxxxxx.iot.us-west-2.amazonaws.com" } ``` We need to use the ATS endpoint. To do this, simply modify the endpointAddress so that it looks like this: `ab8n7nxxxxxxx-ats.iot.us-west-2.amazonaws.com` ``` python provision.py \ --endpoint ab8n7nxxxxxxx-ats.iot.us-west-2.amazonaws.com \ --ca_file /home/pi/.ssh/AmazonRootCA1.pem \ --cert /tmp/provision.cert.pem \ --key /tmp/provision.private.key \ --template_name ConnectedPrinterTemplate \ --template_parameters "{\"Customer\":\"ANYBANK\",\"DeviceID\":\"ffa08nnd-afd9-11ed-a5e7-53bhe1561de9\",\"DeviceName\":\"RASP_PI_ARMV7\",\"Location\":\"HQ\"}" ``` ## Getting the Printer to Print My home printer is an HP LaserJet Pro MFP M428fdw. These commands should work for any HP network-connected printer. ### Install these packages ``` sudo apt install lprng sudp apt-get install hplip sudo apt-get install hplip-ppds ``` ### Add your printer by Local IP ```hp-setup -gi ``` ### List Printers ```lpstat -p``` ### Print a Document ```lp -d -o media=a4 -o sides=one-sided ~/files/test_file``` ## Use Modified pubsub.py Sample to Connect and Listen to MQTT This workflow for connecting to MQTT with a connected printer uses a modified version of the [Python IoT Device SDK PubSub Sample](https://github.com/aws/aws-iot-device-sdk-python-v2/tree/main/samples#pubsub). The default example connects to test/topic, sends 10 messages to that topic, receives those same ten messages and terminates. This modified script connects to the device's MQTT topic and sends a print command using data from the MQTT message, while also regularly publishing telemetry data to IoT Core. Run the following command cd'd into `~/src`: ``` python3 connect_mqtt.py \ --endpoint ab8n7nxxxxxxx-ats.iot.us-west-2.amazonaws.com \ --ca_file /home/pi/.ssh/AmazonRootCA1.pem \ --cert /home/pi/.ssh/perm_cert.pem \ --key /home/pi/.ssh/perm_cert_pk.key \ --device_id ffa08nnd-afd9-11ed-a5e7-53bhe1561de9 \ --device_name RASP_PI_ARMV7 ``` Your IoT device is now regularly publishing device metrics to AWS IoT Core via MQTT, as well as listening for instructions to print on an MQTT topic. Next, head on over to [../connected_printer_frontend/README.md](https://github.com/aws-samples/aws-iot-connected-printer/blob/main/connected_printer_frontend/README.md) for interacting with the resources we've built and provisioned using a React-based frontend application.