K-30 CO2 sensor

#1

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)

#2

What have you tried so far?

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

#3

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

#4

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.

#5

i do manage with that thank you.

But i didn't get the output.

#6

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

Also check the Node-RED logs.

#7

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.

#8

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.

#9

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.

#10

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

#11

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.

#12

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

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

#14

Perhaps this one?