I have programmed a client-server between an Arduino and my mobile app in Android. I ́m using a buetooth HC-06. For the moment is a very basic code just to turn ON/OFF a led, regulate its voltage, and receive the charge of the battery.
This is my Arduino code for receiving the message:
#include <SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial hc06(2,3);
char command;
String string;
boolean ledon = false;
const byte MAX_STRING_LEN = 40;
const int BLINK_INTERVAL = 1000;
char inputString[MAX_STRING_LEN]; // a string to hold incoming data
int BatteryPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
float BatteryCar = 0;
int PercentageBatt = 0;
int intensidad = 0;
byte strLen = 0; // current length of rec'd string
#define led 11
void setup()
{
Serial.begin(9600);
hc06.begin(9600);
pinMode(led, OUTPUT);
Timer1.initialize(10000); // set a timer of length 10000 microseconds (or 0.01 sec - or 1Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
Timer1.attachInterrupt( SendVoltage );
}
void loop()
{
sensorValue = analogRead(BatteryPin);
BatteryCar = sensorValue * (5.0 / 1023.0);
if (hc06.available() == 0)
{
analogWrite(led, 0);
delay (10);
}
while(hc06.available() > 0)
{
command = ((byte)hc06.read());
if((command == '\n') || (command == '\r'))
{
command =='0円';
break;
}
else if (command == 'O')
analogWrite(led, 255);
else
analogWrite(led, 0);
delay(10);
Serial.println(command);
}
}
void SendVoltage ()
{
PercentageBatt = (BatteryCar * 100)/4;
hc06.println(PercentageBatt);
}
This is the data that I ́m receiving:
It supposed that i should only receive "O" or "F". Any suggestion??
thanks in advanced,
-
1It would appear that whatever is sending the data is sending something else. Since we cannot see any of that code nobody can do anything but guess at why.Delta_G– Delta_G2020年09月23日 19:55:51 +00:00Commented Sep 23, 2020 at 19:55
-
simply ignore unexpected datajsotola– jsotola2020年09月23日 22:36:37 +00:00Commented Sep 23, 2020 at 22:36
-
why are you printing the value of command after you modify its value?jsotola– jsotola2020年09月23日 22:38:56 +00:00Commented Sep 23, 2020 at 22:38
-
note: substituting 0円 for \r and for \n is kind of pointlessjsotola– jsotola2020年09月23日 22:43:35 +00:00Commented Sep 23, 2020 at 22:43
-
Does your HC-06 actually run at 9600 baud? Is your serial monitor set for 9600 baud?Majenko– Majenko2020年09月24日 10:10:16 +00:00Commented Sep 24, 2020 at 10:10
1 Answer 1
Well, after a lot of research, it seems that my main problem was that I ́m receiving and sending information at the same time. And with the library Softwareserial, only 1 can be active at the same time. I found two solutions, but to be honest I'm not complete satisfy with neither of them. However, for my application will work.
The first solution: Is to used the HardwareSerial (pin 0,1) itself, instead of using the pin 2 and 3. When I used those, it didn't have any problem. Although I decided no to used because it help me debugging my code.
The second solution: Is to decrease the frecuency of what I ́m sending. For me its ok I send the data every second. So my code would be like this:
#include <SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial hc06(2,3);
char command;
String string;
boolean ledon = false;
const byte MAX_STRING_LEN = 40;
const int BLINK_INTERVAL = 1000;
char inputString[MAX_STRING_LEN]; // a string to hold incoming data
int BatteryPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
float BatteryCar = 0;
int PercentageBatt = 0;
int intensidad = 0;
byte strLen = 0; // current length of rec'd string
#define led 11
void setup()
{
Serial.begin(9600);
hc06.begin(9600);
pinMode(led, OUTPUT);
Timer1.initialize(1000000);
Timer1.attachInterrupt( SendVoltage );
}
void loop()
{
sensorValue = analogRead(BatteryPin);
BatteryCar = sensorValue * (5.0 / 1023.0);
if (hc06.available() == 0)
{
analogWrite(led, 0);
delay (10);
}
while(hc06.available() > 0)
{
command = ((byte)hc06.read());
if((command == '\n') || (command == '\r'))
{
command =='0円';
break;
}
else if (command == 'O')
analogWrite(led, 255);
else
analogWrite(led, 0);
delay(10);
Serial.println(command);
}
}
void SendVoltage ()
{
PercentageBatt = (BatteryCar * 100)/4;
hc06.println(PercentageBatt);
}
I have read about this library (AltSoftSerial), but I couldn't get it work. I have usde the pin 8 and 9, but nothing.
Explore related questions
See similar questions with these tags.