Tuesday, March 26, 2019

Other Inputs

The beauty of Node-Red is it's flexibility. If you have looked on the list of nodes, you may have noticed there are several inputs besides MQTT. Some of the inputs include GPIO, TCP, UDP and Web sockets. The GPIO inputs are the pins on the RaspberryPI. Sockets need an external server.

In previous posts, I mentioned using a DS18B20 temperature sensor. This sensor works great, but has a limitation in where I put it. The garage node I connected it to is physically too close to the house, so the temperature it reads is about 15-20 degrees warmer than it really is. The house is radiating heat, and the sensor is picking up the heat.

To get around this, I could have run a longer wire and put the sensor out in the yard farther. I could have built a temperature node, and mounted it in the yard. Or I could get the input from another source. Running wires farther out in the yard becomes a challenge since people walk in the yard, burying the cable would be possible, but having the sensor close to the ground will measure the radiated ground temperature instead of the air temperature. Getting power out to a remote sensor is a problem, wires would be similar to the remote sensor, solar would be the best, but that adds complexity.

I chose to get the temperature from an external source. I am using openweathermap. There is a node red openweathermap node available. Install the node like the previous post, using the top right hamburger menu, select manage palette, install tab. Enter openweathermap in the search, and press install. A new section will be created in the left node selector section called "weather".

Follow the instructions in the openweathermap Usage section. To use the node, the openweathermap people want to control the volume of queries made against their servers, so they require users to get a key. The normal node-red usage pattern is pretty light against their servers, so it seems they tolerate this volume of use. Follow the directions on the appid page, and check your email for the appid needed.

Once you have your appid, start a new flow. The first node should be openweathermap in node (the one with only a connector on the right). Drag the node to your flow. Double click on the new node, to get to the edit dialog. Enter your appid in the top box, set your node to get the "Current Weather For" and select coordinates for location. (I couldn't get the city, country to work for my location). Give your location a name. Press done.

Enter a debug node next, and connect the two nodes. Press deploy. In the debug tab on the right, the payload will display. The current weather is pretty detailed, including temperature, cloud cover, and other items. The payload looks like:
{ weather: "Clouds",
   detail: "overcast clouds",
   icon: "04n",
   tempk: 281.56,
   tempc: 8.4,
   temp_maxc: 9.4,
   temp_minc: 7.7,
   humidity: 57,
   pressure: 1023,
   maxtemp: 282.59,
   mintemp: 280.93,
   windspeed: 4.1,
   winddirection: 200,
   location: "Eden Prairie",
   sunrise: 1553601964,
   sunset: 1553646776,
   clouds: 90,
  description: "The weather in Eden Prairie at coordinates: 44.84, -93.47 is Clouds (overcast clouds)."}

The openweathermap page has details of things like the icon. The temperature in Kelvin isn't useful to most people, but there are simple conversions to Fahrenheit. Some people can work in millibars but most people, in the US at least, think in terms of inches of mercury for the barometric pressure. 

For me, I just needed the temperature. So I created a function that can strip out just the temperature. The Javascript for this function is:

context.tempC = msg.payload.tempc;
msg.payload = context.tempC;
return msg;

The function reads the messages payload.tempc, and creates a new message that only contains that in the payload. The function can be modified to return any or all of the payload desired. If the dashboard was to show clouds and summary that can be done as well. 

I pass that temperature to a copy of the degCtoF that I wrote for the garage node already, and then I send that value out to the graph widget in the garage tab of the UI. The flow looks like:


This will show the nice graph of the last hour or so, without any database or special storage. 

Many external sites work on the token userid concept. It is related to your account on the server side. Since you aren't sending your name, login or anything special it keeps prying eyes out as that value goes across the internet. Using SSL should ensure the value isn't easily seen either. 

Next time, I'll show another external service, allowing voice control of node red. 

Come on back.

No comments:

Post a Comment