Project 22 - Weather Station

<< Click to Display Table of Contents >>

Navigation:  Projects >

Project 22 - Weather Station

This project creates an IOT weather station using an ESP8266 WiFi module attached to CleO. The CleO bridging libraries are used to allow the Arduino to communicate with ESP8266 over UART. Weather information is polled from http://openweathermap.org/. The Arduino parses the HTTP responses from the server and constructs the GUI. The Wifi3 Click is connected to the Click 2 Interface of the CleO50 module.

 

Component(s)

 

This project uses the following component(s) -

 

WiFi3 Click

 

Note: ESP8266 Firmware on MikroE WiFi3 click needs to be upgraded. Please refer to Known Issues and Limitation topic for further details.

 

 

Hardware Setup

 

The picture below shows the WiFi Click is connected to MikroE Click 2 interface.

 

 

 

Code

 

The setup routine initializes the CleO50 display and the default GUI for the weather station. It then uses the CleO bridging library to reset and configure the EXP8266 WiFi module. Finally it connects to a local WiFi Access Point. Here is code snippet -

 

void setup()

{

Serial.begin(230400);

ESP.BUFFER = BUFFER;

 

/* Initialize CleO and build the default UI */

 CleO.begin();

 CleO.DisplayRotate(2, 0);

 init_ui();

 

/* Open GPIO handles for the RESET and ENABLE pins on the ESP8266 module */

h_esp_enable = CleO.OpenGPIO(CLICK2_CS, Bridge::Output, Bridge::pad_pull_none);

h_esp_reset = CleO.OpenGPIO(CLICK2_RST, Bridge::Output, Bridge::pad_pull_none);

 

/* Set ENABLE = 1 and toggle the RESET line */

 CleO.WriteGPIO(h_esp_enable, ((uint32_t)0x01 << CLICK2_CS));

 CleO.WriteGPIO(h_esp_reset, (uint32_t)0);

 delay(200);

 CleO.WriteGPIO(h_esp_reset, ((uint32_t)0x01 << CLICK2_RST));

 delay(500);

 h_esp_comm = (uint8_t)CleO.OpenUART(Bridge::CleO50_click_2, Bridge::b115200, ESP_READ_TIMEOUT);

 

  WiFiStatus = Bad;

  CurrentAPI = APICurrentWeather;

  IsCelsius = true;

 

/* Load fixed icons that the UI always needs */

 char spf_buffer[32];

 strcpy_P(spf_buffer, ICON_Weather);

 im_t = CleO.LoadIcon(spf_buffer, ICON_THERMOMETER_50_PNG);

 strcpy_P(spf_buffer, ICON_Humidity);

 im_h = CleO.LoadIcon(spf_buffer, ICON_LOCATION_ON);

 strcpy_P(spf_buffer, ICON_Weather);

 im_w = CleO.LoadIcon(spf_buffer, ICON_WIND_PNG);

 strcpy_P(spf_buffer, ICON_Weather);

 im_r = CleO.LoadIcon(spf_buffer, ICON_UMBRELLA_PNG);

 strcpy_P(spf_buffer, ICON_REFSH);

 im_refresh = CleO.LoadIcon(spf_buffer, ICON_REFRESH);

}

 

The loop routine periodically polls the Open Weather Map server and parses it's response in real-time. The parsed data is cached in Arduino RAM, and the GUI is updated to reflect the latest information. URIs for current weather, today's forecast and the daily forecast are:

 

http://api.openweathermap.org/data/2.5/weather?units=metric&q=

http://api.openweathermap.org/data/2.5/forecast?units=metric&cnt=5&q=

http://api.openweathermap.org/data/2.5/forecast/daily?units=metric&cnt=6&q=

 

void loop()

{

 

/* Get the current weather at a given location */

if (poll.IsExpired(POLL_INTERVAL)) {

 

   /* Open a TCP socket at port 80 for HTTP */

     ESP.open_socket();

 

   /* Send a HTTP GET request */

     ESP.send_http_request("http://api.openweathermap.org/data/2.5/weather?q=","Singapore");

 

       /* Parse the response in real time as the Arduino SRAM is limited to 2KB */

         while(!parsed){

         length = CleO.ReadAvailableUART(h_esp_comm, buffer);

         parsed = parse_json_response(buffer, length);

       }

 

   /* Close socket */

     ESP.close_socket();

 

  /* Set flag to update UI */

     update_ui = true;

 

  /* Reset polling timer */

     poll.Reset(millis());

 }

 

 /* Monitor touch events */

 if(control()) update_ui = true;

 

/* Build the UI if new data is available or a touch event is detected */

 if(update_ui) {

   CleO.Start();

   build_ui();

   CleO.Show();

 

/* Free the icons loaded into CleO memory */

   free_assets();  

   }

}

 

A typical JSON response returned by the server looks as below:

 

{

 "weather": [

   {

     "id": 802,

     "main": "Clouds",

     "description": "scattered clouds",

     "icon": "03d"

   }

 ],

 "base": "stations",

 "main": {

   "temp": 304.271,

   "pressure": 1019.9,

   "humidity": 82,

   "temp_min": 304.271,

   "temp_max": 304.271,

   "sea_level": 1020.59,

   "grnd_level": 1019.9

 },

 "wind": {

   "speed": 6.76,

   "deg": 282.001

 },

 "clouds": {

   "all": 32

 },

 "dt": 1476338310,

 "id": 1880252,

 "name": "Singapore",

 "cod": 200

}

 

 

Output

 

Weather Station