Not receiving all data from Arduino Serial port

Hey everyone, total noob here to programming. I have an issue with a script (is it called that for processing?) I receive 2 of the 3 parts of data, but cant for the life of me get the soil moisture readings.

I know its not an issue with my arduino script as I check the serial.print on the monitor, and it looks good. Any help is greatly appreciated.

FYI, this is my first time using processing as well.

Alot of the underlying script was taken from a Youtube video, not sure the name.

// import meter Library
import meter.*;

//Import Serial Library
import processing.serial.*;

Serial port; // define a port

Meter m, m2, m3;

void setup()
{
  //first create the empty window
  size(950, 700); // size of window (width, height) in pixles
  background (0, 0, 0); // RGB
  
  //create a new port
  port = new Serial(this, "COM3", 9600); //Name of the port and Baud rate
  
  //lets add a default meter to the empty window
  
  //Temperature Meter
  m = new Meter(this, 20, 40); // upper left corner co-ord for window
  
  m.setTitleFontSize(25);
  m.setTitleFontName("Arial bold");
  m.setTitle("Temperature (*C)");
  
  //change meter scale values
  String []scaleLabels = {"0", "10", "20", "30", "40", "50"};
  m.setScaleLabels(scaleLabels);
  m.setScaleFontSize(15);
  m.setScaleFontName("Arial Italic");
  m.setScaleFontColor(color(200, 30, 70));
  
  //We can also display the value of the meter
  m.setDisplayDigitalMeterValue(true);
  
  m.setArcColor(color(0, 0, 255));
  m.setArcThickness(12);
  
  m.setNeedleThickness(2);
  m.setNeedleColor(color(10, 100, 10));
  
  m.setMaxScaleValue(50);
  
  m.setMinInputSignal(0);
  m.setMaxInputSignal(50);
  
  
  //Humidity Meter
  
  int mx = m.getMeterX(); //X coord of M
  int my = m.getMeterY(); //Y coord of M
  int mw = m.getMeterWidth(); //Get width of M
  int mh = m.getMeterHeight(); //get Height of M
  
   m2 = new Meter(this, mx + mw + 20, my); // upper left corner co-or for window
  
  m2.setTitleFontSize(25);
  m2.setTitleFontName("Arial bold");
  m2.setTitle("Humidity (%)");
  
  //change meter scale values
  String []scaleLabels2 = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
  m2.setScaleLabels(scaleLabels2);
  m2.setScaleFontSize(15);
  m2.setScaleFontName("Arial Italic");
  m2.setScaleFontColor(color(200, 30, 70));
  
  //We can also display the value of the meter
  m2.setDisplayDigitalMeterValue(true);
  
  m2.setArcColor(color(0, 0, 255));
  m2.setArcThickness(12);
  
  m2.setNeedleThickness(2);
  m2.setNeedleColor(color(10, 100, 10));
  
  m2.setMaxScaleValue(1024);
  
  m2.setMinInputSignal(0);
  m2.setMaxInputSignal(1024);
  
   //Soil Moisture Meter
     
  int mx3 = m.getMeterX(); //X coord of M
  int my3 = m.getMeterY(); //Y coord of M
  int mw3 = m.getMeterWidth(); //Get width of M
  int mh3 = m.getMeterHeight(); //get Height of M
  
   m3 = new Meter(this, mx , my + mh + 20); // upper left corner co-or for window
  
  m3.setTitleFontSize(25);
  m3.setTitleFontName("Arial bold");
  m3.setTitle("Soil Moisture (%)");
  
  //change meter scale values
  String []scaleLabels3 = {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
  m3.setScaleLabels(scaleLabels3);
  m3.setScaleFontSize(15);
  m3.setScaleFontName("Arial Italic");
  m3.setScaleFontColor(color(200, 30, 70));
  
  //We can also display the value of the meter
  m3.setDisplayDigitalMeterValue(true);
  
  m3.setArcColor(color(0, 0, 255));
  m3.setArcThickness(12);
  
  m3.setNeedleThickness(2);
  m3.setNeedleColor(color(10, 100, 10));
  
  m3.setMaxScaleValue(100);
  
  m3.setMinInputSignal(0);
  m3.setMaxInputSignal(1024);
  
}

void draw ()
{
  //Lets give a title to the window
 textSize(30);
 fill(0,255,0); //text color
  text("Biosphere 1", 400, 35); //Co ord of title
  
  if (port.available() > 0){
    String val = port.readString(); //read incomming string on serial port
    //split the values in the string
    String[] list = split (val, ',');
    float temp = float(list[0]); // first value is temperature
    float hum = float(list[1]); // second value is Relitive Humidity
    float Soil = float(list[2]); //Third value is Soil Moisture
    
    m.updateMeter(int(temp));
    m2.updateMeter(int(hum));
    m3.updateMeter(int(Soil));
    
    println("temperature: " + temp + " *C, " + "Humidity: " + hum + " %, " + "Soil Moisture: " + Soil + " %");
  }
    delay(1000);  
}
1 Like

At first glance, maybe the message is not complete by the time processing reads from the serial port.

You might change ā€œif availableā€ to a larger value (instead of greater than 0).
If your incoming message is a fixed length then that would probably do it.

If I am being realistic :wink: I will suggest using readStringUntil()

1 Like

Thank you, issue is resolved. much appreciated.