Video 2.3 connecting a Raspberry Pi to AWS IoT. In this video, they will learn the following connecting sensor to Raspberry Pi installing AWS IoT Python SDK programming Raspberry Pi testing connecting sensor to Raspberry Pi. In this project we will be using and head CSS zero for ultrasonic sensor, which will measure the distance between the source and the target using ultrasonic waves. It has four pins Vcc trigger input, a code bird and a ground pin. The ultrasonic sensor generates a high frequency sound and calculates the time taken for sending a signal and receiving an echo. Therefore, it can be used to measure distance.
Basically it consists of a transmitter, a receiver and a control module. I will explain the working principle in three simple steps First, in order to generate the I dress Sonic sound, we need to set the trigger pin in high state for at least 10 microseconds, then the sensor will send eight cycles Sonic burst, which will travel at the speed of the sound. Finally, if there is any object or an obstacle in its path, then it will bounce back to the receiver, which is the echo pin. Based on the travel time and the speed of the sound, we can calculate the distance. We use simple physics to do this, we know that speed is equal to distance by time. So distance is equal to speed into time, which is speeding duty by to speed, the speed of sound traveling in air, which is 343 meters per second, nothing but 34,300 centimeter per second, T by two wondering why I did this.
This is because the transmitter sends a signal out and then receives the same signal back on reflection. So the total distance and time taken is twice So distance is equal to 34,300 divided by two into t. This gives us 17,150 into T in centimeters. To know more details about the sensor, you can check out the data sheet that is in the resources section. Now we'll start connecting this sensor to the Raspberry Pi. Before you start correcting the sensor, you will need two resistors wondering why here is the reason the echo output pin is a five volt, but the GPIO pin of Raspberry Pi can tolerate up to 3.3 volt. so five volt cannot be directly given to the 3.3 volt input pin.
So we'll use a voltage divider circuit using appropriate resistors to bring down the voltage to 3.3 volts or even less than 3.3 volt voltage divider circuit. This is the voltage divider circuit here near Veen. But if we are giving exports as input then the output At V out, we'll be less than experts based on the resistor values, we can calculate the resistance values using this formula, V out is equal to V in into r two divided by r1 plus r two. So stepping down the voltage from five to 3.3, I'm using one key and two key ohm resistors. In this demo, if you don't have the above mentioned resistors, you can choose any combination of resistors that will satisfy this condition circuit diagram. First, we will place the two resistors in series, one K resistor and then two K resistor.
Now, one of our two is grounded. So connecting the ground pin on the sensor and the ground pin of raspberry pi to one end of our tube. Now given the input five volts to mean so I will be connecting the echo pin which gives a five volt output to one end of the resistor r1. Now V out will be 3.3 volts So connecting V out to GPIO 24. So we have connected the echo pin to GPIO 24, the voltage divider in the middle to step down the voltage. Now, the trigger pin can be directly connected to GPIO 23.
Finally, giving the power supply. I'm connecting the five volt output pin of raspberry pi to the VCC pin on the sensor. I'm attaching the circuit diagram in the resources section. You go through the carefully and make the appropriate connections. Remember, do not connect the echo pin directly use a voltage divider to bring down the voltage to 3.3 volts or is it can burn your Raspberry Pi. As we have completed the hardware connections, let's now install AWS IoT Python SDK in Raspberry Pi.
But wait, why should we install this? Yeah, you are right. AWS IoT Python SDK allows us to Write the Python script which helps devices to access the AWS IoT platform through mq TT. I hope everyone is familiar with mq TT. If not, let's learn the basics. In Qt T stands for message queuing telemetry transport.
Let me explain this to you in basic terms. In mq TT, we will have one broker to which all the devices get connected. A device basically publishes a message on to a topic or subscribes to a particular topic for receiving messages. Okay, to be more clear topic is similar to a filter. topics are the way you register interest for incoming messages, or how you specify where you want to publish the message. Suppose, imagine that you have a light sensor and a temperature sensor connected to your Raspberry Pi.
Also, assume that you have a belt and an AC with the temperature is more, it should send a message to AC and when it's dark, it should send a message to the bulk telling To turn on, but how here, a broker comes into the picture. A broker is primarily responsible for receiving all messages, filtering the messages, deciding who is interested in them, and then publishing those message to all subscribed clients. Okay, now it's time to install AWS IoT Python SDK, open your putty terminal and run these commands. You can find these commands in the resources section. This first command is for installing the SDK on your pipe. Yeah, it's done.
Now, this will have installed the AWS directory on your pipe. Let's navigate into the directory using the second command. Yes. Now, inside this directory, install the Python setup file using the third command. Since we did not log in with root, we will use sudo in the beginning of the command. Now, your Raspberry Pi is almost ready to connect with AWS IoT Core Send the sensor data.
So now we'll go to the final step programming the Raspberry Pi. Clear. Now, we will open the Python file and write the code. So sudo nano test project.pi open the repository that you have downloaded at the beginning of this course. Here, select section two. Now select video 2.3 open the Python file and copy the code and paste it in the test project dot p right now, we will understand the code.
We first imported the libraries that are required in this project. First importing our PI GPIO library and then the time library. This is for importing SDK packages. Here we are initializing the GPIO pins. The GPIO dot BCM means you're referring the pins by the Broadcom SOC channel number. These are the numbers After GPIO for example, GPIO 23 means GPIO number is 23.
But it is not the physical location of the pin on the Raspberry Pi. Next, this is the client name. I'm using the client name as my client ID, but you can use any name of your choice. Next, we will be giving the endpoint. In our case, we will be giving the host address for this address, go to the AWS IoT Core manage things, select the thing that you created in the previous video, which is my art buy. Here you can find interact, select that.
Now, this will be your host address, select and copy this. Now going back to putty and pasting the endpoint address. Next, we will be giving the location of our certificates for mutual authentication purpose. This will be the address where messages advocates are present slash home slash pi slash search slash and then my three certificates. You can see that I have copied the same location where my certificates are present on Raspberry Pi. Next, when there is an unstable internet connection or power supply, the AWS SDK provides an internal cue to store the mq TT messages.
Here. This command allows a connection to the client and This command will publish we can give any name in place of maker dummy here we are initializing trigger and echo pins to pin 23 and pin 24. And here we are setting the pins as GPIO out and GPIO in trigger pin will be the output pin and echo pin will be the input pin. You first need to set the trigger pin to high for 10 microseconds for sending an ultrasonic sound signal. here first we are making showed that the output is low by setting it too low for first two seconds, and then we'll make it high for 10 microseconds and again low. Next, after sending a 10 microsecond signal, we will be calculating the pulse width here, based on when the signal is St. And when it's echo has ended.
This is the formula that we have derived at the beginning of this video, we are rounding the distance up to two decimals. The range of sensor is two centimeters to 400 centimeters. So if the distance is within the limits, then we'll initialize the payload with the distance values. initializing payload here. This will print the distance values present in the payload. The data in the payload will be published to the mq TT topic maker dummies slash data.
Here, I'm printing the data in JSON format. Now to save the file, click Ctrl x, then y, and then enter. Now, make sure that the hardware connections are correct, and then run the Python script using the command Python test project.pi. Now, here you can see the distance by placing an object. I'll place the object at different distances from the sensor, which you can see accordingly. Are you excited to check if the data is being sent to the cloud in AWS IoT on the left hand side, you can find test selected and now you must give the topic name.
The topic name is maker dummies slash data and subscribe. Now, you can see the same data here in the cloud Hurry, we have completed sending the sensor data from Raspberry Pi to AWS IoT. In this video, we learned the following connecting sensor to Raspberry Pi, installing AWS IoT Python SDK interests byte programming Raspberry Pi testing. In the next video, we will connect ESP 32 to AWS IoT Core, the first steps