Friday, November 18, 2016

NodeMCU (ESP8266) as provider of Yahoo Weather

I've developed small library that can be used to access Yahoo Weather over NodeMCU and than provide it over serial port. This can be connected to anything with serial port with - for example Arduino.  In such case you have simple way of accessing weather for your Arduino projects :)

When connecting ESP8266 to Arduino you will need voltage divider to convert 5v from arduino to 3.3v.


yahooWeather.lua

yahooWeather.lua script provides access to Yahoo weather.
yaw.start() will obtain weather immediately and keep refreshing it every yaw.syncPeriodSec seconds. Weather data itself is stored in yahooWeather.lua -> yaw.weather, you will find there further documentation. 

In the example below you can see how to get wether for Munich and update it periodically:

require "yahooWeather"
require "wlan"

yaw.city = "munich"
yaw.country = "de"   
wlan.setup("free wlan", "12345678")

-- update weather every 17 minutes
yaw.syncPeriodSec = 1020

yaw.responseCallback = function()
    print("Weather for today:", yaw.weather[1].date) 
    print(yaw.weather[1].low, yaw.weather[1].high, yaw.weather[1].text)

    print("Weather for tomorrow:", yaw.weather[2].date) 
    print(yaw.weather[2].low, yaw.weather[2].high, yaw.weather[2].text)
end

-- start weather update timer
yaw.start()

Weather for today:  01 Sep 2016
18  25  Partly Cloudy
   
Weather for tomorrow:   02 Sep 2016
16  25  Partly Cloudy

Serial API

Script below registers Serial API Interface providing access to weather. Weather itself will get updated every 17 minutes.

require "serialAPI"
require "serialAPIYahooWeather"
yaw.city = "munich"
yaw.country = "de"
yaw.syncPeriodSec = 1020 -- 17 min
-- setup wlan required by NTP clokc wlan.setup("fred", "1234567890") -- start serial API by enabling gpio and uart sapi.start() -- start yahoo weather with serial API yaw.start()

Now you can execute few simple commands over serial:
# weather description for today
>YF1 text
Rain And Snow

# weather description for tomorrow
>YF2 text
Showers

# not existing command
>YF1 min
ERR:serialAPIYahooWeather.lua:6: attempt to concatenate field '?' (a nil value)

# max temp for tomorrow 
>YF2 low
1

# weather date for today
>YF1 date
09 Nov 2016

You can find full API description in serialAPIYahooWeather.lua