Using OrangeBoard WiFi – Getting weather data in OpenWeatherMap API

details

OrangeBoard WiFi is equipped with a WiFi module in the existing OrangeBoard, so you can use the function of Arduino and WiFi in one board.

Arduino combines a WiFi module that can take a lot of data from the web, so users can see and use it a step further than the traditional Arduino.

 

 

 

summary

In this article, let’s run an example that uses the API to fetch weather information from a WiFi shield.

API is an abbreviation of Application Programming Interface. It refers to a set of functions or subroutines that a program or application calls to the operating system for information processing.

 

WebServer provides an easy-to-use interface to the client through the API, and the client can easily access the desired information by importing and using the API.

 

 

 

 

 

Many Web sites provide these APIs, and there are hundreds of thousands of API types.

For example, if you want to get traffic information for a specific area, you can get traffic information by simply getting the traffic information API and entering the information.

 

 

 

 

The OpenWaetherMap to run this time is also a site that provides the weather information API. If you bring the API, you can also get weather information on WiFi in Arduino.

Let’s run the example and get the weather information.

 

 

 

 

 

Get API key from OpenWeatherMap

 

 

First go to  http://openweathermap.org/ and if you do not have an ID, sign up and sign in.

 

 

 

 

Once you enter the API keys tab, you can see the unique API key values available to you.

The API key value is essential information for getting information from the API.

 

 

 

 

Uploading the Arduino code

 

 

Go back to Arduino and copy the code below and upload it to OrangeBoard WiFi.

 

#include <SPI.h>
#include <WizFi250.h>

int getInt(String input);

#define VARID      "APIKEY"

char ssid[] = "SSID";       // your network SSID (name)
char pass[] = "PASS";        // 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=Seoul,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;
}

 

Result screen

The result screen shows the current temperature value of Seoul periodically as shown below.

 

 

 

 

What if you want to change your location?!

If you want to change the region, please modify the code below.

The code is basically Seoul, but if you change the region, you can print out the temperature values in other regions.

 

 

 

 

What if you want to extract other information about the weather ?!

Currently, only the temperature values are taken from the code and output.

In order to output other information, it is necessary to analyze the data delivered by the API first.

 

If you request data as shown in the picture below, it will be delivered in XML format and we will extract only the desired information through parsing in code.

In case of temperature, “temp” is shown as absolute temperature after 306.15.

 

 

 

 

The code below extracts the temperature value from the XML data requested by the API.

When reading the data along the way, a value of “temp”: is read, and then the value is read and stored as temperature.

 

 

 

 

If you want to import other data, you can read other data if you parse it by putting pressure or humidity instead of temp.

Of course, the temperature was -273 because it was received as the absolute temperature, but other data should be adjusted to the data type.

 

 

Dated :

August 2016

 

Original & Source code :

http://kocoafab.cc/tutorial/view/654

OrangeBoard WiFi is equipped with a WiFi module in the existing OrangeBoard, so you can use the function of Arduino and WiFi in one board.

Arduino combines a WiFi module that can take a lot of data from the web, so users can see and use it a step further than the traditional Arduino.

 

 

 

summary

In this article, let’s run an example that uses the API to fetch weather information from a WiFi shield.

API is an abbreviation of Application Programming Interface. It refers to a set of functions or subroutines that a program or application calls to the operating system for information processing.

 

WebServer provides an easy-to-use interface to the client through the API, and the client can easily access the desired information by importing and using the API.

 

 

 

 

 

Many Web sites provide these APIs, and there are hundreds of thousands of API types.

For example, if you want to get traffic information for a specific area, you can get traffic information by simply getting the traffic information API and entering the information.

 

 

 

 

The OpenWaetherMap to run this time is also a site that provides the weather information API. If you bring the API, you can also get weather information on WiFi in Arduino.

Let’s run the example and get the weather information.

 

 

 

 

 

Get API key from OpenWeatherMap

 

 

First go to  http://openweathermap.org/ and if you do not have an ID, sign up and sign in.

 

 

 

 

Once you enter the API keys tab, you can see the unique API key values available to you.

The API key value is essential information for getting information from the API.

 

 

 

 

Uploading the Arduino code

 

 

Go back to Arduino and copy the code below and upload it to OrangeBoard WiFi.

 

#include <SPI.h>
#include <WizFi250.h>

int getInt(String input);

#define VARID      "APIKEY"

char ssid[] = "SSID";       // your network SSID (name)
char pass[] = "PASS";        // 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=Seoul,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;
}

 

Result screen

The result screen shows the current temperature value of Seoul periodically as shown below.

 

 

 

 

What if you want to change your location?!

If you want to change the region, please modify the code below.

The code is basically Seoul, but if you change the region, you can print out the temperature values in other regions.

 

 

 

 

What if you want to extract other information about the weather ?!

Currently, only the temperature values are taken from the code and output.

In order to output other information, it is necessary to analyze the data delivered by the API first.

 

If you request data as shown in the picture below, it will be delivered in XML format and we will extract only the desired information through parsing in code.

In case of temperature, “temp” is shown as absolute temperature after 306.15.

 

 

 

 

The code below extracts the temperature value from the XML data requested by the API.

When reading the data along the way, a value of “temp”: is read, and then the value is read and stored as temperature.

 

 

 

 

If you want to import other data, you can read other data if you parse it by putting pressure or humidity instead of temp.

Of course, the temperature was -273 because it was received as the absolute temperature, but other data should be adjusted to the data type.

 

 

Dated :

August 2016

 

Original & Source code :

http://kocoafab.cc/tutorial/view/654

COMMENTS

Please Login to comment
  Subscribe  
Notify of