Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Dennis CabaBryan James S. Dela CruzChristian Joshua Dava
Published © GPL3+

Car Counter using Arduino + Processing + PHP

A very simple project for counting cars coming in and out of a parking lot using Arduino, Processing and PHP.

BeginnerFull instructions provided1 hour21,913
Car Counter using Arduino + Processing + PHP

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×2
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT
Processing
PHP

Story

Read more

Schematics

Schematic Diagram

Diagram for connecting the 2 PIR sensors and the LED

1_VaIeZZa0hv.jpg

Code

Arduino Code

Arduino
This is the code that display the car count to the serial and turns the LED on to indicate that a car passes through the PIR sensor.
int pirPinIN = 7;
int pirPinOUT = 10;
int led = 13;
static long counter = 0;

void setup()
{
  pinMode(pirPinIN, INPUT);
  pinMode(pirPinOUT, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  Serial.begin(9600);
}
 
void loop()
{
  long now = millis();
  if (digitalRead(pirPinIN) == HIGH)
  {
      counter++;
      Serial.println(counter);
      digitalWrite(led, HIGH);
      delay(500);
      digitalWrite(led, LOW);
  }
  if (digitalRead(pirPinOUT) == HIGH)
  {
      counter--;
      Serial.println(counter);
      digitalWrite(led, HIGH);
      delay(500);
      digitalWrite(led, LOW);
  }
}

Processing Sketch

Java
This Processing sketch reads the incoming values on the analog ports and then uses Serial Communication functions to output the values.
import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port

void setup()
{
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) 
  {  // If data is available,
  val = myPort.readStringUntil('\n'); // read it and store it in val
  int count = parseInt(val);
    if(count >= 0){
      loadStrings("http://localhost:8000/acdemo/post-message.php?count="+val);
    }
  } 
  
  println(val); //print it out in the console
}

post-message.php

PHP
This code GETs the value passed by the Processing sketch and POST the count to the cloud.
<?php
$count = filter_input(INPUT_GET, 'count', FILTER_SANITIZE_NUMBER_INT);

require('ArtikCloudProxy.php');
$proxy = new ArtikCloudProxy();
$proxy->setAccessToken("YOUR_ACCESS_TOKEN");
$data ='{"count":'.$count.'}';
$payload = array("data"=>$data,"sdid"=>"YOUR_DEVICE_ID","type"=>"message");
$payload = json_encode($payload, JSON_HEX_QUOT | JSON_HEX_TAG);
$response = $proxy->sendMessage($payload);
header('Content-Type: application/json');
echo json_encode($response);

ArtikCloudProxy.php

PHP
This is the ArtikCloudProxy class necessary for connecting to Artik cloud. Save it to the same folder as post-message.php somewhere accessible to the web. Make the necessary changes to the configuration.
<?php
/**
 * ARTIK Cloud helper class that communicates to ARTIK Cloud
 * */
class ArtikCloudProxy {
    # General Configuration
    const CLIENT_ID = "YOUR_CLIENT_ID";
    const DEVICE_ID = "YOUR_DEVICE_ID";
    const API_URL = "https://api.artik.cloud/v1.1";
 
    # API paths
    const API_USERS_SELF = "/users/self";
    const API_MESSAGES_LAST = "/messages/last?sdids=<DEVICES>&count=<COUNT>"; 
    const API_MESSAGES_POST = "/messages";
     
    # Members
    public $token = null;
    public $user = null;
     
    public function __construct(){ }
     
    /**
     * Sets the access token and looks for the user profile information
     */
    public function setAccessToken($someToken){
        $this->token = $someToken;
        $this->user = $this->getUsersSelf();
    }
     
    /**
     * API call GET
     */
    public function getCall($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPGET, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization:bearer '.$this->token));
        $json = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if($status == 200){
            $response = json_decode($json);
        }
        else{
            var_dump($json);
        	$response = $json;
        }

       return $response;
    }
     
    /**
     * API call POST
     */
    public function postCall($url, $payload){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, (String) $payload);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: bearer '.$this->token));
        $json = curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        if($status == 200){
            $response = json_decode($json);
        }
        else{
            var_dump($json);
            $response = $json;
        }
        return $response;
    }
     
    /**
     * GET /users/self API
     */
    public function getUsersSelf(){
        return $this->getCall(ArtikCloudProxy::API_URL . ArtikCloudProxy::API_USERS_SELF);
    }
     
    /**
     * POST /message API
     */
    public function sendMessage($payload){
        return $this->postCall(ArtikCloudProxy::API_URL . ArtikCloudProxy::API_MESSAGES_POST, $payload);
    }
     
    /**
     * GET /historical/normalized/messages/last API
     */
    public function getMessagesLast($deviceCommaSeparatedList, $countByDevice){
        $apiPath = ArtikCloudProxy::API_MESSAGES_LAST;
        $apiPath = str_replace("<DEVICES>", $deviceCommaSeparatedList, $apiPath);
        $apiPath = str_replace("<COUNT>", $countByDevice, $apiPath);
        return $this->getCall(ArtikCloudProxy::API_URL.$apiPath);
    }
}

Credits

Dennis Caba

Dennis Caba

1 project • 4 followers
Bryan James S. Dela Cruz

Bryan James S. Dela Cruz

1 project • 4 followers
Information Technology Student
Christian Joshua Dava

Christian Joshua Dava

1 project • 3 followers
IT student.

Comments