[SOLVED] Vb.net code

Hi,

I’ve started to tinker around with Blynk (SparkFun BlynkBoard) a couple of weeks ago and like it much so far.

The RESTful API works with the provided Python code perfectly, but I have not been able to make it work with the sample vb.net code, so I’m still stuck at writing a pin value.

I wonder if the sample code has been tested from Blynk, as I get error messages when trying to run, the first on line 11:

request.ContentLength = byteArray.Length –> run time error: The property can not be set after the writing has started.

If I comment the line out, the following error arises at line 16:
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse) –> The remote server returned an error: (500) Internal Server Error.

Has anybody a working code available? Any help would be appreciated

I have successfully created a VB RESTful Interface by modifying the sample code as shown on the API docs page. I had to move the ‘request.ContentLength=…’ line to before the ‘Using…’ line, and also I sent the pin state JSON data in the form of a String, not byte array as follows:

Dim state As String = "[" & vbCrLf & "  " & Chr(34) & "0" & Chr(34) & vbCrLf & "]" 

Here is my modified sub for writing a pin value:

    Sub SendData(pin As String, PinState As String)

    If blynkServer IsNot "" And blynkAuth IsNot "" Then
        Dim ServerRequest As String = blynkServer & "/" & blynkAuth & "/pin/" & pin
        Dim request = TryCast(Net.WebRequest.Create(ServerRequest), Net.HttpWebRequest)

        request.ContentLength = PinState.Length

        request.Method = "PUT"

        request.ContentType = "application/json"

        Using writer = New IO.StreamWriter(request.GetRequestStream())
            writer.Write(PinState)
            writer.Close()
        End Using
        Dim responseContent As String
        Using response = TryCast(request.GetResponse(), Net.HttpWebResponse)
            Using reader = New IO.StreamReader(response.GetResponseStream())
                responseContent = reader.ReadToEnd()
            End Using
        End Using

    Else
        MsgBox("Blynk Server and Auth code not set" & vbCrLf & "Press Setup Button and enter data", , "Error")
    End If

End Sub

let me know if this helps.

-Adam

1 Like

Hi Adam,

Thanks for sharing your code, it works like a charm :slight_smile: