RStudio with an Arduino


#1

Hi folk!
I want to build histogram from Arduino sensor data.
For this, I want to using Arduino Uno and RStudio how plotter.

RStudio code for Serial read:

con <- serialConnection(name = "get_temps",
                        port = "COM10",
                        mode = "115200,n,8,1",
                        buffering = "none",
                        newline = 1,
                        translation = "cr")
isOpen(con)
open(con)
isOpen(con)

### grab data from serial port
read.serialConnection(con)

RStudio Result:
I get:

[1]
475 \ n, 474 \ n, 475 \ n, 475 \ n, 475 \ n, 475 \ n, 475 \ n, 475 \ n, 475 \ n, 475 \ n, 474 \ n, 474 \ n, 474 \ n, 474 \ n, 474 \ n, 473 \ n, 473 \ n, 473 \ n, 473 \ n, 472 \ n, 472 \ n, 472 \ n, 471 \ n, 471 \ n, 471 \ n , 470 \ n, 470 \ n, 469 \ n, 469 \ n, 468 \ n, 468 \ n, 467 \ n, 467 \ n, 466 \ n, 465 \ n, 465 \ n, 464 \ n, 463 \ n, 463 \ n, 462 \ n, 461 \ n, 460 \ n, 460 \ n, 459 \ n, 458 \ n, 457 \ n, 456 \ n, 455 \ n, 455 \ n, 454 \ n , 453 \ n, 452 \ n, 451 \ n, 450 \ n, 449 \ n, 448 \ n, 447 \ n, 447 \ n, 446 \ n, 445 \ n, 444 \ n, 443 \ n, 442 \ n, 441 \ n, 441 \ n, 440 \ n, 439 \ n, 438 \ n, 437 \ n, 436 \ n, 435 \ n, 435 \ n, 434 \ n, 433 \ n, 432 \ n , 430 \ n, 429 \ n, 428 \ n, 426 \ n, 425 \ n, 424 \ n, 422 \ n, 421 \ n, 420 \ n, 419 \ n, 418 \ n, 417 \ n, 416 \ n, 415 \ n, 414 \ n, 413 \ n, 412 \ n, 411 \ n, 411 \ n, 410 \ n, 409 \ n, 409 \ n, 409 \ n, 408 \ n, 407 \ n , 407 \ n, 407 \ n, 406 \ n, 406 \ n, 405 \ n, 405 \ n, 404 \ n, 404 \ n, 404 \ n, 403 \ n, 403 \ n, 403 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n , 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 402 \ n, 403 \ n, 403 \ n, 403 \ n, 403 \ n, 403 \ n, 404 \ n, 404 \ n, 404 \ n, 404 \ n, 404 \ n, 405 \ n, 405 \ n, 405 \ n, 405 \ n, 405 \ n, 405 \ n, 405 \ n, 406 \ n , 406 \ n, 406 \ n, 406 \ n, 406 \ n, 406 \ n, 407 \ n, 407 \ n, 407 \ n, 408 \ n, 408 \ n, 409 \ n, 410 \ n, 410 \ n, 411 \ n, 412 \ n, 4 12 \ n …

I don’t know how to convert this data to 475, 474, 475, 475, 475, 475, … without “\n”. After this, I want to create histogram or diagram.


#2

Replace consecutive non-digits by a single whitespace and then split there:

library(magrittr)
library(stringr)

read.serialConnection(con) %>%
  str_replace_all("[^[:digit:]]+", " ") %>%
  str_split(" ", simplify = TRUE) %>%
  as.numeric()

You could also directly use the pattern appearing " \n, " to split.
There could be better solutions, but I don’t know anything about Arduino sensor data.


#3

Thank you! It works.


#4

Is it possible to do on RStudio similarly code to interact with the serial protocol how on Python from https://impfs.github.io/review/ ?


#5

For reading Arduino Serial port I use “read.serialConnection(con)”.
In order to read data by R , I must output data on Serail (Arduino command “Serial.println(data[i]);”) in the format (how аbove 475 \ n, 474 \ n, …). Where, “\n” - it’s new string.
RStudio Result:
[1]
475 \ n, 474 \ n, 475 \ n, 475 \ n, 475 \ n,
All array, I receive how column.

When, I outputing the data how rows (Arduino command “Serial.print(data[i]);”) I get:
[1] " "

Code for Arduino:
void printData() {
for (int i = 0; i < SPEC_CHANNELS; i++) {
// Serial.println(data[i]); // how column
Serial.print(data[i]); // how row
Serial.print(’,’);
}

How to force the RStudio a read string (array) of data containing 288 values?
Thanks.