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

Sending data to the Cosm server.


  • Please log in to reply
1 reply to this topic

#1 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 11 July 2012 - 07:54 PM

Still working on a small learning project out of the book Internet and things and want to send data to the Cosm.com server. (it was Pachube) Have found some code here and on other sites.

I am wondering if it can be programmed a little bit better so it will work faster.
Now i send for every item/feed a new request but i want to sent more then 1 item to the datastream in 1 request. I have tried a few possibilities but without any success. :blink:
Thank you.

Now my code is:


'all the items have the same connection, apiKey and feedId
'Sending the items every 30 seconds


Item1 = "Volt,23"
Item2 = "Ampere,3"
Item3 = "Direction,34.2" 

Do While True

   Using connection As Socket = Connect(URi, 5000)
      SendRequest(connection, apiKey, feedId, Item1) 
      SendRequest(connection, apiKey, feedId, Item2)
      SendRequest(connection, apiKey, feedId, Item3)
   End Using

   'do other things for 30 seconds and read the new data in the items

loop


Private Function Connect(ByVal host As String, ByVal timeout As Integer) As Socket
        ' look up host's domain name to find IP address(es)
        Dim hostEntry As IPHostEntry = Dns.GetHostEntry(host)
        ' extract a returned address
        Dim hostAddress As IPAddress = hostEntry.AddressList(0)
        Dim remoteEndPoint As New IPEndPoint(hostAddress, 80)

        Dim connection = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        connection.Connect(remoteEndPoint)
        connection.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, True)
        connection.SendTimeout = timeout
        Return connection
    End Function

    Private 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)
        Dim CRLF As String = Constants.vbCr & Constants.vbLf
        Dim requestLine = "PUT /v2/feeds/" & feedId & ".csv HTTP/1.1" & CRLF
        Dim requestLineBuffer As Byte() = Encoding.UTF8.GetBytes(requestLine)
        Dim headers = "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





#2 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 16 July 2012 - 08:01 PM

Well it could speed up a little by sending all the items at once.

dim strAllItems as string
strAllItems = Item1 & Constants.vbCr & Constants.vbLf & Item2 & Constants.vbCr & Constants.vbLf & Item3

Using connection As Socket = Connect(URi, 5000)      
   SendRequest(connection, apiKey, feedId, strAllItems)       
End Using





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.