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
        'app timer on
    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
        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
        ' get an IP from DHCP if no previous one
        If (ip.ToString = ""And ethernet.IsCableConnected Then
            ip = IPAddress.GetDefaultLocalAddress()
            WebServer.StartLocalServer(ip.ToString(), 80)
            myweb = WebServer.SetupWebEvent("Hello")
        End If
    End Sub
End Class


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.