LAMPIRAN
1. Source code Arduino. #include "Wire.h"
#include <SoftwareSerial.h> #include <String.h>
#define DS3231_I2C_ADDRESS 0x68 SoftwareSerial sim800l(11, 10); int dLine[28];
byte *cacah=0;
String stringPARA =
"AT+HTTPPARA=\"URL\",\"http://autobike.pe.hu/input.php?fuel="; String stringend = "\"";
String stringPARA2 = ""; String bt = "&battery="; String en = "&engine_st="; String tr = "&transmission_st="; float fuel;
float battery; String engine; String transmission; int power = 8; int start = 9; int pul = 3; int net = 2;
byte bcdToDec(byte val) {
return( (val/16*10) + (val%16) ); }
void setup() {
Wire.begin();
sim800l.begin(19200); // the GPRS baud rate Serial.begin(19200); // the GPRS baud rate delay(500);
pinMode(power,OUTPUT); pinMode(start,OUTPUT); pinMode(pul,OUTPUT); pinMode(net,INPUT); digitalWrite(power,HIGH); delay (100);
cacah = 0; }
void readDS3231time(byte *second, byte *minute,
byte *hour,
byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) {
Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); // set DS3231 register pointer to 00h Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h *second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read()); *hour = bcdToDec(Wire.read() & 0x3f); *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); }
void SubmitHttpRequest() {
stringPARA2 = stringPARA + fuel + bt + battery + en + engine + tr + transmission + stringend;
//Serial.println(stringPARA2); sim800l.println("AT+CSQ"); delay(1000);
ShowSerialData(); // this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.
sim800l.println("AT+CGATT?"); delay(1000);
ShowSerialData();
sim800l.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""); //setting the SAPBR, the connection type is using gprs
sim800l.println("AT+SAPBR=3,1,\"APN\",\"internet\""); //setting the APN, the second need you fill in your local apn server
delay(1000); ShowSerialData();
sim800l.println("AT+SAPBR=1,1"); //setting the SAPBR, for detail you can refer to the AT command mamual
delay(2000); ShowSerialData();
sim800l.println("AT+HTTPINIT"); //init the HTTP request delay(2000);
ShowSerialData();
sim800l.println(stringPARA2); // setting the httppara, the second parameter is the website you want to access
delay(1000); ShowSerialData();
sim800l.println("AT+HTTPACTION=0");//submit the request
delay(3000); //the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
ShowSerialData();
sim800l.println("AT+HTTPREAD");// read the data from the website you access
delay(300); ShowSerialData(); checkForResponse();
sim800l.println("AT+HTTPTERM"); delay(100);
}
void ShowSerialData() {
while(sim800l.available()!=0) Serial.write(sim800l.read()); }
void checkForResponse() {
sim800l.println("AT+HTTPREAD");// read the data from the website you access
//ShowSerialData(); for (int i = 0; i < 50; i++) {
dLine[i] = sim800l.read(); //Serial.println(dLine[i]); }
Serial.println(dLine[28]); if (dLine[28] != 97) {
SubmitHttpRequest(); }
}
void sensor() {
read_engine();
float Sensor1 = analogRead(A1); // membaca sensor tegangan 1 delay (1000);
Serial.println(Sensor1); fuel =(65.00 - Sensor1) *1.85; if (fuel>100 || fuel<0)
{
fuel = 0; }
Serial.println(fuel);
float Sensor2 = analogRead(A2); // membaca sesor tegangan 2 delay (1000);
battery = Sensor2 * (12.0 / 582.0); Serial.println(battery);
digitalWrite(pul,HIGH); // puldown input neutral delay(100);
if ( digitalRead(net) == HIGH) // membaca status transmisi {
transmission = "ON GEAR"; }
else {
transmission = "NEUTRAL"; }
digitalWrite(pul,LOW); Serial.println(transmission); delay(1000);
}
{
float val1 = analogRead(A0); // membaca staus mesin delay(1000);
if (val1>10) {
engine = "ON"; }
else {
engine = "OFF"; }
Serial.println(engine); }
void start_engine() {
digitalWrite(power,LOW); delay (4000);
sensor();
if(fuel>31.45 && battery > 10 && transmission == "NEUTRAL" && engine == "OFF")
{
cacah = 0;
while (engine == "OFF" && cacah <3) {
digitalWrite (start,LOW); delay (500);
digitalWrite (start,HIGH); delay(2000);
read_engine(); cacah++; }
if (engine == "ON") {
sensor(); delay(1000);
SubmitHttpRequest(); delay (60000);
digitalWrite (power,HIGH); delay(1000);
digitalWrite(power,LOW); delay (4000);
sensor();
digitalWrite(power,HIGH); SubmitHttpRequest(); delay(1000);
else {
SubmitHttpRequest(); }
} else {
SubmitHttpRequest(); }
}
void loop() {
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year); delay(1000);
if (hour==07 && minute==00 && second == 00) {
start_engine();
digitalWrite(power,HIGH); }
}
2. Source code PHP a. input.ph
<?php
date_default_timezone_set("Asia/jakarta"); $time = date("h:i:sa");
$date = date ("Y/m/d");
$server = "mysql.hostinger.in"; // host name
$username = "u398539280_bike"; // database username $password = "harpot"; // database password
$dbname = "u398539280_auto"; //database name // Create connection
$conn = mysqli_connect($server, $username, $password, $dbname); // Check connection
VALUES
('".$_GET['fuel']."','".$_GET['battery']."','".$_GET['engine_st']."','".$_GET ['transmission_st']."','$time','$date')";
echo "a";
mysqli_query($conn,$sql);
?>
b. Getdata.php <?php
require_once('connect.php');
$sql = "SELECT * FROM Minerva ORDER BY number DESC LIMIT 1"; $r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r); $result = array();
array_push($result,array(
"battery"=>$res['battery'], "fuel"=>$res['fuel'],
"engine_st"=>$res['engine_st'],
"transmission_st"=>$res['transmission_st'], "date"=>$res['date'],
"time"=>$res['time']));
echo json_encode(array("result"=>$result)); mysqli_close($con);
3. Source code Android
a. Kelas MainActivity.java
import android.widget.Toast;
import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button buttonGet;
private TextView textViewResult; private ProgressDialog loading; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonGet = (Button)findViewById(R.id.buttonGet);
textViewResult = (TextView)findViewById(R.id.textViewResult); buttonGet.setOnClickListener(this);
}
private void getData() {
loading = ProgressDialog.show(this, "Please wait...", "Fetching...", false, false);
String url = Config.DATA_URL;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) { loading.dismiss();
showJSON(response); }
},
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,
error.getMessage().toString(), Toast.LENGTH_LONG).show(); }
});
RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest);
}
private void showJSON(String response) {
String battery = ""; String fuel = ""; String engine_st = ""; String transmition_st = ""; String date = "";
String time = "";
try {
JSONObject jsonObject = new JSONObject(response); JSONArray result =
jsonObject.getJSONArray(Config.JSON_ARRAY); JSONObject bikeData = result.getJSONObject(0); battery = bikeData.getString(Config.KEY_BATTERY); fuel = bikeData.getString(Config.KEY_FUEL);
engine_st = bikeData.getString(Config.KEY_ENGINE_ST);
transmition_st = bikeData.getString(Config.KEY_TRANSMITION_ST); date = bikeData.getString(Config.KEY_DATE);
time = bikeData.getString(Config.KEY_TIME); }
catch (JSONException e) {
e.printStackTrace(); }
textViewResult.setText("Battery:\t" + battery + "\nFuel:\t" + fuel + "\nEngine_st:\t" + engine_st + "\nTransmission_st:\t" + transmition_st+ "\nDate:\t" + date+ "\nTime:\t" + time);
}
@Override
getData(); }
}
b. Kelas AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.valgavin.bikeauto">
<uses-permission android:name="android.permission.INTERNET"/> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true"
android:theme="@style/AppTheme"> <activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
</activity> </application>
</manifest>
c. Kelas Config.java
package com.example.harry.bikeauto; /**
* Created by Harry on 1/30/2017. */
public class Config {
public static final String DATA_URL = "https://autobike.pe.hu/getdata.php";
public static final String KEY_BATTERY = "battery"; public static final String KEY_FUEL = "fuel";
public static final String KEY_ENGINE_ST = "engine_st";
public static final String KEY_TRANSMITION_ST = "transmition_st"; public static final String KEY_DATE = "date";