IoT Cloud | Sample Programs
IoT Cloud Infrastructure – Sample Programs
Sample programs is the first step you take to transition from theory to practical. These sample programs typically demonstrated in a classroom will ensure you are able to immediately see it running in front of your eyes. Added to that our mentors will provide you some exercises where you will be modifying the code in the class itself. By doing this fill-in-the-blank approach, will take out your fear of coding.
Example 1
Brief:
A basic Lambda function in AWS using python.
Source Code:
#-------------------------------------------------------------------------------------------------
# Author : Emertxe (https://emertxe.com)
# Date : Wed 02 Aug 2021 10:00:00 IST
# Function : hello_lambda
# Title : First Lambda Function
# Description : A basic Lambda function in AWS using python
#-------------------------------------------------------------------------------------------------
import datetime
def lambda_handler(event, context):
print ("Hello Lambda")
print ("The time is", datetime.datetime.now())
print ("This print is from function", context.function_name)
Example 2
Brief:
A basic Lambda function which is triggered with the help of AWS IoT Core.
Source Code:
#-------------------------------------------------------------------------------------------------
# Author : Emertxe (https://emertxe.com)
# Date : Wed 03 Aug 2021 10:00:00 IST
# Function : aws_iot_trigger.py
# Title : AWS IoT Trigger
# Description : A basic Lambda function which is triggered with the help of AWS IoT Core
#-------------------------------------------------------------------------------------------------
import json
def lambda_handler(event, context):
num1 = event['number1'];
num2 = event['number2'];
result = num1 + num2;
print "The result is", result;
return {
"result": result
}
Example Input:
IoT cloud sample program output 1:
Example output 2:
Example 3
Brief:
A basic Lambda function function which is triggered with the help of AWS IoT Core # and publish back to client application.
Source Code:
#-------------------------------------------------------------------------------------------------
# Author : Emertxe (https://emertxe.com)
# Date : Wed 03 Aug 2021 10:00:00 IST
# Function : aws_publish_from_aws.py
# Title : Publich from AWS IoT core
# Description : A basic Lambda function which is triggered with the help of AWS IoT Core
# and publish back to client application
#-------------------------------------------------------------------------------------------------
import os
import json
import boto3
# Note: You need to set these environmental variables
ACCESS_KEY=os.environ["ACCESS_KEY"]
SECRET_KEY=os.environ["SECRET_KEY"]
ENDPOINT=os.environ["ENDPOINT"]
def lambda_handler(event, context):
data_payload = {
"Sum": event['number1'] + event['number2']
}
print(data_payload)
# Create an AWS IoT client
data = boto3.client(
'iot-data',
endpoint_url=ENDPOINT,
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY
)
# Publish a simple message to the specified IoT topic
response = data.publish(
topic='MyDevice/result',
qos=1,
payload=json.dumps(data_payload)
)
# Print out the response
print(response)
Example Input:
IoT cloud sample program output 1:
Example output 2: