정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

아두이노 wifi와 gps를 결합한 제품을 구현하려고 합니다!
김다슬 | 2016-08-10

코코아팹 예제에 있는 것에서 아이디어를 얻어 gps로 지역 정보를 받고 그 지역에 날씨를 wifi로 기상청으로 받아와서 led로 날씨 정보를 나타내는 구름을 만드려고 합니다.

gps와 wifi가 각각 구현되었고, 이것을 하나의 코드로 합쳐서 구현하려고 하는데 wifi만 작동되거나 잘 안 될경우도 있습니다ㅠ..(잘작동되지 않는경우가 대부분이에요ㅠㅠ)

무엇이 문제인지 코드 한 번 올려봅니다ㅠㅠ고수님들 도와주세요..!!

 

1)gps코드 (+lcd)

#include
#include
#include

LiquidCrystal lcd (8,9,4,5,6,7);  //pin initialization for lcd
SoftwareSerial ss(2,3);           //software serial pin for skm53 shield
TinyGPS gps;

 

//variables for gps
boolean newData=false;
float flat,flon;
int year;byte month,day,hour,minute,second;

 

boolean state=0;

 

void setup()
{
  lcd.begin(16,2);
  lcd.print("Acquiring GPS...");
  Serial.begin(9600);
  ss.begin(9600);
}


void loop()
{
  while(ss.available())
  {
    char c=ss.read();
    if(gps.encode(c))
    {
      newData=true;
      gps.f_get_position(&flat,&flon);
      gps.crack_datetime(&year,&month,&day,&hour,&minute,&second);
      hour=hour+9;           //hour +9 for GMT+9 time zone
    }
  }

 

  if (newData)
  {
    lcd.clear();
    if (state == 0)           //display latitude and longitude
    {
      lcd.setCursor(0, 0);
      lcd.print("Lat:");
      lcd.setCursor(6, 0);
      lcd.print(flat);

      lcd.setCursor(0, 1);
      lcd.print("Lon:");
      lcd.setCursor(6, 1);
      lcd.print(flon);


      delay(2000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Loca:");
      lcd.setCursor(0, 1);

      if (3737.40952 <= flat <= 3741.5215 && 12702.87736 <= flon <= 12705.64192) {
        lcd.print("NOWON");
      }

      else if (3737.6053 <= flat <= 3740.02234 && 12658.9464 <= flon <= 12700.87978) {
        lcd.print("GANGBUK");
      }

 

      delay(2000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Date:");
      lcd.setCursor(6, 0);
      lcd.print(day);
      lcd.print("/");
      lcd.print(month);
      lcd.print("/");
      lcd.print(year);

      lcd.setCursor(0, 1);
      lcd.print("Time:");
      lcd.setCursor(6, 1);
      lcd.print(hour);
      lcd.print(":");
      lcd.print(minute);
      lcd.print(":");
      lcd.print(second);

      delay(2000);
    }
    newData = false;
    delay(10);
  }
}

 

 

2)wifi코드

#include
#include
#include
#include "utility/debug.h"

 

#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

 

uint32_t ip;

 

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER); // you can change this clock speed but DI

 

#define WLAN_SSID     "Daseul_iPhone"       //와이파이 SSID
#define WLAN_PASS     "********"   //와이파이 password
#define WLAN_SECURITY   WLAN_SEC_WPA2
#define IDLE_TIMEOUT_MS  3000

 

#define WEBSITE      "www.kma.go.kr"
#define WEBPAGE      "/wid/queryDFSRSS.jsp?zone=1162058500"

 

void setup() {
  //각 변수에 정해진 공간 할당
  Serial.begin(9600);


  delay(100);
  //WiFi연결 시도
  Serial.println(F("\nInitialising the CC3000 ..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
    while(1);
  }


  displayMACAddress();
 
 
  Serial.print(F("\nAttempting to connect to ")); Serial.println(F(WLAN_SSID));


  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.println(F("Connected!"));

 

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }


  while (! displayConnectionDetails()) {
    delay(1000);
  }

 

  ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0) {
    if (! cc3000.getHostByName(WEBSITE, &ip)) {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);

 

  Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");
  uint8_t replies = cc3000.ping(ip, 5);
  Serial.print(replies); Serial.println(F(" replies"));
  if (replies)
    Serial.println(F("Ping successful!"));

 
  Serial.println(F("\n\r++++++++++++++++++++++++++++++++"));
}

 

void loop() {

  Serial.println("loop");

 


  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);


  if (client.connected()) {
    client.fastrprint(F("GET "));
    client.fastrprint(WEBPAGE);
    client.fastrprint(F(" HTTP/1.1\r\n"));
    client.fastrprint(F("Host: ")); client.fastrprint(WEBSITE); client.fastrprint(F("\r\n"));
    client.fastrprint(F("\r\n"));
    client.println();
  } else {
    Serial.println(F("Connection failed"));   
    return;
  }

  Serial.println(F("-------------------------------------"));


int out = 0;

  while (client.connected()){
    if (client.available() && out < 1){
        //라인을 기준으로 문자열을 저장한다.
        String line = client.readStringUntil('\n');

        //시간
        int temp11= line.indexOf("");
        if(temp11>0) {
          String tmp_str="";
          String wt_hour = line.substring(line.indexOf(tmp_str)+tmp_str.length(),temp11);
          Serial.print("hour is ");
          Serial.println(wt_hour);


          String line2 = client.readStringUntil('\n');
          String line3 = client.readStringUntil('\n');
          //온도
          int temp= line3.indexOf("");
          if(temp>0) {
            String tmp_str="";
            String wt_temp = line3.substring(line3.indexOf(tmp_str)+tmp_str.length(),temp);
            Serial.print("temperature is ");
            Serial.println(wt_temp);


            String line4 = client.readStringUntil('\n');
            String line5 = client.readStringUntil('\n');
            String line6 = client.readStringUntil('\n');
            String line7 = client.readStringUntil('\n');
            String line8 = client.readStringUntil('\n');
            String line9 = client.readStringUntil('\n');
            //날씨 정보
            int wfEn= line9.indexOf("");
            if(wfEn>0) {
              String tmp_str="";
              String wt_twfEn = line9.substring(line9.indexOf(tmp_str)+tmp_str.length(),wfEn);
              Serial.print("weather is ");
              Serial.println(wt_twfEn);
              Serial.println("====================");
              out = out + 1;

 

             
              break;
              }
            }
          }
    }
  }
Serial.println("finish");

}

 

void displayMACAddress(void)
{
  uint8_t macAddress[6];
 
  if(!cc3000.getMacAddress(macAddress))
  {
    Serial.println(F("Unable to retrieve MAC Address!\r\n"));
  }
  else
  {
    Serial.print(F("MAC Address : "));
    cc3000.printHex((byte*)&macAddress, 6);
  }
}

 

bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
 
  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

 

3)합친것

#include
#include
#include
#include
#include
#include
#include "utility/debug.h"

 

LiquidCrystal lcd (8,9,4,5,6,7);  //pin initialization for lcd
SoftwareSerial ss(2,3);           //software serial pin for skm53 shield
TinyGPS gps;

 

#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

 

uint32_t ip;

 

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);

 

#define WLAN_SSID     "Daseul_iPhone"  //와이파이 SSID
#define WLAN_PASS     "*********"   //와이파이 password
#define WLAN_SECURITY   WLAN_SEC_WPA2
#define IDLE_TIMEOUT_MS  3000

 

#define WEBSITE      "www.kma.go.kr"
#define WEBPAGE      "/wid/queryDFSRSS.jsp?zone=1162058500"

 

//GPS 변수들
boolean newData=false;
float flat,flon;
int year;byte month,day,hour,minute,second;

 

boolean state=0;

 

void setup()
{
  lcd.begin(16,2);
  lcd.print("Acquiring GPS...");
  ss.begin(9600);
  Serial.begin(9600);

 

  delay(100);

 

  //WiFi연결 시도
  Serial.println(F("\nInitialising the CC3000 ..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
    while(1);
  }

 

  displayMACAddress();

 

  Serial.print(F("\nAttempting to connect to ")); Serial.println(F(WLAN_SSID));

 

  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY))
  {
    Serial.println(F("Failed!"));
    while(1);
  }

 

  Serial.println(F("Connected!"));

 

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }

 

  while (! displayConnectionDetails())
  {
    delay(1000);
  }

 

  ip = 0;
  Serial.print(WEBSITE); Serial.print(F(" -> "));
  while (ip == 0)
  {
    if (! cc3000.getHostByName(WEBSITE, &ip))
    {
      Serial.println(F("Couldn't resolve!"));
    }
    delay(500);
  }
  cc3000.printIPdotsRev(ip);

 

  Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");
  uint8_t replies = cc3000.ping(ip, 5);
  Serial.print(replies); Serial.println(F(" replies"));
  if (replies)
  {
    Serial.println(F("Ping successful!"));
  }

 
  Serial.println(F("\n\r++++++++++++++++++++++++++++++++"));
}

 

void loop()
{
  Serial.println("loop");

 

  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);


  if (client.connected())
  {
    client.fastrprint(F("GET "));
    client.fastrprint(WEBPAGE);
    client.fastrprint(F(" HTTP/1.1\r\n"));
    client.fastrprint(F("Host: ")); client.fastrprint(WEBSITE); client.fastrprint(F("\r\n"));
    client.fastrprint(F("\r\n"));
    client.println();
  }
  else
  {
    Serial.println(F("Connection failed"));
    return;
  }

  Serial.println(F("-------------------------------------"));


  int out = 0;


  while (client.connected())
  {
    if (client.available() && out < 1)
    {
      //라인을 기준으로 문자열을 저장
      String line = client.readStringUntil('\n');

      //시간
      int temp11 = line.indexOf("");
      if(temp11 > 0)
      {
        String tmp_str = "";
        String wt_hour = line.substring(line.indexOf(tmp_str) + tmp_str.length(),temp11);
        Serial.print("hour is ");
        Serial.println(wt_hour);


        String line2 = client.readStringUntil('\n');
        String line3 = client.readStringUntil('\n');
         
         
        //온도
        int temp = line3.indexOf("");
        if(temp > 0)
        {
          String tmp_str = "";
          String wt_temp = line3.substring(line3.indexOf(tmp_str)+tmp_str.length(),temp);
          Serial.print("temperature is ");
          Serial.println(wt_temp);


          String line4 = client.readStringUntil('\n');
          String line5 = client.readStringUntil('\n');
          String line6 = client.readStringUntil('\n');
          String line7 = client.readStringUntil('\n');
          String line8 = client.readStringUntil('\n');
          String line9 = client.readStringUntil('\n');
           
           
          //날씨 정보
          int wfEn = line9.indexOf("");
          if(wfEn > 0)
          {
            String tmp_str = "";
            String wt_twfEn = line9.substring(line9.indexOf(tmp_str)+tmp_str.length(),wfEn);
            Serial.print("weather is ");
            Serial.println(wt_twfEn);
            Serial.println("====================");
            out = out + 1;

 

             
            break;
          }
        }
      }
    }
  }


  ss.listen();
  while(ss.available())
  {
    char c=ss.read();
    if(gps.encode(c))
    {
      newData=true;
      gps.f_get_position(&flat,&flon);
      gps.crack_datetime(&year,&month,&day,&hour,&minute,&second);
      hour=hour+9;           //hour +9 for GMT+9 time zone
    }
  }

 

  if (newData)
  {
    lcd.clear();
    if (state == 0)           //display latitude and longitude
    {
      lcd.setCursor(0, 0);
      lcd.print("Lat:");
      lcd.setCursor(6, 0);
      lcd.print(flat);

      lcd.setCursor(0, 1);
      lcd.print("Lon:");
      lcd.setCursor(6, 1);
      lcd.print(flon);


      delay(2000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Loca:");
      lcd.setCursor(0, 1);

      if (3737.40952 <= flat <= 3741.5215 && 12702.87736 <= flon <= 12705.64192) {
        lcd.print("NOWON");
      }

      else if (3737.6053 <= flat <= 3740.02234 && 12658.9464 <= flon <= 12700.87978) {
        lcd.print("GANGBUK");
      }

 

      delay(2000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Date:");
      lcd.setCursor(6, 0);
      lcd.print(day);
      lcd.print("/");
      lcd.print(month);
      lcd.print("/");
      lcd.print(year);

      lcd.setCursor(0, 1);
      lcd.print("Time:");
      lcd.setCursor(6, 1);
      lcd.print(hour);
      lcd.print(":");
      lcd.print(minute);
      lcd.print(":");
      lcd.print(second);

      delay(2000);
    }
    newData = false;
    delay(10);
  }

Serial.println("finish");
}

 

void displayMACAddress(void)
{
  uint8_t macAddress[6];

  if(!cc3000.getMacAddress(macAddress))
  {
    Serial.println(F("Unable to retrieve MAC Address!\r\n"));
  }
 
  else
  {
    Serial.print(F("MAC Address : "));
    cc3000.printHex((byte*)&macAddress, 6);
  }
}

 

bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }

  else
  {
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

 

이전글   |    죄송한데 RF에 대해서 다시한번만 물어볼께요 ㅎㅎ...... 2016-08-09
다음글   |    아두이노 블루투스 센서 데이터 읽기 2016-08-11