IT/YOLO

[9] 아두이노를 활용한 GPS 출력

주니- 2018. 6. 25. 15:56

[8] 아두이노를 활용한 부저 알람과 같이 활용할 수 있는 GPS 출력입니다.

 

아두이노 스케치중 GPS를 출력할 수 있는 스케치는 많습니다.

저같은 경우에는 대표적인 TinyGPS스케치에 오류가 있어 TinyGPS++(클릭시 이동) 를 사용하였습니다.

회로도

 

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define DEBUG true
 
static const int RXPin = 8, TXPin = 7;
static const uint32_t GPSBaud = 9600;
#define espRXPin 2
#define espTXPin 3
 
 
SoftwareSerial esp8266(espTXPin, espRXPin); 
// make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
SoftwareSerial ss(TXPin, RXPin);
TinyGPSPlus gps;
 
void setup() {
  Serial.begin(9600);
  esp8266.begin(9600); // your esp's baud rate might be different
  ss.begin(GPSBaud);  
  sendData("AT+RST\r\n",2000,DEBUG); // reset module
  sendData("AT+CIOBAUD?\r\n",2000,DEBUG); // check baudrate (redundant)
  sendData("AT+CWMODE=3\r\n",1000,DEBUG); // configure as access point (working mode: AP+STA)
  sendData("AT+CWLAP\r\n",3000,DEBUG); // list available access points
  sendData("AT+CWJAP=\"sksdhddl\",\"!@wnsgmldi1_\"\r\n",5000,DEBUG); // join the access point
  sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
}
 
void loop() {  
  if(esp8266.available()) { // check if the esp is sending a message
    if(esp8266.find("+IPD,")) {
      delay(1000); // wait for the serial buffer to fill up (read all the serial data)
      // get the connection id so that we can then disconnect
      int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48
      esp8266.find("pin="); // advance cursor to "pin="
      int pinNumber = (esp8266.read()-48)*10; 
      // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
      pinNumber += (esp8266.read()-48); 
      // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
      digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin    
     
      // make close command
      String closeCommand = "AT+CIPCLOSE="
      closeCommand+=connectionId; // append connection id
      closeCommand+="\r\n";
      sendData(closeCommand,1000,DEBUG); // close connection
    }
  }
  ss.begin(GPSBaud);
  while (ss.available() > 0)
   if (gps.encode(ss.read()))
   displayInfo();
 
   if (millis() > 35000 && gps.charsProcessed() < 10) {
     Serial.println(F("No GPS detected: check wiring."));
     while(true);
   }
}
 
/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; 
   timeout - the time to wait for a response; 
   debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug) {
    String response = "";
    esp8266.print(command); // send the read character to the esp8266
    long int time = millis();
    
    while( (time+timeout) > millis()) {
      while(esp8266.available()) {
        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }
    }
    
    if(debug) {
      Serial.print(response);
    }
    return response;
 
void displayInfo()
{
  Serial.print(F("Location: "));
  if (gps.location.isValid()) {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  } 
  Serial.println();
}

※ 아두이노 wifi AT명령어(클릭시 이동)

esp8266 AT명령어 작동 확인

wifi 쉴드와 공유기 연결 부분

1
2
3
4
5
6
7
8
9
10
11
  esp8266.begin(9600); 
  ss.begin(GPSBaud);
 
  sendData("AT+RST\r\n",2000,DEBUG); 
  sendData("AT+CIOBAUD?\r\n",2000,DEBUG); 
  sendData("AT+CWMODE=3\r\n",1000,DEBUG); 
  sendData("AT+CWLAP\r\n",3000,DEBUG); 
  sendData("AT+CWJAP=\"sksdhddl\",\"!@wnsgmldi1_\"\r\n",5000,DEBUG); 
  sendData("AT+CIFSR\r\n",1000,DEBUG); 
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG); 
  sendData("AT+CIPSERVER=1,80\r\n"1000,DEBUG);

아래의 두 코드는 GPS 위도, 경도 값을 보여주는 코드입니다. 나머지 정보도 더 볼 수 있는데 저는 위도와 경도만을 보기 위해서 이렇게 했습니다.

 

1
2
3
4
5
6
7
8
9
  ss.begin(GPSBaud);
  while (ss.available() > 0)
   if (gps.encode(ss.read()))
   displayInfo();
 
   if (millis() > 35000 && gps.charsProcessed() < 10) {
     Serial.println(F("No GPS detected: check wiring."));
     while(true);
   }

.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void displayInfo()
{
  Serial.print(F("Location: "));
  if (gps.location.isValid()) {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  } 
  Serial.println();
}

값이 INVALID로 나온다면 GPS 신호가 원활하지 않은 상태입니다.

하지만 Location : No GPS detected: check wiring.으로 나온다면 연결을 다시 한 번 확인해주세요.

경도, 위도 출력 정상적인 화면

코드 작동 확인