Hello Gophers! I am trying to write a device driver for a parking sensor, which detects the payload received and tells what the payload contains. But I’m missing some concepts here, can anyone help me with what is missing. Thank you.
I took some a few minutes to run it through the Go Playground. It runs without errors. Keep in mind that I did comment out the following because I’m not sure what you are looking for there.
if payload[14:16] == []byte {
fmt.Println("The firmware version is:")
} else {
fmt.Println("wrong payload")
}
As @NobbZ said, the code posted here is really hard to read and really lowers the chances of someone being able to help. I just happened to have a small break at work and wanted to play around with your code. The more clear you make your posts the better.
@jrswab. I will remove the indentation next time I post. I have few questions:
Everytime I access a byte from hex payload will it consider two characters like ‘0A’ since it comprises of 1 byte.
For example when I say ‘payload[2] == 6’. Here 6 is decimal and payload[2] is hex. Should I convert the payload into decimal before this or not required.
Array indexing starts from MSB or LSB
For the part which you have commented. There are three different payloads sent by the sensor. In the third kind of payload there is no condition there and I just want to print the value of payload or the message it contains (firmware version like 1.0.1).
And the code is printing only wrong payload statements even when the payload is correct. The payload comes as one single string for example “2837f889a888c98e89”. I have to decode it.
Here is the link I have put the other two messages sent by the sensor. https://play.golang.org/p/To6b-_ZA3hJ
The first 2 bytes are ASCII for 0x, are you sure this is received from the wire and not just your example that you hardcoded?
Also every fifth byte is 32, which is a space… I doubt this is a payload as any embedded device or sensor would send it over the wire, as it would increase power consumption on the device for no reason…
@NobbZ. Thanks for the explanation. ( 2 bytes are ASCII for 0x, fifth byte is 32, which is a space)
You’re right as of now i’m giving the hardcoded example payload. Once I have a working code for hardcoded payload I then want to implement the next part.
However the payload sent by the sensor when connected to the LoRaWAN network is still this “110a000f00551001”. without spaces and ‘0x01’.
And the device is a Bosch parking sensor with a built in battery and LoRa chip once its triggered it starts sending the payloads. It’s not a plug and play device.
@hemanth_naidu@NobbZ is right: the payload that you are using for test purposes is wrong.
If you are getting payload in string but in hex encoded, so you need to first decode it. I did something based on your script but not sure it will help you.
var payloadStr = "110a000f00551001"
payload, err := hex.DecodeString(payloadStr)
if err != nil {
panic(err)
}
fmt.Printf("%#v", payload)
// use hex format when you compare them
if payload[0] == 0x11 {
fmt.Println(" Parking status message ")
} else if payload[0] == 0x02 {
fmt.Println(" Heart beat message ")
} else if payload[0] == 0x03 {
fmt.Println(" Start uo message ")
} else {
fmt.Println("Wrong payload")
}
tip: if you are working on bytes, it is good to know the bytes package:
// bytes package has many helpful functions that can ease your job.
if bytes.Equal(payload[0:2], []byte{0x11, 0xa}) {
fmt.Printf(" The firmware version is: %x")
} else {
fmt.Println("wrong payload")
}