Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a led turning on when called with SIM900
07-09-2014, 11:05 AM,
#1
Making a led turning on when called with SIM900
Ok, I've written some code that can turn on a led when you call a sim900 module that is connected on UART. The led will turn off when you cancel the call.

The hardest problem was to scan UART for changes.

I've created a function that tries like 100.000 times to scan uart for a certain character. If it doesn't find the character it will return 0. You can see this as a timeout from that function. At this stage it was also possible to make an infinite loop but then the function would be stuck in searching for a certain character. Making this value to small and the program could miss it.
If it finds the character it will continue to do the same for the next character. If the entire word is found the function will return 1 and the led will go on.

I've worked long and hard on this code. I'm not a good c programmer so optimization or tips are always welcome.

Code:
/*-----------------------------------------------------
Author:  Xumpy
Date: 2014-08-23
Description:

-----------------------------------------------------*/
int readString(char *str){
  char inchar;
  int len;
  int found;
  int loops;
  
  Serial.flush();
  inchar=*str;
  
  for (len=1; inchar!='\0';len++){
    found = 0;
    loops = 0;

    do {
      loops++;
      if (loops==100000){
        return 0;
      }
      if (Serial.read()==inchar){
        found = 1;
      }
      
    }while(found == 0);    
    
    inchar=*(str + len);
  }
  
  if (found==1){
    return 1;
  }
  
  return 0;
}

void setup() {
  pinMode(0, OUTPUT);
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  delay(500);
  Serial.printf("AT\r\n");
  delay(500);
  Serial.printf("AT+CPIN=\"1111\"\r\n");
}

void loop() {
  char ring[100]="RING";
  char no_carrier[100]="NO CARRIER";
  int ledOn = readString(ring);
  
  while (ledOn==1){
    digitalWrite(2,HIGH);
    if (readString(no_carrier)){
      ledOn=0;
      digitalWrite(2,LOW);
    }
  }
}
Reply
08-09-2014, 12:55 PM,
#2
RE: Making a led turning on when called with SIM900
Why reserve 100 bytes for such short strings? You're wasting a lot of possibly precious memory with:

Code:
char ring[100]="RING";
char no_carrier[100]="NO CARRIER";

As for the loop, perhaps an interrupt service routine would be better. See the Wiki at http://wiki.pinguino.cc/index.php/Interrupt_Example for an interrupt example. Your code would not then be tied up waiting in a loop when it could possibly be doing something more useful instead.
Reply
08-09-2014, 04:29 PM, (This post was last modified: 08-09-2014, 04:30 PM by Xumpino.)
#3
RE: Making a led turning on when called with SIM900
Yeah, The reservation of 100 bytes is a little overkill but my current application is still very small.
The reason why I don't take 4 for RING and 10 for NO CARRIER is that you have to be sure that you have zero's in the end of your string. Else the code will not stop reading the string at \0.

As I said I find this to be an easy to understand example of how you can read UART.
Improvements are always welcome and I'm not a very good C programmer (I'm an enterprise developer who takes memory very lightly Wink )

I'm going to look into the Interrup Example. Thanks for the tip.

Regards

Xump

(08-09-2014, 12:55 PM)Pinguino Wrote: Why reserve 100 bytes for such short strings? You're wasting a lot of possibly precious memory with:

Code:
char ring[100]="RING";
char no_carrier[100]="NO CARRIER";

As for the loop, perhaps an interrupt service routine would be better. See the Wiki at http://wiki.pinguino.cc/index.php/Interrupt_Example for an interrupt example. Your code would not then be tied up waiting in a loop when it could possibly be doing something more useful instead.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)