-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lambda_functions.py
178 lines (145 loc) · 4.21 KB
/
Lambda_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#DHT11_Function
import boto3
def lambda_handler(event, context):
client = boto3.client('dynamodb')
max_temperature = 25.0
min_temperature = 18.0
max_humidity = 60
min_humidity = 40
Condition = "N"
if (event['temp'] > max_temperature and event['hu'] > max_humidity):
Condition = "HTH"
elif (event['temp'] < min_temperature and event['hu'] < min_humidity):
Condition = "LTH"
elif event['temp'] > max_temperature:
Condition = "HT"
elif event['hu'] > max_humidity:
Condition = "HH"
elif event['temp'] < min_temperature:
Condition = "LT"
elif event['hu'] < min_humidity:
Condition = "LH"
response = client.put_item(
TableName = 'DHT11_data',
Item = {
'timestamp': {'S': event['timestamp']},
'temp': {'N': str(event['temp'])},
'hu': {'N': str(event['hu'])},
'condition': {'S': Condition},
}
)
return 0
###############################
#DHT11_Function
import boto3
import json
def lambda_handler(event, context):
iot_client = boto3.client('iot-data')
client = boto3.client('dynamodb')
status = ""
if event['temp'] > 26:
status = "N"
message = {"status": "off"}
else:
status = "G"
message = {"status": "on"}
response = client.put_item(
TableName = 'Btemp_data',
Item = {
'timestamp': {'S': event['timestamp']},
'user': {'S': event['user']},
'temp': {'N': str(event['temp'])},
'status': {'S': status}
}
)
# Convert the message to JSON format
message_json = json.dumps(message)
# Define the topic to which you want to publish the message
topic = "raspi/DHT11"
# Publish the message to the specified topic
response = iot_client.publish(
topic=topic,
qos=1, # Quality of Service level
payload=message_json
)
return 0
###############################
#Gas_Function
import boto3
def lambda_handler(event, context):
client = boto3.client('dynamodb')
status = ""
if event['gas'] > 1.0 and event['gas'] < 1.2:
status = "L"
elif event['gas'] > 1.2:
status = "H"
else:
status = "N"
response = client.put_item(
TableName = 'Gas_data',
Item = {
'timestamp': {'S': event['timestamp']},
'status': {'S': status}
}
)
return 0
###############################
#Move_Function
import boto3
def lambda_handler(event, context):
client = boto3.client('dynamodb')
response = client.put_item(
TableName = 'Move_data',
Item = {
'timestamp': {'S': event['timestamp']},
'move': {'S': event['move']}
}
)
return 0
###############################
#Flame_Function
import boto3
import json
def lambda_handler(event, context):
client = boto3.client('dynamodb')
sns = boto3.client('sns')
response = client.put_item(
TableName = 'Flame_data',
Item = {
'timestamp': {'S': event['timestamp']},
'fire': {'S': event['fire']}
}
)
if event['fire'] == "true":
message = "THERE IS A FIRE"
sns.publish(TopicArn = 'arn:FireWarning', Message = "THERE IS A FIRE")
return {
'statusCode': 200,
'body': json.dumps('SNS notification sent.')
}
else:
return 0
###############################
# on and off function
import boto3
import json
def lambda_handler(event, context):
# Create an IoT client
iot_client = boto3.client('iot-data')
message = {
"status": "off" # Change this value to 'off' if needed
}
# Convert the message to JSON format
message_json = json.dumps(message)
# Define the topic to which you want to publish the message
topic = "raspi/c"
# Publish the message to the specified topic
response = iot_client.publish(
topic=topic,
qos=1, # Quality of Service level
payload=message_json
)
return {
'statusCode': 200,
'body': json.dumps('Message sent to IoT Core successfully! this is the offffff')
}