Exemplo n.º 1
0
void cellular_init()
{
	//configure pins
	DDRB = DDRB | (1 << PORTB1);//make Cell phone ON/OFF output
	DDRB = DDRB & (0 << PORTB2); //set pin 3 "poweron MON" to be input
	
	_delay_ms(500); //wait for these pins to be stable

	if(cellular_isOn())
	{
		ToggleCellModule(); //turn it off then!
		while(cellular_isOn()) //wait for it to turn off
		{
			//maybe add an escape condition
		}
	}
	if(!cellular_isOn()) //it's off i guess
	{
		ToggleCellModule();
		while(!cellular_isOn()) //wait for it to turn on
		{
			//maybe?
		}
	}
	ATsend_no("AT\r"); //get the modules attention.
	_delay_ms(7000);
	ATsend_no("AT\r"); //no really we want your attention.
	_delay_ms(5000);	//we need these delays, because some time during
						//the init process the module will send a MWI
						//and the next command we send will likely fail
						//if it is an AT then everything is fine.
						//however this can only increase our odds...
						//without real parsing we have to do this.
}
void cellular_sms_init()
{
	ATsend_no("AT+CMGF=1\r"); //set sms mode to be text
	_delay_ms(5000); //TODO:remove this once we can figure out if commands succeed.
	ATsend_no("AT+CMGF=1\r"); //make sure that this command is set for real.
	_delay_ms(1000); //give her some thinking room.
	ATsend_no("AT+CSMP=17,167,0,16\r"); //this should be persistant, however, setting it every boot can't hurt.
	_delay_ms(2000);
}
void cellular_send_sms(char* msg, char* num)
{
	ATsend_no("AT+CMGS=\"+"); //at command for sms
	ATsend_no(num); //send the number
	ATsend_no("\"\r"); //finish the command
	_delay_ms(1000); //thinking time
	ATsend_no(msg);
	ATsend_no("\r\x1A\r");//send the end message command ascii 'SUB' or ^Z
	_delay_ms(60000); //more thinking time, this theoretically can take up to 60 seconds.
}
int cellular_ON()
{
	if(cellular_isOn())
	{
		//cellular_OFF();
		//cellular_modem_reset();
		//lcd_puts("found to be on\n");
		_delay_ms(2000);
		ATsend_no("AT\r"); //get the modules attention.
		_delay_ms(1000);
		ATsend_no("AT\r");
		_delay_ms(1000);
		ATsend_no("AT#NITZ=1,1\r");
		_delay_ms(1000);
		ATsend_no("AT+CTZU=1,1\r");
		return 0;
	}
	else //if(!cellular_isOn()) //it's off i guess
	{
		//lcd_puts("turn on");
		//ToggleCellModule();
		PORTB = PORTB | (1<<PORTB1);
		_delay_ms(5000);
		PORTB = PORTB & ~(1<<PORTB1);
		_delay_ms(500);
		int count = 0;
		while(!cellular_isOn()&&count<10) //two second time out to turn on to modem rest
		{
			lcd_putc('T');
			_delay_ms(200);
			count++;
		}
		if(count >= 10)
		{
		//	lcd_puts("timeout. reset\n");
			cellular_modem_reset();
			return -1;
		}			
		else
		{
			//lcd_puts("found to be on2\n");
			_delay_ms(2000);
			ATsend_no("AT\r"); //get the modules attention.
			_delay_ms(1000);
			ATsend_no("AT\r");
			_delay_ms(1000);
			//we need these delays, because some time during
			ATsend_no("AT#NITZ=1,1\r");
			_delay_ms(1000);
			ATsend_no("AT+CTZU=1,1\r");
			return 0;
		}			
	}					//the init process the module will send a MWI
			//and the next command we send will likely fail
						//if it is an AT then everything is fine.
						//however this can only increase our odds...
						//without real parsing we have to do this.	
}
//returns 0 for timeout and 1 for connected
int cellular_connected()
{
	int ret = 0;
	char msg[12];
	char done = 0;
	int count = 0;
	while(!done && count < 60)
	{
		ATsend_no("AT+CREG?\r");
		_delay_ms(1000);
		NextMessage(msg);
		NextMessage(msg);
		//while(NextMessage(msg))
		{	
			//lcd_puts(msg);
			//lcd_putc(msg[10]);
			if(msg[9] == '1' || msg[9] == '5')
			{
				done = 1;
				ret = 1;
			}
		}			
		count++;
	}		
	
}
void AT_init()
{
	_delay_ms(600);
	ATsend_no("AT\r");
	//if no answer in a second it should shut down...
	//we need the receive uart to work first...
}
void cellular_getTime(char * time)
{
	ATsend_no("AT+CCLK?\r");
	_delay_ms(1000);
	char temp[30];
	NextMessage(temp);
	NextMessage(temp);
	int i = 0;
	for(i = 0; i < 17;i++)
	{
		time[i] = temp[i+8];
	}
}
int cellular_modem_reset()
{
		PORTB = PORTB | (1<<PORTB3);
		_delay_ms(5000);
		PORTB = PORTB & ~(1<<PORTB3);
		if(cellular_isOn())
		{
			_delay_ms(2000);
			ATsend_no("AT\r");
			return 0;
		}
		else
		{
			_delay_ms(2000);
			return cellular_ON();
		}
}
int cellular_OFF()
{
	/*if(!cellular_isOn())
	{
		cellular_ON();
		//return -2;
	}*/		
	ATsend_no("AT#SHDN\r");
	_delay_ms(5000);
	//else
	if(cellular_isOn())
	{
		//ToggleCellModule();
		PORTB = PORTB | (1<<PORTB1);
		_delay_ms(5000);
		PORTB = PORTB & ~(1<<PORTB1);
		
		int count = 0;
		while(cellular_isOn() && count < 40)//15 second time out to modem reset
		{
			_delay_ms(375);
			count++;
		}
		if(count >= 40)
		{
			//cellular_modem_reset();
			PORTC = PORTC & (0 << PORTC7); //kill myself
			return -1;//silly...
		}			
		else
		{
			_delay_ms(1500);
			//cellular_ON();
			return 0;
		}			
	}
	else
	{
		PORTC = PORTC & (0 << PORTC7); //kill myself
	}
	
}
//gets the body of an sms message given the number to access
//returns 0 for no error, -1 for out of bounds access
int cellular_getSMS(int number, char * msg)
{
	int ret = 0;
	if(number <= 30 && number > 0)
	{
		char message[12] = {"AT+CMGR="};
		message[8] = (number/10)+48;
		message[9] = (number%10)+48;
		message[10] = 0x0D;
		message[11] = 0;
		ATsend_no(message);
		_delay_ms(5000);//5 seconds is guaranteed to work
		NextMessage(msg);
		NextMessage(msg);
		NextMessage(msg);
	}
	else
		ret = -1;
	
}
//returns -1 if out of bounds, 0 for no error, -2 if error
int cellular_deleteSMS(int messageNumber)
{
	int ret = 0;
	if(messageNumber <= 30 && messageNumber > 0)
	{
		char message[12] = {"AT+CMGD="};
		message[8] = (messageNumber/10)+48;
		message[9] = (messageNumber%10)+48;
		message[10] = 0x0D;
		message[11] = 0;
		ATsend_no(message);
		_delay_ms(5000);//5 seconds is guaranteed to work
		char at_msg[20];
		NextMessage(at_msg);
		NextMessage(at_msg);
		if(at_msg[0] == 'O' && at_msg[1] =='K')
			ret = -2;
	}
	else
		ret = -1;
}
void cellular_message_purge()
{
	char message[15] = {"at+CMGD=0,4\r"};
		ATsend_no(message);
		_delay_ms(5000);	
}