Receive Arduino commands from Bluetooth module - arduino

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.

Related

Sending strings from processing to 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!

Read in sensor Data (GPS raw Data) with adruino and store it on SD card

I want to read in GPS Data from a serial port of a ublox chip. I do not only want to read in NMEA sentences but also Raw data (Messages like MEASX, RAWX,SFRBX). If I simply connect my serial ports with a pc and read in the data with RealTerm (win) it works fine. However if I try to read in the data with adruino it reads in the NMEA sentences fine but it does not manage to read in the raw data correctely.
Here is the code I use:
String inData;
void setup() {
Serial.begin(38400);
}
void loop() {
while (Serial.available() > 0)
{
char recieved = Serial.read();
inData += recieved;
// Process message when new line character is recieved
if (recieved == '\n')
{
//Serial.print("Arduino Received: ");
Serial.print(inData);
inData = ""; // Clear recieved buffer
}
}
}
Any ideas how to simply read in a file line by line. I think the problem is i do not know how to handle the raw data - how to read that data in?
Best
picture1 picture2
You don't need to check for CR, the GPS device sends the data in blocks, so if one block is done, the communication will be closed and the available()-statement will no longer be true.
if (gps.available())
{
String Buffer = "";
while (gps.available())
{
char GPSRX = gps.read();
Buffer += GPSRX;
//Serial.write(gps.read());
}
Serial.print(Buffer);
}
with 'gps' is an instance of SoftwareSerial:
#include <SoftwareSerial.h>
SoftwareSerial gps(4, 3); // RX, TX
...
void setup()
{
gps.begin(9600);
...
}
void loop()
{
if (gps.available())
{
...
}
...
}

Basic Arduino serial communication

I'm just trying to get the very basics of serial communication started; I'm trying to use this example I found, from what I understand it should be working. I just want what I type into the serial monitor to be output back, so I can see how it works. I also tried removing the while serial.available in case the serial monitor doesn't trigger that condition.
Here is my code:
// Buffer to store incoming commands from serial port
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Initialized\n");
}
void loop() {
while (Serial.available() > 0)
{
char received = Serial.read();
inData += received;
// Process message when new line character is received
if (received == '\n')
{
Serial.println("Arduino Received: ");
Serial.println(inData);
inData = ""; // Clear received buffer
}
}
}
It currently uploads fine, and prints "initialized", but it doesn't work if I try to "send" any data.
Serial.read() returns an int.
You need to cast to (char) in order to store it as a char.
char received = (char)Serial.read();
Maybe you are never receiving any data for some reason.
Let's try something super simple. Use serialEvent() as suggested by sohnryang and then print some text as soon as Serial.available() triggers:
while (Serial.available() > 0) {
Serial.println("Something has been received");
}
You should see this message every time you send something to Arduino.
Use SerialEvent. So the code will look like this.
String inData;
void setup() {
Serial.begin(9600);
Serial.println("Initialized\n");
}
void loop() {
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inData += inChar;
if (inChar == '\n') {
Serial.println("Arduino Recieved : ");
Serial.println("inData");
inData = "";
}
}
}

Receive data via uart for pic16F788 :mikroC

I've written the java code to send data to pic via serial port and now I need to program the microcontroller to read the data and make PortD.RD6=1 if the it receives 1 and PortD.RD6=0 if it receives 0. I've tried this code but I get many errors .
This is my first mikroC program so I really don't know how to manage these errors.
char output[1];
unsigned short i;
void main(){
TRISD = 0x01;
i = 0;
UART1_Init(9600);
while (1) {
if (UART1_Data_Ready()==1) {
i = UART1_Read(); // read the received data
ByteToStr(i, output);
if (output = "1" ) // this is where I get the error
{PortD.RD6=1;}
else { PortD.RD6=0;}
}}}
One error that I can spot is that ByteToStr returns three characters so it is probably overwriting other memory areas and giving an undefined result. You don't need to do that conversion you can simply read a byte into a char and do a comparison directly on that as follows:
void main()
{
char c;
TRISD = 0x01;
UART1_Init(9600);
while (1) {
if (UART1_Data_Ready()) {
c = UART1_Read();
if (c == '1')
PortD.RD6=1;
else
PortD.RD6=0;
}
}
}

Arduino Serial communication output

I have 2 Arduinos Leonardo and I want them to communicate itself, so I did the following code:
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
String outMessage = ""; // String to hold input
while (Serial.available() > 0) { // check if at least 1 char is available
char inChar = Serial.read();
outMessage.concat(inChar); // add inChar to outMessage
}
if (outMessage != "") {
Serial.println("Sent: " + outMessage); // View Arduino 1 in Serial Monitor 1
Serial1.print(outMessage); // Send to Arduino 2
}
while (Serial1.available() > 0) {
Serial.print("Received: "); // View Arduino 1 in Serial Monitor 2
Serial.print(Serial1.read()); // Received from Arduino 1
Serial.println();
}
}
I want to send a message from Arduino 1, print in Serial Monitor and send via TX1 to Arduino 2 and vice-versa. The problem is that I don't receive what I was expecting. For instance if I type test:
Arduino 1:
Sent: test
Arduino 2:
Received: t
Received: e
Received: s
Received: t
I also tryed to do the receiving side like the sending side and use Serial.write but with no sucess.
Is there a easier way to do that or to fix it?
Thanks
Has mentioned by Hans, you need a protocol.
This is what I use to consider a message in Arduino to be a complete message:
char inData[10];
int index;
boolean started = false;
boolean ended = false;
String message =("I am Arduino 1 and I am ready");
void setup(){
Serial.begin(9600);
Serial.println(message);
}
void loop()
{
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '>')
{
started = true;
index = 0;
inData[index] = '\0';
}
else if(aChar == '<')
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = '\0';
}
}
if(started && ended)
{
int inInt = atoi(inData);
Serial.println(inInt);
}
// Get ready for the next time
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
So, basically a message is considered completed only if it is between the special characters ><, like this: >message<. Then you can do the same on reading.
It does not have to be too complicated. If you look carefully at your last whlie-loop you can see that the software does not get a chance to read more than one character each time it passes through the loop. So that is what you get: one character at a time.
In your first while-loop you did better: you collected all the incoming letters until nothing was available and then sent them all at once. So if you make your last loop look more like the first one, you'll get a better result.
As mentioned a protocol to frame messages is needed between devices. A quick way to do this is to use Bill Porter's EasyTransfer library which does exactly what you are trying to do, over either UART or I2C. It has several examples.
Serial.read() reads only one byte every time you use it. A simple solution would be to store each byte on a char array while Serial.available>0 and then print the String with the whole message that was sent.
char message[40];
int count = 0;
while(Serial.available()>0){
message[count++] = Serial.read();
}
Serial.println(message);

Resources