Skip to content

Instantly share code, notes, and snippets.

@vasmani
Last active January 1, 2016 12:19
Show Gist options
  • Select an option

  • Save vasmani/8144151 to your computer and use it in GitHub Desktop.

Select an option

Save vasmani/8144151 to your computer and use it in GitHub Desktop.
jarduino -- 과연 잘 되어야 하는데.. 방법 : jarduino의 HC-05옆에 점퍼선을 연결한후에 아래 코드를 usb모드로 입력, 실 사용시에는 BT모드로 변환하면 된다.
#include <Event.h>
#include <Timer.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <dht.h>
#define ledpin_red 13 //Arduino output pin for Shift Light Red led
#define BT_CMD_RETRIES 5 //Number of retries for each Bluetooth AT command in case of not responde with OK
#define OBD_CMD_RETRIES 20 //Number of retries for each OBD command in case of not receive prompt '>' char
#define RPM_CMD_RETRIES 15 //Number of retries for RPM command
#define KMH_CMD_RETRIES 15
unsigned int rpm,kmh;//Variables for RPM
boolean bt_error_flag = false;
boolean obd_error_flag = false;
boolean rpm_error_flag = false; //Variable for RPM error
boolean rpm_retries = 0; //Variable for RPM cmd retries
boolean kmh_error_flag = false;
boolean kmh_retries = 0;
int loopCount = 0;
float temperatureTMP;
float temperatureDHT;
float humidity;
dht DHT;
Timer t;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(){
Serial.begin(38400);
delay(300);
lcd.begin(16, 2);
pinMode(ledpin_red, OUTPUT);
lcd_print("CONNECT ELM327");
setupBlueToothConnection();
if (bt_error_flag){
bt_err_flash();
}
obd_init();
if (obd_error_flag){
obd_err_flash();
}
if(!obd_error_flag && !bt_error_flag){
lcd_print("OK!");
}
else {
lcd_print("ERROR!");
}
t.every(500, doLoop);
}
void loop(){
while (!(obd_error_flag)){
lcd.setCursor(0,1);
lcd.print(temperatureTMP,1);
lcd.setCursor(6,1);
lcd.print(temperatureDHT,1);
lcd.setCursor(12,1);
lcd.print(humidity,0);
t.update();
if (obd_error_flag) obd_err_flash();
}
}
void doLoop(){
get_values();
if(loopCount==0 || loopCount%64==0){
temperature_calc();
}
loopCount++;
}
void setupBlueToothConnection(){
sendATCommand("RESET"); //send to HC-05 RESET
delay(200);
// sendATCommand("ORGL"); //send ORGL, reset to original properties
// lcd_print("ORGL");
sendATCommand("ROLE=1"); //send ROLE=1, set role to master
delay(200);
sendATCommand("CMODE=0"); //send CMODE=0, set connection mode to specific address
delay(200);
sendATCommand("BIND=1122,33,DDEEFF"); //send BIND=??, bind HC-05 to OBD bluetooth address
delay(200);
sendATCommand("INIT"); //send INIT, cant connect without this cmd
delay(500);
sendATCommand("PAIR=1122,33,DDEEFF,20"); //send PAIR, pair with OBD address
delay(500);
sendATCommand("LINK=1122,33,DDEEFF"); //send LINK, link with OBD address
delay(500);
}
void sendATCommand(char *command){
char recvChar;
char str[2];
int i,retries;
boolean OK_flag;
if (!(bt_error_flag)){ //if no bluetooth connection error
retries=0;
OK_flag=false;
while ((retries<BT_CMD_RETRIES) && (!(OK_flag))){ //while not OK and bluetooth cmd retries not reached
Serial.print("AT"); //sent AT cmd to HC-05
if(strlen(command) > 1){
Serial.print("+");
Serial.print(command);
lcd_print(command);
}
Serial.print("\r\n");
while (Serial.available()<=0){
}; //wait while no data
i=0;
while (Serial.available()>0){ // while data is available
recvChar = Serial.read(); //read data from HC-05
if (i<2){
str[i]=recvChar; //put received char to str
i=i+1;
}
}
retries=retries+1; //increase retries
if(retries>1) lcd_print("RETRY AT" + String(retries));
if ((str[0]=='O') && (str[1]=='K')) OK_flag=true; //if response is OK then OK-flag set to true
lcd.print(str);
delay(1000);
}
if (retries>=BT_CMD_RETRIES) { //if bluetooth retries reached
bt_error_flag=true; //set bluetooth error flag to true
lcd_print("BT_CMD_OUT");
}
}
}
void send_OBD_cmd(char *obd_cmd){
char recvChar;
char bufin[50];
boolean prompt;
int retriesx;
int i = 0;
if (!(obd_error_flag)){ //if no OBD connection error
prompt=false;
retriesx=0;
while((!prompt) && (retriesx<OBD_CMD_RETRIES)){ //while no prompt and not reached OBD cmd retries
lcd_print(obd_cmd);
Serial.print(obd_cmd); //send OBD cmd
Serial.print("\r"); //send cariage return
while (Serial.available() <= 0); //wait while no data from ELM
while ((Serial.available()>0) && (!prompt)){ //while there is data and not prompt
recvChar = Serial.read(); //read from elm
bufin[i] = recvChar;
if (recvChar==62) prompt=true; //if received char is '>' then prompt is true
i++;
}
lcd_print(bufin);
delay(300);
retriesx=retriesx+1; //increase retries
if(retriesx>1) lcd_print("RETRY OBD"+String(retriesx));
delay(1000);
}
if (retriesx>=OBD_CMD_RETRIES) { // if OBD cmd retries reached
obd_error_flag=true; // obd error flag is true
lcd_print("OBD_ERROR");
}
}
}
//----------------------------------------------------------//
//----------------initialitation of OBDII-------------------//
void obd_init(){
obd_error_flag=false; // obd error flag is false
send_OBD_cmd("ATZ"); //send to OBD ATZ, reset
delay(200);
send_OBD_cmd("ATSP3"); //send ATSP0, protocol ISO-9141-2
delay(200);
send_OBD_cmd("ATE0"); // echo off
delay(200);
send_OBD_cmd("ATS0"); // no spaces
delay(200);
send_OBD_cmd("ATL0"); // no spaces
delay(200);
send_OBD_cmd("ATST0B"); // fine tune timeout
delay(200);
send_OBD_cmd("0100"); //send 0100, retrieve available pid's 00-19
delay(1000);
send_OBD_cmd("0120"); //send 0120, retrieve available pid's 20-39
delay(200);
send_OBD_cmd("010C1"); //send 010C1, RPM cmd
delay(200);
send_OBD_cmd("010D1");
delay(200);
}
void bt_err_flash(){
while(1){
digitalWrite(ledpin_red,HIGH);
delay(100);
digitalWrite(ledpin_red,LOW);
delay(100);
}
}
void obd_err_flash(){
while(1){
digitalWrite(ledpin_red,HIGH);
delay(1000);
digitalWrite(ledpin_red,LOW);
delay(1000);
}
}
void lcd_print(String x){
lcd.clear();
lcd.print(x);
delay(200);
}
void get_values(){
boolean prompt;
char recvChar;
char bufin[10];
char bufin2[10];
int i;
if (!(obd_error_flag)){
prompt=false;
Serial.print("010C1"); // RPM
Serial.print("\r");
while (Serial.available() <= 0);
i=0;
while ((Serial.available()>0) && (!prompt)){
recvChar = Serial.read();
if (i<8) {
bufin[i]=recvChar;
i=i+1;
}
if (recvChar==62) prompt=true;
}
if ((bufin[0]=='4') && (bufin[1]=='1') && (bufin[2]=='0') && (bufin[3]=='C')){
rpm_retries=0;
rpm_error_flag=false;
rpm=0;
for (i=4;i<8;i++){
if ((bufin[i]>='A') && (bufin[i]<='F')){
bufin[i]-=55;
}
if ((bufin[i]>='0') && (bufin[i]<='9')){
bufin[i]-=48;
}
rpm=(rpm << 4) | (bufin[i] & 0xf);
}
rpm=rpm >> 2;
}
if ((rpm>=0) && (rpm<10000)){ //if rpm value is between 0 and 10000
lcd.setCursor(0,0);
if(rpm < 1000){
lcd.print( String(" ") + rpm);
}
else {
lcd.print(rpm);
}
}
delay(250);
// kmh
char temp[3];
i = 0;
prompt = false;
Serial.print("010D1");
Serial.print("\r");
while (Serial.available() <= 0);
i=0;
while ((Serial.available()>0) && (!prompt)){
recvChar = Serial.read();
if (i<6) {
bufin2[i]=recvChar;
i=i+1;
}
if (recvChar==62) prompt=true;
}
if ((bufin2[0]=='4') && (bufin2[1]=='1') && (bufin2[2]=='0') && (bufin2[3]=='D')){
temp[0] = bufin2[4];
temp[1] = bufin2[5];
kmh = (int)strtol(temp, NULL, 16);
}
if(kmh>=0){
lcd.setCursor(5,0);
if(kmh < 10){
lcd.print(String(" ") + kmh);
}
else if(kmh >=10 && kmh < 100){
lcd.print(String(" ") + kmh);
}
else {
lcd.print(kmh);
}
}
}
}
void temperature_calc(){
// TMP36
float voltage = analogRead(A1) * 5.0;
voltage /= 1024.0;
temperatureTMP = (voltage - 0.5) * 100;
// DHT-11
DHT.read11(A0);
temperatureDHT = DHT.temperature;
humidity = DHT.humidity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment