B4R Question HTTP request using GSM SIM800/900

Discussion in 'B4R Questions' started by pokhraj_d, Oct 13, 2018 at 12:19 PM.

  1. pokhraj_d

    pokhraj_d Member

    Hello All,
    Here I have shown how we can use b4r to send http request to any cloud server using gsm SIM800 or gsm SIM900.

    We need to add GSM.bas and Globalstore.bas to add to the current project by using Project->Add existing module.

    You can also add timer or addLooper to send http request every 1 sec.

    Please note I am using GET method to send the request to cloud server.
    Code:
    Sub Process_Globals
        
    Private serial As SoftwareSerial
        
    Private bc As ByteConverter
        
    Private astream As AsyncStreams
        
    Private EOL() As Byte = Array As Byte(1310)
        
    Private busy As Boolean
        
    Private state As Int
        
    Private ServerResponse As Boolean
    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
    End Sub

    Public Sub getHumid(humid() As Byte)
        GlobalStore.Put(
    0,humid)
    End Sub

    private Sub checkResult(buffer() As Byte, cmdstatus As Boolean)
        
    'Select True
        If bc.IndexOf(buffer,"AT+CPIN?") > -1 And bc.IndexOf(buffer,"+CPIN: READY") > -1 And bc.IndexOf(buffer,"OK") > -1 Then
                cmdstatus=
    True
                
    Log("==>Command Success!!")
            
    End If
            
    If bc.IndexOf(buffer, "+CREG: 0,1") > -1 Or bc.IndexOf(buffer, "+CREG: 0,5") > -1 Then
                cmdstatus=
    True
               
    Log("==>Command Success!!")
            
    End If
           
    If bc.IndexOf(buffer, "AT+CGATT?") > -1 And bc.IndexOf(buffer,"+CGATT: 0") > -1 Then
                
    Log("GPRS not attached!!")
                cmdstatus=
    True
            
    End If
                
    If bc.IndexOf(buffer, "AT+CGATT?") > -1 And bc.IndexOf(buffer,"+CGATT: 1") > -1 Then
                    
    Log("==>Command Success!!")
                    
    Log("==>GPRS Attached!!")
                    cmdstatus=
    True
                
    End If
                
    If bc.IndexOf(buffer,"AT+SAPBR=3,1") > -1 And bc.IndexOf(buffer,"OK") > -1 Then
                    cmdstatus=
    True
                   
    Log("==>Command Success!!")
                
    End If
                
    'Set the APN Status
            If bc.IndexOf(buffer,"APN") > -1 And bc.IndexOf(buffer, "OK") > -1 Then
                cmdstatus=
    True
               
    Log("==>Command Success!!")
            
    End If
            
    'Enable GPRS Status
            If bc.IndexOf(buffer, "AT+SAPBR =1,1") > 1 And bc.IndexOf(buffer, "OK") > -1 Then
                   cmdstatus=
    True
                   
    Log("==>Command Success!!")
            
    End If
                
    If bc.IndexOf(buffer, "AT+SAPBR=2,1") > -1 And bc.IndexOf(buffer, "+SAPBR: 1,1") > -1 Then
                    cmdstatus=
    True
                    
    Log("==>Command Success!!")
                
    End If
                
    If bc.IndexOf(buffer,"AT+HTTPINIT") > -1 And bc.IndexOf(buffer,"OK") > -1 Then
                        cmdstatus=
    True
                        
    Log("==>Command Success!!")
                
    End If
    End Sub


    Public Sub Start
        state = 
    1
        ServerResponse = 
    False
        Connect
    End Sub

    Private Sub Connect
        
    Log("Connect State: ", state)
        
    Select state
            
    Case 1
                astream.Write(
    "AT+CPIN?").Write(EOL)  'Check for SIM Status
            Case 2
                astream.Write(
    "AT+CREG?").Write(EOL)  'Check for SIM registered status
            Case 3
                astream.Write(
    "AT+CGATT?").Write(EOL)  'Check for GPRS status
            Case 4  'Set connection type as GPRS
                astream.Write("AT+SAPBR=3,1").Write(",""").Write("contype").Write(""",""").Write("GPRS").Write("""").Write(EOL)
            
    Case 5 'Set the APN
                astream.Write("AT+SAPBR=3,1").Write(",""").Write("APN").Write(""",""").Write("airtelgprs.com").Write("""").Write(EOL)
            
    Case 6
                astream.Write(
    "AT+SAPBR=2,1").Write(EOL) 'Check to see the GPRS is enabled and IP has been allocated
            Case 7
                astream.Write(
    "AT+HTTPINIT").Write(EOL) 'Enable HTTP Mode
            Case 8
                astream.Write(
    "AT+HTTPPARA=""CID"",1").Write(EOL) ''Carrer Identifier
            Case 9
                astream.Write(
    "AT+HTTPPARA=""URL"",""http://xxxxx.com/abc.php?humid").Write("=").Write(GlobalStore.Slot0).Write("""").Write(EOL)
            
    Case 10
                astream.Write(
    "AT+HTTPPARA=""CONTENT"",""application/json""").Write(EOL) ''Carrer Identifier
            Case 11
                astream.Write(
    "AT+HTTPDATA=5,5000").Write(EOL) 'Send the data 5 is no of byte and 5000 is millis
            Case 12
                astream.Write(
    "Delay=6000").Write(EOL)
            
    Case 13
                astream.Write(
    "AT+HTTPACTION=0").Write(EOL) '0 is for GET and 1 is for POST
         
        
    End Select
    End Sub



    Sub AStream_NewData (Buffer() As Byte)
        
    'Log("****************")
        Log(Buffer)
        checkResult(Buffer,
    True)
        
    If ServerResponse Then Return
        busy = 
    False
                
    If  state < 13 Then
                    state = state + 
    1
                    Connect
                
    End If
    End Sub
    Below is my APP Module:
    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'Public variables can be accessed from all modules.
        Public Serial1 As Serial
        
    Private humid As String = "77.77"  'Hardcoded for testing purpose
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)
        
    Log("AppStart")
        GSM.Init(
    10,12)
        GSM.Start
        GSM.getHumid(humid)
    End Sub
     
    Johan Hormaza, Erel and BillMeyer like this.
  2. tigrot

    tigrot Well-Known Member Licensed User

    Don't be so fast in repeating get's every each second. Every successive request could cancel previous if request is not timed out.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice