00:03
Traditional control systems have been disconnected from real-world data beyond
00:08
the factory floor, not because that information was not wanted, but because
00:12
it was hard to connect cloud services to I/O control systems. But with the Internet
00:17
of Things all of that is changing. Now developers have API's to restfully
00:22
access databases and cloud analytics, even earthquake reports and weather
00:26
forecasts among many other data sources. What's more is that all of these
00:31
different endpoints can be connected easily with Node-RED so all this cloud
00:35
data is more accessible than ever. In this workshop I'll show you how to use
00:39
Node-RED to request weather data from the Weather
00:42
Underground API then parse, format, and send it to a SNAP PAC controller where
00:47
it can be used to make control decisions. With Node-RED you can quickly & easily
00:51
connect your physical I/O control systems to real live cloud data.
01:01
My instance of Node-RED is running on a groov AR1 box.
01:04
It's an industrially hardened IoT appliance that is running groov as well
01:08
as Node-RED. The end point from my data is a SNAP PAC Learning Center. It's
01:12
basically a SNAP PAC system with a controller, several IO, modules and a rack.
01:17
We use it for training here at Opto 22. This front panel simulates some typical
01:22
I/O signals like a meter, temperature sensor, potentiometer, LEDs, switches, and a
01:29
buzzer. The controller is running a convenient store strategy that's loaded
01:33
into its memory. My goal is to have the convenience store outside light
01:38
controlled by the sunrise and sunset times from the Weather Underground
01:41
astronomy API object. I'll get started by logging into Node-RED on my groov box.
01:49
The groov AR1 uses both encryption and authentication. The first step is to
01:57
get the data down from Weather Underground. So to get the flow started
02:00
I'll drag in an inject node and go down to the function section to get myself an
02:04
HTTP request node. I'll configure this to get the API object from the URL HTTP
02:16
http://api.wunderground.com/api/ my API key
02:24
/astronomy to get the Sun and Moon rise and set times and finally /q/
02:32
public weather station and my station ID and end it with .JSON. You'll need to
02:39
get your own Weather Underground API key by signing into the developer page on
02:43
WeatherUnderground.
02:49
I'll have this return a parsed JSON object so that it's easier to reference
02:54
the fields that I need. Before I package this data up and send it to my PAC
03:00
controller I'll scroll back up to the output section and drag in a debug node
03:04
to have a look at the object that's outputted. Wire the flow together and
03:11
edit the debug node to output the complete message object. Now I can deploy
03:19
and inject the flow to see the outputted object. Here I can expand payload and Sun
03:28
phase. To see sunrise and sunset have both hours and minutes. To get the path
03:34
to this object I'll use the first button in the debug window to copy this path to
03:38
my clipboard. Now I can format it for Node-RED using a function node. I'll
03:46
scroll down to the output section and drag one on to the wire.
03:53
Right now Node-RED has four separate fields so to make the comparison easier
03:58
in the controller I'll build a string that has our hour :
04:01
minute minute format so that the string length is always 5 characters. To do that
04:07
I'll paste the path from a clipboard, add message to the front, and use this to
04:14
reference the fields that I need. Now I can add the colon character and the
04:24
sunrise minutes. Also since I can see that the hour can be a single-digit
04:33
number I will want to make sure that everything has a leading 0. So instead of
04:37
6:30 I get 06:30 for a consistent string length of 5 characters. To do that I'll
04:44
append a zero to the front of my fields
04:50
and then take those entire strings and use the function 'slice' with the
05:00
parameter -2 to only get two digits. Now that I'm confident that I will always
05:09
have a string length of 5 characters I can save this new string as a variable 'srise'.
05:15
To get sunset I will copy this string, duplicate it and change the field
05:24
sunrise to be sunset and save this as a new variable 'sset'.
05:35
To get this data out of the function node I'll return a new object using the
05:39
curly braces and hand in the fields sunrise to hold s rise and sunset to
05:48
hold s set.
05:54
Now that I'm done with this function node I can get this data into my
05:57
SNAP PAC controller. To do that I'll go up to the Opto section and drag in a
06:02
SNAP PAC write node. I'll edit the first node and select my Learning Center
06:10
controller. it's already set up to be a restful server with an API key ID and
06:16
value set. You can find out how to set these up for your controller at
06:21
developer.opto22.com. With the controller selected I'll enter the
06:26
string variable tag that I need to enter called sunrise. This is already set up on
06:32
the controller. The value that I'm writing to this tag is held in message
06:36
dot sunrise. I'll edit the next node to use the same controller, also write to
06:44
the string variable ,only this one is going to be sunset and it takes the
06:48
variable message dot sunset. Now I can hit done and wire these two nodes to the
06:55
output of the function node. I'll keep the debug node in place so I can see the
07:02
data appear, and, modify the timestamp node to inject at a specific time. At say
07:08
noon every day.
07:12
Now I can hit done, deploy and every day at noon this data will be refreshed in
07:17
the controller and I'm free to do anything I want with it. Let's head over
07:21
to PAC Control and see an example I set up earlier. This flow takes the real time
07:27
from the PAC Controller and assigns it to a string and then compares that
07:31
string to the sunrise and sunset that Node-RED hands in. And, determines whether
07:35
or not it's day. If it is day the outside light needs to be turned off, otherwise,
07:41
the light needs to be turned on since it must be nighttime. After an instruction
07:45
is made I delay five minutes before checking to see if the light needs to be
07:48
toggled again. The actual tags that this is held in is sunrise and sunset and the
07:56
I/O point that this data controls is the outside light. Here you can see the
08:02
outside light is on and neither the sunset nor sunrise times are currently
08:06
in because it's not noon and they haven't been automatically injected. To
08:11
give myself some initial data I'll go over to a Node-RED and manually inject
08:14
the flow and see the data appear on the right hand side. Now when I switch back
08:22
to PAC Control I can see that sunrise and sunset have been updated and the
08:28
outside light has automatically been turned off since it's daytime. So there
08:34
you go I've used Node-RED running on this AR1 to make a restful api call
08:39
to a cloud service and use the return data to drive a control system. This is
08:44
just one example to show you how you can link api's to your physical I/O. To find
08:49
out more check out workshops.opto22.com. thanks for watching