정보나눔

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

OpenWeatherMap API 지역 변경 문의 드립니다.
김은혜 | 2019-06-12

#include
#include

int getInt(String input);

#define VARID      "APIKEY"

char ssid[] = "Eun";       // your network SSID (name)
char pass[] = "dms";        // your network password
int status = WL_IDLE_STATUS;       // the Wifi radio's status

char server[] = "api.openweathermap.org";

unsigned long lastConnectionTime = 0;         // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 1000L; // delay between updates, in milliseconds

boolean readingVal;
boolean getIsConnected = false;
//String valString;
int val, temp;
float tempVal;

String rcvbuf;

// Initialize the Ethernet client object
WiFiClient client;

void httpRequest();
void printWifiStatus();

void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  Serial.println(F("\r\nSerial Init"));

  WiFi.init();

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");

  printWifiStatus();
}

void loop() {
  // if there's incoming data from the net connection send it out the serial port
  // this is for debugging purposes only
  String valString;
  while (client.available()) {

    if ( rcvbuf.endsWith("\"temp\":")) {
      readingVal = true;
      valString = "";
    }

    char c = client.read();

    if ( c != NULL ) {
      if (rcvbuf.length() > 30)
        rcvbuf = "";
      rcvbuf += c;
      //Serial.write(c);
    }

    if (readingVal) {
      if (c != ',' ) {
        valString += c;
        //Serial.write(c);
      }
      else {
        readingVal = false;
        //Serial.println(valString);
        tempVal = valString.toFloat() - 273.0;
        Serial.println(tempVal);
      }
    }
  }
  if (millis() - lastConnectionTime > postingInterval) {
    
    if (getIsConnected) {
      Serial.println(valString);
      Serial.println(F("==================="));
      Serial.print(F("Temperature : "));
      Serial.println(tempVal);
      Serial.println(F("==================="));
    }
    httpRequest();
  }
  rcvbuf = "";
}

// this method makes a HTTP connection to the server
void httpRequest() {
  Serial.println();

  // close any connection before send a new request
  // this will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection
  if (client.connect(server, 80)) {
    Serial.println("Connecting...");

    // send the HTTP PUT request
    client.print("GET /data/2.5/weather?q= Jeonju,KR&appid=");
    client.print(VARID);
    client.println(" HTTP/1.1");
    client.println("Host: api.openweathermap.org");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made
    lastConnectionTime = millis();
    getIsConnected = true;
  }
  else {
    // if you couldn't make a connection
    Serial.println("Connection failed");
    getIsConnected = false;
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

int getInt(String input) {
  char carray[20];
  //Serial.println(input);
  input.toCharArray(carray, sizeof(carray));
  //Serial.println(carray);
  temp = atoi(carray);
  return temp;
}

 

오렌지보드 와이파이 사용했습니다.

 

코코아팹 학습하기에 있는 OpenWeatherMap API 이용하는 

튜토리얼 코드에서 지역이 서울로 설정 되어있는것을 

전주로 바꿨더니 시리얼모니터에 온도 값이 출력되지 않습니다.

 

서울이나 런던으로 했을때는 온도값이 정상적으로 출력되었는데

왜 지방의 온도값은 출력되지 않는건가요?? (전주, 광주 지역이 출력되지 않았습니다.)

이전글   |    아두이노금고제작 키패드와 블루투스사용 도와주세요ㅠㅠ 내일까지입니다..... 2019-06-11
다음글   |    도저히 모르겟네요 2019-06-12