Error on my Processing GUI code for Arduino , I need some help

I think I understand. You had a loop() that had all the calculations in it. Then added a new loop that has the printing, but not calling the calculations. Put the calculations from into the current loop function.

with the new code the data is read in serial port
it is working , but my problem now is with the Processing code for GUI

I will attach the new Arduino:

#include <Wire.h> 
#include "SparkFunMPL3115A2.h" 
#include "SparkFun_Si7021_Breakout_Library.h" 

MPL3115A2 myPressure; 
Weather myHumidity;



const byte WSPEED = 3;
const byte RAIN = 2;
const byte STAT1 = 7;
const byte STAT2 = 8;

const byte REFERENCE_3V3 = A3;
const byte LIGHT = A1;
const byte BATT = A2;
const byte WDIR = A0;

long lastSecond; 
byte seconds; 
byte seconds_2m; 
byte minutes; 
byte minutes_10m; 

long lastWindCheck = 0;
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;


byte windspdavg[120]; 

#define WIND_DIR_AVG_SIZE 120
int winddiravg[WIND_DIR_AVG_SIZE]; 
float windgust_10m[10]; 
int windgustdirection_10m[10]; 
volatile float rainHour[60]; 


int winddir = 0; 
float windspeedmph = 0; 
float windgustmph = 0; 
int windgustdir = 0; 
float windspdmph_avg2m = 0; 
int winddir_avg2m = 0; 
float windgustmph_10m = 0; 
int windgustdir_10m = 0; 
float humidity = 0; 
float temperature = 0; 
float rainin = 0; 
volatile float dailyrainin = 0; 
float pressure = 0;
float batt_lvl = 11.8; 
float light_lvl = 455; 


volatile unsigned long raintime, rainlast, raininterval, rain;


void rainIRQ()

{
  raintime = millis(); 
  raininterval = raintime - rainlast; 

  if (raininterval > 10) 
  {
    dailyrainin += 0.011; 
    rainHour[minutes] += 0.011; 

    rainlast = raintime; 
  }
}

void wspeedIRQ()

{
  if (millis() - lastWindIRQ > 10) 
  {
    lastWindIRQ = millis(); 
    windClicks++; 
  }
}


void setup()
{
  Serial.begin(9600);
  Serial.println("Weather Shield Example");

  pinMode(STAT1, OUTPUT); 
  pinMode(STAT2, OUTPUT); 

  pinMode(WSPEED, INPUT_PULLUP); 
  pinMode(RAIN, INPUT_PULLUP); 

  pinMode(REFERENCE_3V3, INPUT);
  pinMode(LIGHT, INPUT);

 
  myPressure.begin(); 
  myPressure.setModeBarometer(); 
  myPressure.setOversampleRate(7); 
  myPressure.enableEventFlags(); 

  
  myHumidity.begin();

  seconds = 0;
  lastSecond = millis();

  // attach external interrupt pins to IRQ functions
  attachInterrupt(0, rainIRQ, FALLING);
  attachInterrupt(1, wspeedIRQ, FALLING);

  // turn on interrupts
  interrupts();

  Serial.println("Weather Shield online!");

}



void calcWeather()
{
  
  winddir = get_wind_direction();

 
  float temp = 0;
  for (int i = 0 ; i < 120 ; i++)
    temp += windspdavg[i];
  temp /= 120.0;
  windspdmph_avg2m = temp;

  
  long sum = winddiravg[0];
  int D = winddiravg[0];
  for (int i = 1 ; i < WIND_DIR_AVG_SIZE ; i++)
  {
    int delta = winddiravg[i] - D;

    if (delta < -180)
      D += delta + 360;
    else if (delta > 180)
      D += delta - 360;
    else
      D += delta;

    sum += D;
  }
  winddir_avg2m = sum / WIND_DIR_AVG_SIZE;
  if (winddir_avg2m >= 360) winddir_avg2m -= 360;
  if (winddir_avg2m < 0) winddir_avg2m += 360;

 
  windgustmph_10m = 0;
  windgustdir_10m = 0;

  for (int i = 0; i < 10 ; i++)
  {
    if (windgust_10m[i] > windgustmph_10m)
    {
      windgustmph_10m = windgust_10m[i];
      windgustdir_10m = windgustdirection_10m[i];
    }
  }

  
  humidity = myHumidity.getRH();
 

  
  temperature = myPressure.readTemp();
  

  
  rainin = 0;
  for (int i = 0 ; i < 60 ; i++)
    rainin += rainHour[i];

  
  pressure = myPressure.readPressure();


  light_lvl = get_light_level();

  
  batt_lvl = get_battery_level();
}


float get_light_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float lightSensor = analogRead(LIGHT);

  operatingVoltage = 3.3 / operatingVoltage; 

  lightSensor = operatingVoltage * lightSensor;

  return (lightSensor);
}


float get_battery_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float rawVoltage = analogRead(BATT);

  operatingVoltage = 3.30 / operatingVoltage; 

  rawVoltage = operatingVoltage * rawVoltage; 

  rawVoltage *= 4.90; 

  return (rawVoltage);
}


float get_wind_speed()
{
  float deltaTime = millis() - lastWindCheck; 

  deltaTime /= 1000.0; 

  float windSpeed = (float)windClicks / deltaTime; 

  windClicks = 0; 
  lastWindCheck = millis();

  windSpeed *= 1.492; 

  

  return (windSpeed);
}


int get_wind_direction()
{
  unsigned int adc;

  adc = analogRead(WDIR); 

 
  if (adc < 380) return (113);
  if (adc < 393) return (68);
  if (adc < 414) return (90);
  if (adc < 456) return (158);
  if (adc < 508) return (135);
  if (adc < 551) return (203);
  if (adc < 615) return (180);
  if (adc < 680) return (23);
  if (adc < 746) return (45);
  if (adc < 801) return (248);
  if (adc < 833) return (225);
  if (adc < 878) return (338);
  if (adc < 913) return (0);
  if (adc < 940) return (293);
  if (adc < 967) return (315);
  if (adc < 990) return (270);
  return (-1); 
}




void printWeather(char label[], float value, int decPlaces = 0)
{
  Serial.print(label);
  Serial.print(value, decPlaces);
  Serial.println();
}

void loop() 

{
  
  if (millis() - lastSecond >= 1000)
  {
    digitalWrite(STAT1, HIGH); 

    lastSecond += 1000;

   
    if (++seconds_2m > 119) seconds_2m = 0;

   
    float currentSpeed = get_wind_speed();
    windspeedmph = currentSpeed;
    
    int currentDirection = get_wind_direction();
    windspdavg[seconds_2m] = (int)currentSpeed;
    winddiravg[seconds_2m] = currentDirection;
    

    
    if (currentSpeed > windgust_10m[minutes_10m])
    {
      windgust_10m[minutes_10m] = currentSpeed;
      windgustdirection_10m[minutes_10m] = currentDirection;
    }

    
    if (currentSpeed > windgustmph)
    {
      windgustmph = currentSpeed;
      windgustdir = currentDirection;
    }

    if (++seconds > 59)
    {
      seconds = 0;

      if (++minutes > 59) minutes = 0;
      if (++minutes_10m > 9) minutes_10m = 0;

      rainHour[minutes] = 0; 
      windgust_10m[minutes_10m] = 0; 
    }

  
    

    digitalWrite(STAT1, LOW); 
  }

  delay(100);




printWeather("winddir=" , winddir);
printWeather("windspeedmph=" , windspeedmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgusdir=" , windgustdir);
printWeather("windspdmph_avg2m=" , winddir_avg2m);
printWeather("winddir_avg2m=" , rainin , 2);
printWeather("windgustmph_10m=" , windgustmph_10m, 1);
printWeather("windgustdir_10m=" , windgustdir_10m);
printWeather("humidity=" , humidity, 1);
printWeather("tempf=" , temperature, 1);
printWeather("rainin=" , rainin , 2);
printWeather("dailyrainin=" , dailyrainin, 2);
printWeather("pressure=" , pressure, 2);
printWeather("batt_lvl=" , batt_lvl, 2);
printWeather("light_lvl=" , light_lvl, 2);


}




You are not being clear. Your Ard code is alright now? If yes, please post what it prints out in the Serial Monitor. Say whatā€™s wrong with the Processing and post that code.

This is the code that is working with Arduino:

#include <Wire.h> 
#include "SparkFunMPL3115A2.h" 
#include "SparkFun_Si7021_Breakout_Library.h" 

MPL3115A2 myPressure; 
Weather myHumidity;



const byte WSPEED = 3;
const byte RAIN = 2;
const byte STAT1 = 7;
const byte STAT2 = 8;

const byte REFERENCE_3V3 = A3;
const byte LIGHT = A1;
const byte BATT = A2;
const byte WDIR = A0;

long lastSecond; 
byte seconds; 
byte seconds_2m; 
byte minutes; 
byte minutes_10m; 

long lastWindCheck = 0;
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;


byte windspdavg[120]; 

#define WIND_DIR_AVG_SIZE 120
int winddiravg[WIND_DIR_AVG_SIZE]; 
float windgust_10m[10]; 
int windgustdirection_10m[10]; 
volatile float rainHour[60]; 


int winddir = 0; 
float windspeedmph = 0; 
float windgustmph = 0; 
int windgustdir = 0; 
float windspdmph_avg2m = 0; 
int winddir_avg2m = 0; 
float windgustmph_10m = 0; 
int windgustdir_10m = 0; 
float humidity = 0; 
float temperature = 0; 
float rainin = 0; 
volatile float dailyrainin = 0; 
float pressure = 0;
float batt_lvl = 11.8; 
float light_lvl = 455; 


volatile unsigned long raintime, rainlast, raininterval, rain;


void rainIRQ()

{
  raintime = millis(); 
  raininterval = raintime - rainlast; 

  if (raininterval > 10) 
  {
    dailyrainin += 0.011; 
    rainHour[minutes] += 0.011; 

    rainlast = raintime; 
  }
}

void wspeedIRQ()

{
  if (millis() - lastWindIRQ > 10) 
  {
    lastWindIRQ = millis(); 
    windClicks++; 
  }
}


void setup()
{
  Serial.begin(9600);
  

  pinMode(STAT1, OUTPUT); 
  pinMode(STAT2, OUTPUT); 

  pinMode(WSPEED, INPUT_PULLUP); 
  pinMode(RAIN, INPUT_PULLUP); 

  pinMode(REFERENCE_3V3, INPUT);
  pinMode(LIGHT, INPUT);

 
  myPressure.begin(); 
  myPressure.setModeBarometer(); 
  myPressure.setOversampleRate(7); 
  myPressure.enableEventFlags(); 

  
  myHumidity.begin();

  seconds = 0;
  lastSecond = millis();

  // attach external interrupt pins to IRQ functions
  attachInterrupt(0, rainIRQ, FALLING);
  attachInterrupt(1, wspeedIRQ, FALLING);

  // turn on interrupts
  interrupts();

  

}



void calcWeather()
{
  
  winddir = get_wind_direction();

 
  float temp = 0;
  for (int i = 0 ; i < 120 ; i++)
    temp += windspdavg[i];
  temp /= 120.0;
  windspdmph_avg2m = temp;

  
  long sum = winddiravg[0];
  int D = winddiravg[0];
  for (int i = 1 ; i < WIND_DIR_AVG_SIZE ; i++)
  {
    int delta = winddiravg[i] - D;

    if (delta < -180)
      D += delta + 360;
    else if (delta > 180)
      D += delta - 360;
    else
      D += delta;

    sum += D;
  }
  winddir_avg2m = sum / WIND_DIR_AVG_SIZE;
  if (winddir_avg2m >= 360) winddir_avg2m -= 360;
  if (winddir_avg2m < 0) winddir_avg2m += 360;

 
  windgustmph_10m = 0;
  windgustdir_10m = 0;

  for (int i = 0; i < 10 ; i++)
  {
    if (windgust_10m[i] > windgustmph_10m)
    {
      windgustmph_10m = windgust_10m[i];
      windgustdir_10m = windgustdirection_10m[i];
    }
  }

  
  humidity = myHumidity.getRH();
 

  
  temperature = myPressure.readTemp();
  

  
  rainin = 0;
  for (int i = 0 ; i < 60 ; i++)
    rainin += rainHour[i];

  
  pressure = myPressure.readPressure();


  light_lvl = get_light_level();

  
  batt_lvl = get_battery_level();
}


float get_light_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float lightSensor = analogRead(LIGHT);

  operatingVoltage = 3.3 / operatingVoltage; 

  lightSensor = operatingVoltage * lightSensor;

  return (lightSensor);
}


float get_battery_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float rawVoltage = analogRead(BATT);

  operatingVoltage = 3.30 / operatingVoltage; 

  rawVoltage = operatingVoltage * rawVoltage; 

  rawVoltage *= 4.90; 

  return (rawVoltage);
}


float get_wind_speed()
{
  float deltaTime = millis() - lastWindCheck; 

  deltaTime /= 1000.0; 

  float windSpeed = (float)windClicks / deltaTime; 

  windClicks = 0; 
  lastWindCheck = millis();

  windSpeed *= 1.492; 

  

  return (windSpeed);
}


int get_wind_direction()
{
  unsigned int adc;

  adc = analogRead(WDIR); 

 
  if (adc < 380) return (113);
  if (adc < 393) return (68);
  if (adc < 414) return (90);
  if (adc < 456) return (158);
  if (adc < 508) return (135);
  if (adc < 551) return (203);
  if (adc < 615) return (180);
  if (adc < 680) return (23);
  if (adc < 746) return (45);
  if (adc < 801) return (248);
  if (adc < 833) return (225);
  if (adc < 878) return (338);
  if (adc < 913) return (0);
  if (adc < 940) return (293);
  if (adc < 967) return (315);
  if (adc < 990) return (270);
  return (-1); 
}




void printWeather(char label[], float value, int decPlaces = 0)
{
  Serial.print(label);
  Serial.print(value, decPlaces);
  Serial.println();
}

void loop() 



{
  
  if (millis() - lastSecond >= 1000)
  {
    digitalWrite(STAT1, HIGH); 

    lastSecond += 1000;

   
    if (++seconds_2m > 119) seconds_2m = 0;

   
    float currentSpeed = get_wind_speed();
    windspeedmph = currentSpeed;
    
    int currentDirection = get_wind_direction();
    windspdavg[seconds_2m] = (int)currentSpeed;
    winddiravg[seconds_2m] = currentDirection;
    

    
    if (currentSpeed > windgust_10m[minutes_10m])
    {
      windgust_10m[minutes_10m] = currentSpeed;
      windgustdirection_10m[minutes_10m] = currentDirection;
    }

    
    if (currentSpeed > windgustmph)
    {
      windgustmph = currentSpeed;
      windgustdir = currentDirection;
    }

    if (++seconds > 59)
    {
      seconds = 0;

      if (++minutes > 59) minutes = 0;
      if (++minutes_10m > 9) minutes_10m = 0;

      rainHour[minutes] = 0; 
      windgust_10m[minutes_10m] = 0; 
    }

  
    

    digitalWrite(STAT1, LOW); 
  }

  delay(100);

calcWeather();


printWeather("winddir=" , winddir);
printWeather("windspeedmph=" , windspeedmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgusdir=" , windgustdir);
printWeather("windspdmph_avg2m=" , winddir_avg2m);
printWeather("winddir_avg2m=" , rainin , 2);
printWeather("windgustmph_10m=" , windgustmph_10m, 1);
printWeather("windgustdir_10m=" , windgustdir_10m);
printWeather("humidity=" , humidity, 1);
printWeather("tempf=" , temperature, 1);
printWeather("rainin=" , rainin , 2);
printWeather("dailyrainin=" , dailyrainin, 2);
printWeather("pressure=" , pressure, 2);
printWeather("batt_lvl=" , batt_lvl, 2);
printWeather("light_lvl=" , light_lvl, 2);
Serial.println();


}





Serial Port prints:

winddir=315
windspeedmph=0.0
windgustmph=0.0
windgustmph=0.0
windgusdir=0
windspdmph_avg2m=359
winddir_avg2m=0.00
windgustmph_10m=0.0
windgustdir_10m=0
humidity=33.8
tempf=-999.0
rainin=0.00
dailyrainin=0.00
pressure=101929.00
batt_lvl=4.53
light_lvl=0.02

and the new line

My problem now is with the Processing code because Iā€™m trying to build a simple GUI for reading each variable to a written parameter like
Wind direction: 135
Wind Speed: 0.0
ā€¦

Processing code is:

import processing.serial.*;


float winddir;
float windspeedmph;
float windgustmph;
float windgustdir;
float windspdmph_avg2m;
float winddir_avg2m;
float windgustmph_10m;
float windgustdir_10m;
float humidity;
float tempf;
float rainin;
float dailyrainin;
float pressure;
float batt_lvl;
float light_lvl;

String myString = null;
Serial myPort;





void setup()
{
size(700,700);
String portName = Serial.list()[5]; 
myPort = new Serial(this, portName, 9600);
myPort.clear();
myString = myPort.readStringUntil('\n');
myString = null;
}
void draw()
{
  while ( myPort.available() > 0) 
  {  
  myString = myPort.readStringUntil('\n'); 
  if (myString != null) {
    println(myString);
    
    String myString = myPort.readString();
    
      String[] q = splitTokens(myString);
      
    
    
    
    
      winddir = parseFloat(q[0]);
      windspeedmph = parseFloat(q[1]);
      windgustmph = parseFloat(q[2]);
      windgustdir = parseFloat(q[3]);
      windspdmph_avg2m = parseFloat(q[4]);
      winddir_avg2m = parseFloat(q[5]);
      windgustmph_10m = parseFloat(q[6]);
      windgustdir_10m = parseFloat(q[7]);
      humidity = parseFloat(q[8]);
      tempf = parseFloat(q[9]);
      rainin = parseFloat(q[10]);
      dailyrainin = parseFloat(q[11]);
      pressure = parseFloat(q[12]);
      batt_lvl = parseFloat(q[13]);
      light_lvl = parseFloat(q[14]);

      
background(0);
textSize(20);
text("Wind direction:",50,50);
text(winddir,250,50);
text("Wind speed:" ,50,100);
text(windspeedmph,250,100);
text("Wind gust:",50,150);
text(windgustmph,250,150);
text("Wind gust direction:",50,200);
text(windgustdir,250,200);
text("Humidity:",50,250);
text(humidity,250,250);
text("Temperature",50,300);
text(tempf,250,300);
text("Rainfall:",50,350);
text(rainin,250,350);
text("Daily rain:",50,400);
text(dailyrainin,250,400);
text("Pressure",50,450);
text(pressure,250,450);
text("Battery level:",50,500);
text(batt_lvl,250,500);
text("Light level:",50,550);
text(light_lvl,250,550);

     
   }
  } 
}

And now the error to this one is: NullPointerException (line 46)
How can I solve this one?(I will repeat myself -I appreciate a lot your help :pray:)

@fdinumario,

If you are using your code as isā€¦

On Arduino terminate data string with a single:
Serial.print('!');

Processing:
myPort.readStringUntil('!');

Look up all the references and understand why your code was not working and why the above works.

I did get this workingā€¦

:)

Iā€™ve changed Ard code and Processing and I still have same error NullPointerException (line 46)

import processing.serial.*;


float winddir;
float windspeedmph;
float windgustmph;
float windgustdir;
float windspdmph_avg2m;
float winddir_avg2m;
float windgustmph_10m;
float windgustdir_10m;
float humidity;
float tempf;
float rainin;
float dailyrainin;
float pressure;
float batt_lvl;
float light_lvl;

String myString = null;
Serial myPort;





void setup()
{
size(700,700);
String portName = Serial.list()[5]; 
myPort = new Serial(this, portName, 9600);
myPort.clear();
myString = myPort.readStringUntil('\n');
myString = null;
}
void draw()
{
  while ( myPort.available() > 0) 
  {  
  myString = myPort.readStringUntil('!'); 
  if (myString != null) {
    println(myString);
    
    String myString = myPort.readString();
    
      String[] q = splitTokens(myString);
      
    
    
    
    
      winddir = parseFloat(q[0]);
      windspeedmph = parseFloat(q[1]);
      windgustmph = parseFloat(q[2]);
      windgustdir = parseFloat(q[3]);
      windspdmph_avg2m = parseFloat(q[4]);
      winddir_avg2m = parseFloat(q[5]);
      windgustmph_10m = parseFloat(q[6]);
      windgustdir_10m = parseFloat(q[7]);
      humidity = parseFloat(q[8]);
      tempf = parseFloat(q[9]);
      rainin = parseFloat(q[10]);
      dailyrainin = parseFloat(q[11]);
      pressure = parseFloat(q[12]);
      batt_lvl = parseFloat(q[13]);
      light_lvl = parseFloat(q[14]);

      
background(0);
textSize(20);
text("Wind direction:",50,50);
text(winddir,250,50);
text("Wind speed:" ,50,100);
text(windspeedmph,250,100);
text("Wind gust:",50,150);
text(windgustmph,250,150);
text("Wind gust direction:",50,200);
text(windgustdir,250,200);
text("Humidity:",50,250);
text(humidity,250,250);
text("Temperature",50,300);
text(tempf,250,300);
text("Rainfall:",50,350);
text(rainin,250,350);
text("Daily rain:",50,400);
text(dailyrainin,250,400);
text("Pressure",50,450);
text(pressure,250,450);
text("Battery level:",50,500);
text(batt_lvl,250,500);
text("Light level:",50,550);
text(light_lvl,250,550);

     
   }
  } 
}

Line 46 with the splitTokens, why is there 2nd readString just before it. I think the first readString is correct and the 2nd shouldnā€™t be there. Think remove the whole line.

1 Like

It should not and will always be NULL.

@fdinumario Did you look at the example I provided?

parseFloat() will not work on any of the above strings; you are converting a float in a String to a float as a float typeā€¦ canā€™t do that with the extra text you have.

What do you think you need to do to correct this?

There may be other errorsā€¦
I did get this working but fixed everything that was not.

Keep working at it!

References:

:)

now I have no error on Processing code but on display window all parameters are NaN and I suppose is because of parseFloat()

You are correct!

I often use simple examples to explore how the code is working:

What needs to be done to correct this?
Think about the solution before looking at the code.
Take a look at code (Arduino and Processing) and consider how to proceed.

Give it some thought.

:)

I read that is not possible this conversion , but what solution I can use only to take values of variables and write on display window ? or I should attribute to each value after ā€œ=ā€ a parameter?

I modified the Processing code and I used .toString function

import processing.serial.*;


float winddir;
float windspeedmph;
float windgustmph;
float windgustdir;
float windspdmph_avg2m;
float winddir_avg2m;
float windgustmph_10m;
float windgustdir_10m;
float humidity;
float tempf;
float rainin;
float dailyrainin;
float pressure;
float batt_lvl;
float light_lvl;

a1 = winddir.toString();
a2 = windspeedmph.toString();
a3 = windgustmph.toString();
a4 = windgustdir.toString();
a5 = windspdmph_avg2m.toString();
a6 = winddir_avg2m.toString();
a7 = windgustmph_10m.toString();
a8 = windgustdir_10m.toString();
a9 = humidity.toString();
a10 = tempf.toString();
a11 = rainin.toString();
a12 = dailyrainin.toString();
a13 = pressure.toString();
a14 = batt_lvl.toString();
a15 = light_lvl.toString();

String myString = null;
Serial myPort;






void setup()
{
size(700,700);
String portName = Serial.list()[5]; 
myPort = new Serial(this, portName, 9600);
myPort.clear();
myString = myPort.readStringUntil('!');
myString = null;
}
void draw()
{
  while ( myPort.available() > 0) 
  {  
  myString = myPort.readStringUntil('!'); 
  if (myString != null) {
    println(myString);
    
    
    
      String[] q = splitTokens(myString);

      
    
    
    
    
      a1 = parseFloat(q[0]);
      a2 = parseFloat(q[1]);
      a3 = parseFloat(q[2]);
      a4 = parseFloat(q[3]);
      a5 = parseFloat(q[4]);
      a6 = parseFloat(q[5]);
      a7 = parseFloat(q[6]);
      a8 = parseFloat(q[7]);
      a9 = parseFloat(q[8]);
      a10 = parseFloat(q[9]);
      a11 = parseFloat(q[10]);
      a12 = parseFloat(q[11]);
      a13 = parseFloat(q[12]);
      a14 = parseFloat(q[13]);
      a15 = parseFloat(q[14]);

      
background(0);
textSize(20);
text("Wind direction:",50,50);
text(a1,250,50);
text("Wind speed:" ,50,100);
text(a2,250,100);
text("Wind gust:",50,150);
text(a3,250,150);
text("Wind gust direction:",50,200);
text(a4,250,200);
text("Humidity:",50,250);
text(a5,250,250);
text("Temperature",50,300);
text(a6,250,300);
text("Rainfall:",50,350);
text(a7,250,350);
text("Daily rain:",50,400);
text(a8,250,400);
text("Pressure",50,450);
text(a9,250,450);
text("Battery level:",50,500);
text(a10,250,500);
text("Light level:",50,550);
text(a11,250,550);

     
   }
  } 
}

Now I have another error : Syntax Error - Missing operator, semicolon, or ā€˜}ā€™ near ā€˜setupā€™? line 44 where is void setup()

The lines like this are not correct.

That line, and all the others like it, is assigning a value, needs to be inside a function, probably setup.
a1 is not declared, probably ā€œString a1;ā€

same error (Syntax Error - Missing operator, semicolon, or ā€˜}ā€™ near ā€˜setupā€™? - line 44) and Iā€™ve change the code to

import processing.serial.*;


Float winddir;
Float windspeedmph;
Float windgustmph;
Float windgustdir;
Float windspdmph_avg2m;
Float winddir_avg2m;
Float windgustmph_10m;
Float windgustdir_10m;
Float humidity;
Float tempf;
Float rainin;
Float dailyrainin;
Float pressure;
Float batt_lvl;
Float light_lvl;

 a1 = winddir.toString();
 a2 = windspeedmph.toString();
 a3 = windgustmph.toString();
 a4 = windgustdir.toString();
 a5 = windspdmph_avg2m.toString();
 a6 = winddir_avg2m.toString();
 a7 = windgustmph_10m.toString();
 a8 = windgustdir_10m.toString();
 a9 = humidity.toString();
 a10 = tempf.toString();
 a11 = rainin.toString();
 a12 = dailyrainin.toString();
 a13 = pressure.toString();
 a14 = batt_lvl.toString();

String myString = null;
Serial myPort;







void setup()
{
size(700,700);
String portName = Serial.list()[5]; 
myPort = new Serial(this, portName, 9600);
myPort.clear();
myString = myPort.readStringUntil('!');
myString = null;
}
void draw()
{
  while ( myPort.available() > 0) 
  {  
  myString = myPort.readStringUntil('!'); 
  if (myString != null) {
    println(myString);
    
    
    
      String[] q = splitTokens(myString);

      
    
    
    
    
      String a1 = parseFloat(q[0]);
      String a2 = parseFloat(q[1]);
      String a3 = parseFloat(q[2]);
      String a4 = parseFloat(q[3]);
      String a5 = parseFloat(q[4]);
      String a6 = parseFloat(q[5]);
      String a7 = parseFloat(q[6]);
      String a8 = parseFloat(q[7]);
      String a9 = parseFloat(q[8]);
      String a10 = parseFloat(q[9]);
      String a11 = parseFloat(q[10]);
      String a12 = parseFloat(q[11]);
      String a13 = parseFloat(q[12]);
      String a14 = parseFloat(q[13]);
      String a15 = parseFloat(q[14]);

      
background(0);
textSize(20);
text("Wind direction:",50,50);
text(a1,250,50);
text("Wind speed:" ,50,100);
text(a2,250,100);
text("Wind gust:",50,150);
text(a3,250,150);
text("Wind gust direction:",50,200);
text(a4,250,200);
text("Humidity:",50,250);
text(a5,250,250);
text("Temperature",50,300);
text(a6,250,300);
text("Rainfall:",50,350);
text(a7,250,350);
text("Daily rain:",50,400);
text(a8,250,400);
text("Pressure",50,450);
text(a9,250,450);
text("Battery level:",50,500);
text(a10,250,500);
text("Light level:",50,550);
text(a11,250,550);

     
   }
  } 
}

Try something like this:

import processing.serial.*;

float light_lvl;

String a1;
String a2;

void setup(){}

void draw(){
  a1 = winddir.toString();
  a2 = windspeedmph.toString();
}

Iā€™ve changed the code to :

import processing.serial.*;


Float winddir;
Float windspeedmph;
Float windgustmph;
Float windgustdir;
Float windspdmph_avg2m;
Float winddir_avg2m;
Float windgustmph_10m;
Float windgustdir_10m;
Float humidity;
Float tempf;
Float rainin;
Float dailyrainin;
Float pressure;
Float batt_lvl;
Float light_lvl;

 String a1 = winddir.toString();
 String a2 = windspeedmph.toString();
 String a3 = windgustmph.toString();
 String a4 = windgustdir.toString();
 String a5 = windspdmph_avg2m.toString();
 String a6 = winddir_avg2m.toString();
 String a7 = windgustmph_10m.toString();
 String a8 = windgustdir_10m.toString();
 String a9 = humidity.toString();
 String a10 = tempf.toString();
 String a11 = rainin.toString();
 String a12 = dailyrainin.toString();
 String a13 = pressure.toString();
 String a14 = batt_lvl.toString();

String myString = null;
Serial myPort;







void setup(){}
{
size(700,700);
String portName = Serial.list()[5]; 
myPort = new Serial(this, portName, 9600);
myPort.clear();
myString = myPort.readStringUntil('!');
myString = null;
}
void draw(){
  
 a1 = winddir.toString();
 a2 = windspeedmph.toString();
 a3 = windgustmph.toString();
 a4 = windgustdir.toString();
 a5 = windspdmph_avg2m.toString();
 a6 = winddir_avg2m.toString();
 a7 = windgustmph_10m.toString();
 a8 = windgustdir_10m.toString();
 a9 = humidity.toString();
 a10 = tempf.toString();
 a11 = rainin.toString();
 a12 = dailyrainin.toString();
 a13 = pressure.toString();
 a14 = batt_lvl.toString();
}
{
  while ( myPort.available() > 0) 
  {  
  myString = myPort.readStringUntil('!'); 
  if (myString != null) {
    println(myString);
    
    
    
      String[] q = splitTokens(myString);

      
    
    
    
    
       a1 = parseFloat(q[0]);
       a2 = parseFloat(q[1]);
       a3 = parseFloat(q[2]);
       a4 = parseFloat(q[3]);
       a5 = parseFloat(q[4]);
       a6 = parseFloat(q[5]);
       a7 = parseFloat(q[6]);
       a8 = parseFloat(q[7]);
       a9 = parseFloat(q[8]);
       a10 = parseFloat(q[9]);
       a11 = parseFloat(q[10]);
       a12 = parseFloat(q[11]);
       a13 = parseFloat(q[12]);
       a14 = parseFloat(q[13]);
       a15 = parseFloat(q[14]);

      
background(0);
textSize(20);
text("Wind direction:",50,50);
text(a1,250,50);
text("Wind speed:" ,50,100);
text(a2,250,100);
text("Wind gust:",50,150);
text(a3,250,150);
text("Wind gust direction:",50,200);
text(a4,250,200);
text("Humidity:",50,250);
text(a5,250,250);
text("Temperature",50,300);
text(a6,250,300);
text("Rainfall:",50,350);
text(a7,250,350);
text("Daily rain:",50,400);
text(a8,250,400);
text("Pressure",50,450);
text(a9,250,450);
text("Battery level:",50,500);
text(a10,250,500);
text("Light level:",50,550);
text(a11,250,550);

     
   }
  } 
}

And now the error is : cannot convert from float to String - line 86

You need to do more investigation yourself. Find line 86, put a print statement before it to show you what it is that wonā€™t convert. How is it different from what you intended? Make another very small sketch with fixed data and try the conversion.

now I used trim() to separate the actual value from parameter but also I got another error -line 65 NullPointerException . Any suggestions ?

import processing.serial.*;


String winddir;
String windspeedmph;
String windgustmph;
String windgustdir;
String windspdmph_avg2m;
String winddir_avg2m;
String windgustmph_10m;
String windgustdir_10m;
String humidity;
String tempf;
String rainin;
String dailyrainin;
String pressure;
String batt_lvl;
String light_lvl;

 String a1 = trim(winddir);
 String a2 = trim(windspeedmph);
 String a3 = trim(windgustmph);
 String a4 = trim(windgustdir);
 String a5 = trim(windspdmph_avg2m);
 String a6 = trim(winddir_avg2m);
 String a7 = trim(windgustmph_10m);
 String a8 = trim(windgustdir_10m);
 String a9 = trim(humidity);
 String a10 = trim(tempf);
 String a11 = trim(rainin);
 String a12 = trim(dailyrainin);
 String a13 = trim(pressure);
 String a14 = trim(batt_lvl);

String myString = null;
Serial myPort;

void setup()
{
size(700,700);
String portName = Serial.list()[5]; 
myPort = new Serial(this, portName, 9600);
myPort.clear();
myString = myPort.readStringUntil('!');
myString = null;
}
void draw()
{
  while ( myPort.available() > 0) 
  {  
  myString = myPort.readStringUntil('!'); 
  if (myString != null) {
    println(myString);
    
    String myString = myPort.readString();
    
      
        
     

      
background(0);
textSize(20);
text("Wind direction:",50,50);
text(a1,250,50);
text("Wind speed:" ,50,100);
text(a2,250,100);
text("Wind gust:",50,150);
text(a3,250,150);
text("Wind gust direction:",50,200);
text(a4,250,200);
text("Humidity:",50,250);
text(a5,250,250);
text("Temperature",50,300);
text(a6,250,300);
text("Rainfall:",50,350);
text(a7,250,350);
text("Daily rain:",50,400);
text(a8,250,400);
text("Pressure",50,450);
text(a9,250,450);
text("Battery level:",50,500);
text(a10,250,500);
text("Light level:",50,550);
text(a11,250,550);

     
   }
  } 
}

Same advice. Add print statements to show the values. Something is not as you intend.

Iā€™ve added print argument and the value in console is null