News:

Let's find out together what makes a PIC Tick!

Main Menu

MikroElectronika GSM Click

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

Previous topic - Next topic

Volker

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 :-)

V

 


 

keytapper

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

top204

#2
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:

https://www.ebay.co.uk/itm/GPRS-GSM-Modul-M590-M590E-SIM-900-1800-MHz-Frequenz-SMS-DIY-KIT-Arduino-AU/272765192229?hash=item3f82128825:g:4A4AAOSwacdZbA4M

Or this one looks nice:

https://www.ebay.co.uk/itm/GA6-B-Mini-GSM-GPRS-Development-Quad-band-Board-Audio-Board-5V-Replace-SIM800L/401179572066?_trkparms=aid%3D555023%26algo%3DHOMESPLICE.SIMRVI%26ao%3D1%26asc%3D20210125104017%26meid%3D3d3a3cc7d7db45ea9f79be1e6f1ee58b%26pid%3D100752%26rk%3D5%26rkt%3D15%26mehot%3Dpf%26sd%3D272765192229%26itm%3D401179572066%26pmt%3D1%26noa%3D0%26pg%3D2047675%26algv%3DSimplAMLv5hPointwiseWebNoToraCoCoViewsNoHighIdfOrRoundRobinBlenderWithPromotedViewItems%26brand%3DUnbranded&_trksid=p2047675.c100752.m1982

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       '
    SendCommand()

Proc SendCommand()
    Clear sComResString                                         ' Clear the modem response string before a command is transmitted
    HRSOut sCommandString, 13                                  ' Transmit the command
EndProc
   
'-----------------------------------------------------------------------------------------------------------------------
' 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
_Cont:
    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
        EndIf
        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
EndProc

'-----------------------------------------------------------------------------------------------------------------------
' 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
    Do       
        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
        EndIf
        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
    Loop
EndProc

'-----------------------------------------------------------------------------------------------------------------------
' 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
        EndIf
        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
        EndIf
        DelayMS 512                                         ' Delay between commands being sent
    Loop
EndProc
       
'---------------------------------------------------------------------------------
Main:
    Modem_AT()
    Modem_CREG()

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.

Volker

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

V