CHAPTER 5 IMPLEMENTATION AND TESTINGrepository.unika.ac.id/15356/6/13.02.0048 Beny Hidayat BAB...

5
CHAPTER 5 IMPLEMENTATION AND TESTING 5.1 Implementation In this project there are some code in this project, there are code for declaration pin as input or output, code for condition ultrasonic sensor, code for load cell, and code for upload data to IoT server using GSM Shield. 1. Code for declaration pin as input or output. This code is declaration ultrasonic sensors pin, motor servo pin, load cell pin, and GSM Shield pin. 1. #include <NewPing.h> 2. #include <Servo.h> 3. #include <SoftwareSerial.h> 4. #define TRIGGER_PIN   37 5. #define ECHO_PIN      36 6. #define TRIGGER_PIN2  33 7. #define ECHO_PIN2     32 8. #define MAX_DISTANCE 200 9. 10. SoftwareSerial SIM900(0,1); //(TX,RX) GSM Shield 11. int PowerKeySIM900=9; // Power Pin GSM shield 12. 13. 14. #include "HX711.h" 15. 16. #define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch 17. 18. #define DOUT  2 19. #define CLK   3 20. 21. 22. float read_gram; 23. String Stringone; 24. String Stringthree; 25. HX711 scale(DOUT, CLK); 26. Servo servoA; 27. Servo servoB; 28. 9

Transcript of CHAPTER 5 IMPLEMENTATION AND TESTINGrepository.unika.ac.id/15356/6/13.02.0048 Beny Hidayat BAB...

Page 1: CHAPTER 5 IMPLEMENTATION AND TESTINGrepository.unika.ac.id/15356/6/13.02.0048 Beny Hidayat BAB V.pdf · CHAPTER 5 IMPLEMENTATION AND TESTING 5.1 Implementation In this project there

CHAPTER 5

IMPLEMENTATION AND TESTING

5.1 Implementation

In this project there are some code in this project, there are code for

declaration pin as input or output, code for condition ultrasonic sensor, code for

load cell, and code for upload data to IoT server using GSM Shield.

1. Code for declaration pin as input or output.

This code is declaration ultrasonic sensors pin, motor servo pin, load cell pin, and

GSM Shield pin.

1. #include <NewPing.h>2. #include <Servo.h>3. #include <SoftwareSerial.h>4. #define TRIGGER_PIN   375. #define ECHO_PIN      366. #define TRIGGER_PIN2  337. #define ECHO_PIN2     328. #define MAX_DISTANCE 2009. 10. SoftwareSerial SIM900(0,1); //(TX,RX) GSM Shield11. int PowerKeySIM900=9; // Power Pin GSM shield12. 13. 14. #include "HX711.h"15. 16. #define   calibration_factor   ­7050.0   //This   value   is

obtained using the SparkFun_HX711_Calibration sketch17. 18. #define DOUT  219. #define CLK   320. 21. 22. float read_gram;23. String Stringone;24. String Stringthree;25. HX711 scale(DOUT, CLK);26. Servo servoA;27. Servo servoB;28. 

9

Page 2: CHAPTER 5 IMPLEMENTATION AND TESTINGrepository.unika.ac.id/15356/6/13.02.0048 Beny Hidayat BAB V.pdf · CHAPTER 5 IMPLEMENTATION AND TESTING 5.1 Implementation In this project there

10

29. const int buttonPin = 40;30. const int buttonPin2 = 53;31. const int buttonPin3 = 52;32. 33. int ledpin = 8;34. int ledpin2 = 9;35. int ledpin3 = 21;36. int buttonState = 0;37. int buttonState2 = 0;38. int buttonState3 = 0;

2. Code for ultrasonic sensors

This code is for the condition ultrasonic sensors, so the entrance gate can be open

or close.

1. int US1 = ultrasonic1.ping_cm();2. int US2 = ultrasonic2.ping_cm();3. Serial.print("Hasil Sensor 1 :");4. Serial.print(US1);5. Serial.print("cm     |     ");6. Serial.print("Hasil Sensor 2 :");7. Serial.print(US2);8. Serial.println("cm");9.10.       if(US1 <= 10 && US2 >=10)11.       {12.         servoA.write (90); //gerbang buka13.         Serial.println("gerbang buka");14.         delay(5);15.       }16.       else if (US1 >=10 && US2 >=10) 17.       {18.         servoA.write(0); //gerbang tutup19.          Serial.println("gerbang tutup");20.         delay(5);21.       }22.       else if (US1 <=10 && US2 <=10)23.       {24.          servoA.write(0); //gerbang tutup25.          Serial.print("gerbang tutup");26.          delay(5);

Page 3: CHAPTER 5 IMPLEMENTATION AND TESTINGrepository.unika.ac.id/15356/6/13.02.0048 Beny Hidayat BAB V.pdf · CHAPTER 5 IMPLEMENTATION AND TESTING 5.1 Implementation In this project there

11

27. }

3. Code for load cell

This code is for reading weigh using load cell.

1. read_gram =  scale.get_units();2. Serial.print("Reading: ");3. Serial.print(read_gram,   1);   //scale.get_units()   returns   a

float4. Serial.print("   gram");   //You   can   change   this   to   kg   but

you'll need to refactor the calibration_factor5. delay(500);6. Serial.println();7. Stringone="field2=";8. Stringthree = Stringone+ read_gram;

4. Code for GSM Shield

This code is to upload weighed data to IoT server using GSM Shield.

1. Serial.println("AT+CGATT=1");//konfig apn2.   delay(1000);3.    4.    Serial.println("AT+CSTT=telkomsel,\"wap\",\"wap123\"");//

Setting koneksi APN5.   delay(1000);6.   Serial.println("AT+CIICR");7.   delay(1000);8.    9.    Serial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");//Set

ting koneksi GPRS10.   delay(1000);11.   12.   Serial.println("AT+SAPBR=3,1,\"APN\",\"telkomsel\"")

;//setting APN kartu 13.   delay(1000); 14.   15.    Serial.println("AT+SAPBR =1,1"); // mendapatkan IP

local16.   delay(1000);17.   Serial.println("AT+SAPBR =1,1");18.   delay(1000);

Page 4: CHAPTER 5 IMPLEMENTATION AND TESTINGrepository.unika.ac.id/15356/6/13.02.0048 Beny Hidayat BAB V.pdf · CHAPTER 5 IMPLEMENTATION AND TESTING 5.1 Implementation In this project there

12

19.    Serial.println("AT+HTTPINIT"); // inisialisasi HTTPrequest

20.   delay(2000);21.   22.   Serial.println("AT+HTTPPARA=\"URL\",\"api.thingspeak

.com/update?api_key=S4AZ93KHE0KPWEMD&"+   Stringthree   +" \""); 

23.   delay(1000);24.  25.   Serial.println("\"");26.   delay(4500);27.   Serial.println("AT+HTTPACTION=0");//mengirim request28.   delay(10000);//delay mengirim data (request HTTP)29.    Serial.println("AT+HTTPREAD");// membaca data dari

htpp yang diakses30.   delay(300);31.   Serial.println("");32.   delay(100);

Page 5: CHAPTER 5 IMPLEMENTATION AND TESTINGrepository.unika.ac.id/15356/6/13.02.0048 Beny Hidayat BAB V.pdf · CHAPTER 5 IMPLEMENTATION AND TESTING 5.1 Implementation In this project there

13

5.2 Testing

After all the sensors work properly and weight data from vehicle that has

the maximum payload can be uploaded to thingspeak.com, the next step is to

place all the tools on plywood.

Illustration 5.1: Placement the tools