@ Dat - I supposed could be the switch because it is the only one hardware that changes between testing the program at home and testing the program at office.
It’s happened again and I have another detail on this issue: it’s happened after 10 days that it was turned off.
After 10 days that it was turned off, I turned on cerberus to check the MAC Address through FEZ Config and MAC is returned to the “factory value” 00-21-03-00-00-02 instead of the MAC address I had set.
Anyway, I didn’t erase application so I’ll try.
I’ll take a look to the code snippet linked by @ Jay Jay.
As you required, this is the source code:
Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics
Imports System.IO
Imports Microsoft.SPOT.Net.NetworkInformation
Imports System.Text
Imports Microsoft.SPOT.IO
Imports System.Runtime.CompilerServices
Namespace AGITempStationNew
Partial Public Class Program
Private Const DEVICE_NAME As String = "TempStation"
Private Const FIRMWARE_VERSION As String = "2.5"
Private theadInit As Thread, threadSend As Thread
Private WithEvents showTimer As GT.Timer
Private pDisplay As CharDisplayInterface
Private pSettings As IniFileReader
Private strTemperature As String, strHumidity As String
Private pEthernetWorking As Boolean
Private pDeviceAlias As String
Private pAlternateShowInfo As Boolean
Private pDataUrl As String
Private pTemperatureTrim As Single
Private _InitializationCompleted As Boolean
' This is run when the mainboard is powered up or reset.
Public Sub ProgramStarted()
Try
Debug.Print("ProgramStarted() - Initializing Program")
_InitializationCompleted = False
pAlternateShowInfo = False
Debug.Print("ProgramStarted() - Initializing Char Display")
pDisplay = New CharDisplayInterface(charDisplay)
pDisplay.SetBacklight(False)
Thread.Sleep(100)
Debug.Print("ProgramStarted() - Starting Init Thread")
theadInit = New Thread(AddressOf InitializeComponents)
theadInit.Start()
Debug.Print("ProgramStarted() - End")
Catch ex As Exception
Debug.Print("ProgramStarted() - " & ex.Message)
DebugLedPlayBlink(100, 200, 5)
End Try
End Sub
Private Sub ReadTempHumidity()
Try
Dim pDatas As TempHumidSI70.Measurement = tempHumidSI70.TakeMeasurement()
strTemperature = CSng(pDatas.Temperature + pTemperatureTrim).ToString("F1")
strHumidity = CInt(pDatas.RelativeHumidity).ToString
pDatas = Nothing
Catch ex As Exception
Throw
End Try
End Sub
Private Sub InitializeComponents()
'Dim strSezione As String = "Starting"
Debug.Print("InitializeComponents() - Starting")
Try
Mainboard.SetDebugLED(True)
Thread.Sleep(2000)
'###################
'# CARICO LOGO AGI #
'###################
'strSezione = "logo"
Debug.Print("InitializeComponents() - Logo")
With pDisplay
.PrintString(DEVICE_NAME, 0, 0, CharDisplayInterface.TextAlignment.RightAlign)
.PrintString("V" & FIRMWARE_VERSION, 1, 0, CharDisplayInterface.TextAlignment.RightAlign)
.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition0, {&H0, &H0, &H1, &H2, &H2, &H4, &H4, &H8})
.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition1, {&H0, &H10, &H11, &H12, &H12, &H14, &H14, &H14})
.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition2, {&H0, &H1E, &H0, &H0, &H0, &H0, &H0, &H0})
.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition3, {&H0, &H3, &H3, &H3, &H3, &H3, &H3, &H3})
.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition4, {&H8, &H8, &H10, &H1F, &H10, &H10, &H10, &H0})
.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition5, {&H14, &H14, &H14, &H12, &H12, &H11, &H10, &H0})
.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition6, {&H1, &H0, &H0, &H0, &H0, &H1, &H1E, &H0})
.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition7, {&H1B, &HB, &HB, &H13, &H13, &H3, &H3, &H0})
.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition0, 0, 0)
.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition1, 0, 1)
.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition2, 0, 2)
.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition3, 0, 3)
.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition4, 1, 0)
.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition5, 1, 1)
.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition6, 1, 2)
.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition7, 1, 3)
Thread.Sleep(250)
.SetBacklight(True)
Thread.Sleep(4000)
.PrintString(New String(" ", 12), 0, 4, CharDisplayInterface.TextAlignment.NoAlign)
.PrintString(New String(" ", 12), 1, 4, CharDisplayInterface.TextAlignment.NoAlign)
Thread.Sleep(100)
.PrintString(" Caricamento", 0, 4, CharDisplayInterface.TextAlignment.NoAlign)
End With
'########################
'# CARICO SETTING DA SD #
'########################
'strSezione = "impostazioni da sd"
Debug.Print("InitializeComponents() - Reading SD Card")
pDisplay.PrintString("Impostazioni", 1, 4, CharDisplayInterface.TextAlignment.NoAlign)
Try
Thread.Sleep(500)
Dim strText As String = ReadAllText(VolumeInfo.GetVolumes()(0), "settings.agi")
pSettings = New IniFileReader("=")
pSettings.LoadFromText(strText)
strText = Nothing
'Leggo il DeviceAlias
pDeviceAlias = pSettings.GetSetting("DeviceAlias")
If pDeviceAlias.Trim.Length = 0 Then
pDeviceAlias = DEVICE_NAME
Else
If pDeviceAlias.Length > 16 Then
pDeviceAlias = pDeviceAlias.Substring(0, 16)
End If
End If
pTemperatureTrim = CSng(pSettings.GetSetting("TempTrim"))
pDataUrl = pSettings.GetSetting("DataUrl")
If pDataUrl.Substring(pDataUrl.Length - 1, 1) <> "/" Then
pDataUrl &= "/"
End If
pDataUrl &= "devicecomm.aspx" 'DataUrl=http://192.168.2.45/AGITempStationWM/
Catch ex As Exception
Debug.Print(ex.Message)
Throw
End Try
'########################
'# INIZIALIZZO ETHERNET #
'########################
'strSezione = "eth inizio"
Debug.Print("InitializeComponents() - Ethernet Init")
pDisplay.PrintString(" Ethernet", 1, 4, CharDisplayInterface.TextAlignment.NoAlign)
Try
'strSezione = "eth use this nw interface"
Debug.Print("InitializeComponents() - Ethernet UseThisNetworkInterface")
ethernetENC28.UseThisNetworkInterface()
Thread.Sleep(500)
'strSezione = "eth dhcp o static"
Debug.Print("InitializeComponents() - Ethernet DHCP or StaticIP")
If pSettings.GetSetting("LanUseDhcp").Trim.ToLower = "true" Then
'DHCP
'strSezione = "eth dhcp"
Debug.Print("InitializeComponents() - Ethernet DHCP")
With ethernetENC28.NetworkInterface
If .IsDhcpEnabled Then
.ReleaseDhcpLease()
End If
.EnableDhcp()
.EnableStaticDns(New String() {"8.8.8.8"})
EthernetWaitUntilIPAddressIsGranted(ethernetENC28.NetworkInterface.NetworkInterface)
End With
Else
'STATIC IP
'strSezione = "eth static ip"
Debug.Print("InitializeComponents() - Ethernet StaticIP")
With ethernetENC28.NetworkInterface
If Not .IsDhcpEnabled Then
.ReleaseDhcpLease()
End If
.EnableStaticIP(pSettings.GetSetting("LanIpAddress"), pSettings.GetSetting("LanSubnetMask"), pSettings.GetSetting("LanGateway"))
.EnableStaticDns(New String() {"8.8.8.8"})
EthernetWaitUntilIPAddressIsGranted(ethernetENC28.NetworkInterface.NetworkInterface)
End With
End If
'strSezione = "eth check valid address"
Debug.Print("InitializeComponents() - Ethernet Check IP Address")
Thread.Sleep(1000)
If ethernetENC28.IsNetworkConnected And ethernetENC28.NetworkInterface.IPAddress <> "0.0.0.0" Then
pEthernetWorking = True
Else
pEthernetWorking = False
End If
'strSezione = "eth mac addr"
Debug.Print("InitializeComponents() - Ethernet MACAddress")
Thread.Sleep(1000)
Catch ex As Exception
WriteAllText(VolumeInfo.GetVolumes()(0), "errors.txt", "InitializeEthernet - " & ex.Message & " (" & ex.GetType.ToString & ")")
Debug.Print(ex.Message)
Throw
End Try
''########################
''# INIZIALIZZO ETHERNET #
''########################
''strSezione = "eth inizio"
'Debug.Print("InitializeComponents() - Update time with NTP")
'pDisplay.PrintString(" Data e Ora", 1, 4, CharDisplayInterface.TextAlignment.NoAlign)
'Try
' NTPManager.UpdateTimeFromNtpServer("pool.ntp.org", +2)
'Catch ex As Exception
' WriteAllText(VolumeInfo.GetVolumes()(0), "errors.txt", "InitializeNTP - " & ex.Message & " (" & ex.GetType.ToString & ")")
' Debug.Print(ex.Message)
' Throw
'End Try
'####################
'# AVVIO WEB SERVER #
'####################
' /moduledata > restituisce i dati
If pEthernetWorking Then
Debug.Print("InitializeComponents() - WebServer Start")
WebServer.StartLocalServer(ethernetENC28.NetworkInterface.NetworkInterface.IPAddress, 80)
Thread.Sleep(100)
Debug.Print("InitializeComponents() - WebServer Events")
AddHandler WebServer.DefaultEvent.WebEventReceived, AddressOf webHome_WebEventReceived
AddHandler WebServer.SetupWebEvent("reboot").WebEventReceived, AddressOf webReboot_WebEventReceived
End If
'################################
'# AVVIO FUNZIONAMENTO STANDARD #
'################################
pDisplay.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition0, {&H4, &H6, &HA, &H9, &H13, &H13, &H17, &HE})
pDisplay.InitDisplay(" ", " C %")
'Simbolo gradi
pDisplay.PrintChar(223, 1, 5)
'Simbolo umidità
pDisplay.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition0, 1, 11)
Thread.Sleep(100)
showTimer = New GT.Timer(10000, Gadgeteer.Timer.BehaviorType.RunContinuously)
showTimer.Start()
ReadTempHumidity()
Thread.Sleep(100)
DrawScreenInfo()
pSettings = Nothing
Thread.Sleep(100)
_InitializationCompleted = True
Mainboard.SetDebugLED(False)
Debug.Print("InitializeComponents() - End")
Catch ex As Exception
Try
WriteAllText(VolumeInfo.GetVolumes()(0), "errors.txt", "InitializeComponents() - " & ex.Message & " (" & ex.GetType.ToString & ")")
Catch ex2 As Exception
End Try
DebugLedPlayBlink(100, 200, 5)
End Try
End Sub
Private Sub DrawScreenInfo()
Dim pTempRow As String, pTempRH As String
pDisplay.CursorHome()
If pAlternateShowInfo Then
If pEthernetWorking Then
pDisplay.PrintString(ethernetENC28.NetworkInterface.NetworkInterface.IPAddress, 0, 0, CharDisplayInterface.TextAlignment.CenterAlign)
Else
pDisplay.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition1, {&H0, &H3, &H6, &HF, &H1E, &HF, &H6, &H3})
pDisplay.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition2, {&H0, &H2, &H4, &H8, &H4, &H2, &H4, &H8})
pDisplay.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition3, {&H0, &H18, &HC, &H1E, &HF, &H1E, &HC, &H18})
pDisplay.CharacterCustomize(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition4, {&H0, &H0, &H0, &H0, &H1F, &H0, &H0, &H0})
pDisplay.PrintString(" OFFLINE ")
pDisplay.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition4, 0, 10)
pDisplay.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition1, 0, 11)
pDisplay.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition2, 0, 12)
pDisplay.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition3, 0, 13)
pDisplay.CharacterCustomizedShow(CharDisplayInterface.eCharDisplayCustomCharMemPos.MemoryPosition4, 0, 14)
End If
Else
pDisplay.PrintString(pDeviceAlias, 0, 0, CharDisplayInterface.TextAlignment.CenterAlign)
End If
pAlternateShowInfo = Not pAlternateShowInfo
'show temp
pTempRow = " " & strTemperature
'show rh%
pTempRH = strHumidity
If pTempRH.Length = 2 Then
pTempRH = " " & pTempRH
End If
If strTemperature = "0.0" And strHumidity = "0" Then
pTempRow = " -"
pTempRH = " -"
Else
pTempRow = " " & strTemperature
pTempRH = strHumidity
If pTempRH.Length = 2 Then
pTempRH = " " & pTempRH
End If
End If
pDisplay.PrintString(pTempRow, 1, 0, CharDisplayInterface.TextAlignment.NoAlign)
pDisplay.PrintString(pTempRH, 1, 12, CharDisplayInterface.TextAlignment.NoAlign)
pTempRow = Nothing
pTempRH = Nothing
End Sub
Private Sub showTimer_Tick(timer As Gadgeteer.Timer) Handles showTimer.Tick
Try
If Not _InitializationCompleted Then Exit Sub
showTimer.Stop()
If theadInit IsNot Nothing Then
If theadInit.ThreadState = ThreadState.Stopped Then
theadInit = Nothing
End If
End If
ReadTempHumidity()
DrawScreenInfo()
If pEthernetWorking Then
If Not pAlternateShowInfo Then
'Così facendo invio informazioni ogni 20 secondi, non ogni 10.
If threadSend IsNot Nothing Then
If threadSend.ThreadState <> ThreadState.Stopped Then
threadSend.Abort()
Thread.Sleep(100)
End If
threadSend = Nothing
End If
threadSend = New Thread(AddressOf SendDatasToServer)
threadSend.Start()
End If
End If
Debug.Print(">>>>> Memory: " & Debug.GC(True))
showTimer.Start()
Catch ex As Exception
Debug.Print("showTimer_Tick() - " & ex.Message)
Try
WriteAllText(VolumeInfo.GetVolumes()(0), "errors.txt", "showTimer_Tick() - " & ex.Message & " (" & ex.GetType.ToString & ")")
Catch ex2 As Exception
End Try
DebugLedPlayBlink(100, 200, 5)
End Try
End Sub
Private Sub webReboot_WebEventReceived(path As String, method As WebServer.HttpMethod, responder As Responder)
Try
responder.Respond("OK")
Thread.Sleep(1000)
Reboot()
Catch ex As Exception
Debug.Print("webModuleDatas_WebEventReceived() - " & ex.Message)
End Try
End Sub
Private Sub webHome_WebEventReceived(path As String, method As WebServer.HttpMethod, responder As Responder) ' Handles webHome.WebEventReceived
Dim bArr() As Byte, str As String
Try
str = GetHomePageHtml()
bArr = StringToByteArray(str)
responder.Respond(bArr, "text/html")
'responder.Respond(StringToByteArray(GetHomePageHtml), "text/html")
Catch ex As Exception
Debug.Print("webHome_WebEventReceived() - " & ex.Message)
Finally
str = Nothing
bArr = Nothing
'WriteAllText(VolumeInfo.GetVolumes()(0), "resp.txt", ">HOMEPAGE")
End Try
End Sub
Private Function IIF(condition As Boolean, conditionTrue As String, conditionFalse As String) As String
If condition Then
Return conditionTrue
Else
Return conditionFalse
End If
End Function
Private Function GetModuleDatas() As String
'28/03/2015 - Ottimizzo risposta. Se TRUE leggerò solo "data". Se FALSE leggerò solo "error"
Try
Return "{" & _
"""success"":true," & _
"""data"":{""alias"":""" & pDeviceAlias & """,""temperature"":" & strTemperature & ",""humidity"":" & strHumidity & "}" & _
"}"
Catch ex As Exception
Return "{" & _
"""success"":false," & _
"""error"":""" & ex.Message & """" & _
"}"
End Try
End Function
Private Function GetHomePageHtml() As String
Try
'28/03/2015 - 1245 chars
'Return "<html><head>" & _
' "<title>AGI " & DEVICE_NAME & " V" & FIRMWARE_VERSION & "</title>" & _
' "<meta name=""viewport"" content=""width=device-width, user-scalable=no""/>" & _
' "<style type=""text/css"">" & _
' "*,html,table,div{font-family:Arial;font-size:12pt;}" & _
' ".tb1{border:0px;width:200px;}" & _
' ".tb1lbl{width:100px;}" & _
' ".tb2{border:0px;width:300px;}" & _
' ".tb2lbl{width:150px;}" & _
' "</style>" & _
' "</head><body>" & _
' "<div style=""width:100%;text-align:center;font-weight:bold;font-size:20pt;"">" & pDeviceAlias & "</div>" & _
' "<br/><center>———————</center><br/>" & _
' "<table class=""tb1"" align=""center"">" & _
' "<tr><td class=""tb1lbl"">Temperatura:</td><td><b>" & pTemperature.ToString("F1") & "°C</b></td></tr>" & _
' "<tr><td>Umidità:</td><td><b>" & CInt(pHumidity) & "%</b></td></tr>" & _
' "<tr><td>Correz.Temp.:</td><td><b>" & pTemperatureTrim.ToString("F1") & "°C</b></td></tr>" & _
' "</table>" & _
' "<br/><center>———————</center><br/>" & _
' "<table class=""tb2"" align=""center"">" & _
' "<tr><td class=""tb2lbl"">IP:</td><td><b>" & ethernetENC28.NetworkInterface.NetworkInterface.IPAddress & "</b></td></tr>" & _
' "<tr><td>DHCP:</td><td><b>" & IIF(ethernetENC28.NetworkInterface.IsDhcpEnabled, "SI", "NO") & "</b></td></tr>" & _
' "<tr><td>MAC Address:</td><td><b>" & MACAddressToString(ethernetENC28.NetworkInterface.NetworkInterface.PhysicalAddress) & "</b></td></tr>" & _
' "<tr><td>Subnet Mask:</td><td><b>" & ethernetENC28.NetworkInterface.SubnetMask & "</b></td></tr>" & _
' "<tr><td>Gateway:</td><td><b>" & ethernetENC28.NetworkInterface.GatewayAddress & "</b></td></tr>" & _
' "<tr><td colspan=""2""> </td></tr>" & _
' "<tr><td colspan=""2"" style=""text-align: center;""><b><i>© Copyright AGI</i></b></td></tr>" & _
' "</table>" & _
' "</body></html>"
'28/03/2015 - 1010 chars
'Return "<html><head><title>AGI " & DEVICE_NAME & " V" & FIRMWARE_VERSION & "</title>" & _
' "<meta name=""viewport""content=""width=device-width,user-scalable=no""/>" & _
' "<style type=""text/css"">*,html,table,div{font-family:Arial;font-size:12pt;}.p20{font-weight:bold;font-size:20pt;line-height:50px;}</style>" & _
' "</head><body><table style=""border:0px;text-align:center;min-width:200px;""align=""center"">" & _
' "<tr><td colspan=""2""class=""p20"">" & pDeviceAlias & "</td></tr>" & _
' "<tr><td class=""p20""style=""width:50%;"">" & pTemperature.ToString("F1") & "°C</td><td class=""p20"">" & CInt(pHumidity) & "%</td></tr></table><hr style=""width:100px;""/><br/>" & _
' "<table style=""border:0px;width:300px;""align=""center"">" & _
' "<tr><td style=""width:150px;"">Correz.Temp.:</td><td><b>" & pTemperatureTrim.ToString("F1") & "°C</b></td></tr>" & _
' "<tr><td>IP:</td><td><b>" & ethernetENC28.NetworkInterface.NetworkInterface.IPAddress & "</b></td></tr>" & _
' "<tr><td>DHCP:</td><td><b>" & IIF(ethernetENC28.NetworkInterface.IsDhcpEnabled, "SI", "NO") & "</b></td></tr>" & _
' "<tr><td>MACAddress:</td><td><b>" & MACAddressToString(ethernetENC28.NetworkInterface.NetworkInterface.PhysicalAddress) & "</b></td></tr>" & _
' "<tr><td>SubnetMask:</td><td><b>" & ethernetENC28.NetworkInterface.SubnetMask & "</b></td></tr>" & _
' "<tr><td>Gateway:</td><td><b>" & ethernetENC28.NetworkInterface.GatewayAddress & "</b></td></tr>" & _
' "<tr><td colspan=""2""style=""text-align:center;""><b><i>© AGI</i></b></td></tr></table></body></html>"
Return "<html><head><title>AGI " & DEVICE_NAME & " V" & FIRMWARE_VERSION & "</title>" & _
"<meta name=""viewport""content=""width=device-width,user-scalable=no""/>" & _
"<style type=""text/css"">*,html,table,div{font-family:Arial;font-size:12pt;}.p20{font-weight:bold;font-size:20pt;line-height:50px;}</style>" & _
"</head><body><table style=""border:0px;text-align:center;min-width:200px;""align=""center"">" & _
"<tr><td colspan=""2""class=""p20"">" & pDeviceAlias & "</td></tr>" & _
"<tr><td class=""p20""style=""width:50%;"">" & strTemperature & "°C</td><td class=""p20"">" & strHumidity & "%</td></tr></table><hr style=""width:100px;""/><br/>" & _
"<table style=""border:0px;width:300px;""align=""center"">" & _
"<tr><td style=""width:150px;"">Correz.Temp.:</td><td><b>" & pTemperatureTrim.ToString("F1") & "°C</b></td></tr>" & _
"<tr><td>IP:</td><td><b>" & ethernetENC28.NetworkInterface.NetworkInterface.IPAddress & "</b></td></tr>" & _
"<tr><td>DHCP:</td><td><b>" & IIF(ethernetENC28.NetworkInterface.IsDhcpEnabled, "SI", "NO") & "</b></td></tr>" & _
"<tr><td>MACAddress:</td><td><b>" & MACAddressToString(ethernetENC28.NetworkInterface.NetworkInterface.PhysicalAddress) & "</b></td></tr>" & _
"<tr><td>SubnetMask:</td><td><b>" & ethernetENC28.NetworkInterface.SubnetMask & "</b></td></tr>" & _
"<tr><td>Gateway:</td><td><b>" & ethernetENC28.NetworkInterface.GatewayAddress & "</b></td></tr>" & _
"<tr><td colspan=""2""style=""text-align:center;""><b><i>© AGI</i></b></td></tr></table></body></html>"
Catch ex As Exception
Return "exception: " & ex.Message
End Try
End Function
Private Sub EthernetWaitUntilIPAddressIsGranted(ethernetInterface As NetworkInterface)
'****************************************
'ATTENDO CHE IL CIRCUITO PRENDA UN IP
'****************************************
Dim ctrSecondi As Byte = 0
While ethernetInterface.IPAddress.Trim = "0.0.0.0"
If ctrSecondi >= 10 Then
'indirizzo ip non acquisito
ctrSecondi = 99
Exit While
End If
Debug.Print("Awaiting IP Address...")
Thread.Sleep(1000)
ctrSecondi = ctrSecondi + 1
End While
If ctrSecondi = 99 Then
Debug.Print("IP Address failed.")
Else
Debug.Print("IP Address Granted: " & ethernetInterface.IPAddress)
End If
End Sub
Private Function MACAddressToString(value As Byte()) As String
Return value(0).ToString("X") & ":" & _
value(1).ToString("X") & ":" & _
value(2).ToString("X") & ":" & _
value(3).ToString("X") & ":" & _
value(4).ToString("X") & ":" & _
value(5).ToString("X")
End Function
Private Sub DebugLedPlayBlink(MillisecondsLedOn As Integer, MillisecondsLedOff As Integer, NumberOfBlinks As Integer)
With Mainboard
Do
For i As Integer = 1 To NumberOfBlinks
.SetDebugLED(False)
Thread.Sleep(MillisecondsLedOff)
.SetDebugLED(True)
Thread.Sleep(MillisecondsLedOn)
Next
Loop While True
End With
End Sub
Private Function WriteAllText(CerberusVolumeInfo As Microsoft.SPOT.IO.VolumeInfo, path As String, text As String) As Boolean
Dim fPath As String
Dim fs As FileStream
Dim txtBytes() As Byte
Dim vi() As VolumeInfo
Try
If (path.Substring(0, CerberusVolumeInfo.RootDirectory.Length) & "\") = (CerberusVolumeInfo.RootDirectory & "\") Then
fPath = path
Else
fPath = System.IO.Path.Combine(CerberusVolumeInfo.RootDirectory, path)
End If
fs = New FileStream(fPath, FileMode.Append)
txtBytes = Encoding.UTF8.GetBytes(text & vbNewLine)
fs.Write(txtBytes, 0, txtBytes.Length)
fs.Flush()
fs.Close()
fs.Dispose()
fs = Nothing
vi = VolumeInfo.GetVolumes()
For i As Integer = 0 To vi.Length - 1
vi(i).FlushAll()
Next
Thread.Sleep(1500)
Return True
Catch ex As Exception
'Throw
Finally
fPath = Nothing
If fs IsNot Nothing Then
fs.Dispose()
fs = Nothing
End If
txtBytes = Nothing
vi = Nothing
End Try
End Function
Private Function ReadAllText(CerberusVolumeInfo As Microsoft.SPOT.IO.VolumeInfo, path As String) As String
Try
Dim fPath As String, strText As String
If (path.Substring(0, CerberusVolumeInfo.RootDirectory.Length) & "\") = (CerberusVolumeInfo.RootDirectory & "\") Then
fPath = path
Else
fPath = System.IO.Path.Combine(CerberusVolumeInfo.RootDirectory, path)
End If
Dim pStream As Stream
If Not Exists(CerberusVolumeInfo, fPath) Then
Throw New Exception("Il file [" & fPath & "] non esiste.")
End If
pStream = System.IO.File.OpenRead(fPath)
Using pReader As New StreamReader(pStream)
strText = pReader.ReadToEnd
End Using
pStream.Dispose()
pStream = Nothing
fPath = Nothing
Return strText
Catch ex As Exception
Throw
End Try
End Function
Public Shared Function Exists(CerberusVolumeInfo As Microsoft.SPOT.IO.VolumeInfo, FilePath As String) As Boolean
Dim fPath As String
Try
If (FilePath.Substring(0, CerberusVolumeInfo.RootDirectory.Length) & "\") = (CerberusVolumeInfo.RootDirectory & "\") Then
fPath = FilePath
Else
fPath = System.IO.Path.Combine(CerberusVolumeInfo.RootDirectory, FilePath)
End If
Return System.IO.File.Exists(fPath)
Catch ex As Exception
Return False
Finally
fPath = Nothing
End Try
End Function
Public Shared Function StringToByteArray(str As String) As Byte()
Dim encoding As System.Text.UTF8Encoding
Try
encoding = New System.Text.UTF8Encoding
Return encoding.GetBytes(str)
Catch ex As Exception
Throw
Finally
encoding = Nothing
End Try
End Function
Private Sub SendDatasToServer()
Dim pBytes() As Byte, wReq As System.Net.HttpWebRequest
Try
wReq = CType(System.Net.HttpWebRequest.Create(pDataUrl), System.Net.HttpWebRequest)
With wReq
pBytes = StringToByteArray("{" & _
"""data"":{""alias"":""" & pDeviceAlias & """,""temperature"":" & strTemperature & ",""humidity"":" & strHumidity & "}" & _
"}")
.Method = "POST"
.ContentType = "application/json"
.ContentLength = pBytes.Length
Using reqStream = .GetRequestStream
reqStream.Write(pBytes, 0, pBytes.Length)
reqStream.Flush()
reqStream.Close()
End Using
pBytes = Nothing
End With
wReq.Dispose()
wReq = Nothing
Catch ex As Exception
pBytes = Nothing
If wReq IsNot Nothing Then
wReq.Dispose()
wReq = Nothing
End If
WriteAllText(VolumeInfo.GetVolumes()(0), "send_err.txt", ex.Message)
End Try
End Sub
End Class
End Namespace