Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Direct scripting
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Direct scripting

by rios on Thu Jan 09, 2020 7:30 pm

Hello,

I'm trying to modify some vbscript to send simple data to the Adafruit IO Dashboard.
The reason is that this script is built and run from another windows programme.
Any advice on this vbscript would be most welcome.

Code: Select all | TOGGLE FULL SIZE
'
' CLIENT WINSOCK VBSCRIPT
'
' Delays are required where they are located,
' or it sends data too quick, and errors.
'
' Uses Port 80 by default.
' IP and Port are user settings.
'
' Creates a Log file.
' c:\WSClient.log
'
' this only sends basic text, no long essays or files.
' for that, it would require some minor but required changes.
'

Option Explicit
Dim winsock, SendData, RecieveData, secs

'****** CHANGE THESE SETTINGS *********

Const RemoteHost           = " [b]what would be the host?[/b] "
Const RemotePort           = 80

Const ADAFRUIT_IO_USERNAME = " [b]Your Username goes here[/b] "
Const ADAFRUIT_IO_KEY = " [b]Your Adafruit IO key goes here[/b] "

'***************************************

Const sckClosed            = 0 '// Default. Closed
Const sckOpen              = 1 '// Open
Const sckListening         = 2 '// Listening
Const sckConnectionPending = 3 '// Connection pending
Const sckResolvingHost     = 4 '// Resolving host
Const sckHostResolved      = 5 '// Host resolved
Const sckConnecting        = 6 '// Connecting
Const sckConnected         = 7 '// Connected
Const sckClosing           = 8 '// Peer is closing the connection
Const sckError             = 9 '// Error

MsgBox "Client Started."
WriteData Now & " - Client Started"

'// CREATE WINSOCK
On Error Resume Next
Set winsock = Wscript.CreateObject("MSWINSOCK.Winsock", "winsock_")
If Err.Number <> 0 Then
    MsgBox "Winsock Object Error!" & vbCrLf & "Script will exit now."
    WriteData Now & " - Winsock Object Error."
    WScript.Quit
End If
On Error Goto 0

'// CONNECT NOW
winsock.RemoteHost = RemoteHost
winsock.RemotePort = RemotePort
winsock.Connect
       
'// MAIN DELAY - WAITS FOR CONNECTED STATE
'// SOCKET ERROR RAISES WINSOCK ERROR SUB
While winsock.State <> sckError And winsock.state <> sckConnected And winsock.state <> sckClosing And secs <> 25
    WScript.Sleep 1000  '// 1 sec delay in loop
    secs = secs + 1     '// wait 25 secs max
Wend

'// CONNECTION TIMED OUT
If secs > 24 Then
    MsgBox "Timed Out"
    WriteData Now & " - Client Timed Out"
    ClientClose()
End If

'// SEND DATA NOW
winsock.SendData " [b]Data that's sent to adafruit io[/b] "

'// WAIT INCASE NO RESPONSE
Wscript.Sleep 25000
WriteData Now & " - Client No Response. Winsock State: " & winsock.state
ClientClose()

'// WINSOCK DATA ARRIVES
Sub winsock_dataArrival(bytesTotal)
    Dim strData
    winsock.GetData strData, vbString
    RecieveData = strData
    WriteData Now & " - Client Recieved: " & RecieveData
    winsock.SendData " [b]Send This Data[/b] " 
    WScript.Sleep 1000
    WriteData Now & " - Client Sent Data"
    ClientClose()
End Sub

'// WINSOCK ERROR
Sub winsock_Error(Number, Description, SCode, Source, HelpFile, HelpContext, CancelDisplay)
    MsgBox "Cient Error: " & Number & vbCrLf & Description
    WriteData Now & " - Cient Error: " & Number & ". " & Description
    ClientClose()
End Sub

'// EXIT SCRIPT
Sub ClientClose()
    If winsock.state <> sckClosed Then winsock.Close
    Set winsock = Nothing
    WriteData Now & " - Client Closed."
    Wscript.Quit
End Sub

'//    CREATE LOG ENTRY
Function WriteData(Data)
    Dim fso, file
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile("C:\WSClient.log", 8, True)
    file.write Data & vbCrLf
    file.Close
    Set file = Nothing
    Set fso = Nothing
End Function
   

rios
 
Posts: 1
Joined: Thu Jan 02, 2020 6:57 pm

Please be positive and constructive with your questions and comments.