Showing posts with label Node-Red Dashboard. Show all posts
Showing posts with label Node-Red Dashboard. Show all posts

Tuesday, March 24, 2020

MQTT and NodeRed on an airplane?

I've had several posts about using NodeRed for home automation, communicating with various nodes over MQTT. Could I do the same in the airplane? I think I could, and there would be some benefits to it. Certainly less wiring, and maybe more reliable and customizable.


The whole point of this blog originally was to use an Arduino and be able to monitor the engine status. Originally, I was going to use a Mega, and a bunch of shields. The old eco-system caused me to lean that way. Mega on the bottom, a custom shield next, and a Bluetooth shield on top. The world has moved on, and now I can use ESP-32 and 8266's with built in Wifi and Bluetooth. The boards only cost about $10, instead of the $20+ for the Mega and Bluetooth shield.

The sensors can mostly be wired to the ESP-8266 board with SPI (3 wire) communications, through adapter boards. If I need an analog or digital input, it is available on the board as well. There are very inexpensive thermocouple modules available, that would take care of the CHT and EGT measurements. The oil pressure and temperature would be analog inputs if I stick with the stock probes, but there is the Dallas18b20 probe that could be used to measure temperature, but there only one temperature measuring point on the engine. The Dallas 18B20 can be used for OAT measuring.

The oil pressure transducer is off on a flex pipe, with a manifold for to hold the hobbs switch. (theoretically, the oil pressure measurement could be combined in the computer somewhere to enable the hobbs timing measurements). There are nice solid state pressure sensors that may be appropriate for fuel and oil pressure, I need to do additional research.

Volt and Amp measurements can be done using the analog inputs, using the existing shunt for current measurements, or adding another shunt. There are also hall effect sensors that could be used to measure current. Using various current sensors would allow measuring all the important values at once, charge rate, drain rate, etc.

The tach measurement can come off the mag or EI unit. A transistor or optoisolator should work when connected to a pin that accepts interrupts.

Other sensors could be added or deleted anytime using additional nodes. If I want to measure the airflow through the baffles, I could add a node with multiple pitot tubes to see where the air might be leaking.

Then the question becomes, how many nodes should I use? It would be tempting to build one for each sensor, I want to limit the nodes to one function (I've said that before). How gray to I want that idea, one function could be all the thermocouples. Another node for all the oil information (pressure, temperature). Another for the fuel pressure, levels. Convenience may dictate some other arrangement, like thermocouples left side, thermocouples right side, that way the wires don't have to be so long.

Each node will still need 5V or 3V connected. It would be tempting to use the micro-USB connectors on the boards to feed the power. I think, the constant vibration would make the connectors fatigue very quickly, and the power would be falling out. It would probably be best to solder the power to the boards.

A $10 raspberry PI zero could be the NodeRed/MQTT hub. The raspberryPI should be mounted inside the cockpit. I am sure the WiFi signal can get around the firewall on a plastic airplane. Even a metal aircraft should have WiFi from the engine compartment to the cockpit. I'd have to try it out though.

In the cockpit, PLA printed enclosures should work fine. In the engine compartment, there may be problems with PLA or printed enclosures (constant vibration, heat, and rain). 3D printing my own enclosures may be compromised quickly. It may take some experimenting, certainly to make appropriate mounting choices, considering the sensor wiring stresses and heat.

NodeRed should allow nice cockpit displays of the instruments, and allow logging in various formats including CSV. Using the same CSV layout as the COTS instruments (like J.P Instruments) would allow people to analyze engine performance using off the shelf tools (excel, etc). The file could be scp'd from the Arduino, right to a tablet or phone, taken home and analyzed.

I don't know. I see a few worrying things, but nothing that is really scary about doing it. Can you talk me out of it?








Monday, April 8, 2019

Heater Flow

As promised, I am going to present the flow used for the thermostat node. This flow has a nice dashboard, so it can be controlled from my phone, or from the thermostat, or any other flow input.

The flow:



We can start from the center, the "Check Thermostat" function node. This function take inputs from the heater/state, sunroom/thermostat and sunroom/temperature MQTT topics. The heater/state is published by the nodeMCU attached to the heater, and the payload is either "on" or "off" depending on the state of the heater. The sunroom/thermostat and sunroom/temperature topics are from the thermostat nodeMCU from the last post. The sunroom/temperature payload is a number indicating the temperature measured by the DHT-22 sensor converted to degrees Fahrenheit. The sunroom/thermostat payload is a number representing the setting the thermostat was last set to.

 // if temp is less than thermostat turn on heater.
flow.temp = flow.temp || 0.0;
flow.therm = flow.therm || 70.0;
 if (msg.topic === 'sunroom/thermostat') {
  flow.therm = parseFloat(msg.payload);
} else if (msg.topic === 'sunroom/temperature') {
  flow.temp = parseFloat(msg.payload);
} else if (msg.topic === 'heater/state') {
    flow.state = msg.payload;
}
// add a dead band, if on turn off 1 degree late
if (flow.temp < flow.therm) {
    // msg.payload = "on " + flow.therm + " " + flow.temp;
    msg.payload = "on"
} else if ((flow.temp > flow.therm) &&
           (flow.state === 'on')) {
    //msg.payload = "off " + flow.therm + " " + flow.temp;
    msg.payload = "off";
}
return msg;

There is only one thing output from this, and it indicates if this wants the heater "on" or "off". The output of this is determined if the temperature is less than the thermostat (flow.therm) the payload is sent as "on". If the temperature is greater than the the thermostat setting, and the heater is currently on (flow.state from the heater/state topic) then the payload is set to off. If the temperature matches, it does nothing.

Sometimes the sensor is right on the edge 72degrees/73degrees and it jumps back and forth, so the heater turns on and off frequently. Leaving the sensor to not do anything if the temperature matches the thermostat setting allows the heater to have some hysteresis, and take maybe a minute to change states.

The output of the check thermostat goes to the heater/gas topic, listened to by the nodeMCU connected to the heater itself. There is also a debug output and it is handy to have while debugging things. The function code allows additional information in the payload to see what the current temperature and thermometer settings are when changing state, but that will only work with the debug turned on.

The blue boxes are for the dashboard. The current temperature is a gauge_ui element, and is at the top.


The slider under the gauge is the current thermostat. This is a pass through UI element. It will adjust based on messages heard on the "sunroom/thermostat" MQTT topic, and will send values through to the heater thermostat topic.

The settings look like:



There is also a text output that will show the current thermostat setting. The UI is a relative bar, and no other indicators. I typically add the text items so I know what the slider is pointing at.

I have two switches, both pass through. The current heater state is indicated by values coming in on the heater/state MQTT topic. Clicking this button on or off will output the state to the heater/gas MQTT topic turning the heater on or off (manually). The light on/off is similar sending on the heater/light topic and indicating the debug on/off buttons.

There is a filler UI element. Without the filler the UI sometimes randomly wrapped funny.
That is something to play with to get the widgets. to align properly.



A keen eye might notice a connector widget in the middle of the flow that I didn't mention. This is for the voice input. Yes, from my phone, a Google assistant or other device I can control my house by voice. That is for a future post.



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.

Monday, March 18, 2019

More Node-Red


Node-Red is amazingly powerful, and flexible. Maybe in a future post, I set some recipies, including recurring timers, Google Assistant integration and notifications.



Dedicated System Setup


One advantage of using Node-Red in your home is you don't have to worry about security too much. You aren't publishing your daily habits to anyone in the cloud. One disadvantage, you need to give the system all it's smarts. It doesn't mean you can't integrate with the cloud, but it doesn't happen automatically.

Using a RaspberryPI as the dedicated controller will allow the 'PI to be the WiFi hub, DHCP server, MQTT server agent, and provide all the flows without worrying about tying up another machine (home office computer), or having that system walk out the door (laptop).

In my example ESP8266 nodes, I always hard coded the IP address of the server. That works great, since the RaspberryPI is providing all the DHCP and WiFi for the various nodes in the house. If I want to move the system somewhere else, I don't have to worry about reprogramming the nodes server IP address.



The above diagram shows how the home WiFi is separate from the IoT WiFi. To connect to the rest of the internet, a wired connection can be made to the home Cable Mode. Connecting the RaspberryPI to the internet will allow updates and other features to be easier. It doesn't have to be connected to the internet, it is up to you. Since the RaspberryPi connected to the modem will NAT all internet traffic, unless routes are set up in the modem, the RaspberryPI is not visible from the outside network.

For my network, I run the IoT network on WiFi Channel 11 since my home network is on channel 1. Keeping them on separate channels should prevent IoT traffic from being picked up on the home WiFi devices theoretically making things faster. Radio is radio, so if the devices are close enough, they may interfere anyway. (For some reason the Sony PS4 seems to interfere with WiFi devices near it, so if the kids bring the PS4 into the sun room, the IoT devices perform poorly).

My WiFi config file on the RaspberryPI looks like:

pi@raspberrypi:~ $ cat /etc/hostapd/hostapd.conf
driver=nl80211
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
beacon_int=100
auth_algs=1
wpa_key_mgmt=WPA-PSK
ssid=raspi-webgui
channel=11
wpa=2
hw_mode=g
wpa_passphrase=XXXXXXXX
interface=wlan0
#wpa_pairwise=TKIP
rsn_pairwise=CCMP
country_code=US


The dns config file is the default, really no changes. 

Node-Red Dashboard

This will be a gentle introduction, just barely covering what it is capable of. This is really the big win for Node-Red. 

First there you need to install the dashboard nodes. From the Node-Red main window, hamburger menu on the far right. Left click on that to bring up the configuration menu. Select the "Manage Palette". A dialog with two tabs will appear, select the "Install" tab. Type in "dashboard" in the "Filter nodes box" There will be a list of dashboard items available. For now, we only want the default "node-red-dashboard". Select "install" and give it a couple minutes. After the update completes, scroll toward the bottom of the nodes on the left. 

There will be a section of nodes labled "dashboard" with nodes like "button", "slider", "dropdown", etc. These items can be added to a flow, and used to set settings or view values available in the flow. The updated garage flow with simple outputs for the garage state and temperature graph looks like:

The light blue nodes are dashboard nodes. The garage door ones are the current state as text (open/closed) and the temperature is a graph showing the current trend, with degrees in Fahrenheit. To set the node display, use the payload to get the value:


Create a group, as you wish. For my house these nodes are in the Garage, so I created a Garage group. I also have a Sunroom Group and a Basement group. They are all part of the House, as a hidden group. The payload is enclosed in "{{" and "}}" I'll expand on why that later. Alignment can be changed as desired, the top row above is left, center and right aligned. The next is fill, and the bottom right is label over value. The look that this allows is:


The temperature trend for the last 20 minutes or so is shown, getting colder. Both doors are closed. Pretty simple, but a quick look and you can see it. Note the URL. I chose the home network, since my RaspberryPI is plugged into the home network. Node Red is running on port 1880, the default. But after that there is a /ui, so I don't see the whole Node Red flow builder, but just the dashboard. 

How about if you want to see this on your phone? No problem, that is built in:

Enter the same url (like 192.168.0.35:1880/ui in chrome, safari or whatever browser you prefer. All the dashboard features are there. No special apps, or knowledge of programming needed. 

There is so much more that can be done in Node-Red. Start playing, and you should find ideas that I never thought of. More next time.