Hi Guys,
I found some of the code on the internet and then discovered some of it in this forum posting, so I figure I'll ask here.
Basically, the code below does the following
1 - Start Up,
2 - Send test sms
3 - Check for "Button Pressed" event and send SMS
4 - loop through ReadSmsTwo to check for incoming SMS
5 - goto 3
So the problems with the code below are as follows
1 - The incoming SMS is on the SIM card or is delivered (verified), but for some reason, it's never read or the PIC does not read the text in the SMS - see function: void ReadSmsTwo(). void ReadSmsTwo(), should read the incoming SMS which has either ON or OFF as the value or content in the SMS. A NULL as a text word is sent as a reply if nothing is read from the SMS. If the word "ON" is read, the reply to the SMS should be "On" to verify it was recieved. While many SMSes with the word ON are sent, all that is ever sent in reply is the word NULL - by design and the diagnostic result of the SMS not being read. This means that the ReadSmsTwo function does not seem to be reading the incoming SMS out of the receive buffer and converting. This does not seem to make sense as by syntax it's correct . . . at least from my perspective.
2 - While the code "returns' to the main function, it only ever detects that the "Button Pressed" or PIN PORTB.BO pulled to ground before it calls the ReadSmsTwo() function, and seldom or never after when it returns which makes me wonder if something changes on the port settings.
The code sample was found here
https://www.electro-tech-online.com/thr ... 87.153661/, and was allegedly working on a PIC16F887 which from what I can see should be no problem for the PIC16F1847 to run the same code . . . I am open to correction. I would appreciate any assistance because I am either not seeing what I am doing wrong or there is something else not working here.
Code: Select all
sbit TestSms at RB0_bit;
void SetupDevice();
void SendSms();
void ReadSms();
void ReadSmsTwo();
void DeleteSMS(int Command);
void debounce();
unsigned char test,got_something;
char *TST = "Test SMS";
char *MSG = "Button Pressed";
char *SMS;
char *result;
int i=0;
char incchar;
char incoming[80];
bit RecFlag;
void SetupDevice(){
test = 1;
// Configure Analogue Ports
ANSELA = 0;
ANSELB = 0;
// Confgiure Ports and TRIS Registers
TRISB = 0b00000001; // SendSms Input Button
// Configure Uart
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize
}
void SendSms(char Action){
/*
0 - External input
1 - Button Press
2 - Mirror Message
3 - Null
*/
UART1_Write_Text("AT+CMGF=1\r\n"); // Set the GSM module into text mode
Delay_ms(1000);
UART1_Write_Text("AT+CMGS=\"[CELL NUMBER]\"\r\n");
Delay_ms(1000);
if(test ==1){
UART1_Write_Text(TST);
Delay_ms(1000);
test = 0;
UART1_Write(26);
Delay_ms(1000);
return;
}
if(Action == 1){
UART1_Write_Text(MSG);
Delay_ms(1000);
UART1_Write(26);
Delay_ms(1000);
return;
}
if(Action == 2){
UART1_Write_Text(SMS);
Delay_ms(1000);
UART1_Write(26);
Delay_ms(1000);
RecFlag = 0;
return;
}
if(Action == 3){
UART1_Write_Text("NULL");
Delay_ms(1000);
UART1_Write(26);
Delay_ms(1000);
RecFlag=0;
return;
}
}
void ReadSms(){
int count =0,comp=1;
got_something = 0;
UART1_Write_Text("ATE0\r\n"); // Echo OFF
Delay_ms(1000);
UART1_Write_Text("AT+CMGF = 1\r\n"); // Set to text mode
Delay_ms(1000);
UART1_Write_Text("AT+CNMI=1,2,0,0,0\r\n"); // setup receiving (now important at this stage)
Delay_ms(1000);
if (UART1_Data_Ready() == 1) { // if data is received
UART1_Read_Text(SMS, "OK", 254); // reads text until 'OK' is found
}
return;
}
void ReadSmsTwo(){
UART1_Write_Text("ATE0\r\n"); // AT command for Echo OFF
Delay_ms(1000);
UART1_Write_Text("AT\r\n"); // test GSM Modem
Delay_ms(1000);
UART1_Write_Text("AT+CMGF = 1\r\n"); //convert Sim800 module to text mode
Delay_ms(1000);
UART1_Write_Text("AT+CNMI=1,2,0,0,0\r\n"); // setup receiving (now important at this stage)
Delay_ms(1000);
if (UART1_Data_Ready()==1) { // If data is received,
for (i= 0; i < 80; i++) {
incchar = UART1_Read(); // read the received data,
incoming[i] = incchar;
}
}
if (result = strstr (incoming, "ON")); // search for string "ON" in incoming and save it in result
if (memcmp("ON", result, 2) == 0)
{
SMS = "On";
RecFlag =1;
}
if (result = strstr (incoming, "OFF")); // search for string "OFF" in incoming and save it in result
if (memcmp("OFF", result, 3) == 0)
{
SMS = "Off";
RecFlag =1;
}
}
void DeleteSMS(int Command){
switch(Command){
case 1:break;
case 2:break;
case 3:break;
case 4: Uart1_Write_Text("AT+CMGD=0,4\r");Delay_ms(500);break;
default:break;
}
}
void debounce(){
Delay_ms(300);
}
void main() {
Delay_ms(500); // Gives modem time to power up
SetupDevice(); // Device peripherals
SendSms(0);
DeleteSMS(4);
while(1){
if(!PORTB.B0){
debounce();
SendSms(1);
Delay_ms(3000);
}
// Continual Test for incoming SMSes
ReadSmsTwo();
if(RecFlag ==1){
SendSms(2);
}
else{SendSms(3);}
}