Author Topic: Sample .Net code for receiving/displaying "Customized" server upload in Ecowitt  (Read 1345 times)

0 Members and 1 Guest are viewing this topic.

Online Rover1822

  • Forecaster
  • *****
  • Posts: 2017
    • Mini Wind and Solar Data project
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.


Code: [Select]

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


« Last Edit: February 07, 2020, 12:49:33 PM by Scott Barlow »
Ambient:
  WS-2000
  PM 2.5(2)
  WH31B(2)
  WH40E
  WH31P
EcoWitt:
  GW1100
  GW1000(4)
  WH31(2)
  WH57
  WH51(12),
  WH40
  WH5360B
  WN34S
  WittBoy WS90 + GW2000
  WS90 (other one) + GW1100
Personal Sites: Weather Cam

Offline galfert

  • Global Moderator
  • Forecaster
  • *****
  • Posts: 6822
Nice work.

I would suggest changing the thread subject though. The Ecowitt Custom Server upload is a feature that is not unique to the GW1000. Several other Ecowitt and other Fine Offset clone displays also have this feature. The reason for me bringing this up is so that people realize that this solution is applicable to more than just the GW1000.

The confusing part for most people is understanding that only the GW1000 also has a separate additional method to send data that is a local network API. This is totally different than the Custom Server Upload feature. I realize that for the purposes of the thread and the .Net code, that the local network API of the GW1000 is not relevant.
Ecowitt GW1000 | Meteobridge on Raspberry Pi
WU: KFLWINTE111  |  PWSweather: KFLWINTE111
CWOP: FW3708  |  AWEKAS: 14814
Windy: pws-f075acbe
Weather Underground Issue Tracking
Tele-Pole

Online Rover1822

  • Forecaster
  • *****
  • Posts: 2017
    • Mini Wind and Solar Data project
LOL, I'm open to a suggestion for a subject change
Ambient:
  WS-2000
  PM 2.5(2)
  WH31B(2)
  WH40E
  WH31P
EcoWitt:
  GW1100
  GW1000(4)
  WH31(2)
  WH57
  WH51(12),
  WH40
  WH5360B
  WN34S
  WittBoy WS90 + GW2000
  WS90 (other one) + GW1100
Personal Sites: Weather Cam

Offline galfert

  • Global Moderator
  • Forecaster
  • *****
  • Posts: 6822
How about...

Sample .Net code for receiving/displaying "Customized" server upload in Ecowitt protocol


It has become customary to put "Customized" in quotes when referring to the Fine Offset clone consoles that provide this functionality, because that is exactly what the configuration menu title says for this feature.
« Last Edit: February 07, 2020, 01:11:58 PM by galfert »
Ecowitt GW1000 | Meteobridge on Raspberry Pi
WU: KFLWINTE111  |  PWSweather: KFLWINTE111
CWOP: FW3708  |  AWEKAS: 14814
Windy: pws-f075acbe
Weather Underground Issue Tracking
Tele-Pole

Online Rover1822

  • Forecaster
  • *****
  • Posts: 2017
    • Mini Wind and Solar Data project
Done.
I should just push out the executable too

 [ You are not allowed to view attachments ]

Usage, directions below, provided "As Is" executable. This is was done on a doze (windows) 10 platform and .Net 4.5

Unzip the file to a location of your choice.. and if you have followed the directions below , it will listen and show data in a console window. Just start the WeatherServerGW1000.exe "As Adminiistrator"

For WS VIEW
Use a "customized" server setup
Use the "Ecowitt" protocol
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
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
set the port to 8085
Set the upoad interval to what is appropriate for your sensors, I'm using an Ambient ws2000 and set to 16
This must be run "As an Administrator" .. there are ways around that but in this case just do that.

« Last Edit: February 07, 2020, 01:24:28 PM by Scott Barlow »
Ambient:
  WS-2000
  PM 2.5(2)
  WH31B(2)
  WH40E
  WH31P
EcoWitt:
  GW1100
  GW1000(4)
  WH31(2)
  WH57
  WH51(12),
  WH40
  WH5360B
  WN34S
  WittBoy WS90 + GW2000
  WS90 (other one) + GW1100
Personal Sites: Weather Cam

Online Rover1822

  • Forecaster
  • *****
  • Posts: 2017
    • Mini Wind and Solar Data project
OK so based on my previous post.

Using .Net and and always on PC(server) on my intranet and the GW1000.

I'm doing the following
1. Capturing the data from the GW1000 post
2. Uploading the data in a formatted javascript file via FTP (TLS) to my hosted site that is nothing but the variables once a minute
3. The target HTML page is including the JS file and using the variables to populate stuff on the page (top bar)
4. This is combined with a web cam image  (rather large one) that is also uploaded once a minute (separately)

Resulting in this https://rovr1.com/wind/weather.html

Still a work in progress, but the GW1000 allowed me to get there easily
Ambient:
  WS-2000
  PM 2.5(2)
  WH31B(2)
  WH40E
  WH31P
EcoWitt:
  GW1100
  GW1000(4)
  WH31(2)
  WH57
  WH51(12),
  WH40
  WH5360B
  WN34S
  WittBoy WS90 + GW2000
  WS90 (other one) + GW1100
Personal Sites: Weather Cam

 

anything