ANAND PV
Published © GPL3+

LinkIt ONE Smart Vehicle Box(SVB)

A smart way to track, control and protect your vehicles with a mobile app.

IntermediateFull instructions provided3,910

Things used in this project

Hardware components

LinkIt ONE
MediaTek Labs LinkIt ONE
×1
Relay (generic)
×4
ULN2308
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
TMP36
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Android Studio
Android Studio
freeboard.io
dweet.io

Story

Read more

Schematics

Relay Circuit

34

LinkIt ONE connections

Diagram

Code

LinkIt ONE Code

C/C++
LinkIt ONE Complete Code
///Some of the codes are from Arduino lINKiT ONE sample programs.
///Thanks to the original coders

#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
//add GSM header if you want to make any calls or send SMS alerts 
#include <LGPS.h>
//////////////
const int tmp_sensor = A0;                 //Temperature sensor - port A0 (analog)
const int r1    = 0;                       // pin of Relay 1
const int r2    = 1;                       // pin of Relay 2
const int r3    = 2;                       // pin of Relay 3
const int r4    = 3;                       // pin of Relay 4

const int bit1    = 4;                       // pin of led2
const int bit2   = 5;                       // pin of led3
const int bit3   = 6;                       // pin of led

const int PIR = 19;                         //PIR Sensor DIGITAL INPUT
/////////////

///enter your wifi access pont name
#define WIFI_AP "Anand's"
//wifi password
#define WIFI_PASSWORD "123456789"
//type of auth
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
//SITE_URL and server (change it if you want to use pubnub or other services)
#define SITE_URL "www.dweet.io"
#define server "www.dweet.io"
//linit one command page
//link it one reads commands from this page
//you can simply set your commenads or use the mob app given
#define path "/get/latest/dweet/for/linkitonecommand" 

//wifi client object C
LWiFiClient c;
 
 //gps information object
gpsSentenceInfoStruct info;
char buff[256];
//chars representing AC and other relay states + Key-less entry states
char ac_state='0',ign_state='0',light_state='0',alarm_state='0';
char unlock='0', lock='0', hazard='0';
char ign_last_state='0';
 
 /////extract word location
 //each word is separated by a comma
static unsigned char getComma(unsigned char num,const char *str)
{
  unsigned char i,j = 0;
  int len=strlen(str);
  for(i = 0;i < len;i ++)
  {
     if(str[i] == ',')
      j++;
     if(j == num)
      return i + 1; 
  }
  return 0; 
}

//get cordinates
String get_cors(char* strg)
{
    unsigned char loca, loca2=0,flag=0;
    String buff;
    char* result;
    double lat,log;
    
    for(loca=0;loca2<256;loca2++)
    {
      if(strg[loca]==',')
        flag++;
      else
      {
        if(flag == 2)
        {
          result[loca2] = strg[loca];
        }
      }
        

    }
    return result;
}


//Convert string to double (latitude and longitude)
static double getDoubleNumber(const char *s)
{
  char buf[10];
  unsigned char i;
  double rev;
  
  i=getComma(1, s);
  i = i - 1;
  strncpy(buf, s, i);
  buf[i] = 0;
  rev=atof(buf);
  return rev; 
}

//Convert string to integer (Number of satellites)
static double getIntNumber(const char *s)
{
  char buf[10];
  unsigned char i;
  double rev;
  
  i=getComma(1, s);
  i = i - 1;
  strncpy(buf, s, i);
  buf[i] = 0;
  rev=atoi(buf);
  return rev; 
}
 
void parseGPGGA(const char* GPGGAstr)
{
  /* Refer to http://www.gpsinformation.org/dale/nmea.htm#GGA
   * Sample data: $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
   * Where:
   *  GGA          Global Positioning System Fix Data
   *  123519       Fix taken at 12:35:19 UTC
   *  4807.038,N   Latitude 48 deg 07.038' N
   *  01131.000,E  Longitude 11 deg 31.000' E
   *  1            Fix quality: 0 = invalid
   *                            1 = GPS fix (SPS)
   *                            2 = DGPS fix
   *                            3 = PPS fix
   *                            4 = Real Time Kinematic
   *                            5 = Float RTK
   *                            6 = estimated (dead reckoning) (2.3 feature)
   *                            7 = Manual input mode
   *                            8 = Simulation mode
   *  08           Number of satellites being tracked
   *  0.9          Horizontal dilution of position
   *  545.4,M      Altitude, Meters, above mean sea level
   *  46.9,M       Height of geoid (mean sea level) above WGS84
   *                   ellipsoid
   *  (empty field) time in seconds since last DGPS update
   *  (empty field) DGPS station ID number
   *  *47          the checksum data, always begins with *
   */
  double latitude;
  double longitude;
  int tmp, hour, minute, second, num ;
  int value;
  unsigned char whitespace;
  if(GPGGAstr[0] == '$')
  {
    int temperature;
    int PIR_DATA;
    
    //Time
    tmp = getComma(1, GPGGAstr);
    hour     = (GPGGAstr[tmp + 0] - '0') * 10 + (GPGGAstr[tmp + 1] - '0');
    minute   = (GPGGAstr[tmp + 2] - '0') * 10 + (GPGGAstr[tmp + 3] - '0');
    second    = (GPGGAstr[tmp + 4] - '0') * 10 + (GPGGAstr[tmp + 5] - '0');
    
    sprintf(buff, "UTC timer %2d-%2d-%2d", hour, minute, second);
    Serial.println(buff);
    
    //latitude and longitude
    tmp = getComma(2, GPGGAstr);
    latitude = getDoubleNumber(&GPGGAstr[tmp])/100.00;
    tmp = getComma(4, GPGGAstr);
    longitude = getDoubleNumber(&GPGGAstr[tmp])/100.00; 
    PIR_DATA=digitalRead(PIR);
   
   //Get sensor data (temp[Anaglog] and PIR[digital])
    temperature=analogRead(tmp_sensor)-102;
    temperature=(int)(0.4887*temperature);
    
    //print gps+sensor+performed operations into a char buffer in the following format data1=xxxx&data2=xxx
    sprintf(buff, "latti=%10.8f&long=%10.8f&ac=%c&ign=%c&light=%c&alarm=%c&bit1=%c&bit2=%c&bit3=%c&tmp=%d&PIR=%d",
              latitude, longitude,ac_state,ign_state,light_state,alarm_state,unlock,lock,hazard,temperature,PIR_DATA);
    
      
    //////////////////////////////////////upload above string//////////////
    ///// upload downlaod servers need not be same you can change whenever you want
    Serial.println("Connecting to WebSite");
    while (0 == c.connect(SITE_URL, 80))
    {
      Serial.println("Re-Connecting to WebSite");
      delay(1000);
    }
    
    Serial.println("send HTTP GET request");
    c.println("GET /dweet/for/anandsff?" + String(buff));
    c.println("Host: " SITE_URL);
    c.println("Connection: close");
    c.println();
  
    // waiting for server response
    Serial.println("waiting HTTP response:");
    while (!c.available())
    {
      delay(100);
    } 
    
    /////////////////////////////////////Get commands from dweet.io/////////////////////
    if (c.connect(server, 80))
    {
      Serial.println("connected");
      // Make a HTTP request:
      c.print("GET ");
      c.print(path);
      c.println(" HTTP/1.1");
      c.print("Host: ");
      c.println(server);
      c.println("Connection: close");
      c.println();
      
      Serial.println("waiting HTTP response:");
      while (!c.available())
      {
        delay(500);
      }
      int win=5000;
      char v;
      tmp=0;
      ///read first 5000 chars
      while(win)
      {
        
        v = c.read();
        if((v=='*') && (c.read()=='x'))
        { 
          //////Read AC STATE Char fromd weet.io
           v = c.read();
           
           if(v=='1') 
           {
             digitalWrite(r1, HIGH);  
             ac_state=v;
           }
           else if(v=='0') 
           {
             digitalWrite(r1, LOW); 
            ac_state=v; 
           }
             
          ///Read sencond char - Ignition state char
           v = c.read();
 
           if(v=='1') 
           {
             if(ign_state=='1')
              digitalWrite(r2, LOW);
             else if(ign_state=='0')
            {
              ////hot-wiring terminals cannot be on for long period 5s will be enough to start engine
               digitalWrite(r2, HIGH);
               delay(5000);
               digitalWrite(r2, LOW);
            }      
           ign_state=v;       
           }
 
           else if(v=='0') 
           {
             digitalWrite(r2, LOW);  
             ign_state=v;
           }
  
   
           
          ///Read third char - head light state char
           v = c.read(); 
           if(v=='1') 
           {
             light_state=v; 
             digitalWrite(r3, HIGH);  
           }
           else if(v=='0') 
           {
             light_state=v; 
             digitalWrite(r3, LOW);  
           }
           
           ///Read fourth char - alarm state char
           v = c.read();
          
           if(v=='1') 
           {
              alarm_state=v;
             digitalWrite(r4, HIGH);  
           }
           else if(v=='0') 
           {
              alarm_state=v;
             digitalWrite(r4, LOW);  
           }
           
          //////Read 5th Char lock bit 
          //on for 2s
           v = c.read(); 
           
           if(v=='1') 
           {
             if(lock=='1')
              digitalWrite(bit1, LOW);
             else if(lock=='0')
            {
              unlock ='0';
               digitalWrite(bit1, HIGH);
               delay(2000);
               digitalWrite(bit1, LOW);
            }      
           lock=v;       
           }
 
           else if(v=='0') 
           {
             digitalWrite(bit1, LOW);  
             lock=v;
           }
           
            
          ///Read 6th char - unlock
                    //on for 2s
           v = c.read(); 
       
           if((v=='1')) 
           {
             if(unlock=='1')
              digitalWrite(bit2, LOW);
             else if(unlock=='0')
            {
               lock ='0';
               digitalWrite(bit2, HIGH);
               delay(2000);
               digitalWrite(bit2, LOW);
            }      
           unlock=v;       
           }
 
           else if(v=='0') 
           {
             digitalWrite(bit2, LOW);  
             unlock=v;
           }
           
          ///Read 7th char - hazard light bit
                    //on for 2s
           v = c.read(); 
       
          if(v=='1') 
           {
             if(hazard=='1')
              digitalWrite(bit3, LOW);
             else if(hazard=='0')
            { 
               digitalWrite(bit3, HIGH);
               delay(2000);
               digitalWrite(bit3, LOW);
            }      
           hazard=v;       
           }
 
           else if(v=='0') 
           {
             digitalWrite(bit3, LOW);  
             hazard=v;
           }
            
        }
       win--;
      }
 
    }
    /////////////////////////////////////
    ///print full gps info
    Serial.println(buff); 
  
  ///print number of connected satellites for debug purpose only
    tmp = getComma(7, GPGGAstr);
    num = getIntNumber(&GPGGAstr[tmp]);    
    sprintf(buff, "satellites number = %d", num);
    Serial.println(buff); 
  }
  else
  {
    Serial.println("Not get data"); 
  }
}
 
 ////init instructions
void setup()
{
  LTask.begin();
  LWiFi.begin();
  Serial.begin(115200);
  
  ////////////////////
  LGPS.powerOn(); //
  Serial.println("LGPS Power on, and waiting ...");  
  //set analog resolution (by default it is 10)
  analogReadResolution(10);
  //tmp sensor pin as input
  pinMode(tmp_sensor, INPUT);
  //pir pin as output
  pinMode(PIR, INPUT); 
  ///all other relay and key-less drive pins are output
  pinMode(r1, OUTPUT);                   // set all pin OUTPUT
  pinMode(r2, OUTPUT);
  pinMode(r3, OUTPUT);
  pinMode(r4, OUTPUT);
  pinMode(bit1, OUTPUT);                   // set all pin OUTPUT
  pinMode(bit2, OUTPUT);
  pinMode(bit3, OUTPUT);
  //////////////////// set all drive pins low initially
   digitalWrite(r1, LOW); 
   digitalWrite(r2, LOW); 
   digitalWrite(r3, LOW); 
   digitalWrite(r4, LOW); 
   digitalWrite(bit1, LOW); 
   digitalWrite(bit2, LOW); 
   digitalWrite(bit3, LOW);  
  ////////////////////
  
  // keep retrying until connected to AP
  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(500);
  }
  /////////////////
  delay(1000);
  /////////////////
  // keep retrying until connected to website
  //connection testing (for debugging) //you can remove below statements of this function 
  Serial.println("Connecting to WebSite");
  while (0 == c.connect(SITE_URL, 80))
  {
    Serial.println("Re-Connecting to WebSite");
    delay(500);
  }

  // send HTTP request, ends with 2 CR/LF
  Serial.println("send HTTP GET request");
  c.println("GET /dweet/for/anandsff?12=fgfgfgfg");
  c.println("Host: " SITE_URL);
  c.println("Connection: close");
  c.println();
     
}

boolean disconnectedMsg = false;

void loop()
{
            // led4 off 
   // put your main code here, to run repeatedly:
  Serial.println("LGPS loop"); 
  LGPS.getData(&info);
  Serial.println((char*)info.GPGGA); 
  parseGPGGA((const char*)info.GPGGA);
  delay(500); 
}

Android Studio Java File

Java
Android App Java File
package com.example.anand.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView webv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      //   webv = (WebView)findViewById(R.id.wv1);

        //A WebView objct is used to show freeboard page webView
        WebView  webView = (WebView) findViewById(R.id.wv1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        webView.getSettings().setSupportMultipleWindows(false);
        webView.getSettings().setSupportZoom(false);
        webView.setVerticalScrollBarEnabled(false);
        webView.setHorizontalScrollBarEnabled(false);


        webView.loadUrl("https://freeboard.io/board/Jkjhh4");
        //A WebView obkect is used to access issue commands to dweet.io webv
        webv = (WebView) findViewById(R.id.dweets);
        //webv.getSettings().setAllowContentAccess(true);
      //  webv.getSettings().setJavaScriptEnabled(true);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

/////////////////////////1st bit////////////////////////////////
    public  void acon(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*x1xxxxxx");
    }
    public  void acoff(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*x0xxxxxx");
    }

    /////////////////////////2nd bit////////////////////////////////
    public  void ignrst(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xx0xxxxx");
    }
    public  void ign(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xx1xxxxx");
    }
    /////////////////////////3rd bit////////////////////////////////
    public  void hlighton(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xxx1xxxx");
    }
    public  void hlightoff(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xxx0xxxx");
    }
    /////////////////////////4th bit////////////////////////////////
    public  void alarmon(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xxxx1xxx");
    }
    public  void alarmoff(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xxxx0xxx");
    }
    /////////////////////////5th bit////////////////////////////////
    public  void lock(View vv)
    {
     webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xxxxx1xx");
    }

    /////////////////////////6th bit////////////////////////////////
    public  void unlock(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xxxxxx1x");
    }

    /////////////////////////7th bit////////////////////////////////
    public  void hazard(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xxxxxxx1");
    }
    ///////////////////////////////////reset//////////////////////
    public  void resetall(View vv)
    {
        webv.loadUrl("http://www.dweet.io/dweet/for/linkitonecommand?c=*xxxxx000");
    }
  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Android Studio Activity XML File

XML
Android Studio Activity XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:weightSum="1"
        android:background="#000001">

        <WebView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/wv1"
            android:layout_weight="0.97" />

        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.1">


            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="AC ON"
                android:id="@+id/button1"
                android:layout_row="0"
                android:layout_weight="4"
                android:layout_column="0"
                android:onClick="acon" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="IGNITION RESET"
                android:id="@+id/button2"
                android:layout_weight="4"
                android:layout_row="0"
                android:layout_column="1"
                android:onClick="ignrst" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="HEAD  L ON"
                android:id="@+id/button3"
                android:layout_row="0"
                android:layout_weight="4"
                android:layout_column="2"
                android:onClick="hlighton" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ALARM  ON"
                android:id="@+id/button4"
                android:layout_row="0"
                android:layout_weight="4"
                android:layout_column="3"
                android:onClick="alarmon" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="UNLOCK"
                android:id="@+id/button5"
                android:layout_row="0"
                android:layout_weight="4"
                android:layout_column="4"
                android:onClick="unlock" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="HAZARD LIGHT"
                android:id="@+id/button6"
                android:layout_row="0"
                android:layout_weight="4"
                android:layout_column="5"
                android:onClick="hazard" />

/////////////////
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="AC OFF"
                android:id="@+id/button11"
                android:layout_weight="4"
                android:layout_row="1"
                android:layout_column="0"
                android:onClick="acoff" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:id="@+id/button12"
                android:layout_row="1"
                android:layout_column="1"
                android:text="-- IGNITION ON--"
                android:onClick="ign" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:text="HEAD L OFF"
                android:id="@+id/button13"
                android:layout_row="1"
                android:layout_column="2"
                android:onClick="hlightoff" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:text="ALARM OFF"
                android:id="@+id/button14"
                android:layout_row="1"
                android:layout_column="3"
                android:onClick="alarmoff" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:text="LOCK"
                android:id="@+id/button15"
                android:layout_row="1"
                android:layout_column="4"
                android:onClick="lock" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="---- - RESET - ----"
                android:layout_weight="4"
                android:id="@+id/button"
                android:layout_row="1"
                android:layout_column="5"
                android:onClick="resetall" />

            <WebView
                android:layout_width="wrap_content"
                android:layout_height="27dp"
                android:id="@+id/dweets"
                android:layout_row="1"
                android:layout_column="6"
                android:visibility="invisible" />


        </GridLayout>



    </LinearLayout>
</RelativeLayout>

Credits

ANAND PV

ANAND PV

2 projects • 32 followers

Comments