There is no doubt that doing iot dashboard is very fun. But I wanted to give another approach to iot by doing a bot to replace the dashboard.
Here you will find the data flow of the system.
Let's Get Started !!!
Setup to send data to AWS IoT Core
First, I setup the raspberry py with raspbian and followed soracom tutorial found here. The tutorial has all the information to get you started and connect the usb cellular dongle to the raspberry pi. I was lucky to get the iot kit.
After the usb cellular dongle was ready I modified the python script provided by soracom just to send a simulated parameter.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, time, requests, json,random
# you can specify interval(seconds) as first argument
interval=5 if len(sys.argv)==1 else int(sys.argv[1])
# inifinite loop
try:
while True:
dist=random.randrange(2,30,1)
print("Randon Distance "+str(dist))
headers = {'Content-Type': 'application/json'}
payload = {'distance':dist }
print("- sending data to funnel")
try:
r = requests.post('http://funnel.soracom.io', data=json.dumps(payload), headers=headers, timeout=5)
print(r)
except requests.exceptions.ConnectTimeout:
print('ERROR: connection timeout. Is 3G connection online?')
sys.exit(1)
if r.status_code == 400:
print('ERROR: failed to submit data. Did you configure Funnel for your SIM?')
sys.exit(1)
print("sleeping")
sleep(20)
except KeyboardInterrupt:
print("exit")
Forwarding
the data to AWS
I will assume that you read all Soracom documentation and already setup you sim card into Soracom Air.
As you see my group is called challenge I have configured that group to send data Via Funnel to AWS iot Core.
I used this tutorial to setup the Funnel to IoT Core adapter
Looks that everything is setup and ready to receive data into IoT Core. You will receive the data on this format.
Storing Data into DynamoDB with a Rule
Now we need to transfer that data to a dynamoDb with the help of a rule.
So we create a rule that selects any message coming to the "soracom" topic and put it on a dynamoDB table using split message into columns of a DyanamoDb
Now you will need to create a table with the following settings. Using the imei of the cellular device as an identifier for the primary key partition and then using the timestamp as the sort key.
This is how it should look after you configure it.
After you send a couple of messages the content of the DynamoDb looks like this.
and we are interested on the payload field which you don't see it on the picture because it was further at the right. The payload from your sensor look like this once you click on the data that arrived to the dynamoDb. Soracom will automatically append some data like the imei, imsi, operator id and timestamp.
The Setup
My simulation setup has the raspberry pi W Zero with the soracom usb cellular modem and the Pimoroni Automation HAT as the Core of the system aka Machine.
I get the temperature from the PT100 rtd via the 4 to 20ma To 0-10V converter into the HAT.
I get the humidity out from the 0-10 V simulator
The float level is true/false sensor to the 24V input of the HAT
A pump is running to circulate water and inside the bucket there is a resistor to heat up the water so that the temperature sensor will change its value.
This will be the result written on the dynamoDB
Software part
DynamoDB
We have 2 tables.
The first table will be to define the device to know in what devices are installed on a room. Because a room can have many devices and each device can have many sensors.
The second table holds the data send from soracom funnel to the dynamoDB
I followed her tutorials https://youtu.be/5XfhTwVjwgQ to get this going.
Lex bot works on intents where you need to full fill slots. For my first intent, I will get the value of a temperature sensor on the boiler room. Thinking about the iot application I thought that would be the best way to sort out the questions to the bot ex. Please tell me the temperature in the compressor room? What is the value of temperature in the boiler?
So our intent will have two slots(variables) place(for room) and sensor. By passing this two variables from our BOT to lambda we can look into the dynamoDB table for the value.
On this intent, we ask for the value of temperature in the boiler room.
In order to fulfill that intent, you need to use a lambda function to pass the slots and query the dynamoDB.
You can find the lambda code on the code section but it will look like this:
if you get more creative you do more intents to do average. or looks for specific days. Staticts, Highs, Lows etc..
Comments