Smart Waste Management System-Part 1-Sending Data to AWS IoT Core using NodeMCU ESP8266

Instruments & Services: AWS IoT Core, NodeMCU ESP8266 & Ultrasonic Sensor(HC-SR04)

Dhruv Shah
7 min readNov 5, 2020

Overview

As we all know that India is developing day-by-day and also the population is increasing exponentially. Due to the increasing population, it possesses a huge challenge to the garbage management system and hence in sustaining a clean environment. Many Cities around the World are endangered due to poor garbage management.

Our project aims to find a solution by using a Smart Dustbin integrated with ‘NodeMCU ESP8266’ and ‘Ultrasonic Sensor’. From ’Ultrasonic Sensor’ which continuously measures the level of the dustbin. Thus, our project aims at the prevention of overflowing garbage from the dustbin and also sending the dustbin data to the server using AWS Cloud. And the main aim is to have an effective and efficient garbage disposal system and to make the cities clean and hygiene.

Introduction & Working

As the population is increasing, we can see that there is an increase in waste production too. We should consider waste management as a major part of maintaining the well-being of society.

Now, let’s see the basic overview that how our project flows.
1. NodeMCU ESP8266 interfaced with Ultrasonic Sensor (HC-SR04) will send the data to the AWS IoT Core over Wi-Fi.

2. AWS IoT Core is configured in such a way that it will directly store data in the DynamoDB table.

3. Created a REST API using AWS API Gateway which is configured for getting the latest data from the database.

4. Now configure a lambda function in such a way it will always extract the latest data from the database.

5. Last and final step, create your own UI or dashboard and pass the REST API KEY link to get the Dustbin data.

Implementation

This project’s implementation is divided into three-phase:
1. Client-side which includes coding regarding NodeMCU ESP8266 so that it will send data to AWS IoT Core Console and registering your device with AWS.

2. AWS configuration includes storing data into the database, creating an API key, and configuring the Lambda function(python).
(This part will be covered in the next blog.)

3. FrontEnd-side which includes creating a website or app and pass the created API KEY link in that to get the latest data for ease of access.

Client-side:

1.Log-In to the AWS console and under the services pane search for ‘AWS IoT Core’.

2. Navigate to Manage > thing and in that choose ‘Register a thing’.

3. After that choose ‘Create a single thing’.

4. Enter the Name that you want and just click next.

5. Choose on ‘Create Certificate’.

6. Download all the certificates that are generated and store all in one folder. And for the Root CA certificate download ‘RSA 2048 bit key’ from this link.
(Note: If you can’t able to download it then create a blank notepad file and copy all the content from the given link and save it as ‘AmazonRootCA1.pem’.)

7. Once downloaded all the certificates then click on ‘Activate’ and then click ‘Done’.

8. Thus, you will be able to see your newly created thing under the ‘Things’ section on the AWS console.

9. Now redirect to ‘Secure > Policies’.

10. Click on ‘Create a policy’ and enter the ‘Name’ that you want to enter and then enter the below-given values in that section.
Actions — iot:* (The star signifies that we are allowing all actions under “iot” in the policy)

Resource ARN — * (It signifies that we are allowing the actions on all resources)

Effect — Allow.

Then scroll down and click on “Create policy”.

11. redirect to ‘Secure > Certificates’, click on three dots that are showing on the right side of the certificates and click ‘Attach a policy’.

12. Check the check-box of the policy that you recently created and click ‘Attach’.

13. Now we will convert AWS credentials that you have downloaded from above such as Certificate, Private Key, Root CA, etc. from ‘.pem’ to ‘.der’ format.

  • Redirect to the folder where you have stored all your certificates.
  • Open ‘Command Prompt’ and type the below command and if shows the below-shown image then ‘OpenSSL’ is present else you can download from this link and again repeat this step to verify that it has installed or not:
openssl
  • Make sure that you have opened the ‘command prompt’ where the certificates are stored if not then kindly open in that directory.
  • After opening in that directory write the below-given command in command prompt and make sure to write your code instead of xxx.
openssl x509 -in xxx-certificate.pem.crt -out cert.der -outform DER
openssl rsa -in xxx-private.pem.key -out private.der -outform DER
openssl x509 -in AmazonRootCA1.pem -out ca.der -outform DER
  • After that cut all that newly created certificates into another folder named ‘data’.
  • Now download the ESP8266FS-0.5.0.zip from the given link.
  • In your Arduino sketchbook directory, create tools the directory if it doesn't exist yet. You can find the location of your sketchbook directory in the Arduino IDE at ‘File > Preferences > Sketchbook’ location.
  • Unpack the tool into tools directory (the path will look like <sketchbook directory>/tools/ESP8266FS/tool/esp8266fs.jar).
  • Restart Arduino IDE.
  • Now copy the code from the Github link and paste in you Arduino IDE and then make sure to update your Wifi SSID, Password, and AWS Endpoint.
  • Then connect your NodeMCU ESP8266 with your PC and then choose the ‘NodeMCU 1.0’ and choose the correct ‘Port’.
  • Once that is completed click on ESP8266 Sketch Data Upload and on completion of that upload it on the NodeMCU ESP8266.
  • You will be able to see the output screen as shown below on your Arduino’s Serial Monitor:
  • Now navigate to ‘AWS IoT Core’ console, click on ‘Test’. You’ll be to see the below-shown page:
  • Now in the ‘Subscription topic’ enter the topic that you’ve written in your code in my case it’s ‘outTopic’. You can verify from line number 242 which will look like
  • On AWS IoT core console, we can see the output as:

Whoa! We have successfully able to send sensor data to AWS Cloud.

Conclusion

  • In this blog, we have learned about how to send data to the AWS IoT Core console.

So in the next blog, you will be able to learn how to store data in DynamoDB, access the DynamoDB table data with the help of lambda, and will show it on the website with the help of REST API.
You can find the next blog from this link.

I hope, you like this blog, and if have any doubt then kindly comment below.

--

--