The remote client (iPhone) will always send two bytes
in a command string. The sender will always convert an
array to bytes, and the receiver will always convert bytes to
an array. So, the server side code looks like this:
ADVANCED TECHNIQUES FOR DESIGN ENGINEERS
Post comments on this article and find any associated files and/or downloads at
www.nutsvolts.com/magazine/issue/2018/02.
If the LAN connection and server initialization are
successful, we can then move to initialize the WEMOS
The names to the left of As are our aliases. The
names on the right of As are members of libraries. So,
we are setting up our WEMOS D1 Mini as a server using
AsyncStreams and B4RSerializator as our application
data pipe and application data protocol, respectively.
Our Process Global statements also tell us that we have
predefined definitions for the Mini’s I/O pins and that there
are three I/O pins being used by our application. It also
looks like we’ll be keeping tabs on the states of the three
switchx pins.
For the sake of our discussion, we are only coding
three of the available D1 Mini I/O pins. You can code
them all if you wish, as the code for each additional pin
can be copied from the code we have already written.
Private Sub AppStart
Serial1.Initialize(115200)
Log(“AppStart”)
If;espWifi.Connect2(“YourSSID”,”YourPassword”)
Then
server.Initialize(51042,;“server_
NewConnection”)
server.Listen
Log(“Waiting;for;connection.”)
Log(“My;ip:;“,;espWifi.LocalIp)
Else
Log(“Crapped;Out.”)
End;If
switch0.Initialize(wemosPins.D0,;switch0.
MODE_OUTPUT)
switch1.Initialize(wemosPins.D1,;switch1.
MODE_OUTPUT)
switch2.Initialize(wemosPins.D2,;switch2.
MODE_OUTPUT)
switch0.Digital Write(False)
switch1.Digital Write(False)
switch2.Digital Write(False)
switch0State = False
switch1State = False
switch2State;=;False
End;Sub
Sub;Server_NewConnection;(NewSocket;As
WiFiSocket)
astream.Initialize(NewSocket.Stream,
““““““““““““““““““““““““““““““““““““““““““““““““““““““astream_NewData”,;“astream_Error”)
End;Sub
Now that our aliases and libraries are defined, let’s
write some crank-it-up code:
Incoming data will cause the subroutine astream_
NewData to be called. B4RSerializator is used at both ends
of the Wi-Fi link to allow the transmission of an array of
objects with numbers, strings, and arrays of bytes. At the
other end of the link, B4RSerializator returns an array of
bytes.
The first order of business is to connect to our local
LAN’s router. Once that’s accomplished, we can initialize
our D1 Mini server to listen on port 51042. The “Log”
statements are simply informative and show up in the B4R
IDE (integrated development environment).
Sub;astream_NewData;(Buffer();As;Byte)
Dim;be(10);As;Object;‘used;as;a;storage
buffer.
Dim;objects();As;Object;=;ser.
ConvertBytesToArray(Buffer,;be)
For;Each;o;As;Object;In;objects
Select o
Case;“S0”
If switch0State = False Then
switch0.Digital Write(True)
switch0State = True
Else
switch0.Digital Write(False)
switch0State = False
End;If
astream. Write(ser.
ConvertArrayToBytes(Array(“S0”,switch0State)))
Case;“S1”
If switch1State = False Then
switch1.Digital Write(True)
switch1State = True
Else
switch1.Digital Write(False)
switch1State = False
February 2018 53