K-30 CO2 sensor

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)

What have you tried so far?

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

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

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:

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.

i do manage with that thank you.

But i didn't get the output.

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

Also check the Node-RED logs.

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.

Hi, Sorry silence.

//******************************************************************************
// 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.

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.

"TypeError [ERR_INVALID_ARG_TYPE]: The "list[1]" argument must be one of type Array, Buffer, or Uint8Array. Received type string"

Odd. Try this...

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

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

Timeout on serial node.

Below is the python script which works.

#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)

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 there are some downloads. One of them is called communication protocols 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

Perhaps this one?

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 |*
//******************************************************************************

[{"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"}]


Cheers