BASIC4MCU | 질문게시판 | 아두이노에서 블루투스와 gps를 같이 쓰고싶어요
페이지 정보
작성자 김라꾸 작성일2021-06-10 16:23 조회10,078회 댓글4건본문
아두이노 우노를 사용해서 저번에 GPS 모듈로 위경도 좌표를 받아왔는데요
이번에는 받아온 좌표를 블루투스로 휴대폰에 띄워보려고 했는데 코드를 따로따로 넣으면 작동하는데
같이 넣으면 작동이 안되더라고요
컴파일 자체는 되는데 이게 안되는 이유가 무엇일까요?
아래에 코드 첨부했어요
2,3번은 블루투스 Rx,Tx
7번은 온습도 센서
8,9번은 gps(neo-6m)모듈을 연결했어요
이렇게 입력하면 시리얼 모니터에 온습도만 나오네요
SoftwareSerial이 두번나와서 안되는걸까요??
도와주세요
---추가로 여기서 블루투스 코드 부분만 주석으로 처리하니까 또 gps가 뜨네요
그럼 SoftwareSerial을 블루투스랑 gps 같이 쓰려면 어떻게 써야할까요?
--------
#include "DHT.h" // DHT.h 라이브러리를 포함합니다.
#include <SoftwareSerial.h> // 0,1번핀 제외하고 Serial 통신을 하기 위해 선언
#include "TinyGPS++.h"
#include "SoftwareSerial.h"
SoftwareSerial BtSerial(2, 3); // HC-06 TX=11번핀 , RX=10번핀 연결
// 각자 블루투스 모듈을 연결한 번호가 다를 수 있습니다
// 본인 아두이노에 맞게 설정해서 바꾸면 됩니다
#define DHTPIN 7 // DHTPIN을 2로 설정합니다.
#define DHTTYPE DHT11 // DHTTYPE를 DHT11로 설정합니다.
SoftwareSerial serial_connection(9, 8); //RX=pin 8, TX=pin 9
TinyGPSPlus gps;//This is the GPS object that will pretty much do all the grunt work with the NMEA data
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600); // 시리얼 통신을 시작하며, 통신속도는 9600
BtSerial.begin(9600); // 통신 속도 9600bps로 블루투스 시리얼 통신 시작
serial_connection.begin(9600);//This opens up communications to the GPS
Serial.println("GPS Start");//Just show to the monitor that the sketch has started
}
void loop() {
BtSerial.write(Serial.read()); // Serial 핀에 입력이 들어오면, 바이트단위로 읽어서 블루투스로 출력
int h = dht.readHumidity(); // 변수 h를 선언하며 습도값을 대입
int t = dht.readTemperature(); // 변수 t를 선언하며 온도값을 대입
Serial.print("Humidity: "); // 시리얼 프린터에 출력 (이하생략)
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" C");
BtSerial.print("Humidity: "); // 시리얼 프린터에 출력 (이하생략)
BtSerial.print(h);
BtSerial.print(" %\t");
BtSerial.print("Temperature: ");
BtSerial.print(t);
BtSerial.println(" C");
while(serial_connection.available())//While there are characters to come from the GPS
{
gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
}
if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
{
//Get the latest info from the gps object which it derived from the data sent by the GPS unit
Serial.println("Satellite Count:");
Serial.println(gps.satellites.value());
Serial.println("Latitude:");
Serial.println(gps.location.lat(), 6);
Serial.println("Longitude:");
Serial.println(gps.location.lng(), 6);
Serial.println("Speed MPH:");
Serial.println(gps.speed.mph());
Serial.println("Altitude Feet:");
Serial.println(gps.altitude.feet());
Serial.println("");
}
댓글 4
조회수 10,078master님의 댓글
master 작성일
#include <SoftwareSerial.h> // 0,1번핀 제외하고 Serial 통신을 하기 위해 선언
#include "SoftwareSerial.h"
중복해서 선언하지말고 하나는 삭제하세요
master님의 댓글
master 작성일
소프트웨어 시리얼이 2개 이상일 때에 수신은 단 한 개만 가능합니다.
GPS 수신을 받아야 하므로 블루투스 수신은 포기하세요
블루투스 송신은 가능합니다.
셋업함수 마지막에 gps 수신 지정을 해주어야 합니다.
serial_connection.listen(); // gps 수신지정
//
BtSerial.write(Serial.read()); // Serial 핀에 입력이 들어오면, 바이트단위로 읽어서 블루투스로 출력
이 코드 삭제하세요
하드웨어 시리얼을 read()하면 실제 데이터가 올 때까지 다른 처리를 아무것도 하지 않습니다.
구지 넣는다면
if(Serial.available())BtSerial.write(Serial.read()); // Serial 핀에 입력이 들어오면, 바이트단위로 읽어서 블루투스로 출력
available() 체크해서 수신이 있는 경우에만 실행 하도록하면 됩니다.
김라꾸님의 댓글
김라꾸
말씀해주신 내용 보고 블루투스 수신이 문제인것같아서 수신인 2번핀을 빼고
코드를 고쳐봤는데 블루투스에서 안뜨네요;;
제가 블루투스상에서 시리얼 모니터에 뜨게 하려고 BtSerial.print("Humidity: "); 이런식으로 표현한것때문일까요..?
블루투스 시리얼에 뜨게 하는 표현을 바꿔야할까요?
----- 밑에는 중복된 선언 지우고 말씀해주신대로 바꾼 코드에요--
#include <DHT.h> // DHT.h 라이브러리를 포함합니다.
#include <SoftwareSerial.h> // 0,1번핀 제외하고 Serial 통신을 하기 위해 선언
#include <TinyGPS++.h>
SoftwareSerial BtSerial(2, 3); // HC-06 TX=3번핀 , RX=2번핀 연결
#define DHTPIN 7 // DHTPIN을 7로 설정합니다.
#define DHTTYPE DHT11 // DHTTYPE를 DHT11로 설정합니다.
DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial serial_connection(9, 8); //RX=pin 8, TX=pin 9
TinyGPSPlus gps;
void setup() {
Serial.begin(9600); // 시리얼 통신을 시작하며, 통신속도는 9600
BtSerial.begin(9600); // 통신 속도 9600bps로 블루투스 시리얼 통신 시작
serial_connection.begin(9600); //This opens up communications to the GPS
serial_connection.listen(); // gps 수신지정
}
void loop() {
serial_connection.listen(); // gps 수신지정
int h = dht.readHumidity(); // 변수 h를 선언하며 습도값을 대입
int t = dht.readTemperature(); // 변수 t를 선언하며 온도값을 대입
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" C");
BtSerial.print("Humidity: ");
BtSerial.print(h);
BtSerial.print(" %\t");
BtSerial.print("Temperature: ");
BtSerial.print(t);
BtSerial.println(" C");
while(serial_connection.available())//While there are characters to come from the GPS
{
gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
}
if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
{
//Get the latest info from the gps object which it derived from the data sent by the GPS unit
Serial.println("Latitude:");
Serial.println(gps.location.lat(), 6);
Serial.println("Longitude:");
Serial.println(gps.location.lng(), 6);
BtSerial.print("Latitude:");
BtSerial.print(gps.location.lat(), 6);
BtSerial.print("Longitude:");
BtSerial.print(gps.location.lng(), 6);
}
}
master님의 댓글
master 작성일
습도센서 코드없이 다시 돌려보세요
void loop() {
serial_connection.listen(); // gps 수신지정
루프함수 바로 아래에 있는 코드는 삭제하세요