Softata: Service Commands - 1
softata rpipico firmata arduino csharp bme280 grove pico sdk-for-c-arduino tcpip
Commands are sent from the .NET library (Softalib) which is the client, to the WiFi TCPIP Service running on the Arduino device. An outlive of the client-server code.
Server Communication
Server Setup
#include <WiFi.h>
const char* ssid = "SSID";
const char* password = "PWD";
int port = PORT;
WiFiServer server(port);
void setup() {
WiFi.mode(WIFI_STA);
WiFi.setHostname("PicoW2");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.printf("\nConnected to WiFi\n\nConnect to server at %s:%d\n", WiFi.localIP().toString().c_str(), port);
server.begin();
}
Server Message Reception
WiFiClient client = server.available();
while (true) {
watchdog_update();
while (!client.available()) {
delay(100);
watchdog_update();
}
byte length = client.read();
int count = 0;
byte msg[maxRecvdMsgBytes];
if (length == 0) {
return;
}
while (client.available() && (count != length)) {
msg[count] = client.read();;
watchdog_update();
count++;
}
if (count != length) {
//Errant condition
}
// Process the received message ...
...
So the process is to read the first byte of message as the length of message then read that many bytes into a byte array (not including that first byte).
Basic Client Communication
Send
List<byte> sendmsg = new List<byte> { 0,(byte)MsgType }
// Add other byte data
sendmsg[0] = (byte)(sendmsg.Count-1);
// Get bytes from list.
byte[] sendBytes = sendmsg.ToArray<byte>();
int sent = client.Send(sendBytes, 0, sendBytes.Length, SocketFlags.None);
Receive Response
//Wait for response
while (client.Available == 0) ;
byte[] data = new byte[100]; //Received msg limited top 100 bytes
int recvd = client.Receive(data);
string result = Encoding.UTF8.GetString(data).Substring(0, recvd);
// Make the check work in both ways.
// expect is typically "OK:"
if ((!expect.Contains(result)) && (!result.Contains(expect)))
{
// Errant condition
}
// Return result string
Basic Server Commands
These start with an ASCII code for an ASCII uppercase character, the first letter of basic command names:
switch (msg[0]) {
case (byte)'B': // Begin
client.print("Ready"); // Sent at start connection.
break;
case (byte)'E': //End
client.print("Done");
// Force reset
resetFunc();
break;
case (byte)'N': //Null
client.print("OK");
return;
break;
case (byte)'R': //Reset
client.print("Reset");
// Force reset
resetFunc();
case (byte)'V': //Get Version
client.print(APP_VERSION);
break;
case (byte)'D': //Get Device Types
{
String devicesCSV = Grove::GetListofDevices();
client.print(devicesCSV);
}
break;
default:
//Process other commands
Other high level commands can be added here filling in the gaps in the range A to Z.
Nb: Softata commands not to be between 65 to 90, 0x41 to 0x5A, āAā to āZā.
C## Implementation of Basic Commands
The Console app starts with these calls to the Softatalib that then forwards them to the service.
SoftataLib.Init(ipaddressStr, port);
SoftataLib.SendMessageCmd("Begin");
string Version = SoftataLib.SendMessageCmd("Version");
Console.WriteLine($"Softata Version: {Version}");
string devicesCSV = SoftataLib.SendMessageCmd("Devices");
Console.WriteLine($"{devicesCSV}");
Console app initialization
The first SoftataLib call sets up the client and does not communicate with the service.
public static string SendMessageCmd(string cmd)
{
// Construct command and parameters as list of bytes
List<byte> sendmsg = new List<byte> { 1, (byte)cmd[0] };
byte[] sendBytes = sendmsg.ToArray<byte>();
int sent = client.Send(sendBytes, 0, sendBytes.Length, SocketFlags.None);
//Wait for response
while (client.Available == 0) ;
byte[] data = new byte[100];
int recvd = client.Receive(data);
string result = Encoding.ASCII.GetString(data).Substring(0, recvd).Trim();;
switch (cmd)
{
// If data is expected to be returned, return the result.
// Note: The expect validation mechanism not used here.
// If the command was end, shut the client down so the at app can exit:
case end:
Thread.Sleep(2000);
client.Shutdown(SocketShutdown.Both);
client.Close();
break;
}
}
Softatalib.SendMessageCmd()
Topic | Subtopic | |
This Category Links | ||
Category: | Softata Index: | Softata |
Next: > | Softata | Service Commands - 2 |
< Prev: | Softata | Architecture |