This project aims to create an easy and lightweight platform to get readings from sensors located in different parts of the city.
Get environment variables from devices located in different parts of a city.
The sensors will be of the following types :
- temperature
- pressure
- co2
- precipitation
I think this is the easiest method for gathering city wide data which does not require server side hosting. The page can be made completely static by leveraging the power of S3 and AWS IoT. As a result it can be embedded anywhere without burdening a single server box.
In this writeup, I have only demonstrated the use of a temperature sensor. The cloud connection code and HTML/JS code will remain the same for all additional sensors.
Hardware
- Amazon powered MW302 IoT Starter kit
- Sparkfun digital temperature sensor
Tech stack
- mapbox.js for OpenStreetMaps
- AWS Starter SDK for MW302
- Amazon IoT
- Amazon S3
Step 1 : Create a Thing using your AWS IoT Dashboard
Login to your AWS account and go to the IoT service. Create a new thing, by selecting the Embedded C option. Download the keys that are generated in the process.
If you get stuck in this workflow, you can refer this article. I even made a small video of how to do this. You can find it on my channel called Makerville.
Step 2 : Configure device
If you are using the same sensor as me, you can find the mcu application code in the software section below. All you need to do is compile and upload it to the device.
If you are new to the MW302 platform, you can find instructions on host setup and flashing on their wiki
http://github.com/marvell-iot/aws_starter_sdk/wiki
I made a detailed video of how to setup a MW302 device to connect to AWS IoT. You can find it on my channel called Makerville.
Step 3 : Create bucket on S3
Login to your AWS account and go to the S3 service. Create a new bucket and name it accordingly. I have named mine as stateofthecity.
Step 4 : Edit CORS configuration of bucket
To make the content of the bucket available publicly, we will have to make changes to the CORS configuration. I have attached a screenshot of the changes that you need to make below.
Step 5 : Set rule on AWS IoT
Next we are going to configure a thing to push content to the just create bucket. This is done using the awesome Rules Engine.
The Rules Engine allows you to select a topic, parse the event generated by it and then link it to any AWS Service.
I have attached a screenshot of the rule for reference.
Step 6 : Host HTML
S3 is amazing. You can use the same bucket that you created earlier to host a static HTML file which fetches the data from S3. Pretty cool right?
My static webpage for the promising city of Pune, India is hosted on
http://stateofthecity.s3-website-us-west-2.amazonaws.com/
We could have used AWS SDK for JS to directly connect it to AWS IoT, instead of using S3 as a go-between. The reason I chose not to do it is because that would mean me posting my access keys and tokens in the public (recipe for disaster!).
Hence the data is fetched from publicly accessible S3 objects :
$.get( "https://s3-us-west-2.amazonaws.com/stateofthecity/thing1.json", function( data ) ...
The JSON data is then parsed to find the value
var d = JSON.parse(data);
window.thing1 = String(d.state.reported.temperature);
The complete HTML/JS code is attached in the software section below.
Thats it !
We are done ! Quick and easy. There is so much more to do with this ! Think of the many sensors that could be connected
A quick recap of the flow :
1. Create a Thing using your AWS IoT Dashboard
2. Configure device
3. Create bucket on S3
4. Edit CORS configuration of bucket
5. Set rule on AWS IoT
6. Host HTML
Comments