Sending strings from processing to arduino - arduino

I'm trying to set up communications between my PC, and my Arduino with the Processing environment, but the Arduino doesn't seem to be getting any of the messages I send. I doubled checked, and I know I can receive messages from the Arduino, but I can't send anything back. Does anyone know how to fix this?
Here's my test code for processing:
import processing.serial.*;
Serial myPort;
void setup(){
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw(){
myPort.write("test");
while (myPort.available() > 0) {
String inByte = myPort.readString();
println(inByte);
}
}
Here's my test code for the Arduino:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
String data;
void loop() {
// put your main code here, to run repeatedly:
//Serial.println("is running");
if (Serial.available() > 0) {
// read the incoming byte:
data = Serial.readString();
// say what you got:
Serial.print("I received: ");
Serial.println(data);
}
}
I'd appreciate any help I can get! Thanks!

Okay, after looking into several different posts on the Arduino forum, I figured out what the problem is. The function processing uses to send data over serial does not automatically include a return character at the end of the string. This is important because the Arduino won't read from the serial buffer until it sees that return character. All I had to do was add "\r\n" to the end of each string I sent over serial, and that solved the problem!

Related

esp8266 soft AP displays the wrong name

device: esp8266 (nodeMCU, esp-12E)
I have a esp8266 that previously run micropython on it. and now i wanted to return back to arduino programing.
using the example code on the docs:https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/soft-access-point-examples.html
#include <ESP8266WiFi.h>
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("setting up soft-AP");
boolean result = WiFi.softAP("ESPsoftAP", "11234");
if (result = true)
{
Serial.println("ready!");
}
else
{
Serial.println("failed!");
}
}
void loop(){
Serial.printf("station connected = %d\n", WiFi.softAPgetStationNum());
delay(3000);
}
but the access point that shows up is named "MicroPython-7b15141" and the password does not work.
which to me seems like the line
boolean result = WiFi.softAP("ESPsoftAP", "11234");
was not able to do its function for some reason.
however on the serial monitor, the text "station connected = 0" does show.
so what went wrong here?
in your sketch the Wifi parameters are not updated because the Wifi password is too short, therefore it keeps the previous ssid and password.
Try this, now it works: All the best:
boolean result = WiFi.softAP("ESPsoftAP", "1234567890")

Processing - missing serial data from Arduino, using readStringUntil()

I've been trying to create an oscilloscope for serial data from my Arduino. In the Arduino serial plotter I can get a good waveform up to suitable frequencies, but when I try send the data to Processing it doesn't receive all the data from the Arduino. Is there a way around this?
Arduino
const int analogIn = A6;
int integratorOutput = 0;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
integratorOutput = analogRead(analogIn);
Serial.println(integratorOutput);
}
Processing
void serialEvent (Serial port) {
// get the ASCII string:
String inString = port.readStringUntil('\n');
if (inString != null) {
inString = trim(inString); // trim off whitespaces.
inByte = float(inString); // convert to a number.
inByte = map(inByte, 0, 1023, 100, height-100); //map to the screen height.
println(inByte);
newData = true;
}
}
Thanks!
It's because readStringUntil is a non blocking function. Let's assume Arduino is printing a line: 12345\n The serial port at 115200 bits per seconds is relatively slow, so it's possible that the at some point the receiving buffer contains only a part of the message, for example: 1234. When the port.readStringUntil('\n') is executed it doesn't encounter a \n in the buffer so it fails and returns a NULL. You can solve this problem by using bufferUntil as in this example

Receive Arduino commands from Bluetooth module

First off this is my first Arduino project but I have coded basic c++ before. Anyway I am struggling to get the Arduino to acknowledge when save data to a variable. I know it's receiving the data but its not getting appended to my data variable. Here's the code and if I could get a basic explanation of what I did wrong I would appreciate it.
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1);
//declair pins
void setup() {
Serial.begin(9600);
//setup pins
}
void loop() {
String data;
bool run = false;
data = "";
while (Serial.available()) {
char inChar = (char)Serial.read();
Serial.print(inChar); //I know it is getting the sent data because prints here
if (inChar == '\n') {
Serial.print("Received Command: ");
if (data == "")
Serial.print("Data is NULL"); //this always prints too
Serial.print(data);
Serial.print('\n');
run = true;
break;
} else {
data += inChar;
}
}
if(run) {
//do stuff
}
}
I know there are plenty of websites I could copy paste from but I want to make my own and understand what I did wrong. I did read them though.
For example if I send command from my phone the output would be
command
Received Command: Data is NULL
The following segment will always appear null here on the first loop unless you receive characters other than newline when your serial begins listening because the program executes from top to bottom and you haven't given data a value yet here.
if (data == "") {
Serial.print("Data is NULL"); //this always prints too
}
P.S. This expression
String data;
data = "";
can be simplified to
String data ="";
More efficient.
I'll try to provide more information if I can figure out the issue once I get some sample data.

Use Serial in Arduino as variable

I would like to choose which Serial should be used on my Mega (e.g. Serial for development & testing, and then Serial3 for communication with another board). I came with this code:
HardwareSerial HWSerial = Serial; //or Serial1, Serial2, Serial3
void setup() {
HWSerial.begin(115200);
}
void loop() {
HWSerial.println("Hello world!");
delay(1000);
}
However, this produces only first two bytes and a NUL ("He␀").
What am I doing wrong?
Note: I know I could use
HardwareSerial* HWSerial = &Serial; //or Serial1, Serial2, Serial3
void setup() {
HWSerial->begin(115200);
}
void loop() {
HWSerial->println("Hello world!");
delay(1000);
}
which works. I was, however, in fact planning to use ArduinoJSON and the serializeJson(JSONObj, serial) function which won't accept a pointer, so I'm stuck there - and my approach compiles, but again, produces only first two bytes...

Reading Arduino int array with Processing

I have an arduino-processing communication problem.
I have arduino code that gives me X-and/Y coordinates from a touchScreen
and it works well, no problem with that, I got my X- and Y coordinates.
BUT I need to visualize that, I am trying to write code in Processing 3.0, so that I will be able to see on my computer, where the touchFolie was being touched.
So I need to send the x-y from arduino to processing so that I will be able to draw.
Does anyone know how can I send an integer array[X,Y] from arduino to processing ??
It will be helpful to have a play with the Serial library in Processing and the SerialRead example (in Processing > File > Examples > Libraries > serial > SimpleRead)
Let's say you're starting from scratch.
To send data from Arduino you need to open a Serial connection.
At this stage the only important details is the baud rate: how fast is the data flowing.
Here's a minimal Arduino data out example:
void setup() {
//initialize the Serial library with baud rate 115200
Serial.begin(115200);
}
void loop() {
//write data to the serial port
Serial.println("test");
delay(1000);
}
If you open Serial Monitor in the Arduino software and set the baud rate to 115200 you should see test printed once a second.
To read the same data in Processing, aside from specifying the baud rate, you must also specify the serial port (what you have selected in Tools > Port and is also listed at the bottom right of your current Arduino sketch):
import processing.serial.*;
void setup() {
//update the serial port index based on your setup
println(Serial.list());
Serial arduino = new Serial(this, Serial.list()[0], 115200);
arduino.bufferUntil('\n');
}
void draw() {
}
void serialEvent(Serial p) {
//read the string from serial
String rawString = p.readString();
println(rawString);
}
Notice that we tell Processing to buffer until it reaches a '\n' character so we don't have to worry about waiting for every single character and append it manually to a String. Instead using bufferUntil(), serialEvent() and readString() most of the work is done for us.
Now that you can send a String from Arduino and read it in Processing, you can do some String manipulation, like splitting a string into multiple using a separator via the split() function:
String xyValues = "12,13";
printArray(xyValues.split(","));
The last part would be converting the split values from String to int:
String xyValues = "12,13";
String[] xy = xyValues.split(",");
printArray(xy);
int x = int(xy[0]);
int y = int(xy[1]);
println("integer values: " + x + " , " + y);
So, in theory, you should be able to do something like this on Arduino:
int x,y;
void setup() {
//initialize serial
Serial.begin(115200);
}
void loop() {
//simulating the x,y values from the touch screen,
//be sure to replace with the actual readings from
//NOTE! If the screen returns values above 255, scale them to be from 0 to 255
x = map(analogRead(A0),0,1024,0,255);
y = map(analogRead(A1),0,1024,0,255);
//write the data to serial
Serial.print(x);
Serial.print(",");
Serial.print(y);
Serial.print("\n");
}
then on the Arduino side:
import processing.serial.*;
float x,y;
void setup() {
size(400,400);
//update the serial port index based on your setup
Serial arduino = new Serial(this, Serial.list()[0], 115200);
arduino.bufferUntil('\n');
}
void draw() {
background(0);
ellipse(x,y,10,10);
}
void serialEvent(Serial p) {
//read the string from serial
String rawString = p.readString();
//trim any unwanted empty spaces
rawString = rawString.trim();
try{
//split the string into an array of 2 value (e.g. "0,127" will become ["0","127"]
String[] values = rawString.split(",");
//convert strings to int
int serialX = int(values[0]);
int serialY = int(values[1]);
//map serial values to sketch coordinates if needed
x = map(serialX,0,255,0,width);
y = map(serialY,0,255,0,height);
}catch(Exception e){
println("Error parsing string from Serial:");
e.printStackTrace();
}
}
Note The Arduino code above will probably not solve your problem as it is, you need to integrate your touch sensor code, but hopefully it provides some hints on how you can tackle this.
Sending a String, then parsing it is one approach, but not the most efficient. If you have your x,y values in a 0-255 range, you could send just 2 bytes (each coordinate as a single char) instead of up to 8 bytes, but for now it may be much easier to play with Strings rather than jump into bytes straight away.
This tutorial will help you.
5 minutes and you are able to connect each others!
EDIT:
First of all look the first part of the tutorial ("From Arduino..." "...To processing").
In arduino you have just to send your coordinates in your Serial.
Serial.println(coordX);
Serial.println(coordY);
In processing you receive this coordinates as Text but you are able to convert it in Float with parseFloat() function.
This is the code you need in Processing to receive your coordinates and store them in variables.
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
float x = 0;
float y = 0;
boolean first = true;
setup() {
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw() {
if ( myPort.available() > 0) { // If data is available,
val = myPort.readStringUntil('\n'); // read it and store it in val
if (first) {
x = parseFloat(val);
first = false;
println("x= "+val); //print it out in the console
}
else {
y = parseFloat(val);
first = true;
println("y= "+val); //print it out in the console
}
}
I hope this will help you solve your problem.

Resources