IoT with FEZ rules them all

Hi RoSchmi,

Thanks for your reply! At ST there is still a living discussion about the SPWF04S but nobody seems to ask, May be it should be asked there? ST Community

I will upload to Githup teh additions to the GHI SPWF04S driver. I think I had to use GHI’s code sign assembly? This I had to remove first to be able to use the GHI driver VS 2017 solution.

The code to use the ST Socket Server I will post here. Here are some special thing to watch and use the WIND event in harmony.

Best Regards,
Aart

We have a lot of work done internally but can’t share anything yet. The guys made me promise.

Gus, Gus, Gus… When has this ever stopped you ?

2 Likes

Hi RoSchmi,

Should I ask at the ST forum about EOL of SPWF04S?

Also just discovered that the with STS command the status of “reset_reason” allways returns 4 (=hardware reset). Gives the FEZ board a hard reset to the Wifi module at power on?

Thanks for knowledge!

Regards,
Aart

According to the eol report front ST a part used on their module has been eol’d.

Seems odd to me…

1 Like

Somebody asked already… but with no answer from STM.

I don’t know. I’m just exploring the module myself.

Lol. I wish they can be at least honest about it.

I reckon…

It is not easy to find out why unless you know what to search for (SPWF04S PCN).

So from the PCN the part has “Standard Mutiple Source” with no suggested repleacement part…

Like i said… Odd

2 Likes

Are these the successors of STM SPWF04Sx ?
https://www.inventeksys.com/uncategorized/azure-iot-cloud/

@aartastona
Hi, there is just a discussion about adding some commands to the GHI SPWF04SA WiFi driver.
If you could post your commands for socket server applications they could be discussed to be part of an evt. future release of the library.

Hi RoSchmi,

I didn’t work on this lately. I have made an android app to switch the leds on or off and read te status, This works. If you put led1 on on one phone you can see it a few moments later on other phones. Next by pressing btn1 the FEZ becames a AP with a predefined IP address and port number. Once connected directly you can join a wifi network. In that case the FEZ gets its IP from the network. The last 3 digits of the IP4 will be made configurable so that the user know which IP is to be used. Everything work well for some hours and the it hangs in the GHI Wifi driver at an semaphore implementation at overwritten function: WaitOne. I had to look up the details again.

The added driver functions are available. Do you want the signatures of the added function or also the implemetations. And where do I make them available? Here?

Best regards,
Aart

Hi @aartastona
thanks, nice project.
If you have made additional commands that could be valuable for others (e.g. socket server commands) we could ask GHI to include them in the official driver.
I think that if there are not to many commands you could post them here in the form like this example:

        public void ClearTlsServerRootCertificate() {
        var cmd = this.GetCommand()
            .AddParameter("content")
            .AddParameter("2")
            .Finalize(SPWF04SxCommandIds.TLSCERT);

        this.EnqueueCommand(cmd);

        cmd.ReadBuffer();
        cmd.ReadBuffer();
        this.FinishCommand(cmd);
    }

Best regards
RoSchmi

Hi RoSchmi,

I have added the following functions to the SPWF04SxInterface class in the file: SPWF04SxInterface.cs:

public int OpenServerSocket(int port, SPWF04SxConnectionType connectionType, SPWF04SxConnectionSecurityType connectionSecurity)
public int ReadServerSocket(int SockIdServer, int SockIdClient, byte[] buffer, int buflen = 0)
public void WriteServerSocket(int SockIdServer, int SockIdClient, byte[] buffer, int buflen)
public void CloseServerSocket(int SockIdServer, int SockIdClient = -1)
public string GetStatus(string sts_var)
public string GetConfigValue(string cfg_var)
public void SetConfigValue(string cfg_var, string newval)
public string GetSSID()
public void SetMiniAP(string ssid, string password, string ipaddress = null)

The implementatation of these functions will be send in the next message.

Furthermore the Semaphore class (see below) in the file: Helpers.cs was modified several times without solving the problem. After several hours, depending on the socket requests frequency (more phones / clients sending sockets) the driver hangs. In that case the software loops forever because: this.evt.WaitOne(33, false) allways return false. I have changed form 10 to 33ms and seems somewaht to gelp. When it loops here the board had to be reset to resolve.

Best Regards,
Aart

internal class Semaphore : WaitHandle {
private readonly object lck = new object();
private readonly ManualResetEvent evt = new ManualResetEvent(false);
private int count;

    public override bool WaitOne() {
        while (true)
        {
            //if (this.evt.WaitOne(10, false)) { astona
            if (this.evt.WaitOne(33, false))
            {
                lock (this.lck)
                {
                    if (this.count > 0)
                    {
                        if (--this.count == 0)
                            this.evt.Reset();

                        return true;
                    }
                }
            }
        }
    }
    

    public int Release() {
        lock (this.lck) {
            var cnt = this.count;

            this.count++;

            this.evt.Set();

            return cnt;
        }
    }
}

Implementation of the functions mentioned before:

public int OpenServerSocket(int port, SPWF04SxConnectionType connectionType, SPWF04SxConnectionSecurityType connectionSecurity)
{
var cmd = this.GetCommand()
.AddParameter(port.ToString())
.AddParameter(connectionType == SPWF04SxConnectionType.Tcp ? (connectionSecurity == SPWF04SxConnectionSecurityType.Tls ? “s1” : “t”) : “u”)
.Finalize(SPWF04SxCommandIds.SOCKDON);

this.EnqueueCommand(cmd);

var a = cmd.ReadString();
var b = cmd.ReadString();

if (connectionSecurity == SPWF04SxConnectionSecurityType.Tls && b.IndexOf("Loading:") == 0)
{
    a = cmd.ReadString();
    b = cmd.ReadString();
}

this.FinishCommand(cmd);

return a.Split(':') is var result && result[0] == "On" ? int.Parse(result[1]) : throw new Exception("Request failed");

}

public int ReadServerSocket(int SockIdServer, int SockIdClient, byte[] buffer, int buflen = 0)
{
var cmd = this.GetCommand()
.AddParameter(SockIdServer.ToString())
.AddParameter(SockIdClient.ToString())
.AddParameter(buflen.ToString())
.Finalize(SPWF04SxCommandIds.SOCKDR, buffer, 0, buflen);

this.EnqueueCommand(cmd);

cmd.ReadBuffer();

if (buflen == 0)
    buflen = buffer.Length;
var current = 0;
var total = 0;
do
{
    current = cmd.ReadBuffer(buffer, total, buflen);
    total += current;
} while (current != 0);

this.FinishCommand(cmd);

return total;

}

public void WriteServerSocket(int SockIdServer, int SockIdClient, byte[] buffer, int buflen)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));
if (buflen < 0) throw new ArgumentOutOfRangeException();
if (buflen > buffer.Length) throw new ArgumentOutOfRangeException();

var cmd = this.GetCommand()
    .AddParameter(SockIdServer.ToString())
    .AddParameter(SockIdClient.ToString())
    .AddParameter(buflen.ToString())
    .Finalize(SPWF04SxCommandIds.SOCKDW, buffer, 0, buflen);

this.EnqueueCommand(cmd);

cmd.ReadBuffer();

this.FinishCommand(cmd);

}

public void CloseServerSocket(int SockIdServer, int SockIdClient = -1)
{
SPWF04SxCommand cmd;
if (SockIdClient >= 0)
cmd = this.GetCommand()
.AddParameter(SockIdServer.ToString())
.AddParameter(SockIdClient.ToString())
.Finalize(SPWF04SxCommandIds.SOCKDC);
else
cmd = this.GetCommand()
.AddParameter(SockIdServer.ToString())
.Finalize(SPWF04SxCommandIds.SOCKDC);

this.EnqueueCommand(cmd);

cmd.ReadBuffer();

this.FinishCommand(cmd);

}

public string GetStatus(string sts_var)
{
SPWF04SxCommand cmd;
cmd = this.GetCommand()
.AddParameter(sts_var)
.Finalize(SPWF04SxCommandIds.STS);

this.EnqueueCommand(cmd);

var a = cmd.ReadString();
var b = cmd.ReadString();

this.FinishCommand(cmd);
if (a.IndexOf(':') >= 0)
    return a.Split(':')[1];
else
    return "";

}

public string GetConfigValue(string cfg_var)
{
SPWF04SxCommand cmd;
cmd = this.GetCommand()
.AddParameter(cfg_var)
.Finalize(SPWF04SxCommandIds.GCFG);

this.EnqueueCommand(cmd);

var a = cmd.ReadString();
var b = cmd.ReadString();

this.FinishCommand(cmd);

if (a.IndexOf(':') >= 0)
    return a.Split(':')[1];
else
    return "";

}

public void SetConfigValue(string cfg_var, string newval)
{
SPWF04SxCommand cmd;
cmd = this.GetCommand()
.AddParameter(cfg_var)
.AddParameter(newval)
.Finalize(SPWF04SxCommandIds.SCFG);

this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);

cmd = this.GetCommand()
.Finalize(SPWF04SxCommandIds.WCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);

}

public string GetSSID()
{

var cmd = this.GetCommand()
.Finalize(SPWF04SxCommandIds.SSIDTXT);
this.EnqueueCommand(cmd);

var a = cmd.ReadString();
var b = cmd.ReadString();

this.FinishCommand(cmd);

if (a.IndexOf(':') >= 0)
    return a.Split(':')[1];
else
    return "";

}

public void SetMiniAP(string ssid, string password, string ipaddress = null)
{
this.DisableRadio();

var cmd = this.GetCommand()
.AddParameter(ssid)
.Finalize(SPWF04SxCommandIds.SSIDTXT);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);

cmd = this.GetCommand()
.AddParameter("wifi_priv_mode")
.AddParameter("2")
.Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);

cmd = this.GetCommand()
.AddParameter("wifi_auth_type")
.AddParameter("0")
.Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);

cmd = this.GetCommand()
    .AddParameter("wifi_mode")
    .AddParameter("3")
    .Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);

cmd = this.GetCommand()
    .AddParameter("wifi_wpa_psk_text")
    .AddParameter(password)
    .Finalize(SPWF04SxCommandIds.SCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);

this.EnableRadio();

if (ipaddress != null && ipaddress.Length > 0)
{
    this.DisableRadio(); //reset??
    cmd = this.GetCommand()
        .AddParameter("ip_ipaddr")
        .AddParameter(ipaddress)
        .Finalize(SPWF04SxCommandIds.SCFG);
    this.EnqueueCommand(cmd);
    cmd.ReadBuffer();
    this.FinishCommand(cmd);
    this.EnableRadio();
}

cmd = this.GetCommand()
.Finalize(SPWF04SxCommandIds.WCFG);
this.EnqueueCommand(cmd);
cmd.ReadBuffer();
this.FinishCommand(cmd);

}

2 Likes