Serial Talk- Sending data to ESP32 (and Arduino Uno) from Java in cmd
While you are playing with ESP32, you would have seen that for each time you have to make a sketch, compile and upload to run your desired electronic task. Just for a recall, we had discussed about handling the inbuilt LED in ESP32 and that of Arduino Uno earlier. We made the LED in ESP32 which rely on the pin number 2 to blink for every 1 second. For that sketch, it is fixed. To change the duration, we have to edit the sketch again, compile and upload it. What a tired job right?

What if we are able to control the ESP32 through a GUI? Now you have got the thing. Yes I’m talking about Serial Communication. We’ve already discussed about doing a Serial Communication from ESP32 to Java in cmd in this writing.
Before going to adjust the blinking duration, let us first see how to send input to ESP32 from Java in cmd. If we succeed that, we shall do whatever the command to our board. Initiually, we need to write a sketch that should enable the board to receive data through Serial Communication.
Initialize the variable that is going to be assigned with the input values
char operation = 0;Then we have to define the setup() and loop() which is clearly explained here.
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}Then, in our loop(), we have to define the conditions.
void loop() {
//Read from Serial if Data is Available
if (Serial.available() > 0)
{
operation = Serial.read();
}Remember that the data we sent through from the client are byte cast char, therefore, the value that get sent through are ASCII numerical codes. All we need to do to use this data is to convert / compare it back with a char type value.
Now to make the LED off,
//LED Off
if(operation == ‘0’)
digitalWrite(13, LOW);To make the LED to glow with out blink
//LED On
else if(operation == ‘1’)
digitalWrite(13, HIGH);Then finally to make it blink
//LED Blink
else if(operation == ‘2’)
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}So now we have set the sketch for our ESP32. What should we, the noble Java Programmers do now?
To engage in serial communication, we need to add a library called jSSC (Java Simple Serial Connector). Let the class name be SerialCommunication.

/**
*
* @author A Nickson Joram
*/
import java.util.Scanner;
import jssc.SerialPort;
import jssc.SerialPortList;
import jssc.SerialPortException;
public class SerialCommunication{
public static void main(String[] args) throws InterruptedException{
//Define Port
SerialPort serialPort = new SerialPort(“COM4”); //My port handing ESP32 was COM4
try{
//Open Serial Port and set Parameters
serialPort.openPort();
serialPort.setParams(9600,8,1,0);
//Create Scanner to read from Console
Scanner reader = new Scanner(System.in);After that, the system have to instruct that which is for what purpose.
while(true){
System.out.print(“Operation (0 = Off / 1 = On / 2 = Blink ): “);Remember that through Serial, we are sending bytes through, therefore, we must convert the char input we got into byte form. In another word, the value represented in the system encoding, aka ASCII code. 0 is 48, 1 is 49, 2 is 50… luckily, we can type cast in Java. Mess around with this program to see the different ASCII representation for each of your character input!
After that,
if(reader.hasNext()){
String buf = reader.next();
char buf2[] = buf.toCharArray();
serialPort.writeByte((byte)buf2[0]);
System.out.println((byte) buf2[0] + “ <= sent!”);
}Then we have to close the try catch as this.
catch (SerialPortException ex){
System.out.println(ex);
}So the output?
Yeah, with respect of your input among 0, 1 and 2, the task shall be executed in your ESP32. This program can be enhanced accordance to our need.
Java programmers now may have the smile which is unique for us. Hell yeah, we are immortal. Aren’t we?
