irina kim
Published

Using fine dust data obtained from open APIs

Let's use the fine dust information provided through openapi.

BeginnerProtip97
Using fine dust data obtained from open APIs

Things used in this project

Hardware components

W5100S-EVB-Pico
WIZnet W5100S-EVB-Pico
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

w5100s-evb-pico

Code

webclientRepeating_api

Arduino
#include <SPI.h>
#include <Ethernet.h>

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// initialize the library instance:
EthernetClient client;
boolean IsComplete = false;

String line="";
char server[] = "apis.data.go.kr";  // also change the Host line in httpRequest()
//IPAddress server(64,131,82,241);

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

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet
  Ethernet.init(17);  // WIZnet W5100S-EVB-Pico

  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
    Serial.print("My IP address: ");
    Serial.println(Ethernet.localIP());
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    line +=c;
    if(c=='\n'&& IsComplete==true){
      data_parsing();
      IsComplete=false;
  //  Serial.write(c);
    }
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
    IsComplete = true;
    
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // 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("27.101.215.194", 80)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
    client.println("GET /B552584/ArpltnInforInqireSvc/getMsrstnAcctoRltmMesureDnsty?stationName=%EA%B0%95%EB%8F%99%EA%B5%AC&dataTerm=month&pageNo=1&numOfRows=10&ServiceKey=<인증키>&ver=1.3");
   // client.println("Host: www.arduino.cc");
    //client.println("User-Agent: arduino-ethernet");
    //client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}
void data_parsing() {
// date
  String date;
  int dataTime_start= line.indexOf(F("<dataTime")); 
  int dataTime_end= line.indexOf(F("</dataTime>"));  
  date = line.substring(dataTime_start + 10, dataTime_end); 
  Serial.print(F("date: ")); Serial.println(date);
 //PM10 
  String pm10;  
  int pm10Value_start= line.indexOf(F("<pm10Value>")); 
  int pm10Value_end= line.indexOf(F("</pm10Value>"));  
  pm10 = line.substring(pm10Value_start + 11, pm10Value_end); 
  Serial.print(F("pm10: ")); Serial.println(pm10);
 
//PM2.5
  String pm25;  
  int pm25Value_start= line.indexOf(F("<pm25Value>"));  
  int pm25Value_end= line.indexOf(F("</pm25Value>"));  
  pm25 = line.substring(pm25Value_start + 11, pm25Value_end); 
  Serial.print(F("pm25: ")); Serial.println(pm25);

}

Credits

irina kim
12 projects • 25 followers
Contact

Comments

Please log in or sign up to comment.