Serial connection between Processing and Arduino doesn’t work

Hello,
I’m trying to send data from Arduino to processing via serial.
I’m getting no errors on either side, and all seems to work.
Just one problem : the data doesn’t arrive in processing…

Here is the code from Arduino:

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

void loop()
{
Serial.println("Hello world!");
delay(100);
}

Here is the code from Processing:

import processing.serial.*;


Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port

void setup()
{
  // I know that the first port in the serial list on my mac
  // is Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
   //change the 0 to a 1 or 2 etc. to match your port
  String arduinoPort = Serial.list()[0];
  myPort = new Serial(this, arduinoPort, 115200);
}
void serialEvent(Serial myPort) 
{
  val = myPort.readStringUntil('\n');         // read it and store it in val
  println(val); //print it out in the console
}

Thanks in advance for your help!
Mathis

Hi

This is your example it’s working

https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all

Hello @Mathis,

An initial observation is that your baud rates do not match:

The baud rates should be the same.

:)

Thanks it was just a mistake in my copy paste.
It still doesn’t work, void serialEvent() is never called, for whatever reason :confused:

I’ve just found the problem.
I was using an esp8266 which seems not to work with Processing unfortunately :frowning: .
I’ve changed it with an arduino board and all is working properly.

2 Likes

Hi @Mathis, I have two NodeMCU boards. I think these are simply ESP8266 mounted on a larger PCB. There are a few differences from Arduino, but for me nothing to stop it working with Processing. I’ve found the most significant difference is that the ESP program does not reset when Processing (or anything on the PC) opens the port. So I never see any prints from the setup() function. This code prints everything the ESP sends. I’m not using serialEvent.

import processing.serial.*;
Serial myPort;
int val;

void setup() {
  frameRate(40);
  String portName = "COM4";
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  int val;
  while (myPort.available() > 0) {
    val = myPort.read();
    print((char) val);
  }
}

Output:

IP: 192.168.1.135, Temp: = 20 *C, Humid: = 74 %, Temps: = 20, 20
IP: 192.168.1.135, Temp: = 20 *C, Humid: = 74 %, Temps: = 20, 20
IP: 192.168.1.135, Temp: = 20 *C, Humid: = 74 %, Temps: = 20, 20

1 Like

This version of your sketch works. Note the four //!

import processing.serial.*;

Serial myPort;
String val;

void setup()
{
  frameRate(40);              //!
  String arduinoPort = "COM4";
  myPort = new Serial(this, arduinoPort, 9600);
  myPort.bufferUntil('\n');   //!
}
void serialEvent(Serial myPort) {
  val = myPort.readString();  //!
  print(val);
}
void draw(){}                 //!
1 Like

Hello @RichardDL,

Why this frameRate(40) ?

Just curious…

In this example it will even work with noLoop().

Reference:
https://processing.org/reference/libraries/serial/Serial_serialEvent_.html

:)

I think I’ve seen somewhere that you can’t repeatedly do serial.available() at infinite speed. In any case not good do something like that too fast. Have to give the serial hardware and software time to do its own thing. 40 gives 25 mS/cycle. That’s probably faster than most users/applications care about and gives time for 25 chars to arrive at 9600 baud.

When I added the loop(), it started working, frameRate(40) was better. After I added the .bufferUntil I didn’t go back and try without the first two fixes.

1 Like