Sub Process_Globals
Public Serial1 As Serial
Private astream As AsyncStreams
Private TempSensor As Pin
Private TempSensorPinNumber As Byte = 0x0A
Private MeasureTimer As Timer
Private MeasureTimerInterval As ULong = 2
Private eth As Ethernet
Private ethClient As EthernetSocket
Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
Private ClientIP() As Byte = Array As Byte(192, 168, 0, 99)
Private ServerIP() As Byte = Array As Byte(192, 168, 0, 4)
Private const ServerPort As UInt = 51042
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
TempSensor.Initialize(TempSensorPinNumber, TempSensor.MODE_OUTPUT)
eth.Initialize(MacAddress, ClientIP)
MeasureTimer.Initialize("MeasureTimer_Tick", MeasureTimerInterval * 1000)
EthernetConnect(0)
End Sub
Private Sub MeasureTimer_Tick
Dim rawvoltage As UInt = TempSensor.AnalogRead
Dim volts As Double = rawvoltage/205.0
Dim celsiustemp As Double = 100.0 * volts - 50
If ethClient.Connected Then
Dim s As String = NumberFormat(celsiustemp, 0, 2)
astream.Write(s.GetBytes)
Else
Log("Ethernet not connected")
End If
End Sub
'Connect to the ethernet server. If ok, then start the timer to gather temperature data
Sub EthernetConnect(unused As Byte)
If ethClient.ConnectIP(ServerIP, ServerPort) = False Then
Log("Error: Trying to connect again")
CallSubPlus("EthernetConnect", 1000, 0)
Return
End If
astream.Initialize(ethClient.Stream, "Astream_NewData", "Astream_Error")
MeasureTimer.Enabled = True
End Sub
Sub Astream_NewData (Buffer() As Byte)
Log("Received from server: ", Buffer)
End Sub
Sub Astream_Error
Log("Astream Error")
ethClient.Close
CallSubPlus("EthernetConnect", 1000, 0)
End Sub