Error in sending entire variable from raspberrypi (python) to arduino - arduino

I want to transfer a number"500" stored in a variable "int3" from python to arduino via serial communication.
The arduino reads the data using Serial.read(), but prints only "5".
Thanks in advance.
I have int3, byte 2 and byte 3 to be sent from python, but want arduino to print the value of int3.
import serial
import time
ser=serial.Serial('/dev/ttyACM0',9600)
int3 = 500
int3 = b'%d' %int3
while (1):
ser.write(int3)
ser.write(b'2')
ser.write(b'3')
#print type(ser.write)
time.sleep(1)
print(int3)
String r;
void setup(){
Serial.begin(9600);
// while(!Serial)
// {
// ;
// }
}
void loop(){
if(Serial.available() > 0){
r =(Serial.read() - 0); //conveting the value of chars to integer
Serial.print(r[0]);
delay(100);
//while(Serial.available () !=0) r=Serial.read();
}
}

Replace the if with a while loop:
String fromRaspberry;
void loop(){
char c;
while (Serial.available() > 0) {
c = Serial.read(); //read char
fromRaspberry += c; //add the read char to string
}
int x = fromRaspberry.toInt(); //convert string to int
Serial.println(x);
// clear the string for new input:
fromRaspberry = "";
}

Related

Sending array data from Processing to Arduino

I successfully managed to send a single integer from processing to Arduino but now I want to send an array of three integers and I can't get it working. I want to create a buzzer feedback with Arduino which processing will control which buzzer to activate. For example, the data send from processing should be [1,0,1] meaning sensor 1 and 3 should start working. The buzzers should be able to be activated simultaneously in case that [1,1,1] goes through.
This is the code I have so far:
I am trying to understand what data is being sent back to Arduino to know how to use it and I keep getting either a null value or a random integer.
I'm trying to learn how to do this so apologies if the code is bad.
Arduino
void setup(){
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop(){
if (Serial.available()){
const char data = Serial.read();
char noteBuzzer[] = {data};
for (int i = 0 ; i < sizeof(noteBuzzer); i++) {
}
Serial.print(noteBuzzer[1]);
}
}
Processing
import processing.serial.*;
String notes[];
String tempo[];
Serial myPort;
String val;
void setup(){
size(200,200);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
notes = loadStrings("data/notes.txt");
tempo = loadStrings("data/tempo.txt");
}
void draw() {
if (keyPressed == true)
{
if (key == '1') {
println("Start");
readNotes();
}
}
}
void readNotes(){
for (int i = 0 ; i < notes.length; i++) {
println(notes[i]);
//println(tempo[i]);
myPort.write(notes[i]);
delay(int(tempo[i])); //this will be the tempo?
if ( myPort.available() > 0)
{
val = myPort.readStringUntil('\n');
println("Arduino",val);
}
}
}
If you're data is an array that always has 3 items and each of those items are always either 1 or 0 (bits), you could store that whole data in a single byte (and still have 5 more bits to spare). Sending and receiving a byte is pretty simple with Arduino.
Here's a basic sketch that shows you how to flip 3 bits in a single byte:
// the state as a byte
byte buzzerState = 0B000;
void setup(){
textFont(createFont("Courier New",18),18);
}
void draw(){
background(0);
text("DEC: " + buzzerState +
"\nBIN:" + binary(buzzerState,3),10,40);
}
void keyPressed(){
if(key == '1'){
buzzerState = flipBit(buzzerState,0);
}
if(key == '2'){
buzzerState = flipBit(buzzerState,1);
}
if(key == '3'){
buzzerState = flipBit(buzzerState,2);
}
}
// flips a bit at a given index within a byte and returns updated byte
byte flipBit(byte state,int index){
int bit = getBitAt(state,index);
int bitFlipped = 1 - bit;
return setBitAt(state,index,bitFlipped);
}
// returns the integer value of a bit within a byte at the desired index
int getBitAt(byte b,int index){
index = constrain(index,0,7);
return b >> index & 1;
}
// sets an individual bit at a desired index on or off (value) and returns the updated byte
byte setBitAt(byte b,int index, int value){
index = constrain(index,0,7);
value = constrain(value,0,1);
if(value == 1) b |= (1 << (index));
else b &= ~(1 << (index));
return b;
}
Use keys '1','2' and '3' to flip the bits.
Note that in keypress we're always updating the same byte.
The text will display the decimal value first and the binary value bellow.
This is the most efficient way to send your data and the simplest in terms of serial communication. On the Arduino side you can simply use bitRead() on the byte you get from Serial.read(). For more on binary/bits/bytes be sure to read the BitMath Arduino tutorial. Binary may seem intimidating at first, but it's really not that bad once you practice a bit and it's totally worth knowing.
Here's an updated version of the code above that sends the byte to Arduino on the first available serial port (be sure to change Serial.list()[0] with what makes sense for your setup and press 's' to send an update to Arduino:
import processing.serial.*;
// the state as a byte
byte buzzerState = 0B000;
Serial port;
void setup(){
textFont(createFont("Courier New",18),18);
try{
port = new Serial(this,Serial.list()[0],9600);
}catch(Exception e){
e.printStackTrace();
}
}
void draw(){
background(0);
text("DEC: " + buzzerState +
"\nBIN:" + binary(buzzerState,3),10,40);
}
void keyPressed(){
if(key == '1'){
buzzerState = flipBit(buzzerState,0);
}
if(key == '2'){
buzzerState = flipBit(buzzerState,1);
}
if(key == '3'){
buzzerState = flipBit(buzzerState,2);
}
if(key == 's'){
if(port != null){
port.write(buzzerState);
}else{
println("serial port is not open: check port name and cable connection");
}
}
}
// flips a bit at a given index within a byte and returns updated byte
byte flipBit(byte state,int index){
int bit = getBitAt(state,index);
int bitFlipped = 1 - bit;
return setBitAt(state,index,bitFlipped);
}
// returns the integer value of a bit within a byte at the desired index
int getBitAt(byte b,int index){
index = constrain(index,0,7);
return b >> index & 1;
}
// sets an individual bit at a desired index on or off (value) and returns the updated byte
byte setBitAt(byte b,int index, int value){
index = constrain(index,0,7);
value = constrain(value,0,1);
if(value == 1) b |= (1 << (index));
else b &= ~(1 << (index));
return b;
}
And here's a super basic Arduino sketch:
byte buzzerState;
void setup() {
Serial.begin(9600);
//test LEDs setup
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop() {
if(Serial.available() > 0){
buzzerState = Serial.read();
bool bit0 = bitRead(buzzerState,0);
bool bit1 = bitRead(buzzerState,1);
bool bit2 = bitRead(buzzerState,2);
//test LEDs update
digitalWrite(10,bit0);
digitalWrite(11,bit1);
digitalWrite(12,bit2);
}
}
If you connect 3 LEDs to pins 10,11,12 you should them toggle as you press keys '1','2','3' then 's' in Processing
One way around binary in Processing could be using a String representation of your data (e.g. "00000101" for [1,0,1]) and unbinary() to convert that String to an integer value you can write to serial, but it will be a bit annoying to getting and setting a character at an index (and potentially parsing that char to it's integer value and back)
When you need to send more than a byte things get a bit more complicated as you need to handle data corruption/interruptions, etc. In these situations it's best to setup/design a communication protocol based on your needs and this isn't easy if you're just getting started with Arduino, but not impossible either. Here's an example, there are many more online.
One quick and dirty thing you could try is sending that data as string terminated by a new line character (\n) which you could buffer until in Arduino then read 4 bytes at a time, discarding the \n when parsing:
e.g. sending "101\n" from Processing, representing [1,0,1] then on the Arduino side use Serial.readStringUntil('\n') and a combination of charAt() and toInt() to access each integer within that that string.
Here's an example Processing sketch:
import processing.serial.*;
// the state as a byte
String buzzerState = "010\n";
Serial port;
void setup(){
textFont(createFont("Courier New",18),18);
try{
port = new Serial(this,Serial.list()[0],9600);
}catch(Exception e){
e.printStackTrace();
}
}
void draw(){
background(0);
text(buzzerState,30,50);
}
void keyPressed(){
if(key == '1'){
buzzerState = flipBit(buzzerState,0);
}
if(key == '2'){
buzzerState = flipBit(buzzerState,1);
}
if(key == '3'){
buzzerState = flipBit(buzzerState,2);
}
if(key == 's'){
if(port != null){
port.write(buzzerState);
}else{
println("serial port is not open: check port name and cable connection");
}
}
}
String flipBit(String state,int index){
index = constrain(index,0,2);
// parse integer from string
int bitAtIndex = Integer.parseInt(state.substring(index,index+1));
// return new string concatenating the prefix (if any), the flipped bit (1 - bit) and the suffix
return state = (index > 0 ? state.substring(0,index) : "") + (1 - bitAtIndex) + state.substring(index+1);
}
And an Arduino one based on Arduino > File > Examples > 04.Communication > SerialEvent:
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
// test LEDs setup
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// process string
bool bit0 = inputString.charAt(2) == '1';
bool bit1 = inputString.charAt(1) == '1';
bool bit2 = inputString.charAt(0) == '1';
//test LEDs update
digitalWrite(10,bit0);
digitalWrite(11,bit1);
digitalWrite(12,bit2);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Note this is more prone to error and used 4 times as much data as the the single byte option.

Sending two arrays to Arduino from Processing

I'm trying to pass multiple variables (n number of strings and n number of ints) from processing to my Arduino. I found this tutorial online and managed to send a single value. Now I have two arrays that both need to be accessed by the Arduino filesTypes[] and filesSizes[]. filesTypes[] consists of a 3 char long strings while fileSizes[] is an array of different integers.
Here is my Processing code:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String[] fileTypes;
int[] fileSizes;
String[][] lines;
void setup()
{
size(200,200); //make our canvas 200 x 200 pixels big
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to
match your port
myPort = new Serial(this, portName, 9600);
launch( sketchPath("") + "/test.bat");
}
void draw() {
if (mousePressed == true)
{ //if we clicked in the window
txtToStrg();
myPort.write('1'); //send a 1
txtToStrg();
} else
{ //otherwise
myPort.write('0'); //send a 0
}
}
void txtToStrg(){
String[] lines = loadStrings("list.txt");
fileTypes = new String[lines.length];
fileSizes = new int[lines.length];
for (int i = 0 ; i < lines.length; i++) {
if(lines[i] != null) {
String[] splitLine = split(lines[i], ' ');
fileTypes[i] = splitLine[0];
fileSizes[i] = int(splitLine[1]);
println(fileTypes[i] + " = " + fileSizes[i]);
}
}
}
And here my Arduino code:
char val; // Data received from the serial port
int ledPin = 4 ; // Set the pin to digital I/O 13
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available())
{ // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == '1')
{ // If 1 was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // otherwise turn it off
Serial.print(val);
}
delay(10); // Wait 10 milliseconds for next reading
}
If a pass anything but a char it stops working.

Cannot receive large amount of serial data from Arduino

I am trying to receive sensor data from Arduino and write the readings into a file using Processing IDE (using serial communication / USB).
After doing a large amount of tests, I am pretty sure it's the Processing side unable to process the data. Only the first few (< 100) "samples" are written successfully, after that Serial.available() always returns false.
I am sending two-byte chunks from Arduino, 57600 baud, default settings (8 bit, no parity, 1 stop bit).
Arduino code:
unsigned int data = 0;
unsigned char buf[2];
void setup() {
Serial.begin(57600);
}
void loop() {
data = analogRead(A0);
buf[0] = data & 0xFF; // low byte
buf[1] = data >> 8; // high byte
Serial.write(buf, 2);
Serial.flush();
delayMicroseconds(300);
}
Processing code:
import processing.serial.*;
Serial serialPort;
String serialData;
PrintWriter output;
int recordingTime = 1000; // how many miliseconds of data stream to record
byte[] dataBuffer = new byte[2]; // reserve memory for 2 bytes and initialize to 0 (java stuff)
int receivedBytes = 0;
void setup()
{
serialPort = new Serial(this, "/dev/ttyUSB0", 57600);
output = createWriter("serialData.txt");
}
void draw()
{
while(millis() < recordingTime) {
if (serialPort.available() > 0) {
receivedBytes = serialPort.readBytes(dataBuffer);
output.print("\n");
output.print("Received bytes: ");
output.print(receivedBytes);
output.print("\n");
output.println(binary(dataBuffer[0])); // low byte
output.println(binary(dataBuffer[1])); // high byte
output.println("");
}
else {
output.print("\n");
output.println("No data available");
}
}
output.flush();
output.close();
exit();
}
Output:
Received bytes: 2
11101001
00000011
Received bytes: 2
11101001
00000011
Received bytes: 2
11101001
00000011
...after some lines...
No data available
No data available
No data available
No data available
No data available
No data available
Why is this happening? Why is there "no data available" after few samples? If I watch the serial monitor output in Arduino IDE, it works fine.
I can read the serial data from Arduino using screen and using Python. Still cannot get it to work in Processing - only receiving few samples (17 exactly).
Screen command: $ screen <port name> <baud rate> (press ctrl+a and then shift+k to stop the program; add -L flag for logging to a file). Works as expected.
I managed to achieve the same results using Pyserial library in Python:
#
# log_serial.py
# Writes incoming serial data to file.
import time
import serial
# Edit this parameters =========================================================
serialPort = "/dev/ttyUSB0"
baudrate = 57600
recordTime = 1000 # milliseconds
# ==============================================================================
def millis():
"""
Returns current (wall-)time in milliseconds
"""
return int(round(time.time() * 1000))
ser = serial.Serial(serialPort, baudrate)
with open("output.txt", "w") as f:
startTime = millis()
f.write("Recording started at: ")
f.write(str(startTime))
f.write("\n")
while (millis() - startTime) <= recordTime:
inData = ser.read(2) # reads two bytes
inInt = int.from_bytes(inData, byteorder='little') # merges them into an integer
f.write(str(inInt))
f.write("\n")
f.write("Recording finished at: ")
f.write(str(millis()))
f.write("\n")
Still don't know why the Processing version can't handle it...
There was a bug (wrong while loop condition) in my first Processing code. Here is updated version, using Serial.read() rather than Serial.readBytes(buffer). Still doesn't solve my problem, only getting 17 samples:
import processing.serial.*;
Serial serialPort;
String serialData;
PrintWriter output;
int recordingTime = 5000; // how many miliseconds of data stream to record
void setup()
{
serialPort = new Serial(this, "/dev/ttyUSB0", 57600);
output = createWriter("serialData.txt");
}
void draw()
{
int startTime = millis();
while((millis() - startTime) <= recordingTime) {
if (serialPort.available() > 0) {
int b1 = serialPort.read();
int b2 = serialPort.read();
int value = (b2 << 8) + (b1 & 0xFF);
output.println(value);
}
}
output.flush();
output.close();
exit();
}
Please try to run this code and post the content of the written file afterwards:
import processing.serial.*;
Serial serialPort;
String serialData;
PrintWriter output;
int recordingTime = 5000; // how many miliseconds of data stream to record
void setup()
{
serialPort = new Serial(this, "/dev/ttyUSB0", 57600);
output = createWriter("serialData.txt");
}
void draw()
{
int startTime = millis();
while((millis() - startTime) <= recordingTime) {
if (serialPort.available() > 0) {
int b1 = serialPort.read();
int b2 = serialPort.read();
int value = (b2 << 8) + (b1 & 0xFF);
output.print(millis()); //to each received value there is written the actual millis()-value
output.print(",");
output.println(value);
}
}
output.flush();
output.close();
exit();
}
I have no problems with the Serial communication between Processing and my Arduino Uno/Mega using serialPort.read() instead of serialPort.readBytes(dataBuffer). Maybe this makes a difference...
Have you already tried to increase the variable recordingTime?

Serial communication on Arduino

I have an assignment for school where I need to turn on a led with the serial message #ON%, and turn the led off with #OFF%. The # and % are the identifiers for the correct string. So I made this code:
(bericht means message in Dutch)
String readString = "";
int recievedCharacter;
String bericht = "";
int ledPin = 6;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(ledPin, OUTPUT);
}
void loop()
{
while (Serial.available() > 0)
{
delay(4);
char readChar = (char) Serial.read(); // 'Convert' to needed type
bericht += + readChar; // concatenate char to message
}
if(bericht.startsWith("#"))
{
if(bericht == "#ON%")
{
Serial.println(bericht);
Serial.println("goed");
digitalWrite(ledPin, HIGH);
//message = "";
}
if(bericht == "#OFF%")
{
Serial.println("goed");
digitalWrite(ledPin, LOW);
//message = "";
}
}
}
The problem is the program will never get into the if(bericht == "#ON%") section...
Sorry if this is a silly question but with a lot of googling I just can't figure it out...
The problem is here:
bericht += + readChar; // concatenate char to message // XXX '+ char' => int
this actually appends an integer to the message. Remove the +:
bericht += readChar; // concatenate char to message // Goed!

How to transmit a String on Arduino?

I want 2 Arduinos Leonardo to communicate, send a string for instance, so I have to use Serial1 to communicate via RS232 on pins 0 (RX) and 1 (TX).
I need to write binary data in that pins, the problem is how can I send a String using Serial1.write. Serial1.print works without errors but I think it does not do what I want. Any suggestion?
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
while (!Serial); // while not open, do nothing. Needed for Leonardo only
}
void loop() {
String outMessage = ""; // String to hold input
while (Serial.available() > 0) { // check if at least one char is available
char inChar = Serial.read();
outMessage.concat(inChar); // add Chars to outMessage (concatenate)
}
if (outMessage != "") {
Serial.println("Sent: " + outMessage); // see in Serial Monitor
Serial1.write(outMessage); // Send to the other Arduino
}
}
this line Serial1.write(outMessage); is giving me the error
"no matching function for call to 'HardwareSerial::write(String&)'"
You're using the String object(Wiring/C++). The function is using C strings: Serial.write(char*). To turn it into a C string, you use the toCharArray() method.
char* cString = (char*) malloc(sizeof(char)*(outMessage.length() + 1);
outMessage.stoCharArray(cString, outMessage.length() + 1);
Serial1.write(cString);
If we do not allocate the memory for our C string with malloc, we will get a fault. The following code WILL crash.
void setup() {
Serial.begin(9600);
String myString = "This is some new text";
char* buf;
Serial.println("Using toCharArray");
myString.toCharArray(buf, myString.length()+1); // **CRASH** buf is not allocated!
Serial.println(buf);
}
void loop() {
// put your main code here, to run repeatedly:
}
In the Serial Monitor the only message we will get is: Using toCharArray. At that point execution stops. Now if we correct the problem and use malloc() to allocate memory for our buffer and also use free() when done....
void setup() {
Serial.begin(9600);
String myString = "This is some new text";
char* buf = (char*) malloc(sizeof(char)*myString.length()+1);
Serial.println("Using toCharArray");
myString.toCharArray(buf, myString.length()+1);
Serial.println(buf);
Serial.println("Freeing the memory");
free(buf);
Serial.println("No leaking!");
}
void loop() {
// put your main code here, to run repeatedly:
}
The output we see in the Serial Monitor is:
Using toCharArray
This is some new text
Freeing the memory
No leaking!
Use toCharArry(), write() uses char*, not string, here is what i mean:
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
while (!Serial);
}
void loop() {
String outMessage = "";
while (Serial.available() > 0) {
char inChar = Serial.read();
outMessage.concat(inChar);
}
if (outMessage != "") {
Serial.println("Sent: " + outMessage);
char* CharString; //
outMessage.toCharArray(cString, outMessage.length()) // My Changes Are Here
Serial1.write(CharString); //
}
}

Resources