The only documentation I got was When I say:
Hi xxx. I get F4 06 01 FF
Turn onf the light. I get F4 06 02 FF
same as other commands.
I can use serial monitor to get those hex when I connect directly to the module.
The problem now is just using ESP8266 to read those hex under Arduino.
in general, information is serially transmitted in either ASCII or binary (e.g. ethernet). i think what you asking is how to translate an ASCII string formatted as hex data into non-ascii values
I don't think so. clearly the module is using a binary protocol if ((uint8_t)buf[i] == 0xF4)
it would be interesting to have more than snippets (Snippets R Us!) and see how _hb_uart_recv() is being called, likely by another function listening to the Serial input and building a buffer (buf) which will then be parsed to extract a command into the rec_buf buffer. it seems 0xF4 is the start marker
may be...
not knowing what the device is nor the protocol does not help.
the head marker might actually be 0xF4 0x06
looking at the snippet it seems that a command can be either 4 bytes or 6 bytes long (may be, if we trust the comment) but it seems they trigger a command without looking at the other bytes but you need to have received more than 4 bytes. so the FF might not be the end...
@sasukebinbin - I'd say assume that the start marker is 0xF4 and collect bytes until a timeout or 0xFF and then just compare with your values
the tutorial would get you going. try that and post your progress
Thanks everyone for the reply.
I have figure out how to achieve this.
And it's really simple.
Just use the example code for readBytes
const int BUFFER_SIZE = 50;
char buf[BUFFER_SIZE];
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// check if data is available
if (Serial.available() > 0) {
// read the incoming bytes:
int rlen = Serial.readBytes(buf, BUFFER_SIZE);
// prints the received data
Serial.print("I received: ");
for(int i = 0; i < rlen; i++)
Serial.print(buf[i]);
}
}
That should work most of the time, it will be a bit slow because there is a 1s timeout (by default) for readBytes() to return if no new byte is coming through (and seems you get only 4 or 6 bytes and your buffer is 50) . That will add visible latency to your handling of the recognised sentence.
But I actually get 4 bytes. So what’s the buffer size I should set here?
then make it 4 would be a minimum. 6 would get a bit safer, there would be a delay but if it doesn't bother you that might actually help with synchronising