Arduino with HM-10 doesn't always receive the complete message that was sent [closed]
I am a beginner trying to communicate between an iPhone and an Arduino Uno.
I have an HM-10 BLE module device wired to my Arduino. I use it to send and receive messages from my iPhone. My Arduino runs the SoftwareSerial example sketch:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
Like this, everything works fine: The Arduino sends messages to my iPhone as expected and prints "ping" on the serial monitor when I send "ping" to the Arduino.
However if I add these lines in
void loop(){
if (mySerial.available()) {
Serial.write(mySerial.read);
String message = String(mySerial.readString);
if (message == "ping") {
mySerial.write("pong");
}
}
} // the other code stays the same
it suddenly only says "p" on the serial monitor even though I sent "ping". The if statement won't be triggered either, so it won't send "pong" back.
I can't find any flaws in my code, it even worked yesterday with this exact same code.
Thank you for your help!
bluetooth iphone serial arduino
closed as off-topic by Máté Juhász, Burgi, PeterH, n8te, LotPings Jan 3 at 20:35
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question is not about computer hardware or software, within the scope defined in the help center." – Burgi, PeterH, n8te, LotPings
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am a beginner trying to communicate between an iPhone and an Arduino Uno.
I have an HM-10 BLE module device wired to my Arduino. I use it to send and receive messages from my iPhone. My Arduino runs the SoftwareSerial example sketch:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
Like this, everything works fine: The Arduino sends messages to my iPhone as expected and prints "ping" on the serial monitor when I send "ping" to the Arduino.
However if I add these lines in
void loop(){
if (mySerial.available()) {
Serial.write(mySerial.read);
String message = String(mySerial.readString);
if (message == "ping") {
mySerial.write("pong");
}
}
} // the other code stays the same
it suddenly only says "p" on the serial monitor even though I sent "ping". The if statement won't be triggered either, so it won't send "pong" back.
I can't find any flaws in my code, it even worked yesterday with this exact same code.
Thank you for your help!
bluetooth iphone serial arduino
closed as off-topic by Máté Juhász, Burgi, PeterH, n8te, LotPings Jan 3 at 20:35
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question is not about computer hardware or software, within the scope defined in the help center." – Burgi, PeterH, n8te, LotPings
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I am a beginner trying to communicate between an iPhone and an Arduino Uno.
I have an HM-10 BLE module device wired to my Arduino. I use it to send and receive messages from my iPhone. My Arduino runs the SoftwareSerial example sketch:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
Like this, everything works fine: The Arduino sends messages to my iPhone as expected and prints "ping" on the serial monitor when I send "ping" to the Arduino.
However if I add these lines in
void loop(){
if (mySerial.available()) {
Serial.write(mySerial.read);
String message = String(mySerial.readString);
if (message == "ping") {
mySerial.write("pong");
}
}
} // the other code stays the same
it suddenly only says "p" on the serial monitor even though I sent "ping". The if statement won't be triggered either, so it won't send "pong" back.
I can't find any flaws in my code, it even worked yesterday with this exact same code.
Thank you for your help!
bluetooth iphone serial arduino
I am a beginner trying to communicate between an iPhone and an Arduino Uno.
I have an HM-10 BLE module device wired to my Arduino. I use it to send and receive messages from my iPhone. My Arduino runs the SoftwareSerial example sketch:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
Like this, everything works fine: The Arduino sends messages to my iPhone as expected and prints "ping" on the serial monitor when I send "ping" to the Arduino.
However if I add these lines in
void loop(){
if (mySerial.available()) {
Serial.write(mySerial.read);
String message = String(mySerial.readString);
if (message == "ping") {
mySerial.write("pong");
}
}
} // the other code stays the same
it suddenly only says "p" on the serial monitor even though I sent "ping". The if statement won't be triggered either, so it won't send "pong" back.
I can't find any flaws in my code, it even worked yesterday with this exact same code.
Thank you for your help!
bluetooth iphone serial arduino
bluetooth iphone serial arduino
asked Jan 2 at 14:37
vorname nachnamevorname nachname
11
11
closed as off-topic by Máté Juhász, Burgi, PeterH, n8te, LotPings Jan 3 at 20:35
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question is not about computer hardware or software, within the scope defined in the help center." – Burgi, PeterH, n8te, LotPings
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Máté Juhász, Burgi, PeterH, n8te, LotPings Jan 3 at 20:35
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question is not about computer hardware or software, within the scope defined in the help center." – Burgi, PeterH, n8te, LotPings
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is really a programming question belonging on Stackoverflow rather than a usage question belonging on Superuser.
At any rate, your issue is that you are consuming the serial data in two places, so they cannot both get all of it, but will rather steal from each other.
Serial.write(mySerial.read);
String message = String(mySerial.readString);
The first call gets a single character - your 'p'
, and the next call gets anything remaining until its end condition or timeout is satisfied.
Probably you should take out the first line, and just print out the String (if any) obtained by the readString()
method.
Beware though that there is a lot that is less than ideal with your overall approach:
readString()
is timeout based, and you don't really know that data will arrive on the opposite side of a blueooth LE radio link without odd inter-packet gaps, especially with the way that serial-over-ble shoves data through a characteristic.Software serial is an ungainly construct, basically the MCU's CPU has to decode the serial line state itself, because your Arduino's ATmega328 has only a single hardware serial peripheral traditionally reserved for programming and debug output. There are many other inexpensive embedded platforms with multiple hardware serial ports, some programmable via the Arduino ecosystem.
Note that your HM-10 is itself a programmed MCU, however the bluetooth aspect only works with a single expensive toolchain. Competing parts from Nordic have Arduino ports available, ie, you can run your Arduino code right on the BLE chip and avoid having that extra serial link.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is really a programming question belonging on Stackoverflow rather than a usage question belonging on Superuser.
At any rate, your issue is that you are consuming the serial data in two places, so they cannot both get all of it, but will rather steal from each other.
Serial.write(mySerial.read);
String message = String(mySerial.readString);
The first call gets a single character - your 'p'
, and the next call gets anything remaining until its end condition or timeout is satisfied.
Probably you should take out the first line, and just print out the String (if any) obtained by the readString()
method.
Beware though that there is a lot that is less than ideal with your overall approach:
readString()
is timeout based, and you don't really know that data will arrive on the opposite side of a blueooth LE radio link without odd inter-packet gaps, especially with the way that serial-over-ble shoves data through a characteristic.Software serial is an ungainly construct, basically the MCU's CPU has to decode the serial line state itself, because your Arduino's ATmega328 has only a single hardware serial peripheral traditionally reserved for programming and debug output. There are many other inexpensive embedded platforms with multiple hardware serial ports, some programmable via the Arduino ecosystem.
Note that your HM-10 is itself a programmed MCU, however the bluetooth aspect only works with a single expensive toolchain. Competing parts from Nordic have Arduino ports available, ie, you can run your Arduino code right on the BLE chip and avoid having that extra serial link.
add a comment |
This is really a programming question belonging on Stackoverflow rather than a usage question belonging on Superuser.
At any rate, your issue is that you are consuming the serial data in two places, so they cannot both get all of it, but will rather steal from each other.
Serial.write(mySerial.read);
String message = String(mySerial.readString);
The first call gets a single character - your 'p'
, and the next call gets anything remaining until its end condition or timeout is satisfied.
Probably you should take out the first line, and just print out the String (if any) obtained by the readString()
method.
Beware though that there is a lot that is less than ideal with your overall approach:
readString()
is timeout based, and you don't really know that data will arrive on the opposite side of a blueooth LE radio link without odd inter-packet gaps, especially with the way that serial-over-ble shoves data through a characteristic.Software serial is an ungainly construct, basically the MCU's CPU has to decode the serial line state itself, because your Arduino's ATmega328 has only a single hardware serial peripheral traditionally reserved for programming and debug output. There are many other inexpensive embedded platforms with multiple hardware serial ports, some programmable via the Arduino ecosystem.
Note that your HM-10 is itself a programmed MCU, however the bluetooth aspect only works with a single expensive toolchain. Competing parts from Nordic have Arduino ports available, ie, you can run your Arduino code right on the BLE chip and avoid having that extra serial link.
add a comment |
This is really a programming question belonging on Stackoverflow rather than a usage question belonging on Superuser.
At any rate, your issue is that you are consuming the serial data in two places, so they cannot both get all of it, but will rather steal from each other.
Serial.write(mySerial.read);
String message = String(mySerial.readString);
The first call gets a single character - your 'p'
, and the next call gets anything remaining until its end condition or timeout is satisfied.
Probably you should take out the first line, and just print out the String (if any) obtained by the readString()
method.
Beware though that there is a lot that is less than ideal with your overall approach:
readString()
is timeout based, and you don't really know that data will arrive on the opposite side of a blueooth LE radio link without odd inter-packet gaps, especially with the way that serial-over-ble shoves data through a characteristic.Software serial is an ungainly construct, basically the MCU's CPU has to decode the serial line state itself, because your Arduino's ATmega328 has only a single hardware serial peripheral traditionally reserved for programming and debug output. There are many other inexpensive embedded platforms with multiple hardware serial ports, some programmable via the Arduino ecosystem.
Note that your HM-10 is itself a programmed MCU, however the bluetooth aspect only works with a single expensive toolchain. Competing parts from Nordic have Arduino ports available, ie, you can run your Arduino code right on the BLE chip and avoid having that extra serial link.
This is really a programming question belonging on Stackoverflow rather than a usage question belonging on Superuser.
At any rate, your issue is that you are consuming the serial data in two places, so they cannot both get all of it, but will rather steal from each other.
Serial.write(mySerial.read);
String message = String(mySerial.readString);
The first call gets a single character - your 'p'
, and the next call gets anything remaining until its end condition or timeout is satisfied.
Probably you should take out the first line, and just print out the String (if any) obtained by the readString()
method.
Beware though that there is a lot that is less than ideal with your overall approach:
readString()
is timeout based, and you don't really know that data will arrive on the opposite side of a blueooth LE radio link without odd inter-packet gaps, especially with the way that serial-over-ble shoves data through a characteristic.Software serial is an ungainly construct, basically the MCU's CPU has to decode the serial line state itself, because your Arduino's ATmega328 has only a single hardware serial peripheral traditionally reserved for programming and debug output. There are many other inexpensive embedded platforms with multiple hardware serial ports, some programmable via the Arduino ecosystem.
Note that your HM-10 is itself a programmed MCU, however the bluetooth aspect only works with a single expensive toolchain. Competing parts from Nordic have Arduino ports available, ie, you can run your Arduino code right on the BLE chip and avoid having that extra serial link.
answered Jan 2 at 15:56
Chris StrattonChris Stratton
31017
31017
add a comment |
add a comment |