Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Struggling with ethernet


  • Please log in to reply
2 replies to this topic

#1 aberdeen57

aberdeen57

    New Member

  • Members
  • Pip
  • 2 posts

Posted 26 April 2012 - 11:53 PM

I have been working to build an application to record humidity, temperature with a time stamp. I have been able to get the program working and saving the data to the SD card. Success!

However I am having tremendous difficulty getting etherent functionality working. I am using 4.2 RC4 as I am a VB.net programmer. I have set a static IP, and duplicated the subnet mask, default gateway and DNS primary from the IPconfig settings on my computer. I can ping the device from the command prompt and it appears present. However, I can't seem to generate anything beyond this.

I have used the sample code from the Getting Started on the Internet of Things book to try and connect to pachube and while it compiles and launches it fails to pull the DNS.

If I replace the "api.pachube.com" with actual IP then program runs up until connection.Connect(remoteEndPoint) at which point it basically hangs without response.

Help! Any suggestions on how to proceed? I just can't seem to make any headway.

Thanks,

Tom

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware.NetduinoPlus

Public Class HelloPachubeSockets
    Public Shared Sub Main()
        Debug.Print("------------------ NEW RUN -----------------")
        Const apiKey As String = "NOT FOR YOUR EYES"
        Const feedId As String = "AGAIN I HIDE"
        Const samplingPeriod As Integer = 10000
        ' 20 seconds
        Const maxVoltage As Double = 3.3
        Const maxAdcValue As Integer = 1023
        'Dim voltagePort As AnalogInput = New AnalogInput(Pins.GPIO_PIN_A1)
        Dim lowPort As OutputPort = New OutputPort(Pins.GPIO_PIN_A0, False)
        Dim highPort As OutputPort = New OutputPort(Pins.GPIO_PIN_A2, True)
        Dim connection As Socket = Nothing
        
        While True
            ' main loop
            WaitUntilNextPeriod(samplingPeriod)
            Debug.Print("time: " & DateTime.Now)
            Debug.Print("memory available: " & Debug.GC(True))
            If connection Is Nothing Then
                ' create connection
                Try
                    connection = Connect("api.pachube.com", samplingPeriod \ 2)
                Catch e As Exception
                    'Debug.Print(e.)
                    Debug.Print("connection error")
                End Try
            End If
            If connection IsNot Nothing Then
                Debug.Print("socket is open")
                Try

                    Dim rawValue As Double = 125 'voltagePort.Read()
                    Dim value As Double = (rawValue * maxVoltage) / maxAdcValue
                    Dim sample As String = "voltage," & value.ToString("f")
                    Debug.Print("new message: " & sample)
                    SendRequest(connection, apiKey, feedId, sample)
                Catch generatedExceptionName As SocketException
                    connection.Close()
                    connection = Nothing
                End Try
            Else
                Debug.Print("no socket is open")
            End If
        End While
    End Sub
    Private Shared Function Connect(ByVal host As String, ByVal timeout As Integer) As Socket
        ' look up host’s domain name to find IP address(es)
        Debug.Print("Just looking for the dns stuff")
        Dim hostEntry As IPHostEntry = Dns.GetHostEntry(host)


        Debug.Print("hostentry: " + hostEntry.ToString)
        ' extract a returned address
        Dim hostAddress As IPAddress = hostEntry.AddressList(0)
        Debug.Print("host add" + hostAddress.ToString)

        Dim remoteEndPoint As New IPEndPoint(hostAddress, 80)
        Debug.Print("remoteendpoint" + remoteEndPoint.ToString)

        ' connect!
        Debug.Print("connect...")
        Dim connection = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Debug.Print("connection seemed to work")
        connection.Connect(remoteEndPoint)
        Debug.Print("Try to acutally connect to " + remoteEndPoint.ToString)
        connection.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, True)
        connection.SendTimeout = timeout
        Debug.Print("returning the socket")
        Return connection
    End Function
    Private Shared Sub SendRequest(ByVal s As Socket, ByVal apiKey As String, ByVal feedId As String, ByVal content As String)
        Dim contentBuffer As Byte() = Encoding.UTF8.GetBytes(content)
        Const CRLF As String = "\r\n"
        Dim requestLine As String = "PUT /v2/feeds/" & feedId & ".csv HTTP/1.1" & CRLF
        Dim requestLineBuffer As Byte() = Encoding.UTF8.GetBytes(requestLine)
        Dim headers As String = "Host: api.pachube.com" & CRLF & "X-PachubeApiKey: " & apiKey & CRLF & "Content-Type: text/csv" & CRLF & "Content-Length: " & contentBuffer.Length & CRLF & CRLF
        Dim headersBuffer As Byte() = Encoding.UTF8.GetBytes(headers)
        s.Send(requestLineBuffer)
        s.Send(headersBuffer)
        s.Send(contentBuffer)
    End Sub
    Private Shared Sub WaitUntilNextPeriod(ByVal period As Integer)
        Dim now As Long = DateTime.Now.Ticks \ TimeSpan.TicksPerMillisecond
        Dim offset = CInt(now Mod period)
        Dim delay As Integer = period - offset
        Debug.Print("sleep for " & delay & " ms\r\n")
        Thread.Sleep(delay)
    End Sub
End Class


#2 ColinR

ColinR

    Advanced Member

  • Members
  • PipPipPip
  • 142 posts
  • LocationCape Town, South Africa

Posted 27 April 2012 - 06:42 AM

Do you have a valid DNS address set?

#3 aberdeen57

aberdeen57

    New Member

  • Members
  • Pip
  • 2 posts

Posted 30 April 2012 - 09:40 PM

It turns out the our router was only allowing two devices per ethernet cable. I was trying a four port switch at this end and it just wouldn't work. Figures that it is some sort of network issue! Thanks for the help!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.