How To Add a New Sensor In Node Red

Add a new sensor In NCD node-red lib

Recently I started to learn a bit about node-red. Its a graphical based drag-and-drop kind of software platform. Which makes it ideal for users like me who work mostly in hardware and have very little knowledge of the core software. 

In this guide we will learn how we can add a new sensor in existing ncd node-red lib.  ncd is adding new wireless sensors in their IoT enterprise portfolio and it’s possible that a lot of sensors are not supported in node-red yet. 

Adding a new sensor in node-red is super easy. It can be done by just following two steps.

Note : This guide is for people who are lazy like me and don’t want to put too much time in…..

Adding the sensor in backend

You can find a WirelssGateway.js file in your ~/.nodered/node_modules/ncd-red-wireless/lib folder. 

In this file we will add our new sensor data breakout. This file already contains most of the sensor and to add a new sensor you can look for the sensor which has a similar kind of data pattern as the sensor you are planning to add. 

The current WirelessGateway.js can be found here .

In this guide we will add a 2 channel 4-20mA device to node-red. In order to do that we will need two things

  1. Sensor type ( in this case its 52) 
  2. Look for Similar kind of Device in existing WirelessGateway.js file ( in this case there is 2 channel 4-20mA device ( device type 45) which is already supported) 

Once we have the above two things worked out we can work on modifying the file.

Step one —

  1. Copy the device type 45 and paste it 

'45': {
            name: '16-Bit 1-Channel 4-20mA',
            parse: (d) => {
                var adc = signInt(d.slice(0, 2).reduce(msbLsb));
                return {
                    adc: adc,
                    mA: adc * 20 / 28842,
                    byteOne: d[0],
                    byteTwo: d[1]
                };
            }
        },
        

      2. Edit the pasted part based on device data structure ( check out device product manual)

'52': {
            name: '16-Bit 2-Channel 4-20mA',
            parse: (d) => {
                var adc1 = signInt(d.slice(0, 2).reduce(msbLsb));
                var adc2 = signInt(d.slice(2, 4).reduce(msbLsb));
                return {
                    adc1: adc1,
                    adc2: adc2,
                    mA1: adc1 * 0.0006863,
                    mA2: adc2 * 0.0006863,
                    byteOne: d[0],
                    byteTwo: d[1],
                    byteTwo: d[2],
                    byteTwo: d[3]
                };
            }
        },

Adding the sensor in drop-down menu

Once we’re through adding the sensor in backend, the node-red will understand what kind of sensor is it and and how to parse its data. but the front add still has no idea about this new sensor. 

In order to add in sensor in drop-down section so that we can select the sensor from node-red home page we will need to modify wireless.html file

The current wireless.html file can be found here

The Wireless.html can be found in your ~/.nodered/node_modules/ncd-red-wireless folder. 

We will need to edit this file at two locations

Step One — 

  1. Add the 2 Channel 4-20mA Receiver in Sensor Type Section

Before adding it will look like this 

        label: function() {
            if(this.name) return this.name;
            if(this.addr) return this.addr.split(":").slice(4).join(':');
            if(this.sensor_type){
                var types = {
                    "1": "Temperature/Humidity",
                    "2": "2ch Push Notification/Voltage Detection",
                    "3": "ADC/4-20 mA/DC Voltage",
                    "4": "Thermocouple",
                    "5": "Gyro/Magneto/Temperature",
                    "6": "Temperature/Barometeric Pressure",
                    "7": "Impact Detection",
                    "8": "Vibration",
                    "9": "Proximity and Light",
                    "10": "Light",
                    "13": "Current Monitor",
                    "18": "Pulse Frequency",
                    "24": "Activity Detection",
                    "25": "Asset Monitor",
                    "34": "Tank Level Sensor",
                    "35": "1 Channel Counter",
                    "36": "2 Channel Counter",
                    "37": "7 Channel Push Notification",
                    "10006": "4-Channel Relay",
                    "10006": "4-Channel 4-20 mA Input",
                    "10007": "4-Channel Current Monitor",
                    "10012": "2-Relay + 2-Input",
                }
                return types[this.sensor_type];
            }
            return 'Wireless Device';
        },

after adding the sensor type it will look like this 

        label: function() {
            if(this.name) return this.name;
            if(this.addr) return this.addr.split(":").slice(4).join(':');
            if(this.sensor_type){
                var types = {
                    "1": "Temperature/Humidity",
                    "2": "2ch Push Notification/Voltage Detection",
                    "3": "ADC/4-20 mA/DC Voltage",
                    "4": "Thermocouple",
                    "5": "Gyro/Magneto/Temperature",
                    "6": "Temperature/Barometeric Pressure",
                    "7": "Impact Detection",
                    "8": "Vibration",
                    "9": "Proximity and Light",
                    "10": "Light",
                    "13": "Current Monitor",
                    "18": "Pulse Frequency",
                    "24": "Activity Detection",
                    "25": "Asset Monitor",
                    "34": "Tank Level Sensor",
                    "35": "1 Channel Counter",
                    "36": "2 Channel Counter",
                    "37": "7 Channel Push Notification",
                    "52": "2 Channel 4-20mA Receiver",
                    "10006": "4-Channel Relay",
                    "10006": "4-Channel 4-20 mA Input",
                    "10007": "4-Channel Current Monitor",
                    "10012": "2-Relay + 2-Input",
                }
                return types[this.sensor_type];
            }
            return 'Wireless Device';
        },

Step two — 

     2. Add the 2 Channel 4-20mA Receiver in Sensor Type In Drop-Down

Before adding it will look like this

    </div>
    <div class="form-row">
        <label for="node-input-sensor_type"><i class="icon-tag"></i> Sensor Type</label>
        <select id="node-input-sensor_type">
            <option value="1">Temperature and Humidity</option>
            <option value="2">2ch Push Notification/Voltage Detection</option>
            <option value="3">ADC/4-20 mA/DC Voltage</option>
            <option value="4">Thermocouple</option>
            <option value="5">Accel, Gyro, Magneto and Temperature</option>
            <option value="6">Temperature and Barometeric Pressure</option>
            <option value="7">Impact Detection</option>
            <option value="8">Vibration</option>
            <option value="9">Proximity</option>
            <option value="10">Light</option>
            <option value="12">3-channel Thermocouple</option>
            <option value="13">Current Monitor</option>
            <option value="14">10-Bit 1-Channel 4-20mA</option>
            <option value="15">10-Bit 1-Channel ADC</option>
            <option value="16">Soil Moisture Sensor</option>
            <option value="17">24-Bit AC Voltage Monitor</option>
            <option value="18">Pulse/Frequency</option>
            <option value="19">2-channel 24-bit Current Monitor</option>
            <option value="20">Precision Pressure & Temperature (pA)</option>
            <option value="21">AMS Pressure & Temperature</option>
            <option value="23">2-channel Thermocouple</option>
            <option value="24">Activity Detection</option>
            <option value="25">Asset Monitor</option>
            <option value="26">Pressure & Temperature (PSI)</option>
            <option value="27">Environmental</option>
            <option value="28">3-Channel Current Monitor</option>
            <option value="29">Linear Displacement Sensor</option>
            <option value="30">Structural Monitoring Sensor</option>
            <option value="32">Particulate Matter Sensor</option>
            <option value="35">One Channel Counter</option>
            <option value="36">Two Channel Counter</option>
            <option value="37">7 Channel Push Notification</option>
            <option value="39">RTD Temperature Sensor</option>
            <option value="40">Vibration w/Time Domain (partial support)</option>
            <option value="41">RPM</option>
            <option value="50">Predictive Maintenance Sensor</option>
            <option value="10000">4-Channel Relay</option>
            <option value="10006">4-Channel 4-20 mA Input</option>
            <option value="10007">4-Channel Current Monitor</option>
            <option value="10012">2-Relay + 2-Input</option>
        </select>
    </div>

after adding the sensor type it will look like this 

    </div>
    <div class="form-row">
        <label for="node-input-sensor_type"><i class="icon-tag"></i> Sensor Type</label>
        <select id="node-input-sensor_type">
            <option value="1">Temperature and Humidity</option>
            <option value="2">2ch Push Notification/Voltage Detection</option>
            <option value="3">ADC/4-20 mA/DC Voltage</option>
            <option value="4">Thermocouple</option>
            <option value="5">Accel, Gyro, Magneto and Temperature</option>
            <option value="6">Temperature and Barometeric Pressure</option>
            <option value="7">Impact Detection</option>
            <option value="8">Vibration</option>
            <option value="9">Proximity</option>
            <option value="10">Light</option>
            <option value="12">3-channel Thermocouple</option>
            <option value="13">Current Monitor</option>
            <option value="14">10-Bit 1-Channel 4-20mA</option>
            <option value="15">10-Bit 1-Channel ADC</option>
            <option value="16">Soil Moisture Sensor</option>
            <option value="17">24-Bit AC Voltage Monitor</option>
            <option value="18">Pulse/Frequency</option>
            <option value="19">2-channel 24-bit Current Monitor</option>
            <option value="20">Precision Pressure & Temperature (pA)</option>
            <option value="21">AMS Pressure & Temperature</option>
            <option value="23">2-channel Thermocouple</option>
            <option value="24">Activity Detection</option>
            <option value="25">Asset Monitor</option>
            <option value="26">Pressure & Temperature (PSI)</option>
            <option value="27">Environmental</option>
            <option value="28">3-Channel Current Monitor</option>
            <option value="29">Linear Displacement Sensor</option>
            <option value="30">Structural Monitoring Sensor</option>
            <option value="32">Particulate Matter Sensor</option>
            <option value="35">One Channel Counter</option>
            <option value="36">Two Channel Counter</option>
            <option value="37">7 Channel Push Notification</option>
            <option value="39">RTD Temperature Sensor</option>
            <option value="40">Vibration w/Time Domain (partial support)</option>
            <option value="41">RPM</option>
            <option value="50">Predictive Maintenance Sensor</option>
            <option value="52">2 Channel 4-20mA Receiver</option>
            <option value="10000">4-Channel Relay</option>
            <option value="10006">4-Channel 4-20 mA Input</option>
            <option value="10007">4-Channel Current Monitor</option>
            <option value="10012">2-Relay + 2-Input</option>
        </select>
    </div>

Now restart the node-red and you should see the newly added sensor in node-red drop-down menu.