VB Gadgeteer Webserver, SDK 4.2 QFE2 and Spider Premium Library’s

This is an small sample code to show how to build our basic gadgeteer web server using VB with Spider and the 4.2 RTM Premium library’s.
Start Visual Studio with a new gadgeteer project and copy/paste…That’s all! (remember add library’s references)


Imports System.Net
Imports System.Text
Imports Microsoft.SPOT.Hardware
 
Imports GHI.Premium.Net
 
Imports GT = Gadgeteer
 
Partial Public Class Program
 
    Private WithEvents ethernet As New EthernetBuiltIn()
    
    Private ip As New IPAddress(New Byte() {0, 0, 0, 0})
    Private WithEvents myweb As WebEvent
    '
    Private WithEvents timer As GT.Timer = New GT.Timer(1000)
 
    Public Sub ProgramStarted()
        'Initialize adapter
        ethernet.Open()
        ethernet.NetworkInterface.EnableDhcp()
        ethernet.NetworkInterface.EnableDynamicDns()
        NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet)
        'app timer on
        timer.Start()
    End Sub
 
    Private Sub ethernet_CableConnectivityChanged(sender As Object, e As GHI.Premium.Net.EthernetBuiltIn.CableConnectivityEventArgsHandles ethernet.CableConnectivityChanged
        If e.IsConnected Then
            ' reboot device after plug... to startup again
            PowerState.RebootDevice(True)
        End If
    End Sub
 
    Private Sub ReceivedWebEventHandler(path As String, method As WebServer.HttpMethod, responder As ResponderHandles myweb.WebEventReceived
        ' webb response
        responder.Respond(New System.Text.UTF8Encoding().GetBytes("Hello from " + ip.ToString() + " At " + System.DateTime.Now.ToString()), "text/html")
    End Sub
 
    Private tic As Boolean
    Private Sub timer_Tick(timer As Gadgeteer.TimerHandles timer.Tick
        ' blink debug led... system working
        tic = Not tic
        Mainboard.SetDebugLED(tic)
        ' get an IP from DHCP if no previous one
        If (ip.ToString = "0.0.0.0"And ethernet.IsCableConnected Then
            ip = IPAddress.GetDefaultLocalAddress()
            WebServer.StartLocalServer(ip.ToString(), 80)
            myweb = WebServer.SetupWebEvent("Hello")
        End If
    End Sub
End Class


Regards,
PepLluis,

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>