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, DarkSky.

Before you get started building your weather service, you'll need to set up a free account for the DarkSky API. To do so, visit the Darksky signup page.

Once you've registered, you'll be shown your Secret Key and a Sample API Call, like https://api.darksky.net/forecast/<secret_key>/<lat>,<long>, and now you are ready to start building your Stream_Weather service with these step-by-step instructions.


Add blocks

Select your WeatherCloud instance in your Virtual Plant system, then 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 Geocode block, this will be used to transform an address or location into Lat/Long coordinates:

  1. From the available tab in the block library, search for the Geocode block.
  2. Click the block to install it on your cloud instance.
  3. Drag a Geocode block onto your canvas.
  4. Configure the block as follows:
    1. Name: Location
    2. Query: For this field you can use a city, postal code, or address. For example, London, England or 90210 are both valid.
  5. Click accept to save the block configuration.


Add an HTTPRequests block, this will be used to poll the DarkSky API using the lat/long coordinates from Location:

  1. From the available tab in the block library, search for the HTTPRequests block.
  2. Click the block to install it on your cloud instance.
  3. Drag an HTTPRequests block onto your canvas.
  4. Configure the block as follows:
    1. Name: WeatherUpdates
    2. HTTP Method: GET
    3. URL Target: https://api.darksky.net/forecast/[[DARKSKY_SECRET_KEY]]/{{$location['latitude']}},{{$location['longitude']}}
    4. Exclude Existing: False (unchecked)

You may notice that the URL Target looks a lot like the Sample API Call shown when you created your account with DarkSky. We're adding some variable parameters (latitude and longitude) to that sample request, and storing the secret key in your instance's environment variables. To access and update your environment variables, follow these steps:

  1. Select your instance, WeatherCloud, from the left nav bar
  2. Click the Edit Icon on the top nav bar
  3. Under environment variables you can see that DARKSKY_SECRET_KEY is created, and you only need to paste your secret key and click Accept. More information on using environment variables is available in the docs.



Add a Modifier block:

  1. Configure the block as follows:
    1. Name: FormatWeatherData
    2. Check the Exclude existing fields checkbox. The outgoing signal will now contain only the fields, or attributes, defined in this block.
    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($currently['temperature']) }}. This will put the current temperature into a new signal attribute called temp.
    5. In the second set of boxes enter:
      Name: rain
      Value: {{ $currently['precipIntensity'] > 0 }}. The rain attribute on the outgoing signal will contain the answer to the question "is the current precipitation intensity greater than zero?" as a True or False value.
    6. In the third set of boxes enter:
      Name: location
      Value: {{ $location['address'] }}. As before this will put your location into a new signal attrivbute: location.
  2. Click accept to save the block configuration.

The DarkSky API returns a lot of past, present and forecast data in a nested key: value structure. The data we are concerned with for this workshop is the current precipitation conditions. This Modifier block is putting the desired values into a more simple signal that excludes all of the additional information that we do not need right now.

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 GeoCode block to relocate 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 GeoCode 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.


Troubleshooting

If your logs contain something like "signal object has no attribute 'location'", make sure that Exclude Existing? is unchecked in your HTTPRequests block.


Review

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 ""