'******** GMS MODULE ****************
Sub Process_Globals
Private serial As SoftwareSerial
Private bc As ByteConverter
Private astream As AsyncStreams
Private EOL() As Byte = Array As Byte(13)
Private busy As Boolean
Private messageToSend(160) As Byte
Private SMSSlot(4) As Byte
Private retryEmptyMessages As Byte
End Sub
Public Sub Init(rx As Byte, tx As Byte)
serial.Initialize(9600, rx, tx)
astream.Initialize(serial.Stream, "astream_NewData", Null)
astream.WaitForMoreDataDelay = 100 'make sure that we receive full messages
busy = False
EnableSMSEvents
End Sub
Public Sub EnableSMSEvents As Boolean
Return SendCommand("AT+CNMI=2,1", True)
End Sub
'PhoneNumber - should start with + and include country code.
Public Sub Call (PhoneNumber() As Byte) As Boolean
If busy Then Return False
busy = True
astream.Write("ATD").Write(PhoneNumber).Write(";").Write(EOL)
Return True
End Sub
'PhoneNumber - should start with + and include country code.
Public Sub SendSMS(PhoneNumber() As Byte, message() As Byte) As Boolean
If busy Then Return False
busy = True
astream.Write("AT+CMGS=""").Write(PhoneNumber).Write("""").Write(EOL)
'copy the message to the global variable
bc.ArrayCopy(message, messageToSend)
Return True
End Sub
Private Sub SendSMSPart2
busy = True
astream.Write(messageToSend).Write(Array As Byte(0x1a))
End Sub
Private Sub SendSMSPart3
Log("Message was sent successfully")
End Sub
Public Sub Hangup As Boolean
Return SendCommand("ATA", False)
End Sub
Private Sub SendCommand (cmd() As Byte, ResponseExpected As Boolean) As Boolean
If busy Then Return False
busy = ResponseExpected
astream.Write(cmd).Write(EOL)
Return True
End Sub
Public Sub CheckRegistered
SendCommand("AT+CREG?", True)
End Sub
Private Sub CheckRegisteredResult (buffer() As Byte)
If bc.IndexOf(buffer, "+CREG: 0,1") > -1 Or bc.IndexOf(buffer, "+CREG: 0,5") > -1 Then
Log("Registered")
Else
Log("Not registered")
End If
End Sub
Private Sub SMSArrived1(buffer() As Byte)
Dim i As Int = bc.IndexOf(buffer, ",")
Dim slot() As Byte = bc.SubString(buffer, i + 1)
bc.ArrayCopy(slot, SMSSlot)
Log("SMS received, slot = ", SMSSlot)
retryEmptyMessages = 0
Delay(200)
astream.Write("AT+CMGR=").Write(slot).Write(EOL)
busy = True
End Sub
Private Sub SMSArrived2(Buffer() As Byte)
busy = True
Dim afterCMGR As Boolean = False
Dim emptyMessage As Boolean = True
'Log("buffer: ", Buffer)
For Each line() As Byte In bc.Split(Buffer, Array As Byte(13, 10))
If afterCMGR Then
If line.Length = 0 Or bc.StartsWith(line, "OK") Then Continue
Log("Msg: ", line)
If emptyMessage Then
Main.MessageArrived(line)
emptyMessage = False
End If
Else If bc.StartsWith(line, "+CMGR") Then
Dim counter As Int = 0
For Each s() As Byte In bc.Split(line, """")
If counter = 3 Then
Log("From: ", s)
Else If counter = 7 Then
Log("Date: ", s)
End If
counter = counter + 1
Next
afterCMGR = True
End If
Next
If emptyMessage And retryEmptyMessages < 3 Then
retryEmptyMessages = retryEmptyMessages + 1
Log("Trying to read message again.")
Delay(200)
astream.Write("AT+CMGR=").Write(SMSSlot).Write(EOL)
Else
'delete the SMS
astream.Write("AT+CMGD=").Write(SMSSlot).Write(EOL)
End If
End Sub
Private Sub Ring(Buffer() As Byte)
Dim clip() As Byte = "+CLIP: """
Dim callingNumber() As Byte = "unknown"
Dim i1 As Int = bc.IndexOf(Buffer, clip)
If i1 > -1 Then
Dim i2 As Int = bc.IndexOf2(Buffer, """", i1 + clip.Length)
If i2 > -1 Then
callingNumber = bc.SubString2(Buffer, i1 + clip.Length, i2)
End If
End If
Log("Ring... Number: ", callingNumber)
'hangup up the call
Hangup
End Sub
Sub AStream_NewData (Buffer() As Byte)
busy = False
Select True
Case bc.IndexOf(Buffer, "+CREG:") > -1
CheckRegisteredResult(Buffer)
Case bc.IndexOf(Buffer, "RING") > -1
Ring(Buffer)
Case bc.IndexOf(Buffer, "AT+CMGS") > -1
SendSMSPart2
Case bc.IndexOf(Buffer, "+CMGS:") > -1
SendSMSPart3
Case bc.IndexOf(Buffer, "+CMTI:") > -1
SMSArrived1(Buffer)
Case bc.IndexOf(Buffer, "+CMGR:") > -1
SMSArrived2(Buffer)
Case Else
Log("****************")
Log(Buffer)
End Select
End Sub