Extra credit: Weather

Estimated time: 10–15 minutes

You will want to complete the prior sections of this workshop before starting with this one.

In a real world situation, the number of times you'll need to water your plant will be dependent on how frequently it rains. To simulate local weather conditions, go to the source of all rain—the "cloud" (pun absolutely intended)—to round out your distributed system.

Because your nio cloud instance, local instance, and UI are all connected to Pubkeeper, when you create your new weather service on your cloud instance, it will communicate directly to your local instance. As a result, you will be able to see the weather in your plant's location appear on your UI.

Using your nio cloud instance, you will introduce weather to the data streams within the nio system via an API call to a reliable source, the Weather Underground.

Before you get started building your weather service, you'll need to set up a free account with the Weather Underground API. To do so, visit the Weather Underground sign-up page.

Once you have completed the sign-up process, you should be given an API key. If you are having difficulty locating the key, you can go to this link.

Once you have your API key, you are ready to start building your Stream_Weather service with these step-by-step instructions.

Add blocks

Within the Tutorials cloud instance you created in the System Designer workshop, create a service named Stream_Weather.

Add an IdentityIntervalSimulator block:

  1. From the block library, drag an IdentityIntervalSimulator block onto the canvas.
  2. Configure the block as follows:
    1. Name: Sim15Minutes
    2. Interval: 15 minutes
  3. Click accept to save the configuration.

Add a WeatherUndergroundConditions block:

  1. From the available tab in the block library, search for the WeatherUnderground blocks.
  2. Click the block to install it on your cloud instance.
  3. Drag a WeatherUndergroundConditions block onto your canvas.
  4. Configure the block as follows:
    1. Name: WeatherUpdates
    3. City: <any city in the US> i.e. Missoula
    4. State: <any state in the US> i.e. Montana
  5. Click accept to save the block configuration.
  6. Select your instance in the left navigation panel and click the edit button in the toolbar. Inside the edit instance modal, add an environment variable for your API key.
    2. Value: <copy and paste from Weather Underground account>

Add a Modifier block:

  1. Configure the block as follows:
    1. Name: FormatWeatherData
    2. Check the Exclude existing fields checkbox
    3. Add fields to the block by clicking the + fields button three times.
    4. In the first set of boxes enter:
      Name: temp
      Value: {{ str($temp_f) }}
    5. In the second set of boxes enter:
      Name: rain
      Value: {{ 'rain' in $weather.lower() }}
    6. In the third set of boxes enter:
      Name: location
      Value: {{ $display_location['city'] }}, {{ $display_location['state'] }}
  2. Click accept to save the block configuration.

For further explanation of the nio Expression syntax, please reference the docs.

Add a Publisher block:

  1. Configure the block as follows:
    1. Name: PublishWeather
    2. Topic: plant.weather
  2. Click accept to save the block configuration.

Connect blocks

Connect the blocks according to the image at right:

Save and start this new service, and go take a look at your UI. Is it raining?

Most likely, it's not raining in your plant's city. Go back into the Stream_Weather service and click into the WeatherUndergroundConditions block to locate your plant somewhere that is actually raining. Take a look at the weather around the country to see where it is currently raining (hint: check the US Doppler to find a city with rain). If you are able to find a city with some rain in the forecast, enter that location into the WeatherUndergroundConditions block's fields to see the weather change on your UI. Again, if it is raining, you will notice the soil moisture of your plant increasing even when the irrigation is off.


Take a step back to fully understand the design and value proposition of this distributed system.

You just introduced a third component (a cloud instance) to your system–each of these components communicates with the others via the Pubkeeper broker.

The automation service located within the local nio instance (at the "farm") is irrigating the plant when it falls below 50% and stops when it reaches 90%. With the addition of the weather, the local instance will not irrigate if the rain has supplied enough water to keep it within the proper range of 50%–90%. We call that a win-win!

To learn more fundamental concepts of nio, check out the other tutorials!

proceed to nio 101 »

results matching ""

    No results matching ""