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. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,715
    Likes:
    37
    :)
    Code (text):


    void loop() {
    char b[6];

    //while(Serial.available()) //seems not needed afterall, at least not yet
    //{
    a= mySerial.readString();  //data from bluetooth
    a.toCharArray(b, a.length());
    Serial.println(a);

    if(a=="OK" || a=="ok")
    {
      digitalWrite(ledpin,HIGH);
    delay(1000);
      digitalWrite(ledpin,LOW);

    }
    else
    {
      blinkS(b);
    }

    //}
    }

     
     
    • Like Like x 1
  2. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    hmm led blinks 5 times at first, then even without even input it starts blinking again at same frequency....
    Here, took output with analyzer, any help?
    Channel 0 is RX, forgot to rename labels...
    ok.png
     
  3. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    For reference, here is \r\nOK\r\n, sent from bluetooth, earlier was from SIM800
    ok_bluetooth.png
     
  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

    Here is decoded output, responce is indeed strange from sim800
    ok_uart.png
     
  6. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,715
    Likes:
    37
    what about if you send KK? i have a feeling djs is on to something there with serial.read what if you tried a= serial.read; ?


    Code (text):

    void loop() {
    string b = "";

    //while(Serial.available()) //seems not needed afterall, at least not yet
    //{
    b= mySerial.readString();  //data from bluetooth
    Serial.println(b);
    a += b;
    if (b == "\r" ){
    a.replace("\n", ""); // cut \n out of a
    a.replace("\r", "");
    if(a=="OK" || a=="ok")
    {
      digitalWrite(ledpin,HIGH);
    delay(1000);
      digitalWrite(ledpin,LOW);
    }
    else
    {
      blinkS(b);
    }
    a = "";
    //}
    }
    }



     
     
    Last edited: Feb 19, 2016
    • Like Like x 1
  7. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    KK doesn't light up led, and serial.read doesn't work at all if in place of serial.readString
    still conversion error:
    Code (c):

    Arduino: 1.6.7 (Windows 7), TD: 1.27, Board: "Arduino/Genuino Uno"

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

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

      blinkS(b);

      ^

    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.
     
    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() {
    String b = "";

    //while(Serial.available()) //seems not needed afterall, at least not yet
    //{
    b= mySerial.readString();  //data from bluetooth
    Serial.println(b);
    a += b;
    if (b == "\r" ){
    a.replace("\n", ""); // cut \n out of a
    a.replace("\r", "");
    if(a=="OK" || a=="ok")
    {
      digitalWrite(ledpin,HIGH);
    delay(1000);
      digitalWrite(ledpin,LOW);
    }
    else
    {
      blinkS(b);
    }
    a = "";
    //}
    }
    }

     
     
  8. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,715
    Likes:
    37
    Code (text):

    void loop() {
    string b = "";

    //while(Serial.available()) //seems not needed afterall, at least not yet
    //{
    b= mySerial.readString();  //data from bluetooth
    Serial.println(b);
    a += b;
    if (b == "\r" ){
    a.replace("\n", ""); // cut \n out of a
    a.replace("\r", "");
    if(a=="OK" || a=="ok")
    {
      digitalWrite(ledpin,HIGH);
    delay(1000);
      digitalWrite(ledpin,LOW);
    }
    a = "";
    }
    //}
    }
     
     
    • Like Like x 1
  9. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    Tested that, no change, didn't work. Buddy suggested firmware upgrade, could be worth shot.
     
  10. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,715
    Likes:
    37
    try this, watch the data being echoed in terminal, if printline(string) doesn't work just delete it..
    Code (text):

    void loop() {
    string b = "";

    //while(Serial.available()) //seems not needed afterall, at least not yet
    //{
    b= mySerial.readString();  //data from bluetooth
    Serial.println("string:");
    Serial.println(b);
    a += b;
    Serial.println(a);
    a.replace("\n", ""); // cut \n out of a
    a.replace("\r", "");
    if(a=="OK" || a=="ok")
    {
      digitalWrite(ledpin,HIGH);
    delay(1000);
      digitalWrite(ledpin,LOW);
    a = "";
    }

    //}
    }
     
     
    • Like Like x 1
  11. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    hmm, serial shoots carbage out in arduino serial, baudrate matches though, it shoots out ÿÿÿÿÿ, if receives "OK" from sim800L, if I send "<cr><lf>OK<cr><lf>, output is ÿÿÿ
     
  12. djsfantasi

    djsfantasi Member

    Joined:
    Mar 15, 2011
    Messages:
    161
    Likes:
    21
    Location:
    Metro Boston
    Does the number of bits, parity and stop bits match? What is the speed?
     
    Last edited: Feb 23, 2016
    • Like Like x 1
  13. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    yes, they all match,9600 baud, none parity, 1 stop bit, 8 bits
    IPR returs 0=auto baud, ICF returs 3,255=8 data 0 parity 1 stop (none parity)
     
    Last edited: Feb 24, 2016
  14. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    This is one of many test codes, so it can be used with arduino:
    Code (c):

    #include <SoftwareSerial.h>
    #include <String.h>
    String lat = "52.6272690";
    String lng = "-1.1526180";
    SoftwareSerial sim800l(10, 11); // RX, TX
    float sensorValue;
    const int buttonPin = 7;
    int buttonState = 0;
    float tempC;
    float tempCavg;
    int avgcount = 0;
    void setup()
    {

    pinMode(buttonPin, INPUT);
    sim800l.begin(9600);
    Serial.begin(9600);
    delay(500);
    }

    void loop()
    {


    buttonState = digitalRead(buttonPin);

    if (buttonState == 0) {
    while(avgcount < 50){
    sensorValue = analogRead(A0);
    tempC = sensorValue * 5.0;
    tempC = tempC / 1024.0;
    tempC = (tempC - 0.05) * 100;
    tempCavg = tempCavg + tempC;
    avgcount++;
    }
    delay(300);
    Serial.println(tempCavg/ 50);
    tempCavg = tempCavg / 50;
    SendTextMessage();

    }

    if (sim800l.available()){
    Serial.write(sim800l.read());
    }
    }

    void SendTextMessage()
    {
    Serial.println("Sending Text...");
    sim800l.print("AT+CMGF=1\r"); // Set the shield to SMS mode
    delay(100);

    sim800l.print("AT+CMGS=\"+44795*******\"\r");
    delay(200);
    // sim800l.print("http://maps.google.com/?q=");
    // sim800l.print(lat);
    // sim800l.print(",");
    // sim800l.print(lng);
    sim800l.print("The temperature is: ");
    sim800l.print(tempCavg);
    sim800l.print(" degrees C");
    sim800l.print("\r"); //the content of the message
    delay(500);
    sim800l.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)
    delay(100);
    sim800l.println();
    Serial.println("Text Sent.");
    delay(500);
    tempCavg = 0;
    avgcount = 0;
    }

    void DialVoiceCall()
    {
    sim800l.println("ATD+4479********;");//dial the number, must include country code
    delay(100);
    sim800l.println();
    }
     
     
  15. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    and just while ago tested that sketch as working, so this is interesting problem, sim module works whatever i send it to do, but when trying to interface it so it controls arduino, no-go.
    But, there is read-from-sim800 function in that code too o_O
     
  16. djsfantasi

    djsfantasi Member

    Joined:
    Mar 15, 2011
    Messages:
    161
    Likes:
    21
    Location:
    Metro Boston
    The only reference to the data speed of the SIM800 in the provided data sheet was 115200? How have you changed it to 9600?
     
    • Like Like x 1
  17. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    So far I haven't changed anything, it was 9600 from the beginning; only boot-data is send at higher baud, 115200 that tells sms ready and stuff like that. Normal data is then sent with 9600 baud, or whatever is set. See AT+IPR from datasheet, that can be used to set baud
     
  18. Dr_Doggy

    Dr_Doggy Well-Known Member

    Joined:
    Aug 11, 2007
    Messages:
    1,715
    Likes:
    37
    so baud does match in terminal? maybe try changing it to the next one over?
     
    • Like Like x 1
  19. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    I got it working! turned out, sim echoed command sent to it, and I figured that out by using LCD as translator what it actually sends, so \r and \n showed as random gibberish. So, we were close, here's final code:
    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. In fact, if this string is enabled, hardware serial MUST be open to this sketch work.
    //{

    a= mySerial.readString();  //data from bluetooth
    Serial.println(a);
    if(a=="AT\r\r\nOK\r\n")   //here is command echoed, as well as responce, so structure is <command>\r\r\n<response>\r\n
    {
      digitalWrite(ledpin,HIGH);
    }
    else
    {
      digitalWrite(ledpin,LOW);
    }
    //}
    }
     
    and this code I used as translator so to say:
    Code (c):


    #include <Wire.h>
    #include <LiquidTWI.h>

    // Connect via i2c, default address #0 (A0-A2 not jumpered)
    LiquidTWI lcd(0);
    String a;


    void setup() {
      lcd.begin(20, 4);
      Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
    }

    void loop() {

      while (Serial.available()) {
    lcd.setCursor(0,0);
      a = Serial.readString(); // read the incoming data as string
      Serial.print(a);
      lcd.print(a);
      lcd.print("  ");
      }
    }
     
    and lcd's output
    IMG_1283.jpg
    Thanks for help! :)
     
    Last edited: Feb 26, 2016
  20. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    only now I'd like to get rid of those garbage characters.....tips? :D
     
  21. fezder

    fezder Well-Known Member

    Joined:
    Dec 11, 2011
    Messages:
    1,665
    Likes:
    100
    Location:
    Mikkeli, Finland
    found help to this possibly while browsing, will try those later (echo off & discard cr and similar characters)
     

Share This Page