Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

(solved)SIM800L+Arduino (serial communications carrion return & lew line puzzling)

Status
Not open for further replies.

fezder

Well-Known Member
Ok, so the problem now is this:
I have sim800L module, that i've succeded to get working, was quite chore to program....but now I'm trying to implment it to arduino/teensy!
And in case someone wonders, this is not part of any project yet, just learning how to use it :).
I have code that I'm planning to use as hard-base, but I think carrion return and/or newline causes trouble, my current code is as like this:
AT-command gives ok as return, and it should light up led, just a test skecth for now (Sending AT-command doesn't cost anything so good for starters)
C:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String a;
int ledpin=13;

void setup()
{
pinMode(ledpin,OUTPUT);
Serial.begin(9600);  //harware serial
  mySerial.begin(9600);  //bluetooth module, for sending AT-commands
}

void loop() {

//while(Serial.available()) //seems not needed afterall, at least not yet
//{

a= mySerial.readString();   //data from bluetooth
Serial.println(a);
if(a=="OK ")
{
  digitalWrite(ledpin,HIGH);
}
else
{
  digitalWrite(ledpin,LOW);
}
//}

}
and this is the output that module shoots out, OK as excpected, and ERROR just for reference
REALTERM.png
So, how should those CRLF after returning stuff from sim800 should be implemented to code?
Sorry if this was confusing, ask if need arises!
 
What do you mean by "should be implemented to code"? Serial communications end each line with CR LF. Except for custom protocols. When reading, that character pair denotes the end of line. Println adds those characters to its output.

Your terminal program probably has a setting to display control codes. If you turn it off, it will not look as strange. Normally those characters are there, we just don't see them.
 
By implementing I meant just basic code, just chose wrong words perhaps. For start all that's needed is like if sim800 sends string "OK", light up led.
Terminal program I use now is realterm. The question is how those CRLF characters should be placed in serial.read(), as even if I read "OK" from serial monitor, led doesn't light up, even thought tested that it lights up, if I send "OK" from any other TTL-serial device, like bluetooth dongle, or USB-TTL converter...did this help at all?
 
This is first time I see CR LF characters in serial monitor, that might cause confusion here.
 
On the Arduino, serial.read returns one character at a time. So the first call would return "O", the second would return "K", next call returns a CR and so on. Here is a link to the documentation .

You need a routine to read a line and return a character array or a String.
 
This part of my code returns all data as string, so no need to read one-by-one
C:
a= mySerial.readString();   //data from bluetooth
Serial.println(a);
this part puzzles me, even thought I read "ok" from serial monitor, that sim800L returns "ok" led still won't lit up.
C:
if(a=="OK ")
 
cr / lf is ascii 13 , and ascii 10

but my guess is that it is cause there is a space after ok ("OK ")which is different than ("OK")
 
from datasheet:
Commands are usually followed
by a response that includes.
"<CR><LF><response><CR><LF>
 
Thanks for reminder!
 
tested
C:
if(a=="OK\r\n")
if(a=="\r\nOK\r\n")
if(a=="OK")
neither worked, hmm, this is trickier than excpected
 
BUT, arduino accepts these:
C:
if(a=="\r")
if(a=="\n")
 
Yes, and furhter tested, this works IF I send this specific string via bluetooth module, so there's something else tricky here:
C:
if(a=="\r\nOK\r\n") //All 3 combined, works via bluetooth
hmm, my rigol scope has serial decode, perhaps it could shed some light (or logic analyzer) what sim module ACTUALLY sends
 
a scope could help , this code will also let us use led for debugging, it will flash 5xwhen booting up

if a=ok it will do a 1 second flash

if a<>ok then it will dump the variable a to the led, so the led will flash x amount of times(x number of flashes will be the ascii value of first char) then 1second delay then x number of flashes for second char.... till the variable is empty so,

(a=="OK\r\n") // 79flashes, 1sec pause, 75 flashes, 1 sec pause, 13 flashes, 1sec pause, 10 flashes
(a=="OK") // 79flashes, 1sec pause, 75 flashes,
(a=="ok") // 111flashes, 1sec pause, 107 flashes,

dont worry so much about getting flash count , but how many groups of flashes to tell us how long the string is, (according to the micro controller)

Code:
void blinkC(unsigned char b){
while (b>0){
  digitalWrite(ledpin,HIGH);
delay(200);
  digitalWrite(ledpin,LOW);
delay(200);
b--;
}
)

void blinkS(const char* ch){
  while (*ch )
  {
blinkC(*ch++);
delay(1000);

}
}

void setup()
{
pinMode(ledpin,OUTPUT);
Serial.begin(9600);  //harware serial
  mySerial.begin(9600);  //bluetooth module, for sending AT-commands
blinkC(5);
delay(1000);
}

void loop() {

//while(Serial.available()) //seems not needed afterall, at least not yet
//{
a = "";
a= mySerial.readString();   //data from bluetooth
Serial.println(a);
if(a=="OK" || a=="ok")
{
  digitalWrite(ledpin,HIGH);
delay(1000);
  digitalWrite(ledpin,LOW);

}
else
{
  blinkS(a);
  
}

//}
}
 
where you get all these ideas? geez, will try in a few!
 
lol, been there, done that! specifically with baseline controllers and such, when i have no debugger, in fact i often wonder how others would handle these situations, im sure they would have much more elegant methods than what you see me do...ripping apart code piece by piece, going step by step

i found a plugin for visual studio to run adruino that has "in circuit debugging" , so that way i can use the "watch" and breakpoints like i do on mplab for pic(makes it alot easier when you can see whats actually happening in the memory), but so far have not been able to get it going...

also i wonder what happens, in your code when:
if(a=="K")
 
hmm, your debugging code whined:
C:
Arduino: 1.6.7 (Windows 7), TD: 1.27, Board: "Arduino/Genuino Uno"

C:\Users\Atte\AppData\Local\Temp\arduino_80e294bf3d42b7ec2afe902418a07b3e\sketch_feb18a.ino: In function 'void loop()':

sketch_feb18a:50: error: cannot convert 'String' to 'const char*' for argument '1' to 'void blinkS(const char*)'

  blinkS(a);

  ^

exit status 1
cannot convert 'String' to 'const char*' for argument '1' to 'void blinkS(const char*)'

Invalid library found in C:\Users\Atte\Documents\Arduino\libraries\LCD_Spectrum_Analyzer: C:\Users\Atte\Documents\Arduino\libraries\LCD_Spectrum_Analyzer
Invalid library found in C:\Users\Atte\Documents\Arduino\libraries\LCD_Spectrum_Analyzer: C:\Users\Atte\Documents\Arduino\libraries\LCD_Spectrum_Analyzer

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

full code, there was couple typos :)
C:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
String a;
int ledpin=13;

void blinkC(unsigned char b){
while (b>0){
  digitalWrite(ledpin,HIGH);
delay(200);
  digitalWrite(ledpin,LOW);
delay(200);
b--;
}
}

void blinkS(const char* ch){
  while (*ch )
  {
blinkC(*ch++);
delay(1000);

}
}

void setup()
{
pinMode(ledpin,OUTPUT);
Serial.begin(9600);  //harware serial
  mySerial.begin(9600);  //bluetooth module, for sending AT-commands
blinkC(5);
delay(1000);
}

void loop() {

//while(Serial.available()) //seems not needed afterall, at least not yet
//{
a = "";
a= mySerial.readString();  //data from bluetooth
Serial.println(a);
if(a=="OK" || a=="ok")
{
  digitalWrite(ledpin,HIGH);
delay(1000);
  digitalWrite(ledpin,LOW);

}
else
{
  blinkS(a);
}

//}
}
also i wonder what happens, in your code when:
if(a=="K")
^about this, only lights up led if K is received, not ok, OK, or k, or even KKKKKKKK
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top