Parking Sensor With Particle Argon

by akubiak in Circuits > Microcontrollers

255 Views, 2 Favorites, 0 Comments

Parking Sensor With Particle Argon

IMG-8779.JPG
IMG-8780.JPG

This project uses a HC-SR04 ultrasonic distance sensor in order to sense when a car has parked into a specific spot. Once a car has been parked in that spot, a text message will be sent to your phone and the time and date of the event will be saved into a spreadsheet.

I created this project because my dad parks in a garage that's further away from our house, so when he gets to his garage after work he has to call my mom to come pick him up and take him home. This project would send a text to my mom's phone when he is parked and would make the whole process easier and more simple. The data on the spreadsheet would also be interesting to look at if there are any patterns as to at what time he leaves and what time he comes back.

You can also use this project in many different types of ways and make it detect other things as well.

Supplies

Particle Argon

Breadboard

Ultrasonic Distance Sensor - HC-SR04

Wires

Cardboard

The Wiring

IMG-8762.JPG
Capture.PNG

Connect Vcc to VUSB

Connect Trig to D2.

Connect Echo to D6.

Connect Gnd to Gnd.

Make sure that your sensor is not tilted in any way because this will skew the results.

Creating the Spreadsheet

Screenshot 2023-02-02 8.12.49 AM.png

Create a new spreadsheet and give it a title. I named mine ParkingMonitor.

The first column will be your device ID for better organization so the next two columns will be the date and parking status. Name your columns accordingly like I did.

Click on the Extensions menu and select Apps Script.

Replace the default code with this script:

function doPost(e) {

var publishedAt = new Date(e.parameter.published_at);

var dataArray = [];
try {
dataArray = JSON.parse(e.parameter.data);
}
catch(e) {
}

var sheet = SpreadsheetApp.getActiveSheet();

var row = [e.parameter.coreid, publishedAt, e.parameter.data];

row = row.concat(dataArray);

sheet.appendRow(row);

var result = {};
result.ok = true;

return ContentService.createTextOutput(JSON.stringify(result))
.setMimeType(ContentService.MimeType.JSON);
}


This code publishes your device's ID in the first column, the date and time the event was run in the second column, and the data (parked or not) in the third column. You can also change around the order if you would like to.

If you don't want your device ID to be a column in your spreadsheet, you can go to the line that starts with var row and delete e.parameter.coreid.

Publishing the Script

In order for this code to actually work you have to publish and deploy the script.

In the upper right hand corner, click on Deploy and select New deployment.

Execute as: Set this option to me.

Who has access: set this option to Anyone.

Click Deploy

Web app URL: This link will be used in the next step when creating the webhook. Make sure to copy it for later use but keep it a secret, because anyone with this link will be able to make changes to your spreadsheet.

Creating the Webhooks

Screenshot 2023-01-27 12.56.11 PM.png

You are going to be making two webhooks, one that will communicate with the spreadsheet and one that will communicate with IFTTT to send the text to your phone.

The Spreadsheet Webhook

  • Go to https://console.particle.io/integrations, the Integrations tab on Particle's homepage.
  • Click on create new integration and select Webhook, then give the event a name.
  • Paste the link you copied into the URL field.
  • Set Request Type to Post.
  • Set Request Format to Web Form.

The Text Webhook

  • Follow the previous steps to create a new webhook and give the event a name. This webhook will be for triggering the text to send.
  • Leave the URL field blank for now, we will come back to this in the next step.

IFTTT

Capture.PNG
  • Go to https://ifttt.com/create and click Add on If This.
  • Search up webhooks and select it.
  • Click on Receive a web request with a JSON payload and type in the name of your event (What you named your text webhook).
  • Next, click on Then That and search ClickSend Sms and select it. ClickSend gives you about $2 for free to begin with but then after that you have to fund your own texts. I sent exactly 70 texts before running out of money.
  • Click on the Send Sms action and fill out the To and From fields.
  • In the message field copy and paste this: Parked: {{ {{OccurredAt}}}}.

This will send a text message to your phone when the event was triggered (a parked car) as well as the date and time of when it occurred.

Replace TestEvent with the name of your event and paste your API key after /key/

https://maker.ifttt.com/trigger/TestEvent/json/with/key/bCYXXXXXXXX_YfdXXXXXeV
  • Go back to the text webhook you created and paste your newly created link into the URL field

Writing the Code

Go to https://build.particle.io/build/new to automatically create a new project or you can create one manually by going to their Web IDE and clicking on create new app. After giving your app a title, click on the bookmark icon in the lower left sidebar and in the search bar type "HC_SR04" and include that library into your project. Once you include the library you can paste in this code:


int trigPin = D2;
int echoPin = D6;

double distance = 25.0;   
double minDist = 10.0; 
double rangeInCm;
String parked = "no";
HC_SR04 rangeFinder = HC_SR04(trigPin, echoPin);

void setup()
{
Serial.begin(9600);       
}

void loop()
{
  delay(200);

  while(parked == "no"){
    delay(300);
    rangeInCm = rangeFinder.getDistanceCM();   
    Serial.print(rangeInCm);        
    Serial.println(" centimeters");
    delay(300);
     
    if(rangeInCm < distance && rangeInCm > minDist)
    {
      parked = "yes";
      Particle.publish("youreventname", parked, PUBLIC);
Particle.publish("yourtexteventname", parked, PUBLIC);

    }
  }
   
  while(parked == "yes"){
    delay(300);
    rangeInCm = rangeFinder.getDistanceCM();
    Serial.print(rangeInCm);    
    Serial.println(" centimeters");
    delay(300);
     
    if(rangeInCm > distance && rangeInCm > minDist)
    {
      parked = "no";
      Particle.publish("youreventname", parked, PUBLIC); 
Particle.publish("yourtexteventname", parked, PUBLIC);      

    }
  }
}

Make sure to replace youreventname with your own event's name in quotation marks and yourtexteventname with your other event's name.

This code checks if there is something less than 25 centimeters near the sensor and if there is, this means that there is a car. You can change this number to anything that you want as long as it's less than 400cm, the max distance of the sensor. In my project I placed the breadboard so that the ultrasonic sensor faces up and under where the car would be so my distance was based on the distance from the sensor to the bottom of my dad's car. I also experimented with another orientation for the sensor which was putting it on the wall facing my dad's car. For this one I gave a lot of room for error because my dad doesn't always park the exact same amount away from the wall every time. I decided to not go with this orientation, though, because my dad sometimes walks in front of his car and this would send false "parked" messages to my mom's phone. The one that goes on the floor and faces up has no such issues because he is never in the garage unless his car is already there, so there is no risk of him walking over it or anything like that. You can orient the sensor however you would like but make sure that the distance you put into your code is reasonable and accurate by measuring the correct distance and then adding on a few extra centimeters just in case.

Testing


The line Serial.print(rangeInCm); in the code is used for testing to see if the distance given by the ultrasonic distance sensor is accurate. The line prints the distance that it gets from the getDistanceCM() function into a serial monitor. There are many different serial monitors that you could use for this testing but here is the one I used:

https://chrome.google.com/webstore/detail/serial-monitor/ohncdkkhephpakbbecnkclhjkmbjnmlo?hl=en

This is a google app that can only be used on the Google browser so if you are using a different browser and you want to test to see if your sensor is accurate, then you should find a serial monitor that will suit your needs.

Important Note:

The sensor will return -1 if it reads a distance that is less than 10 centimeters because that is the minimum possible distance that the sensor can recognize (or greater than 400cm). Another reason that it could either be reading -1 or reading very inaccurate numbers is if you plugged your VCC wire into 3V instead of 5V, which is something that I am guilty of doing myself. Also make sure that you are using something solid to test with and not your hand because the ultrasonic sensor uses soundwaves to measure distance so you want something that the soundwaves can easily bounce off of. To test the sensor use something like cardboard and slide it in front of the sensor with the serial monitor up and running to see if the distance seems accurate or not. You can also measure the distance yourself with a ruler if you aren't sure if it's entirely accurate.

After you are done doing all this testing you can delete the Serial.print lines if you would like to clean up your code.

FINISHED PRODUCT

mrlaw

This is what you can expect as a final project after you finish putting everything together and writing all the code.

My Final Thoughts

This project accomplished what I wanted to be done but I wish that I could've expanded more on this idea. I did complete what I wanted to originally, but before beginning this project, I thought that I would've had more time left over at the end to add some extra modifications to it. Something that I was interested in adding was somehow getting traffic data and then sending it to my dad's phone in order to let him know how long it will take my mom to drive to his garage. I couldn't figure out how to do this unfortunately because there was really no recent traffic data on any of the nearby streets.

Struggles

I had spent a long time trying to figure out why my sensor kept reading -1 centimeters and I tried many different ways to fix it. For example, I tried multiple different HC-SR04 ultrasonic distance sensor libraries, I tried moving around my wires into different spots, and I even switched out the wires in case they were broken. In the end, it turned out that my only mistake was plugging VCC into 3V instead of 5.

Another problem I had was using a HC-SR04 library that wasn't working and I couldn't figure out why. Turns out the issue was that the library itself was not compatible with the version of my Argon. I never got to do a video showcasing my project in the final orientation that I decided on because I had run out of my free ClickSend money to send texts.

At the beginning of this project, I manually calculated the distance instead of using a library because I didn't know that libraries were even a thing. I don't regret this, though, because I got to learn a lot about how the ultrasonic sensors even work.

Overall I wish I could've gotten more done but I'm still happy with the project because it does what I wanted it to do. It was my first time doing something like this and making this project has made me interested in exploring other similar circuit projects. Creating the spreadsheet and having it work felt very rewarding and satisfying.