The button code is self-documenting. When a button
n PHONESHOT 2. The WEMOS
D1 Mini’s IP address has been
entered and the Connect button
touched. We are now connected
to the Mini server and all of the
I/O pins we control are in the
OFF state.
Globals. A logical TRUE value in our connected variable
will allow our data pipe to be initialized and assign
“astream” as a prefix name for all of the subroutines
associated with our new AsyncStreams data pipe:
Private;Sub;Socket_Connected;(Successful;As
Boolean)
If Successful Then
connected;=;True
astream.Initialize(socket.InputStream,
socket.OutputStream,;“astream”)
StateChanged
End;If
End;Sub
Private;Sub;Astream_Error
Log(“Disconnected”)
connected;=;False
StateChanged
End;Sub
the log area of the B4R IDE.
I’ve captured the B4R log
entry in Screenshot 2.
The Mini is waiting for a
connection, and listening at
IP address 192.168.1.222 on
port 51042. So, we simply
enter 192.168.1.222 in the
txfIP textfield and touch the
Connect pushbutton:
Private;Sub;Astream_Terminated
Astream_Error
End;Sub
The StateChanged subroutine is called to handle the
chain graphic, which is represented as text in the label we
call lblStatus:
Sub;btnConnect_Click
Connect(txfIP. Text)
End;Sub
Public;Sub;Connect(ip;As;String)
If;astream.IsInitialized;Then
astream.Close
End;If
socket.Initialize(“socket”)
socket.Connect(ip,;51042,;30000)
Public;Sub;StateChanged
If;connected;Then
lblStatus.TextColor;=;Colors.Green
lblStatus.Text;=;“□”
Else
lblStatus.TextColor;=;Colors.Red
lblStatus.Text;=;“□”
End;If
End;Sub
The chain graphic is a text squiggle that can’t be read
by a human. That’s why you see nothing recognizable
between the quotes in the StateChanged subroutine.
The btnConnect subroutine is called when the Connect
button is touched. When the Connect subroutine launches,
a quick check is made to ensure that our AsyncStreams
data pipe is not already in use, our socket object is
initialized, and the connection request containing our
socket values (192.168.1.222 and 51042) is transmitted.
We’ll give it 30,000 milliseconds to work.
It’s sufficient to say that the status of the chain is
dependent on the Boolean variable connected. If the
connected variable is TRUE, the chain is linked and green.
On the other hand, if the connected variable is FALSE, the
chain is broken and red. Our link is up in Phoneshot 2.
To control the Mini’s output pins, we simply touch the
desired button (S0, S1, or S2):
If all goes as designed (or not), the Socket_Connected
subroutine is launched:
Sub;btnS0_Click
astream. Write(ser.
ConvertArrayToBytes(Array(“S0”)))
End;Sub
Sub;btnS1_Click
astream. Write(ser.
ConvertArrayToBytes(Array(“S1”)))
End;Sub
Sub;btnS2_Click
astream. Write(ser.
ConvertArrayToBytes(Array(“S2”)))
End;Sub
The status of our new connection attempt is stored in
the Boolean connected variable we defined in the Process
56 February 2018