I have a the following code on my server that receives a HTTP message from a remote Arduino device.
The sequence of events is as follows:
Message received from remote device - I receive the message correctly in "Sub AStream_NewData (Buffer() As Byte)"
I then query the MySQL database table for a value in the table field - This works correctly and I get the value from "Sub getTracking(myDeviceID As String"
I must now send the value back to the remote device - This is where I am stumped.
Code:
Log from above code:
How do I now reply to the remote device with the value I get from the MySQL table?
The sequence of events is as follows:
Message received from remote device - I receive the message correctly in "Sub AStream_NewData (Buffer() As Byte)"
I then query the MySQL database table for a value in the table field - This works correctly and I get the value from "Sub getTracking(myDeviceID As String"
I must now send the value back to the remote device - This is where I am stumped.
Code:
B4X:
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 400
#End Region
Sub Process_Globals
'the ports should be opened on firewall for both incoming & outgoing ports
'port of the broker
Private PORT As Int = 5000
'port for rdc
Private RDC_PORT As Int = 4001
'server IP addres
Private SERVER_IP As String = "XXX.XXX.129.17"
Type DBResult (Tag As Object, Columns As Map, Rows As List)
Type DBCommand (Name As String, Parameters() As Object)
Private const rdcLink As String = $"http://${SERVER_IP}:${RDC_PORT}/rdc"$
Private fx As JFX
Private MainForm As Form
Private client As Socket
Private SERVER As ServerSocket
Private astream As AsyncStreams
Private txtLogs As TextArea
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
Form1.Title = $"HTTP Test - ${PORT}/${RDC_PORT}"$
txtLogs.Text = ""
ListenForClients
'**********************************************
End Sub
Sub ListenForClients
Log("ListenForClients started...")
SERVER.Initialize(PORT, "server")
Log("Server Initialized: " & SERVER.GetMyIP & "-" & PORT)
Do While True
SERVER.Listen
Log("Server Listening for connections...")
Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
NewConnection(NewSocket)
End If
Sleep(1000) 'prevent a busy loop if there is an error
Loop
End Sub
Sub NewConnection (NewSocket As Socket)
CloseExistingConnection
client = NewSocket
astream.Initialize(client.InputStream, client.OutputStream, "astream")
Log("Data Received...")
End Sub
Sub CloseExistingConnection
If astream.IsInitialized Then
astream.Close
End If
If client.IsInitialized And client.Connected Then
client.Close
End If
End Sub
Sub astream_Terminated
astream_Error
End Sub
Sub astream_Error
End Sub
Sub AStream_NewData (Buffer() As Byte)
Dim myDeviceID As String
Dim MyStr As String
Dim OAOD As String = Chr(13) & Chr(10)
Dim resp As String = "HTTP/1.1 200 OK" & OAOD & "Content-Length: 0" & OAOD & OAOD
'
MyStr = BytesToString(Buffer,0,Buffer.Length,"UTF-8")
MyStr = MyStr.trim
astream.Write(resp.GetBytes("UTF8"))
astream.SendAllAndClose
Log("Got: " & MyStr)
Log("Len Received: " & MyStr.Length)
'***************************
If MyStr.IndexOf("TRACK") > -1 Then
Log("Got Track")
myDeviceID = MyStr.SubString(MyStr.Length - 5)
Log ("Tracking ID: " & myDeviceID)
getTracking(myDeviceID)
End If
'***************************
End Sub
Sub getTracking(myDeviceID As String)
'sql.get_tracking=SELECT tracking FROM devices WHERE deviceid=?
Dim myTracking As String
Dim req As DBRequestManager = CreateRequest
Dim cmd As DBCommand = CreateCommand("get_tracking", Array(myDeviceID))
Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
If j.Success Then
req.HandleJobAsync(j, "req")
Wait For (req) req_Result(res As DBResult)
For Each row() As Object In res.Rows
myTracking = row(res.Columns.Get("tracking"))
Log("Tracking: " & myTracking)
Next
Else
Log("ERROR: " & j.ErrorMessage)
End If
j.Release
End Sub
Sub CreateCommand(Name As String, Parameters() As Object) As DBCommand
Dim cmd As DBCommand
cmd.Initialize
cmd.Name = Name
If Parameters <> Null Then cmd.Parameters = Parameters
Return cmd
End Sub
Sub CreateRequest As DBRequestManager
Dim req As DBRequestManager
req.Initialize(Me, rdcLink)
Return req
End Sub
Private Sub btnClear_Click
txtLogs.Text = ""
End Sub
Log from above code:
B4X:
ListenForClients started...
Server Initialized: 10.0.17.2-5000
Server Listening for connections...
Data Received...
Got: POST / HTTP/1.1
Host: 197.234.129.17:5000
Accept: */*
Connection: Keep-Alive
Content-Type: application/text
User-Agent: SIMCOM_MODULE
Content-Length: 10
~l00524302:
TRACKIS005
Len Received: 172
Got Track
Tracking ID: IS005
Tracking: 1
Server Listening for connections...
How do I now reply to the remote device with the value I get from the MySQL table?