Introduction: NODEMCU LUA ESP8266 Get Conditions for Your Urban center (Updated)

To use these programs, you need to have the sjson and rtctime modules installed in your firmware.

You need to create an account with openweathermap.org to become an API central.

I have included 2 programs, 1 demonstrates how to decode various json strings and the other gets weather details from a server.

The weather programme is pretty much a standard Become request from a server and then decoding a json string.

Pace 1: Building the Modules

I notice the Cloud Build Service very easy to utilize at https://nodemcu-build.com/

You need to ensure that you take got the sjson and rtctime modules in your build - sjson has now replaced cjson in the build. When building the firmware, you lot seem to max out at 24 modules, so you have to be selective.

Load your new build into 0x00000.

Note: Load esp_init_data_default.bin at 0x3FC000 otherwise your flash will not work!

More than info here https://nodemcu.readthedocs.io/en/dev/en/flash/

I am using a WeMos D1 mini for this project, although it should work on whatsoever ESP8266.

Stride 2: JSON Files

JSON stands for JavaScript Object Notation, they are strings of text in a special format.

JSON syntax is derived from JavaScript object notation syntax:

  • Data is in name/value pairs
  • Information is separated by commas
  • Curly braces hold objects
  • Square brackets agree arrays

A unproblematic example is myObj = {"name":"John", "historic period":34}

In nodemcu it is declared as:

myObj = '{"name":"John", "age":34}' myObj = [[{"name":"John", "age":34}]] myObj = "{\"name\":\"John\", \"age\":34}"

The important thing to remember is that json strings utilise double quotes, and so we accept to agree them in single quotes, double square brackets or escape the double quote (\"). All 3 lines accept the same meaning in nodemcu.

myObj1 = '{"name":"John", "age":34}' myObj2 = [[{"name":"John", "age":34}]] myObj3 = "{\"proper name\":\"John\", \"age\":34}" print(myObj1) print(myObj2) impress(myObj3)            

result

{"proper name":"John", "age":34} {"name":"John", "age":34} {"name":"John", "historic period":34}            

I have included a file (json.lua) with some examples of JSON strings and how to manipulate them. Take a bit of time to study how they work.

One example from the program is

posn = '{"lon":-2.47,"lat":53.67}' t = sjson.decode(posn) for k,v in pairs(t) do      print(k,v)  finish print("Longitude  = "..t.lon) print("Latitude   = "..t.lat)            

The variable posn holds the json cord.

posn is decoded to variable t

It can be read in two different ways: within a for loop or as t.lon or t.lat.

result

lon	-ii.47 lat	53.67 Longitude  = -2.47 Latitude   = 53.67

Step 3: Create an Account

To get a weather condition update, you demand to set up an account with a weather condition provider and get an API key so we tin can access their site.

You go your API fundamental here: https://habitation.openweathermap.org/api_keys. The API key is 32 characters long and looks something like 74563a891225dce1a5796d1faddc5e41.

Y'all can also go your city code from their website or Cities' ID List. It decrompresses into a json file of over 18Mb and contains cities from all over the globe and includes their Breadth and Longitude positions.

For example in the United kingdom of great britain and northern ireland, metropolis lawmaking for Rishton is 2639310, and Preston is 2639912.

The weather program uses an API key and a urban center code.

Step 4: Programming

The program weather condition.lua gets the weather information from openweathermap.org and displays the contents of the json payload.This is an example of what is returned.

{"coord":{"lon":-2.41,"lat":53.77}, "weather":[{"id":803,"chief":"Clouds","clarification":"cleaved clouds","icon":"04d"}], "base":"stations", "primary":{"temp":xiv.8,"pressure level":996,"humidity":82,"temp_min":fourteen,"temp_max":16}, "visibility":10000, "current of air":{"speed":7.7,"deg":320}, "clouds":{"all":75}, "dt":1498755000, "sys":{"type":1,"id":5098,"message":0.0041,"country":"GB","sunrise":1498707755,"sunset":1498769020}, "id":2639310, "name":"Rishton", "cod":200}

You lot have to make sense of all this data and my program tries to exercise that.

Sample output from weather program.

              Connected to openweathermap.org Weather condition for Rishton GB Long -two.41 Lat 53.77 ID          2639310 Temperature xvi.25 °C Max Temp    17 °C Min Temp    15 °C Pressure    1008 hPa Humidity    82% Visibility  10000 thousand id          500 Main        Rain Description calorie-free pelting Windspeed   7.2 Current of air dir    320 Sunrise     3.43 Sunset      twenty.43 Date        xiv.fifty	xxx/6/2017            

1 thing to notice with the array is that it is indexed before the terminal item forecast.weather condition[1].id.

Step 5: Additional Fabric

Since I wrote this instructable, I have taken the projection farther and tin can now display the weather in a browser.

Key 192.168.0.l into your browser once you have successfully run weather2.lua.

Weather2.lua is an extension of weather.lua.

Step 6: Conclusion

The programs bear witness how to decode a json string and make some sense of information technology.

The program could be taken further and a weather condition icon could be displayed on a LCD screen with the weather information, instead of in a browser.

You don't have to utilize openweathermap.org, equally other API weather providers give similar information. The json cord is just encoded differently, so you have to tweak the lawmaking to match this.

I have included another programme here that works with apixu.com API weather. It is not equally refined as the other 2, but you volition get the general thought. I discover that apixu is an easier site to navigate than openweathermap.org.

Instead of displaying all the individual items with a caption, I accept dumped all the data into 2 arrays (key{} and value{}) and and so displayed their contents. At that place are no arrays in the json string from apixu.com making for simpler decoding.

1 Person Made This Projection!

Recommendations

  • Annihilation Goes Contest

    Anything Goes Contest