1. 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.
    Dismiss Notice

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

Discussion in 'Arduino' started by fezder, Feb 16, 2016.

  1. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    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)
    Code (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!
     
  2. djsfantasi

    djsfantasi Member

    Joined:
    Mar 15, 2011
    Messages:
    161
    Likes:
    21
    Location:
    Metro Boston
    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.
     
    • Informative Informative x 1
  3. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    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?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland

    This is first time I see CR LF characters in serial monitor, that might cause confusion here.
     
  6. djsfantasi

    djsfantasi Member

    Joined:
    Mar 15, 2011
    Messages:
    161
    Likes:
    21
    Location:
    Metro Boston
    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.
     
    • Informative Informative x 1
  7. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    This part of my code returns all data as string, so no need to read one-by-one
    Code (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.
    Code (c):

    if(a=="OK ")
     
     
  8. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,718
    Likes:
    37
    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")
     
    • Informative Informative x 1
  9. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
  10. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    from datasheet:
    Commands are usually followed
    by a response that includes.
    "<CR><LF><response><CR><LF>
     
  11. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,718
    Likes:
    37
    also "OK" is differnt than "ok" , ascii is case sensitive
     
    • Informative Informative x 1
  12. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    Thanks for reminder!
     
  13. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,718
    Likes:
    37
    maybe this:
    if(a=="OK\r\n")
     
    • Like Like x 1
  14. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    tested
    Code (c):

    if(a=="OK\r\n")
    if(a=="\r\nOK\r\n")
    if(a=="OK")
     
    neither worked, hmm, this is trickier than excpected
     
  15. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    BUT, arduino accepts these:
    Code (c):

    if(a=="\r")
    if(a=="\n")
     
     
  16. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,718
    Likes:
    37
    so the led lights up for both of those??
     
  17. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    Yes, and furhter tested, this works IF I send this specific string via bluetooth module, so there's something else tricky here:
    Code (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
     
  18. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,718
    Likes:
    37
    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 (text):


    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);
     
    }

    //}
    }


     
     
  19. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    where you get all these ideas? geez, will try in a few!
     
  20. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,718
    Likes:
    37
    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")
     
  21. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    hmm, your debugging code whined:
    Code (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 :)
    Code (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);
    }

    //}
    }

     
    ^about this, only lights up led if K is received, not ok, OK, or k, or even KKKKKKKK
     

Share This Page