PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

MikroElectronika GSM Click

Started by Volker, Feb 17, 2021, 07:54 PM

Previous topic - Next topic


Hi Every one

Its so good to be part of the Proton Compiler Family again.

I am planning to use a GSM Click from MikroElectronika for a small project.

Although around the block in terms of using Proton Basic, I never really got into GSM/GPRS applications.

I would really appreciate some help in terms of basic sample code, just to get me going

Thanks :-)





I may help to translate the Mikrobasic, but their libraries  are difficult to read. Hope some member has a snippet for GSM. Which is basically a UART dialog.
Ignorance comes with a cost


Mobile phone modems use a simple AT+ interface, and the commands are well documented.

Talking to a modem involves sending the relevant AT+ command via a serial interface, then waiting for the serial response. If the response does not come within a certain time, try again a few times.

Instead of being, totally, ripped off my Mikro, why not buy a similar system for a fraction of the price, that does exactly the same job. i.e. A mobile phone modem on a board. For example:

Or this one looks nice:

Mobile modems are now an old technology and there are many, many available for very little money, and they require very few components around them.

All GSM, 3G, and 4G modems operate exactly the same, and they all use exactly the same AT+ commands to make calls, or send data or examine the networks etc...

Below is a , stripped down, section of code that I wrote to do network analysis, for far too many years. It has the modem send mechanism and the response mechanism, and also shows two procedures that work with them:

    Device = 18F25K20
    Declare Xtal = 16
' Setup USART1
    Declare Hserial1_Baud = 9600        ' Setup the Baud rate of USART1
    Declare HRSOut1_Pin = PORTC.6       ' Setup the pin used for USART1 TX
    Declare HRSIn1_Pin = PORTC.7        ' Setup the pin used for USART1 RX
' Create some global variables
    Dim bModemReceiveBuffer[128] As Byte Heap                   ' Holds the bytes received from the modem
    Dim sComResString As String * 64 At bModemReceiveBuffer     ' Holds a parsed string returned from the modem (aliased to bModemReceiveBuffer)
    Dim sCommandString As String * 32 Heap                      ' Holds the command string to send to the modem 
' Send a text string to the modem
' Input     : String sCommandString holds the text to send to the modem
' Output    : None
' Notes     : Appends a carriage return to the text sent
$define Modem_SendCommand(pCommand) '
    sCommandString = pCommand       '

Proc SendCommand()
    Clear sComResString                                         ' Clear the modem response string before a command is transmitted
    HRSOut sCommandString, 13                                  ' Transmit the command
' Get the response from the modem (with an alterable timeout value)
' Input     : pTimeout holds the timeout value
' Output    : Array bModemReceiveBuffer holds the response
'           : Returns 0 if the receive timed out
' Notes     : Discards any initial <CR> or <LF> bytes transmitted by the modem
Proc Modem_WaitForResponse(pTimeout As Word), Bit
    Dim bChar As Byte
    Dim bCharpos As Byte
    Dim wFSR1 As FSR1L.Word
    wFSR1 = AddressOf(bModemReceiveBuffer)                      ' Point FSR1L\H to the beginning of the receive buffer
    bCharpos = 0
    HSerIn pTimeout, TimeOut, [bChar]                           ' Receive a byte from the modem
    If bChar <= 13 Then _Cont                                   ' Look again if it's a <CR> or less
    POSTINC1 = bChar                                            ' Add the character received to the array

    Repeat                                                      ' Create a loop
        HSerIn pTimeout, TimeOut, [bChar]                       ' Receive a byte from the modem (with timeout)
        If bChar = 13 Then                                      ' Is the byte a <CR> (13)?
            INDF1 = 0                                           ' Yes. So add a NULL to terminate the string
            Result = 1                                          ' Indicate not timed out
            ExitProc                                            ' Exit the procedure
        POSTINC1 = bChar                                        ' Otherwise. Add the character received to the array
        Inc bCharpos                                            ' Move up to the next position within the string
    Until bCharpos >= SizeOf(bModemReceiveBuffer)               ' Until the end of the array's length is reached
TimeOut:                                                        ' Come here if a serial timeout occured
    Result = 0                                                  ' Indicate a timeout occured

' Issue the AT command repeatedly until it receives "AT" back
' Input     : None
' Output    : Returns 0 if an error occured
' Notes     : None
Proc Modem_AT(), Bit
    Dim bEventCount As Byte = 9
    Result = 1                                      ' Default to no errors
        If bEventCount = 0 Then                     ' Have we transmitted the command several times without success?
            Result = 0                              ' Yes. So indicate that a problem occured
            ExitProc                                ' Exit the procedure
        Modem_SendCommand("AT")                     ' Send the command to the modem
        Modem_WaitForResponse(1024)                 ' Get the response with a timeout
        If sComResString = "OK" Then Break          ' Have we received a response of OK? If so, default to a good signal and exit the loop
        Dec bEventCount                             ' Decrement the event counter
        DelayMS 1024                                ' Delay between commands sent

' Attempt to register on the home network
' Input     : None
' Output    : Returns 255 if an error occured
'           : Otherwise... Returns the registration value
'           :   0 = Network not registered.
'           :   1 = Registered on Home Network.
'           :   2 = Searching for an Operator to Register With.
'           :   3 = Unable to Register with a Network. Registration Denied.
'           :   4 = Unable to Register with a Network. Unknown Reason.
'           :   5 = Currently Sitting on a Roaming Network.
' Notes     : Attempts to register continuously, until a response if given by the modem
Proc Modem_CREG(), Byte
     Do                                                      ' Create a loop       
        Modem_SendCommand("AT+CREG?")                       ' Send a command to the modem
        Modem_WaitForResponse(4096)                         ' Get the response with a timeout
        If sComResString = sCommandString Then              ' Is the response the same as the command sent?
            Result = 255                                    ' Yes. So return an invalid response
            ExitProc                                        ' Exit the procedure
        If Len(sComResString) >= 9 Then                     ' Have we received enough characters in the response from the modem?
            Result = sComResString#9                        ' Yes. So extract the second response value
            ExitProc                                        ' Exit the procedure
        DelayMS 512                                         ' Delay between commands being sent

The initialisation of the modem depends on the modem model. Some modems give lots of, pointless, text out when they are first powered up. So a procedure will need to be created to wait for the serial data to end, before sending/receiving commands.


hi guys

@ keytapper & top204

Thanks for replying and also your help and tips in this regard, much appreciated. Yip the GSM Click from MikroElectronika looks cool, but must admit its expensive.

kind regards
Enjoy the weekend