Arduino- Node Red Dashboard Separate Sensor Data Logging

#1

I am using 2 sensors ( photo resistor and temperature, in the middle is just LED and buzzer regarding on the response change with photo resistor sensor) and would like to put up on the gauge and chart in the dashboard. Right now, the reading reads in line with comma in between, the chart is combining them up which makes them unstable and gauge is only showing for 1 sensor. how could I separate and do different reading, gauge and chart for each sensor? Below is the arduino code and node red.

arduino:

int sensePin = 0;
int ledPin = 9;
int val;
int tempPin = 1;
const int buzzer = 12;

void setup()
{
  pinMode(ledPin, OUTPUT);
  analogReference(DEFAULT); 
  pinMode(buzzer, OUTPUT);
   Serial.begin(9600);
  
}

void loop()
{
  // read the sensor
  int val = analogRead(sensePin);
  
  if(val < 960)
  {
    digitalWrite(ledPin, HIGH);
    tone(buzzer, 1000); 

  }
  else
  {
    digitalWrite(ledPin, LOW);
    noTone(buzzer);    
  
  }
    
    val = analogRead(tempPin);
  float mv = ( val/1024.0)*5000; 
  float cel = mv/10;
  float farh = (cel*9)/5 + 32;


  Serial.print(cel);
  Serial.print("*C , ");
  Serial.println(analogRead(sensePin)); 
  delay(1000);


}

node-red:

[{"id":"155d5a28.f6d206","type":"debug","z":"ef68beb7.75aa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":382,"y":196,"wires":[]},{"id":"99c64bbb.3ec6a8","type":"ui_text","z":"ef68beb7.75aa","group":"9aaba0ca.eddc2","order":0,"width":"0","height":"0","name":"","label":"PHOTORESISTOR, TEMPERATURE","format":"{{msg.payload}}","layout":"row-left","x":458,"y":103,"wires":[]},{"id":"792451eb.62aaa","type":"ui_gauge","z":"ef68beb7.75aa","name":"","group":"9aaba0ca.eddc2","order":0,"width":0,"height":0,"gtype":"gage","title":"direction","label":"units","format":"{{value}}","min":0,"max":"2000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"1000","seg2":"1500","x":358,"y":45,"wires":[]},{"id":"15dee299.b5ad8d","type":"ui_chart","z":"ef68beb7.75aa","name":"","group":"9aaba0ca.eddc2","order":0,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"bezier","nodata":"","dot":false,"ymin":"0","ymax":"1000","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"1","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":354,"y":420,"wires":[[],[]]},{"id":"e6a955df.4030b8","type":"serial in","z":"ef68beb7.75aa","name":"","serial":"2365f3c7.2d0b6c","x":159,"y":244,"wires":[["792451eb.62aaa","99c64bbb.3ec6a8","155d5a28.f6d206","15dee299.b5ad8d","9f39afc.9482e5"]]},{"id":"9f39afc.9482e5","type":"serial out","z":"ef68beb7.75aa","name":"","serial":"2365f3c7.2d0b6c","x":350,"y":289,"wires":[]},{"id":"9aaba0ca.eddc2","type":"ui_group","z":"","name":"SENSORS","tab":"c326fe58.3d375","order":1,"disp":true,"width":"14","collapse":false},{"id":"2365f3c7.2d0b6c","type":"serial-port","z":"","serialport":"COM3","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false},{"id":"c326fe58.3d375","type":"ui_tab","z":"","name":"READING","icon":"dashboard"}]
#2

Is that the exact arduino code or did you type it in because it looks like there are some errors in it

What sensors are you using?

What is the debug node showing?

#3

@youthtech please edit your post and put three backticks on a line before the flow and on the line after it. That tells discourse that it should not mess with the text and in particular it should not change the quotes into smart quotes. As it is currently it is not possible to import the flow.
Thanks.

#4

Format Serial.print(ln) in Arduino as:
1(sensor1value)
2(sensor2value).

At node red side use function with if then and substring, and two outputs.

If 1st string is “1” then send rest of the message to the 1st output.
Same for 2nd output but if 1st string is “2”.

#5

yes, it is the exact arduino code, I’m using a photo resistor and temperature sensor LM35. debug node shows the readings from the photo resistor and temperature.

#6

I am sorry, I’m confuse. which line should I put the three back ticks? is it for both arduino and node red or just the node red?

#7

When you share any sort of code or flow json that should not be treated as normal written text, then you should put ``` before and after it.

Here is some code without the backticks:

void setup()
{
pinMode(ledPin, OUTPUT);
analogReference(DEFAULT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);

}

Here is some code with the ``` (backticks):

void setup()
{
pinMode(ledPin, OUTPUT);
analogReference(DEFAULT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);

}

#8

alright. just knew this, thanks for teaching. I edited it.

#9

Can you post a screen shot of the debug output so we can see exactly what you have.

#10

debug

#11

Hi, One possible way (among many) is to use below code inside a function node with 2 outputs:

let reg = /\d+/g;
let result = msg.payload.match(reg);
[a, b] = result;
temp = {payload: parseInt(a)};
light = {payload: parseInt(b)};
return [temp, light];

Explanation:

JavaScript Problem: Extracting Numbers from a String

Function node:

[{"id":"a7d3a24b.5b0d7","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"14139795.2a1468","type":"function","z":"a7d3a24b.5b0d7","name":"Extract data","func":"let reg = /\\d+/g;\nlet result = msg.payload.match(reg);\n[a, b] = result;\ntemp = {payload: parseInt(a)};\nlight = {payload: parseInt(b)};\nreturn [temp, light];","outputs":2,"noerr":0,"x":514.1000556945801,"y":206.00001525878906,"wires":[[],[]]}]
#13

I have a question, what to put for a and b for enable it to work? I tried put ldr as a and cel as b but only a shows up, b does not. I have adjusting the arduino codes for easier labelling as follow:

int LDR = A0;
int ledPin = 9;
int LDRReading = 0; 
int val;
int tempPin = A1;
const int buzzer = 12;

void setup()
{
  pinMode(ledPin, OUTPUT);
  analogReference(DEFAULT); 
  pinMode(buzzer, OUTPUT);
   Serial.begin(9600);
  
}

void loop()
{
  LDRReading = analogRead(LDR);  
  
  if(LDRReading < 800)
  {
    digitalWrite(ledPin, HIGH);
    tone(buzzer, 1000); 

  }
  else
  {
    digitalWrite(ledPin, LOW);
    noTone(buzzer);    
  
  }
    
    val = analogRead(tempPin);
  float mv = ( val/1024.0)*5000; 
  float cel = mv/10;
  float farh = (cel*9)/5 + 32;
  float ldr = (LDRReading)*1;

  Serial.print(ldr);
  Serial.print(" , ");
  Serial.print(cel);
  Serial.println(); 
  delay(500);


}

#14

Hi, the very same function node (same code) should work as long as your arduino output is two numbers separated by comma (which seems to be the case). Can you insert a debug node in the output of the serial node and another debug node in the output of the function node and than show us a screenshot of the debug side panel ?

#15

debug

#16

@Andrel - your reg expression converts the periods (".") in the numbers to commas (",") - i.e. for “854.00, 38.09” you get 854,00,38,09 so
[a, b] = result;
returns 854 and 0

#17

If the data is as you say i.e. "854.00, 38.09” - I would feed it to a CSV node and then you can access the data with msg.payload.col1 and msg.payload.col2

1 Like
#18

Hi zenofmud, your are are indeed, my bad. Thanks for pointing out :wink:

Youthtech,

The proposed solutions from zenofmud and markost should work. I will provide below a second attempt but again I did not test thoroughly. It will work despite that the code is not removing the “*C” from the string.

I found this explanation:

parseFloat parses its argument, and returns a floating point number. If it encounters a character other than a sign (+ or -), numeral (0-9), a decimal point, or an exponent, it returns the value up to that point and ignores that character and all succeeding characters. Leading and trailing spaces are allowed.

let result = msg.payload.split(",");
[a,b] = result;
temp = {payload: parseFloat(a)};
light = {payload: parseFloat(b)};
return [temp,light];

Testing flow:

[{"id":"2c35d982.dae386","type":"tab","label":"Flow 18","disabled":false,"info":""},{"id":"eb696b33.4bf5d8","type":"function","z":"2c35d982.dae386","name":"Extract data","func":"let result = msg.payload.split(\",\");\n[a,b] = result;\ntemp = {payload: parseFloat(a)};\nlight = {payload: parseFloat(b)};\nreturn [temp,light];","outputs":2,"noerr":0,"x":540,"y":181,"wires":[["3df65eb1.173642"],["a8c8d257.faffa"]]},{"id":"94eb5816.f85108","type":"inject","z":"2c35d982.dae386","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":195.99994659423828,"y":181.9999876022339,"wires":[["3baa0c18.d23554"]]},{"id":"3baa0c18.d23554","type":"function","z":"2c35d982.dae386","name":"","func":"msg.payload = \"854.30 \\, 35.15\\n\\f\";\nreturn msg;","outputs":1,"noerr":0,"x":346.99995040893555,"y":182.9999876022339,"wires":[["eb696b33.4bf5d8"]]},{"id":"3df65eb1.173642","type":"debug","z":"2c35d982.dae386","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":725.8999443054199,"y":107.99998474121094,"wires":[]},{"id":"a8c8d257.faffa","type":"debug","z":"2c35d982.dae386","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":724.8999443054199,"y":185.99998474121094,"wires":[]}]
#19

hi,

i think that easiest way for the start solving your problem is:

1st step:

format your arduino code

instead

 Serial.print(ldr);
  Serial.print(" , ");
  Serial.print(cel);
  Serial.println(); 
  delay(500);

use:

 Serial.print("ldr=");
 Serial.println(ldr,2);   //2 decimal place
 Serial.print("cel=");  
 Serial.println(cel,2);   //2 decimal place
   
  delay(500);

See what you have at debug

#20

This solve the problem. Thank you so much for your help.

1 Like
#21

the dashboard doesn’t show any value of both readings, but debug shows both.