Hello all,
I know a lot of people frown on .Net, but this is just a silly example of an stand alone HTTPListener application that will get posts from a GW1000 and display to a console window.
No web server required. This is just the code in VB, if you have visual studio you can just run it. Notes are in the code.
Just depends on what you want to do with the data, could be starting point for a desktop application, etc.
An alternative to PHP etc.
Imports System.Net
Imports System.Globalization
Imports System.IO
Imports System.Text
''' <summary>
''' Simply waits for a post listed on the port this is set for , in this case 8085. This code assumes using the Ecowitt format from a GW1000.
''' The Output is to a console window (DOS Window), but you can do whatever you want with the data by altering the code below.
''' Write to file, FTP to server, Post the data ... this is just an example
''' For my use I will be converting this to a windows service
''' </summary>
''' <remarks>
''' 1. Assumes you have a configured ECOWITT GW1000 with your sensors
''' 2. This is meant to be run on a local intranet local to the GW1000 as the prefix used will take a request from any URL as long as the port matches
''' 3. The GW1000 set up is simple, running firmware 1.5.6 in this case, use WS VIEW and set accordingly
''' 3a. Use a "customized" server setup
''' 3b. Use the "Ecowitt" protocol
''' 3c. Set the server "IP/Hostname" to the IP where this application is running (does not need a domain dame or FQDN), just the straight IP XXX.XXX.XXX.XXX
''' 3d. Set the Path to whatever you want , i just use "/hello/" The path is not used at all in this instance as this is a standalone app. If you change the URL prefix, you will need to alter this
''' 3e. set the port to 8085
''' 3f. set the upoad interval to what is appropriate for your sensors, I'm using an Ambient ws2000 and set to 16
''' 4. If you did all of the above you should not need to modify the source below and it will just work as is
''' 4a. This must be run "As an Administrator" .. there are ways around that but in this case just do that. This means Visual Studio when debugging and the executable if run separately
'''
''' </remarks>
Module Listener
'work around for LONG urls
'Go to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\HTTP\Parameters and create a DWORD (32 bit) parameter named UrlSegmentMaxLength. Set the value to 0 and you won't be bothered with long url issues anymore.
Sub Main()
Dim prefixes(0) As String
prefixes(0) = "http://+:8085/"
ProcessRequests(prefixes)
End Sub
Private Sub ProcessRequests(ByVal prefixes() As String)
' URI prefixes are required,
If prefixes Is Nothing OrElse prefixes.Length = 0 Then
Throw New ArgumentException("prefixes")
End If
' Create a listener and add the prefixes.
Dim listener As System.Net.HttpListener = _
New System.Net.HttpListener()
For Each s As String In prefixes
listener.Prefixes.Add(s)
Next
Try
' Start the listener to begin listening for requests.
listener.Start()
Console.WriteLine("Listening...")
Do
Dim response As HttpListenerResponse = Nothing
Try
Dim context As HttpListenerContext = listener.GetContext()
Dim indata As String = ProcessRequest(context)
Console.Clear()
Console.WriteLine(indata) 'raw post data
Dim values() As String
values = Split(indata, "&") 'just split it into name = value pairs
For Each value As String In values
Debug.Print(value)
Console.WriteLine(value)
Next
Catch ex As HttpListenerException
Console.WriteLine(ex.Message)
Finally
If response IsNot Nothing Then
response.Close()
End If
End Try
Loop
Catch ex As HttpListenerException
Console.WriteLine(ex.Message)
Finally
' Stop listening for requests.
listener.Close()
Console.WriteLine("Done Listening...")
End Try
End Sub
Private Function ProcessRequest(context As HttpListenerContext) As String
' Get the data from the HTTP stream
Dim body As String = New StreamReader(context.Request.InputStream).ReadToEnd()
'you actually do not need to post a response back in this case.. but makes it browser friendly if you want to test
Dim b() As Byte = Encoding.UTF8.GetBytes("ACK")
context.Response.StatusCode = 200
context.Response.KeepAlive = False
context.Response.ContentLength64 = b.Length
Dim output As Stream = context.Response.OutputStream
output.Write(b, 0, b.Length)
output.Close()
Return body
End Function
End Module