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.
:)
Code:
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);
}

//}
}
 
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
 
For reference, here is \r\nOK\r\n, sent from bluetooth, earlier was from SIM800
ok_bluetooth.png
 
Here is decoded output, responce is indeed strange from sim800
ok_uart.png
 
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:
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:
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; ?
KK doesn't light up led, and serial.read doesn't work at all if in place of serial.readString
still conversion error:
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.
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 = "";
//}
}
}
 
Code:
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 = "";
}
//}
}
 
Tested that, no change, didn't work. Buddy suggested firmware upgrade, could be worth shot.
 
try this, watch the data being echoed in terminal, if printline(string) doesn't work just delete it..
Code:
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 = "";
}

//}
}
 
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 ÿÿÿ
 
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:
This is one of many test codes, so it can be used with arduino:
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();
}
 
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
 
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
 
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:
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:
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:
only now I'd like to get rid of those garbage characters.....tips? :D
 
found help to this possibly while browsing, will try those later (echo off & discard cr and similar characters)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top