Hi, how to implement in the code some kind of "interupt" ? I am reading serial data from the device and publishing all results to relevand mqtt topics ~ every 8 seconds. Also I have a subcribed topics on ESP8266 when message arrives it needs to send serial command to device, but device itself is a little bit slow and it takes ~ 2 seconds to be apllied and during this time I dont want to send "old" values but need to wait and send "updated" ones.
It's not too difficult to handle as a "state machine" architecture
I would suggest to study Serial Input Basics to handle the Serial Input
J-M-L:
It's not too difficult to handle as a "state machine" architectureI would suggest to study Serial Input Basics to handle the Serial Input
in short to make serial read only when no serial send command ?
Not sure what you are saying
I mean handle events as things come in and in case you receive a MQTT message then send the configuration message to your serial device and drop whatever has been accumulating in the serial buffer and what’s coming in for the next 2 seconds and then start again with a clean buffer. Not knowing what comes from your Serial device it’s difficult to say how you get in sync again, hopefully there is a start and stop flag in what you get to help you do this
J-M-L:
Not sure what you are sayingI mean handle events as things come in and in case you receive a MQTT message then send the configuration message to your serial device and drop whatever has been accumulating in the serial buffer and what’s coming in for the next 2 seconds and then start again with a clean buffer. Not knowing what comes from your Serial device it’s difficult to say how you get in sync again, hopefully there is a start and stop flag in what you get to help you do this
it will be in sycn as I query serial by sending "magic message" and get response from serial device every ~8 seconds. So mabe just to put "flag" as if new MQTT message arrives it will be true and during this dont read serial data ? Read is only during false condition.
while (Serial.available() > 0 && MQTT == false) {
rc = Serial.read();
....
....
}
well you will still need to handle the sequence. Imagine you send your "magic message" and then you get the MQTT message so you send another configuration message, when you read next the Serial device, you'll likely get the data from your initial request.
If you can keep the sequence "clean" ie send the magic message wait for the answer and handle the answer, then check MQTT and if you have something to do send the configuration message then - if timing is not super critical and you don't have anything else to handle in between, you could even have an active wait for a couple seconds there with a delay(2000);, then resume sending the magic message and getting the data etc
My opinion though is that it's very important to not second guess timing on the Serial port and have a robust handling of what's expected, what's coming etc
J-M-L:
well you will still need to handle the sequence. Imagine you send your "magic message" and then you get the MQTT message so you send another configuration message, when you read next the Serial device, you'll likely get the data from your initial request.If you can keep the sequence "clean" ie send the magic message wait for the answer and handle the answer, then check MQTT and if you have something to do send the configuration message then - if timing is not super critical and you don't have anything else to handle in between, you could even have an active wait for a couple seconds there with a
delay(2000);, then resume sending the magic message and getting the data etcMy opinion though is that it's very important to not second guess timing on the Serial port and have a robust handling of what's expected, what's coming etc
how to handle the answer if I'am doing each byte value read and publishing to specific MQTT topics, just use global variable like int count = 0 and count++ . Then I will know for example if count = 202 do the cofiguration task if required ?
It sure what the question is... and better clarity on your exact system would be needed