Friday, November 18, 2016

NodeMCU (ESP8266) as provider of Yahoo Weather

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

When connecting ESP8266 to Arduino, you need a 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 refresh it every yaw.syncPeriodSec seconds. Weather data itself is stored in yahooWeather.lua -> yaw.weather, you will find their 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

The script below registers Serial API Interface providing access to weather. The 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 a 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 the whole API description in serialAPIYahooWeather.lua