Go Down

Topic: parse command not working with bluetooth (Read 944 times) previous topic - next topic

Adam84

May 14, 2014, 12:59 am Last Edit: May 14, 2014, 01:03 am by Adam84 Reason: 1
Hi all,
Im new to arduino (got my first this afternoon) and havent touched progamming since QBasic in my early teens way back in the 90s so i appoligise if this is a stupid question.
I intend to make a data logger and then possibly play with a bit of home automation. Anyway after messing around with the examples and watching a couple of youtube videos I wrote/copy and pasted the following to try my dht22 sensors :
Code: [Select]

#include "DHT.h"

#define DHTPIN_A 2     // what pin we're connected to
#define DHTPIN_B 3


#define DHTTYPE DHT22   // DHT 22  (AM2302)


DHT dht1(DHTPIN_A, DHTTYPE);
DHT dht2(DHTPIN_B, DHTTYPE);

void setup() {
//  pinMode(led, OUTPUT);
 Serial.begin(9600);
 Serial.println("DHTxx test!");

 dht1.begin();
 dht2.begin();



}
String command;

void loop() {
 
 if(Serial.available())
   {  
     char c = Serial.read();
     if(c == '\n')
     {
       parseCommand(command);
       command = "";
     }
     else
     {
       command += c;
     }
   }
}
 

void parseCommand(String com)


{
float h1 = dht1.readHumidity();  
 float t1 = dht1.readTemperature();
 float h2 = dht2.readHumidity();
 float t2 = dht2.readTemperature();

if(com == "temp")
{
  Serial.print("Temperature: ");
   Serial.print(t1, 2);
   Serial.print("*C\t");
   Serial.print(t2, 2);
   Serial.println("*C");
} else if(com == "humidity")
{
  Serial.print("Humidity: ");
   Serial.print(h1, 2);
   Serial.print("%\t");
   Serial.print(h2, 2);
   Serial.println("%");
}
else{
  Serial.println(com);
}
}

When used connected to my computer the programme works fine, you type "temp" it returns the two temp readings, "humidity" does the same for the relative humidity. All is good. Except when i try to use my HC-06 bluetooth module the whole thing breaks down. The 'else' case in paseCommand returns whatever you have sent if it isnt a reconised command. I send "temp" i get "temp" back, I send "humidity" i get it echoed back. Infact it'll echo whatever you send at it. I'm lost as to why it works when connected to my laptop but not through bluetooth not connected to the pc. Any Ideas would be much appreciated?

Brad Burleson

#1
May 14, 2014, 02:56 am Last Edit: May 14, 2014, 03:33 am by Brad Burleson Reason: 1

I'm lost as to why it works when connected to my laptop but not through bluetooth not connected to the pc. Any Ideas would be much appreciated?


Just a guess... since you have to match the string exactly, perhaps there is a return or line-feed at the end when connected thru Bluetooth that isn't there when you use the serial monitor?

I'd suggest printing the length of the string as well as it's contents (use 'strlen') in the else clause.  Something like:

Code: [Select]

else{
  Serial.println(com);
  Serial.print("Length=");
  Serial.println(strlen(com));
}


Just a thought.

Hope this helps,

Brad
KF7FER

EDIT: The point of printing the length is not to resolve your problem but to see if there is any extra characters in the string that you didn't know about.  You can then take action to ignore them.

Adam84

Thanks for the quick response. Good idea. I've just tried it and im getting:
DHTtester3.ino: In function 'void parseCommand(String)':
DHTtester3:76: error: cannot convert 'String' to 'const char*' for argument '1' to 'size_t strlen(const char*)'
Do i need to change the varibale type before i can use strlen to get its lengh?

Adam84

Sorted the error out. Needed to be string.lenght to get the length. Well guessed though. Through the bluetooth there is an extra hidden charector somewhere (I assume at the end?). Now to work out how to remove it..... Thanks again for the advice. :)

PaulS

I always recommend printing strings (or Strings ugh!) between markers.
Code: [Select]
Serial.print("The string is: [");
Serial.print(string);
Serial.println("]");


If string contains "1234", you'll see
Code: [Select]
The string is: [1234]

If string contains extra characters, like a carriage return or linefeed, you'll see either
Code: [Select]
]he string is: [1234
or
Code: [Select]
The string is: [1234
]
The art of getting good answers lies in asking good questions.

Go Up