60 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); digitalwrite(start,high); delay (100);
61 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"); // 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?"); sim800l.println("at+sapbr=3,1,\"contype\",\"gprs\""); the SAPBR, the connection type is using gprs //setting
62 sim800l.println("at+sapbr=3,1,\"apn\",\"internet\""); APN, the second need you fill in your local apn server //setting the sim800l.println("at+sapbr=1,1"); for detail you can refer to the AT command mamual delay(2000); sim800l.println("at+httpinit"); delay(2000); //setting the SAPBR, //init the HTTP request sim800l.println(stringpara2); // setting the httppara, the second parameter is the website you want to access 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. sim800l.println("at+httpread");// read the data from the website you access delay(300); 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 delay(300);
63 // 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); delay(100); if ( digitalread(net) == HIGH) transmission = "ON GEAR"; else transmission = "NEUTRAL"; digitalwrite(pul,low); Serial.println(transmission); void read_engine() // puldown input neutral // membaca status transmisi
64 float val1 = analogread(a0); if (val1>10) engine = "ON"; else engine = "OFF"; Serial.println(engine); // membaca staus mesin 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(); SubmitHttpRequest(); delay (60000); digitalwrite (power,high); digitalwrite(power,low); delay (4000); sensor(); digitalwrite(power,high); SubmitHttpRequest();
65 else SubmitHttpRequest(); else SubmitHttpRequest(); void loop() byte second, minute, hour, dayofweek, dayofmonth, month, year; readds3231time(&second, &minute, &hour, &dayofweek, &dayofmonth, &month, &year); 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 $sql = "INSERT INTO Minerva (fuel,battery, engine_st, transmission_st, time, date)
66 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);?>
67 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);, new Response.ErrorListener() @Override
68 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 public void onclick(view v)
69 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"; public static final String KEY_TIME = "time"; public static final String JSON_ARRAY = "result";