/***********************************************************************************
Project: SIM900 remote relay
Author: Christian Lerche - Lerchetech.dk
Date: 10-06-2011
Notes:
LPC1768 on custom board.
***********************************************************************************/
//------------------------------Include--------------------------------------------
#include "mbed.h" // Standard include
#include "platform.h" // include platform for custom board
#include <string>
using namespace std;
//#define PROTOPLATFORM // use of LPC_pinnames activated
//------------------------------Input/Output---------------------------------------
DigitalOut myled(LED1); // Just digitalout
Serial SIM900(p9, p10); // TX RX
Serial pc(USBTX, USBRX);
DigitalOut PWRKEY(p11); // High to turn on module
DigitalOut Relay(LED4); //Indicate relay
DigitalOut L3(LED3);
DigitalOut L2(LED2);
char lookup[] = "LTR 8606";
char * buffer;
char inputbuffer[512];
int i = 0;
int received = 0;
int u = 0;
int newsmsrec = 0;
char on[] = "Lt1";
char off[] = "Lt0";
char * pch;
char * end;
char phonenum[8];
char textbuf[160];
char * str;
int phonenumcheck = 0;
//------------------------------Interrupts-----------------------------------------
void serialinterrupt() { // Interrupt routine whenever serial receive
if (SIM900.readable()) {
inputbuffer[i]=SIM900.getc(); // copy to buffer
pc.putc(inputbuffer[i]);
i++;
}
// Look for +CMTI: "SM",1
L2 = !L2;
return;
}
void turnon() { // Sequence to turn on SIM900
PWRKEY = 1;
wait_ms(1200);
PWRKEY = 0;
}
void setup() {
turnon();
wait(5);
SIM900.printf("ATE0\r\0"); // SIM900 won't return every typed char
wait_ms(100);
LPC_UART0->FCR |= 0x06; // Flush the serial buffer.
SIM900.printf("AT+CMGF=1\r\0");
wait_ms(100);
LPC_UART0->FCR |= 0x06; // Flush the serial buffer.
SIM900.printf("AT+CMGDA=\"DEL ALL\"\r\0");
wait_ms(100);
LPC_UART0->FCR |= 0x06; // Flush the serial buffer.
memset(inputbuffer, '0', 512);
SIM900.attach(&serialinterrupt); // Jump to serialinterrupt when receiving byte
wait(15); // Wait for SIM900 to get ready
PWRKEY = 0;
}
void sendsms(string sms) {
}
/*
char compare_string(char const *compare_string){
char *ch;
char ret_val
}
*/
void readsms(char place, char *phone, char *text, int max_length) {
if(place == 0){
return;
}
pc.printf("readsms started\r\n");
SIM900.printf("AT+CMGR=%c\r",place);
pc.printf("1\r\n");
wait_ms(500);
pc.printf("%d\r\n",inputbuffer); // print the decimal value of inputbuffer.
/*
for(int o = 0; o > 128; o++){
switch(inputbuffer[o]){
case('+'):
switch(inputbuffer[o+1]){
case('4'):
switch(inputbuffer[o+2]){
case('5'):
phonenumcheck = 1;
break;
default:
break;
}
default:
break;
}
default:
break;
}
}
*/
phone = strstr(inputbuffer,"+45"); // locate +45 in inputbuffer, next copy the text
pc.printf("3\r\n");
u = phone-inputbuffer;
pc.printf("4\r\n");
str = strncpy(phonenum,inputbuffer+(u+3),8); // str = char *
pc.printf("%c\r\n",str); // or str?!?!?
text = strstr(inputbuffer,"LTR"); // text is array. Maybe char*?
pc.printf("6\r\n");
u = text-inputbuffer;
pc.printf("7\r\n");
strncpy(textbuf,inputbuffer+u,160);
pc.printf("8\r\n");
pc.printf("Copy phonenumber to phonenum buffer\r\n");
for(int telefonnummer = 0; telefonnummer < 8; telefonnummer++){
phonenum[telefonnummer] = inputbuffer[pch-inputbuffer+(3+telefonnummer)];
} // This functions copies phonenumber of message to memory.
// This COULD be done with a -cpy function, but they don't seem to work.
// strncpy should copy from pointed array to array, in n chars.
pc.printf("Copy text message\r\n");
for(int tekstbesked = 0; tekstbesked < 160; tekstbesked++){
if(inputbuffer[text-inputbuffer+tekstbesked] == '\0'){
tekstbesked = 160;
}
textbuf[tekstbesked] = inputbuffer[text-inputbuffer+tekstbesked];
}
int ok = 0;
ok = strncmp(textbuf,lookup,7);
/*
int q = 0;
for(int y = 0; y<7; y++){
if(textbuf[y] == lookup[y]){
q++;
}
}
*/
if(/*q == 7*/ok >= 0){
pc.printf("ok was >= 0");
switch(textbuf[9]){
case('1'):
Relay = 1;
break;
case('0'):
Relay = 0;
break;
default:
break;
}
}
SIM900.printf("AT+CMGDA=\"DEL READ\"\r");
pc.printf("9\r\n");
memset (inputbuffer, '0', 512);
pc.printf("Delete inputbuffer\r\n");
delete pch;
pc.printf("Delete pch\r\n");
delete text;
pc.printf("Delete text\r\n");
i = 0;
return;
}
char CMTI(){
char * newsms;
newsms = strstr(inputbuffer,"+CMTI:");
if(newsms-inputbuffer <= 0){
delete newsms;
return 0;
}
char var = inputbuffer[newsms-inputbuffer+12];
delete newsms;
return(var);
}
//------------------------------Main routine---------------------------------------
int main() {
pc.baud(19200);
SIM900.baud(19200);
setup();
buffer = inputbuffer;
while (1) {
myled = !myled;
wait_ms(10000);
u = CMTI();
pc.printf("U was: %c\r\n",u); // CMTI reads the number of the new sms
//readsms(u, phonenum, textbuf, 160); // Read the sms, return non, but use phonenum and buffer
if(u > 0){
SIM900.printf("AT+CMGR=%c\r",u);
wait_ms(500);
char *phonenumber;
phonenumber = strstr(inputbuffer,"\"REC UNREAD");
pc.printf("%d\r\n",phonenumber);
strncpy(phonenum,phonenumber,8);
for(int hej=0; hej < 8; hej++){
pc.printf("%d",phonenum[hej]);
}
}
u = 0;
}
}
Here's the code, is real messy, sorry about that.
The only routines I call i CMTI and the serial interrupt.
Hi people.
I'm trying to write a program that reads the incoming messages on a SIM900 gsm modem. First of, it works. I can read the interrupt-buffer i wrote. But: I can't work out how to use strstr, strncpy and those. I've tried to look all over the web, but I can't seem to find the solution. Can anybody POINT me to the right direction? Serial interrupt, for now this, maybe serialbuffered later.
Am I doing it completely wrong?
Thanks in advance. Lerche