SmartBin Using Netduino 3 WiFi & ClicksendSMS ... - Instructables

13
instructables SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway by brijesh_sng The idea of this project is to monitor the garbage level of your home or society's waste bin remotely using IoT technologies. So, this avoids the overflow of the bin by timely informing to your local waste collection authorities. Here to demonstrate the project I made the prototype using Netduino 3 WiFi development board and the ultrasonic sensor for hardware build and connected device to ClickSend Cloud SMS service via the Internet. So, this enables to send SMS alert to any mobile phone number without the need for a dedicated SIM card anywhere in the world. What You Need for This Project: Hardware: 1. Wilderness Labs (Netduino) Netduino3 WiFi x1 2. Ultrasonic Sensor - HC-SR04 x1 Software Apps and Online Service: 1. Microsoft Visual Studio 2015 2. ClickSend SMS Cloud Service SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 1

Transcript of SmartBin Using Netduino 3 WiFi & ClicksendSMS ... - Instructables

instructables

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway

by brijesh_sng

The idea of this project is to monitor the garbage levelof your home or society's waste bin remotely usingIoT technologies. So, this avoids the overflow of thebin by timely informing to your local waste collectionauthorities. Here to demonstrate the project I madethe prototype using Netduino 3 WiFi developmentboard and the ultrasonic sensor for hardware buildand connected device to ClickSend Cloud SMSservice via the Internet. So, this enables to send SMSalert to any mobile phone number without the needfor a dedicated SIM card anywhere in the world.

What You Need for This Project:

Hardware:

1. Wilderness Labs (Netduino) Netduino3 WiFi x1

2. Ultrasonic Sensor - HC-SR04 x1

Software Apps and Online Service:

1. Microsoft Visual Studio 2015

2. ClickSend SMS Cloud Service

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 1

Step 1: Setting Up a Development Environment

Visit the Link-> http://developer.wildernesslabs.co/Netduino/About/Downloads/to download various software under DevelopmentEnvironment and follow the sequence mentioned on

the link during installation.

For Windows user1) Visual Studio 2015.

2) .NET Micro Framework (.NETMF) v4.3.2 - QFE2 SDK

3) .NETMF Plugin for Visual Studio 2015

4) Netduino SDK

You will have to signup first on Wilderness Labsdeveloper site to download .NETMF Plugin andNetduino SDK. For Mac follow steps as given on theofficial site.

From here we can proceed to write our first sampletest code for testing. Follow steps as shown in thevideo above to create your first application. You cancopy the code of blinky_code.cs file to program.cs

Another way of Creating Application:Follow steps mentioned on the link: http://developer.wildernesslabs.co/Netduino/Getti...

Only things you need to remember in both cases issetting .NET Micro Framework 4.3 as targetframework and reference must contain ->Microsoft.Spot.Hardware,SecretLabs.NETMF.Hardware andSecretLabs.NETMF.Hardware.Netduinoas shown in the above images.

Otherwise, you will get an error as shown in the lastimage, with red underlines.

This happens when you go by creating a consoleapplication. Because by default it won't add referenceneeded for Netduino. So, you have to add manually.If you follow by creating Netduino Application, then bydefault it takes those files in references. I have shownboth because we need to follow this procedure forfurther section explained next. Especially how to addreferences for various needs.

https://youtu.be/mXnyEeEmo88

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 2

https://www.instructables.com/ORIG/FPO/HWS1/JY8YRE7Y/FPOHWS1JY8YRE7Y.cs…Download

Step 2: Configure WiFi Setting of Netduino 3 Wifi

To begin with the configuration you need NetduinoDeploy application and STM32 driver if not alreadyinstalled by visiting same download link underFirmware Updater and Configuration Tool. Again todownload NetduinoDeploy app you will have to signin.

Once done you can verify with the screenshot of thedevice manager in a different mode(normal & DFU) ofNetduino board as shown in the above images.

To put Netduino in DFU mode you need to pressswitch available onboard then plug the USB powercable keeping button pressed and after this release.

Once done then start NetduinoDeploy.WPF.exe andfirst update the firmware of Netduino to the latest one

as shown in the images above.

Now you can add WiFi Network setting, for my router,it is WPA2 and 128bit key encryption. Check for yourrouter vendor's site to get that information. Thescreenshot is shown in above images for networksetting.

Note: If you stuck with any problem while performingfirmware update or WiFi settings because I facedsome difficulties and finally resolved with the help onthe forum. So you can post any difficulties on theforum or comment below -> https://community.wildernesslabs.co/t/how-to-conn...

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 3

Step 3: Interfacing Ultrasonic Sensor

Here I am using the HC-SR04 ultrasonic sensor to detect the fill level inside Bin. Make a connection as shown inthe image above.

After this create a test application and copy code of ultrasonicsensortest_code.cs provided below

Code is simple self-explanatory with comments if still facing issue comment below.

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 4

https://youtu.be/rC9hCumlyAE

https://www.instructables.com/ORIG/F1R/VMW6/JYCUJ8O5/F1RVMW6JYCUJ8O5.cs…Download

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 5

Step 4: Getting ClickSend Cloud API Key

Visit https://www.clicksend.com/in/ and signup tocreate your account on ClickSend Cloud SMSgateway service. For the trial, service provides youwith some initial balance.

Once done, then login to view Dashboard where youwill get credential needed to use service. Click on APICredential on top to copy your username and APIkey, as shown in the above images.

Step 5: Testing ClickSend With Browser

This service supports different protocols(Rest, HTTP, SMTP, SMPP, and FTP). Detail documentation is availableon link https://developers.clicksend.com/docs/

For this project, I am using HTTP/ HTTPS protocol whose documentation is available on link https://clicksendhttpapiv2.docs.apiary.io/#refere...On the above link of HTTP protocol documentation, you will find the testing method and trial numbers. You canuse these numbers without any charges.

Below is some test which you can try out. Simply copy those https link with your username and password in yourbrowser. So when you hit enter then you will receive a response as shown in the above images for two cases.Also, the screenshot of mobile phone SMS is shown for the real number test case.

Case 1: With test Number, replace username and key with your

https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=+61411111111&message=hello

Case 2: With a real number,

https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=+917955555555&message=Hello%20Netduino

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 6

Step 6: HTTP/HTTPS Request Testing on Netduino 3 WiFi

Here I will show how to make HTTP/HTTPS callsusing Netduino and try the same example shown inthe above section to call ClickSend API withNetduino instead of a browser.

You can get sample code available on Link- http://developer.wildernesslabs.co/Netduino/Input...

which show how Netduino connects to the network,then gets IPAddress and finally makes the simpleHTTP request to google.com Let's create an HTTPRequest application

I copied the same code but got errors, as shown inbelow steps and solution to resolve those.

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 7

Step 7: Resolving Compile Error

Errors pointed in the red box. Also, look at themarked reference section because here we have toadd references to removed those errors.

Simple just right click on References then click onadd reference, next select particular reference andclick on OK. We need to add two referencesSystem.Http(related toHttpWebRequest,WebRequest and

HttpWebResponse) and System.IO(related toStreamReader)

After this compile issue resolved but faced anotherissue during runtime after deploying code into theboard with the following error(Error: ff000000) shownin below steps and solution. Reason marked with anamber-colored box.

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 8

Step 8: Resolving RunTime Error

So, the required changes in code are

bool _IsRunning; public bool IsRunning{ get { return _IsRunning; } set { _IsRunning = value; }}

Get below test code (httprequesttest_code.cs) with correction. Just make changes to comment and check bothconditions of ClickSend API call with the test number and real number.

if (goodToGo) { MakeWebRequest("http://google.com"); //MakeWebRequest("https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=+61411111111&message=hello"); //MakeWebRequest("https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=+YOURNUMBERWITHCOUNTRYCODE&message=hello");}

Response received for both conditions shown above.

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 9

https://www.instructables.com/ORIG/FNA/H4J6/JYCUJCNV/FNAH4J6JYCUJCNV.cs…Download

Step 9: Final Coding and Working Demo

Let's now proceed to create a final application to test the actual project idea. Create a new SmartBin application asshown in the above steps and then download and copy smartbinfinal_code.cs code provided below in yourapplication.

I have added comments to understand the need for various namespace for the application. Also, do not forget toadd references System.Http and System.IO

using System; //for nativeeventhandler using System.IO; //for StreamReader methodsusing System.Net; //for httpwebrequest and ipaddress methodsusing System.Threading; //for Thread methodusing Microsoft.SPOT; //for Debug.Print methodsusing Microsoft.SPOT.Hardware; //for OutPort,InterrupPort,etcusing SecretLabs.NETMF.Hardware.Netduino; //for netduino hardware methods e.g Pinsusing Microsoft.SPOT.Net.NetworkInformation; //for networkinterface and related methods

To final code, I have change MakeWebRequest() method of class App from protected to public

public void MakeWebRequest(string url)

so that it is accessible from the Main method of Program class, as shown below

app.MakeWebRequest("https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=+61411111111&message=Please%20Empty%20WasteBin");

The working logic of code shown below:

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 10

HC_SR04 mUS = new HC_SR04(Pins.GPIO_PIN_D4, Pins.GPIO_PIN_D5); long initialLevel;initialLevel = mUS.Ping();byte cnt = 1;Debug.Print("Empty BinLevel: " + mUS.Ping().ToString() + "mm");long currentLevel;float fillPercent;while (true){ fillPercent = initialLevel - currentLevel; Debug.Print("Fill percentage of Bin is " + fillPercent.ToString() + "%"); if (fillPercent > 80) { if (cnt == 2) { Debug.Print("More than 80% Full"); Thread.Sleep(100); //replace with your ClickSend API credential and mobile phone number //app.MakeWebRequest("https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=+61411111111&message=Please%20Empty%20WasteBin"); app.MakeWebRequest("https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=YOUNUMBERWITHCOUNTRYCODE&message=Please%20Empty%20WasteBin"); cnt = 0; } } if (fillPercent > 50) { if (cnt == 1) { Debug.Print("More than 50% Full"); Thread.Sleep(100); //replace with your ClickSend API credential and mobile phone number //app.MakeWebRequest("https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=+61411111111&message=WasteBin%20is%20Half%20Filled"); app.MakeWebRequest("https://api-mapper.clicksend.com/http/v2/send.php?method=http&username=YOURUSERNAME&key=YOURAPIKEY&to=YOUNUMBERWITHCOUNTRYCODE&message=WasteBin%20is%20Half%20Filled"); cnt = 2; } } if (fillPercent < 10) { Debug.Print("Emptied!"); cnt = 1; } Thread.Sleep(5000);}

When the device is power on it connects to the network and then it first measures the initial empty bin level. Afterwhich it will keep on measuring the current calculated distance which goes on reducing with an increase in wastelevel. Using those data fill level is calculated in percentage.

Once the fill percentage cross margin of 50% then the first alert is sent via SMS and a final alert sent when thelevel is more than 80%. The process will be repeated once the bin is Emptied.

//www.youtube.com/embed/PTCLNveZMZ0

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 11

https://www.instructables.com/ORIG/F1V/G7BG/JYCUJCTJ/F1VG7BGJYCUJCTJ.cs…Download

Step 10: Packaging

Some packaging highlights which you could try out shown above.

Other Possible Applications of Project:

To measure Container Oil LevelTo measure Water Level Of TankTo Measure Chemical Level

Thanks!!

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 12

SmartBin Using Netduino 3 WiFi & ClicksendSMS Gateway: Page 13