Video 4.2 sending messages to Sq s. In this video, we will learn the following programming lambda, understanding the code testing. In the previous video, we created a lambda function, my sp 32 underscore function and an IoT rule. My ESP 32 underscore lambda underscore rule. Now, we will program the lambda function, open AWS lambda. Here, select my ESP 32 underscore function, we will start writing a Python code here in the lambda function, which will send on and off messages to ask us based on the temperature values. Now, open the repository that you have downloaded at the beginning of this course.
Here, you can find section four selected, then, video 4.2. Now opened up identified with the name code for writing messages. SQL copied entire code. Now go back to the lambda function and select my sp 32 underscore function here, and then scroll down. In function code, you have lambda function here, paste the entire code. This code will sync on and off messages to the sq.
Sq based on the temperature values. Now, let's understand the code. In this first line, we are importing AWS SDK for Python. Next, here, we need to give access key and secret key of the IM user that we have created earlier. Open the Google Sheet that we have downloaded by creating the Iam user ID from this copy and paste access key. Similarly, copy the secret key and paste it here.
Next region for North Virginia it is us hyphen East hyphen, one. You can give the region in which you You are creating your project. Next queue URL on top, select services and search for Sq s, right click and open the link in a new tab. Here, we will create a new queue. For creating a new queue, select Create a new cue here. Now, we need to give the name for our cue, I will give my sp 32 underscore triggering underscore cue, standard cue and then click Create cue.
Yes, we created a new cue for triggering. Now let's copy this queue URL from here and paste it in the lambda function like this. Next, we are declaring a function post underscore message with client message body and URL as arguments. response. This sends a message to the queue with the specified queue URL and message body. Next, we are defining a function with the handle name as lamda underscore handler with event and context as arguments.
Generally, you can give any function name or argument names throughout the course. Okay, let's first see what is lambda underscore handler. While creating a lambda function, we need to specify a handler. This handler will act as a function in the code. The lambda function will act when a service executes the code. This is a general syntax structure for creating a handler function in bitin.
Here, we are configuring Boto, basically for providing the credentials to Boto three, we are passing the credentials as parameters by creating the client. Next, we are declaring an event temperature. Here we are normally storing the temperature values into x. If the temperature is more than 25 degrees, then we'll post a message on to the Sq Sq and if the temperature is less than Then 25 degrees, then we'll post an off message to the Sq sq. On top here, you can find save, selected, scroll up and here near my sp 32 underscore function you can find saved. Cool, we completed the code.
Now, we need to write a Python code on raspberry pi, which will check the queue and then turn often on the LED based on the message in the queue. But before that, we will test if the messages are being sent to this queue sq. Connect ESP 32 and the history 11 sensor to your laptop. Now, open our dyno and then tools serial monitor, you can see that the data is being published to AWS IoT Core. Now, we will go to the IoT Core and then select test here. Then giving the topic name.
Subscribe to the topic. Yes, we can see the data here. increase the temperature slowly. Now, we will go to the Sq Sq that we just created. Select queue actions on the top view or delete messages, start polling for messages. Here you can see the messages based on your temperature values.
Now, disconnect ESP 32 from your PC as your queue will be flooded with messages refresh Sq s, you can see that we have messages. If you are unable to receive any message then go to AWS lambda here on the top you can find monitoring selected. Now, if you have any errors, it will show in the third graph. Previously when I copied the code, I had an indentation error. So it showed me edit line. Now, I have resolved it, but how did I exactly know there?
Scroll down and you can find The recent logs selected. Now you can see that there are no errors in recent logs. So, I will show you my older logs, select logs on the left hand side. Now selecting this. Here, I will select an older log. Here you can see a syntax error selected, it will show you the exact location and reason for there.
This cloud watch logs will help you in resolving any error by using any service for solving this error. I removed all the indentations and gave them correctly again. Now, I will end this video here. In this video, we learned the following programming lambda, understanding the code testing. In the next video, we will program the Raspberry Pi for checking the queue. See you in the next video.