# K-30 CO2 sensor

**URL:** <https://discourse.nodered.org/t/k-30-co2-sensor/3399>\
**Category:** General\
**Created:** [24 September 2018 00:12 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399 "2018-09-24T00:12:48Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [24 September 2018 00:12 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/1 "2018-09-24T00:12:48Z")

</div>

Hi,

I am running this python script for monitoring CO2 level, the code is running perfectly. How i can convert this script into NODE RED. Thank you.

#rpi serial connections  
#Python app to run a K-30 Sensor  
import serial  
import time  
ser = serial.Serial("/dev/ttyAMA0",baudrate =9600,timeout = .5)  
print " AN-137: Raspberry Pi3 to K-30 Via UART\n"  
ser.flushInput()  
time.sleep(1)

while 1:

#for i in range(1,21):

ser.flushInput()  
ser.write("\xFE\x44\x00\x08\x02\x9F\x25")  
time.sleep(.5)  
resp = ser.read(7)  
high = ord(resp[3])  
low = ord(resp[4])  
co2 = (high\*256) + low  
print "i = ",i, " CO2 = " +str(co2)  
time.sleep(.1)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [24 September 2018 06:19 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/2 "2018-09-24T06:19:35Z")

</div>

What have you tried so far?

Certainly you can read from serial, process the input data and output to a Dashboard (web page).

---

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [13 February 2019 03:17 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/3 "2019-02-13T03:17:31Z")

</div>

Good day,  
I am getting this error  
'''serial port /dev/ttyAMA0 error: Error: Error: Permission denied, cannot open /dev/ttyAMA0'''

 ![CO2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/15ff170b2c111a80707a16e3fd48101f3d22cc16.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [13 February 2019 07:07 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/4 "2019-02-13T07:07:42Z")

</div>

OK, so the error is completely clear. The user that is running Node-RED does not have permissions to read from the serial port. This is common on Linux systems where standard users don't have access. You need to add the user to the group.

On my Pi3:

```auto
pi@pi3:~ $ ls -l /dev/ttyAMA0
crw-rw---- 1 root dialout 204, 64 Feb 11 10:14 /dev/ttyAMA0

```

So you need to add the user running Node-RED - `pi` in my case, to the `dialout` group.

---

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [13 February 2019 08:57 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/5 "2019-02-13T08:57:11Z")

</div>

i do manage with that thank you.

But i didn't get the output.

 ![serial](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a3fb13b415dc4ca43254533b358bc5cc4111d87f.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [13 February 2019 09:03 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/6 "2019-02-13T09:03:12Z")

</div>

The status under the serial node may give you an idea as to why.

Also check the Node-RED logs.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [13 February 2019 09:23 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/7 "2019-02-13T09:23:10Z")

</div>

Looks like the device either has different serial settings than you expect or it isn't terminating its output in a meaningful way so that the serial node doesn't recognise that the input has finished.

---

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [21 April 2019 12:53 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/8 "2019-04-21T12:53:53Z")

</div>

Hi, Sorry silence.

```auto
// ******************************************************************************
// Address | Command | Address | N-Bytes to Read | Check Sum |*
// 1 byte | 1 byte | 2 bytes | 1 bytes | 2 bytes |*
// 0XFE | 0X44 | 0X00 | 0X80 | 0X02 | 0X9F | 0X25 |*
// ******************************************************************************

msg.payload="\xFE\x44\x00\x08\x02\x9F\x25";
return msg;

```

upon deploy, i am able to see the serial note is indicating connected. Upon inject a pulse : msg.payload : undefined

While i test the code with python script as shown in my first post on raspberry terminal, the system running in good order and able to see results.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [21 April 2019 14:01 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/9 "2019-04-21T14:01:28Z")

</div>

> [@tramanah](#):
>
> msg.payload="\xFE\x44\x00\x08\x02\x9F\x25"; return msg;

that is creating a string. you need to create a buffer so it is binary

```
msg.payload = Buffer.from("FE440008029F25", "hex");
return msg;

```

Also you may want to look at using the serial-request node as that can send a packet then wait for the response (which is what you seem to need here) - though for this simple case it shouldn't matter.

---

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [21 April 2019 23:39 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/10 "2019-04-21T23:39:00Z")

</div>

> [@dceejay](#):
>
> Buffer.from("FE440008029F25", "hex");

"TypeError [ERR\_INVALID\_ARG\_TYPE]: The "list[1]" argument must be one of type Array, Buffer, or Uint8Array. Received type string"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 April 2019 01:21 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/11 "2019-04-22T01:21:41Z")

</div>

Odd. Try this...

```auto
msg.payload=[0xFE,0x40,0x00,0x08,0x02,0x9F,0x25];
return msg;

```

If that doesn't work post your flow incase it's something silly.

---

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [22 April 2019 11:19 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/12 "2019-04-22T11:19:52Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> [{"id":"73cf045c.ae53cc","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"32bad176.239f8e","type":"serial request","z":"73cf045c.ae53cc","name":"","serial":"bf893c9b.5daea","x":470,"y":80,"wires":[["8f2957b1.a8c8f8","79c24081.894c3"]]},{"id":"f1e07bca.f1a658","type":"inject","z":"73cf045c.ae53cc","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["d6130027.4d02d"]]},{"id":"d6130027.4d02d","type":"function","z":"73cf045c.ae53cc","name":"K-30","func":"// ******************************************************************************\n// Address | Command | Address | N-Bytes to Read | Check Sum |*\n// 1 byte | 1 byte | 2 bytes | 1 bytes | 2 bytes |*\n// 0XFE | 0X44 | 0X00 | 0X80 | 0X02 | 0X9F | 0X25 |*\n//****************************************************************************** \n\n//msg.payload=\"\\xFE\\x44\\x00\\x08\\x02\\x9F\\x25\";\n//msg.payload = Buffer.from(\"FE440008029F25\", \"hex\");\nmsg.payload=[0xFE,0x40,0x00,0x08,0x02,0x9F,0x25];\nreturn msg;\n\n","outputs":1,"noerr":0,"x":300,"y":80,"wires":[["32bad176.239f8e","2002e45e.f0a7dc"]]},{"id":"8f2957b1.a8c8f8","type":"function","z":"73cf045c.ae53cc","name":"msg receive","func":"\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":180,"wires":[["f818d47a.ee9e88"]]},{"id":"79c24081.894c3","type":"debug","z":"73cf045c.ae53cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":700,"y":60,"wires":[]},{"id":"f818d47a.ee9e88","type":"debug","z":"73cf045c.ae53cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":470,"y":180,"wires":[]},{"id":"2002e45e.f0a7dc","type":"debug","z":"73cf045c.ae53cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":480,"y":20,"wires":[]},{"id":"bf893c9b.5daea","type":"serial-port","z":"","serialport":"/dev/ttyAMA0","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","newline":"\\n","bin":"false","out":"char","addchar":"\\n","responsetimeout":"1000"}]
> 
> ```

Timeout on serial node.

Below is the python script which works.

```auto
#Python app to run a K-30 Sensor
import serial
import time
ser = serial.Serial("/dev/ttyAMA0",baudrate =9600,timeout = .5)
print " AN-137: Raspberry Pi3 to K-30 Via UART\n"
ser.flushInput()
time.sleep(1)

while 1:

#for i in range(1,21):

ser.flushInput()
ser.write("\xFE\x44\x00\x08\x02\x9F\x25")
time.sleep(.5)
resp = ser.read(7)
high = ord(resp[3])
low = ord(resp[4])
co2 = (high*256) + low
print "i = ",i, " CO2 = " +str(co2)
time.sleep(.1)

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 April 2019 12:10 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/13 "2019-04-22T12:10:13Z")

</div>

Are you getting any errors in the debug side bar or the console?

Are you certain the data bits, parity and stop bits are matching the device?

Is the serial port being blocked by another application?

Another thing, that looks a lot like a modbus command but it's not quite right.

Perhaps you could try sending 0xFE 0x04 0x00 0x01 0x00 0x01 0x74 0x05

If you [look here](https://www.co2meter.com/products/k-30-co2-sensor-module) there are some downloads. One of them is called [communication protocols](http://co2meters.com/Documentation/Other/SenseAirCommGuide.zip) in that zip is reference to MODBUS and there is a spreadsheet listing all of the available registers.

You could perhaps try one of the modbus contribute nodes?

I haven't used any but I think you need to look for MODBUS RTU

good luck

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 April 2019 12:13 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/14 "2019-04-22T12:13:47Z")

</div>

Perhaps this one?

> **[node-red-contrib-arc-modbus](https://flows.nodered.org/node/node-red-contrib-arc-modbus)**
>
> a powerful modbus tool for node-red

---

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [10 May 2019 07:31 UTC](https://discourse.nodered.org/t/k-30-co2-sensor/3399/15 "2019-05-10T07:31:21Z")

</div>

Hi guys,

Finally i do manage to get reading from Node Red. I did contact supplier and get the correct address for Modbus communicaiton: -  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
// Address | Command | Address | N-Bytes to Read | Check Sum |\*  
// 1 byte | 1 byte | 2 bytes | 2 bytes | 2 bytes |\*  
// 0XFE | 0X04 | 0X00 0X03 | 0X00 0X01 | 0XD5 | 0XC5 |\*  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

```auto
[{"id":"87fd39a4.ab0968","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"6b7c1fab.1753a","type":"modbus-read","z":"87fd39a4.ab0968","name":"","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"254","dataType":"InputRegister","adr":"3","quantity":"1","rate":"500","rateUnit":"ms","delayOnStart":false,"startDelayTime":"","server":"bb4c0249.00a82","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":370,"y":360,"wires":[["47ae9e29.d0d03"],[]]},{"id":"47ae9e29.d0d03","type":"debug","z":"87fd39a4.ab0968","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":600,"y":360,"wires":[]},{"id":"bb4c0249.00a82","type":"modbus-client","z":"","name":"","clienttype":"serial","bufferCommands":true,"stateLogEnabled":false,"tcpHost":"127.0.0.1","tcpPort":"502","tcpType":"DEFAULT","serialPort":"COM7","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectTimeout":"2000"}]

```

 ![node%20red](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/af739d2df5ac72e7cfb49608a608a07fc06e8472.png)  
Cheers
