Getting data from two or more sensors - Arduino/P5.JS

Hi there,
i'm working in a p5js and arduino project that will include two or more sensors, sending data via p5serialport

arduino code


#include <dht11.h>
#define DHT11PIN 4

dht11 DHT11;

int sensorChuva = A5;
int chuva = 0;


void setup()
{
  Serial.begin(9600);
}
void loop()
{
  Serial.println();

  int chk = DHT11.read(DHT11PIN);
  int chuva = sensorChuva = analogRead(A5);

   Serial.print((int)DHT11.humidity);
   Serial.print(", ");
  Serial.println((int)chuva);

}

p5js scketch

function gotData() {
  var currentString = serial.readStringUntil("\r\n");  // read the incoming string
  trim(currentString);                    // remove any trailing whitespace
 if (!currentString) return;             // if the string is empty, do no more
console.log(currentString);             // println the string

    map(int(latestData));            // save it for the draw method
    latestData = currentString;            // save it for the draw method

    console.log(currentString[0]);
    console.log(currentString[1]);

In this way, I can send the data, but when i try to read "console.log(currentString[0]); console.log(currentString[1]);" it doesn't returns the data of each sensor. It returns the first and the second digit of the first sensor.
I suppose that's happening because when the p5serial send the data it transform in a string array, but i don't know how to fix it.

Someone can help me?

Post the full code

I would suggest to study Serial Input Basics to handle this

You seem to have some issues when parsing the incoming data from serial in your p5js code.

Ideally, you would add starting and ending markings like in this example from Serial Input Basics that @J-M-L shared.

Anyway, in your code you're reading the serial data as a string and accessing the indexes of that string. For example, if your currentString variable is "34,82", then currentString[0] and currentString[1] would be 3 and 4 respectively. You have to split that string into multiple objects using "," as a separator and map them to a numeric type:

let sensorData = currentString.split(',').map(Number);

Then you'll have an array of numbers that you can use.

Here is a generic updated version of your p5js snippet, but you will probably need to edit it to fit your needs.

function gotData() {
  let currentString = serial.readStringUntil("\r\n"); // read the incoming string
  if (!currentString) return; // if the string is empty, do no more

  // Split the string into an array of values
  let sensorData = currentString.split(',').map(Number);
  console.log(sensorData); // Log the array of sensor data

  // Now you can access the data for each sensor by index
  let humidity = sensorData[0];
  let rainSensor = sensorData[1];
  
  // Use the data as needed in your sketch

}

Let us know if this works!

hi @barreto2311!

it works! thank you so much :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.