Hi,
In the context of one of my projects, a “monitor and command” middleware for embedded projects (http://monitorandcommand.codeplex.com/), I am trying to develop a “Photon/Wiring Client” library
that allow the Photon to send/receive messages to/from the a Server.
I managed to code the “send” part rather easily, and my photon has now been sending temperature and humidity messages to the Server for days.
But I cannot get the “read” part to work. Whatever I try, I cannot get the available() and read() methods to return anything but 0, even though I am 100% sure that the Server sends messages back to the device.
Coming from .NET/NETMF, where I use Threads, I am rather new to Wiring/Photon, and I am now wondering if what I am trying to achieve is feasible with Wiring/Photon.
If I simplify things to the Max, my library defines a mcClient object that references a TCPClient and implements the methods required to send/receive fixed size Messages to/from the Server. What I am trying to achieve is, in the loop() method, to perform periodic work while listening to incoming messages eventually sent by the Server.
My main loop thus look like this :
loop() {
delay(50); //is it required ?
if (mcClient.IsConnected()) {
if (mcClient.ReceivedMessage()) //behind the hood, we use available() here to test if we received anything from the server
{
Message message = mcClient.ReadMessage(); // we get the message from the client
HandleMessage(message); //We handle the message
}
//Periodically, we do work, like reading sensors and sending messages to the Server
if (millis() - lastTime > DO_WORK_PERIOD)
{
DoWork(); //Behind the hood, we send messages using TCPClient, and it works perfectly
lastTime = millis();
}
} else {
delay(RECONNECTION_DELAY);
ConnectAndRegister();
}
}
Is something like this feasible ? Is my approach the right one ?
I would be glad if anybody could at least send me in the right direction.
Thanks in advance,
Paul