Interfacing Arduino with NodeRed on RaspberryPi

#1

Hello,

Im just diving into Node-RED and im currently working on a project that involves me to display a PSI sensor data from an arduino onto a raspberry pi running node-red. I have uploaded the standardfirmata sketch to the arduino and I am able to get the node to recognize it, but the value is not correct. I thought adding a function node would help with an equation in order to get the correct value but not sure where to start with coding in the function node.

0 Likes

#2

but not sure where to start with coding in the function node.

Start at the beginning :stuck_out_tongue_winking_eye:

When you say the data is incorrect what do you mean? Can you give an example of what it is and what it should be? And maybe the equation you would use.

You might be able to just use a change node with a JSONata expression

Also add a debug node on the output of the node retreiving the data. That will show the data and it's type which will be needed to determine how to convert it.

0 Likes

#3

Thanks for the reply, i guess my real problem is im lacking in knowledge in javascript but i created a python program to interact with the Arduino and get serial data.

My flow starts with a inject => exce (which contains the python program and an error2?) => then into a function node (which is where im stuck) => into a gauge node and a debug node

my python program is simple as follows

import serial

ser =serial.Serial('/dev/ttyACM0', baudrate = 9600, timeout=1)

while 1:

    arduinoData = ser.readline()
    print(arduinoData)

and Arduino code:

int rawValue; // A/D readings
int offset = 102; // zero pressure adjust
int fullScale = 922; // max pressure (span) adjust
float pressure; // final pressure


void setup() {
  // put your setup code here, to run once:
  analogReference(DEFAULT);
  Serial.begin(9600);
}

void loop() {

  // put your main code here, to run repeatedly:
  int rawValue = analogRead(A0); 
   pressure = ((((rawValue - offset) * 1.2 / (fullScale - offset))*145.038)-44.64); // pressure conversion
 // Serial.println(pressure, 3);
  //delay(100);

  if (pressure < 0) {

  Serial.println('0'); 
  
  } else {
    
    Serial.println(pressure);
  }
  
}
0 Likes

#4

A couple things, some you have covered some you haven't. when you open a thread I find it very helpful to start off detailing the hardware, OS release, version of NR and node.js and a copy of the flow - hopefully reduced to the minimum showing the issue.

Put a debug node (displaying the complete msg object) on the output of the exec node.
What dooes it show?
What are th parameters you ase passing fro the inject node?
What are your settings in the exec node?

0 Likes

#5

I am using a Raspberry Pi 3 B+ running Raspian, and an arduino uno.

  • NR V0.19.4
  • Node.js V8.11.1

[{"id":"fff2794a.02c1d8","type":"exec","z":"9b9b6f6c.90d18","command":"python engine.py","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":251,"y":171.5,"wires":[,["29088ee3.e14b82"],]}]

  • i get msg.payload : undefined in the debug screen

  • the inject repeats every 1 second

  • the command in my exec node is command: python Engine.py; append msg.payload is checked and i have it while the command is running - spawn mode.

0 Likes

#6

Some problems with your flow, can't be imported

image

0 Likes

#7

Hi.

Welcome to the magic world of Node-Red.

Getting Arduinos talking to Node-Red is fun.

Just on what you said:

The PSI data should be sent out - as it seem reading it: by the serial port? - to the RPI.
That's ok, I guess.

What I would suggest you do is try this first to establish a base line that the Arduino is doing the right thing.

Load the IDE and open the serial port to the Arduino.
You should see the data being sent out line by line.
The exact format is not too important at this stage, other than every line should be the same format.
Say something like:
12.7
12.5
12.8
11.9
12.3
12.8
and so on.
Be it has PSI at the end doesn't matter at this stage.

These data packets are sent to NR and processed.

If that isn't working, you are in trouble before you get started.

If they are all good, maybe then you need to get the serial input node and connect it to a debug node.

This should give you the same results as you did in the Arduino's IDE.

Given that, then you need to look at the flow.

But until then.......

I hope that helps you with finding where the data is going wrong.

All the best.

0 Likes