I am trying to build home automation system based on serial communication like RS485 protocol. I use Nick Gammon non blocking library to send and receive data as well feedback / call back function. I successfully achieved the communication between different module but response time slightly reduced.
I have a doubt in call back function which I am using in my so called protocol. I think non blocking library already has feedback system which I cannot understand and my self made feedback system is doing double work which slow down the response time.
Below part use in gammon blocking library (ref.to the link) and I want to achieve the same with non-blocking library.
// receive response
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
digitalWrite (LED_PIN, received == 0); // turn on LED if error
// only send once per successful change
if (received)
old_level = level;
Currently protocol which I am using like
byte msg [] = {1, 1, 1}; // send assembled msg from local panel to relay module where Device #, Button #, Button status
Relay module will receive the data and reply as below
if (myChannel.update ())
{
memcpy (buf, myChannel.getData (), myChannel.getLength ()); // make a copy of the data
//////////////----------------------------------------///////////////
if (buf [0] == 1 && buf [1] == 1 && buf [2] == 1) //buf[0] = Slave ID , buf[1] = Button ID , buf[2] = Slave ID
{
LED_state1 = 1;
byte msg [] = {1, 1, 1, 3};
myChannel.sendMsg (msg, sizeof (msg));
}
else if (buf [0] == 1 && buf [1] == 1 && buf [2] == 0) {
LED_state1 = 0;
byte msg [] = {1, 1, 0, 4};
myChannel.sendMsg (msg, sizeof (msg));
}
Local panel will update the button indicator once successfully received specific reply as below
if (myChannel.update ())
{
memcpy (buf, myChannel.getData (), myChannel.getLength ()); // make a copy of the data
if (buf [0] == 1 && buf [1] == 1 && buf [2] == 1 && buf [3] == 3) { // 3 confirm the reply msg for ON
LED_state1 = 1;
}
if (buf [0] == 1 && buf [1] == 1 && buf [2] == 0 && buf [3] == 4) { // confirm the reply msg for OFF
LED_state1 = 0;
}
Any other suggestion are welcome.
I search another library which modified from gammon library but I am unable to understand due to week programming skill. if some one can help me to understand feedback part of the library.
Thanks,