Update packet building logic to work with header byte

This commit is contained in:
2023-12-09 09:56:04 +11:00
parent df13de502d
commit 207ffe9cc9
2 changed files with 74 additions and 71 deletions

View File

@@ -7,6 +7,9 @@
#define VERSION_5 (0x05) #define VERSION_5 (0x05)
#define VERSION_6 (0x06) #define VERSION_6 (0x06)
#define REMOTE_ID (0x369E)
#define HUB_ID (0x0000)
uint8_t rollingCode1 = 0xAC; uint8_t rollingCode1 = 0xAC;
uint8_t rollingCode2 = 0x82; uint8_t rollingCode2 = 0x82;
@@ -16,129 +19,124 @@ CRC16 crc(0x755B, 0x0, 0xBA68, false, false);
void setPacketLength(uint8_t); void setPacketLength(uint8_t);
void setConstants(); void setConstants();
void setSourceRemoteAddress(); void setSourceAddress(uint16_t);
void setHubSourceAddress(); void setTargetAddress(uint16_t);
void setRollingCodes(); void setRollingCodes();
void setProtocolVersion(uint8_t); void setProtocolVersion(uint8_t);
void calculateCRC(); void calculateCRC();
void constructUpPacket() void constructUpPacket(uint16_t shadeID)
{ {
setPacketLength(LENGTH_17); setPacketLength(LENGTH_17);
setConstants(); setConstants();
setSourceRemoteAddress(); setSourceAddress(REMOTE_ID);
setTargetAddress(shadeID);
setRollingCodes(); setRollingCodes();
setProtocolVersion(VERSION_6); setProtocolVersion(VERSION_6);
buf[11] = 0x04; // Shade ID (group 4 for now) buf[16] = 0x52; // ?
buf[12] = 0x00; // Shade ID buf[17] = 0x55; // Command
buf[18] = 0x00; // Empty?
buf[16] = 0x55; // Command
buf[17] = 0x00; // Empty?
calculateCRC(); calculateCRC();
} }
void constructDownPacket() void constructDownPacket(uint16_t shadeID)
{ {
setPacketLength(LENGTH_17); setPacketLength(LENGTH_17);
setConstants(); setConstants();
setSourceRemoteAddress(); setSourceAddress(REMOTE_ID);
setTargetAddress(shadeID);
setRollingCodes(); setRollingCodes();
setProtocolVersion(VERSION_6); setProtocolVersion(VERSION_6);
buf[11] = 0x04; // Shade ID (group 4 for now) buf[16] = 0x52; // ?
buf[12] = 0x00; // Shade ID buf[17] = 0x44; // Command
buf[18] = 0x00; // Empty?
buf[16] = 0x44; // Command
buf[17] = 0x00; // Empty?
calculateCRC(); calculateCRC();
} }
void constructStopPacket() void constructStopPacket(uint16_t shadeID)
{ {
setPacketLength(LENGTH_17); setPacketLength(LENGTH_17);
setConstants(); setConstants();
setSourceRemoteAddress(); setSourceAddress(REMOTE_ID);
setTargetAddress(shadeID);
setRollingCodes(); setRollingCodes();
setProtocolVersion(VERSION_6); setProtocolVersion(VERSION_6);
buf[11] = 0x04; // Shade ID (group 4 for now) buf[16] = 0x52; // ?
buf[12] = 0x00; // Shade ID buf[17] = 0x53; // Command
buf[18] = 0x00; // Empty?
buf[16] = 0x53; // Command
buf[17] = 0x00; // Empty?
calculateCRC(); calculateCRC();
} }
void constructPositionPacket(float percentage) void constructPositionPacket(uint16_t shadeID, float percentage)
{ {
setPacketLength(LENGTH_21); setPacketLength(LENGTH_21);
setConstants(); setConstants();
setHubSourceAddress(); // TODO: Will this source address work? setSourceAddress(HUB_ID);
setTargetAddress(shadeID);
setRollingCodes(); setRollingCodes();
setProtocolVersion(VERSION_5); setProtocolVersion(VERSION_5);
buf[11] = 0x4E; // Shade ID? buf[16] = 0x3F; // Command
buf[12] = 0xF1; // Shade ID? buf[17] = 0x5A; // Empty?
buf[15] = 0x3F; // Command buf[18] = 0x04; // Constant in this size packet?
buf[16] = 0x5A; // Empty? buf[19] = 0x40; // Constant in this size packet?
buf[20] = 0x50; // Constant in this size packet?
buf[17] = 0x04; // Constant in this size packet?
buf[18] = 0x40; // Constant in this size packet?
buf[19] = 0x50; // Constant in this size packet?
uint16_t position = (uint16_t)(0xFFFF * percentage); uint16_t position = (uint16_t)(0xFFFF * percentage);
buf[20] = (uint8_t)(position & 0x00FF); buf[21] = (uint8_t)(position & 0x00FF);
buf[21] = (uint8_t)((position & 0xFF00) >> 8); buf[22] = (uint8_t)((position & 0xFF00) >> 8);
calculateCRC(); calculateCRC();
} }
void setPacketLength(uint8_t length) { void setPacketLength(uint8_t length) {
buf[0] = length; // Packet size buf[1] = length; // Packet size
} }
void setConstants() { void setConstants() {
buf[1] = 0x00; // Constant? buf[0] = 0xC0; // Header byte
buf[2] = 0x05; // Constant?
buf[4] = 0xff; // Constant? buf[2] = 0x00; // Constant when sending, can be 0x10 when receiving a packet
buf[5] = 0xff; // Constant? buf[3] = 0x05; // Constant
buf[8] = 0x86; // Constant? buf[5] = 0xFF; // Constant
buf[6] = 0xFF; // Constant
buf[15] = 0x52; // Constant? buf[9] = 0x86; // Constant?
} }
void setSourceRemoteAddress() { void setSourceAddress(uint16_t sourceID) {
buf[6] = 0x36; // Source address? // Physical source address (could be the address of a repeater when receiving a packet)
buf[7] = 0x9e; // Source address? buf[7] = (uint8_t)((sourceID & 0xFF00) >> 8);
buf[8] = (uint8_t)(sourceID & 0x00FF);
buf[13] = 0x36; // Source address again? // Logical source address (usually the same as the physical source address)
buf[14] = 0x9e; // Source address again? buf[14] = (uint8_t)((sourceID & 0xFF00) >> 8);
buf[15] = (uint8_t)(sourceID & 0x00FF);
} }
void setHubSourceAddress() { void setTargetAddress(uint16_t targetID) {
buf[6] = 0x00; // Source address? // Logical target address
buf[7] = 0x00; // Source address? buf[12] = (uint8_t)((targetID & 0xFF00) >> 8);
buf[13] = (uint8_t)(targetID & 0x00FF);
buf[13] = 0x00; // Source address again?
buf[14] = 0x00; // Source address again?
} }
void setRollingCodes() { void setRollingCodes() {
buf[3] = rollingCode1; // Rolling code 1 buf[4] = rollingCode1; // Rolling code 1
buf[10] = rollingCode2; // Rolling code 2 buf[11] = rollingCode2; // Rolling code 2
} }
void setProtocolVersion(uint8_t version) { void setProtocolVersion(uint8_t version) {
buf[9] = version; // Protocol version? buf[10] = version; // Protocol version?
} }
void calculateCRC() { // must be called after the buffer has been filled void calculateCRC() { // must be called after the buffer has been filled
@@ -165,8 +163,8 @@ void calculateCRC() { // must be called after the buffer has been filled
uint8_t checksum1 = (uint8_t)((result & 0xFF00) >> 8); uint8_t checksum1 = (uint8_t)((result & 0xFF00) >> 8);
uint8_t checksum2 = (uint8_t)(result & 0x00FF); uint8_t checksum2 = (uint8_t)(result & 0x00FF);
buf[length + 1] = checksum1; // Checksum buf[length + 2] = checksum1; // Checksum
buf[length + 2] = checksum2; // Checksum buf[length + 3] = checksum2; // Checksum
} }
void printHex(uint8_t num) { //print a byte as two hex chars void printHex(uint8_t num) { //print a byte as two hex chars

View File

@@ -10,6 +10,11 @@
#define RF_CS_PIN (15) // pins for: esp #define RF_CS_PIN (15) // pins for: esp
#define RF_IRQ_PIN (4) // run a jumper from IRQ pin to pin 2 #define RF_IRQ_PIN (4) // run a jumper from IRQ pin to pin 2
RF24 radio(RF_CE_PIN, RF_CS_PIN);
// Copied from Powerview Hub userdata API (eg: http://POWERVIEW_HUB_IP_ADDRESS/api/userdata/ and find the field labeled "rfID")
static const uint16_t rfID = 0x2EC8;
EspMQTTClient client( EspMQTTClient client(
SECRET_WIFI_SSID, // Wifi SSID SECRET_WIFI_SSID, // Wifi SSID
SECRET_WIFI_PASSWORD, // Wifi Password SECRET_WIFI_PASSWORD, // Wifi Password
@@ -20,10 +25,6 @@ EspMQTTClient client(
1883 1883
); );
byte addr[3] = { 0xC0, 0xC8, 0x2E }; // The address was captured as "2EC8C0" and the bytes were reversed
RF24 radio(RF_CE_PIN, RF_CS_PIN);
void setup() { void setup() {
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
buf[i] = 0x00; buf[i] = 0x00;
@@ -50,17 +51,21 @@ void onConnectionEstablished() {
client.subscribe("hotdog/test_mqtt_blind/set", [] (const String &payload) { client.subscribe("hotdog/test_mqtt_blind/set", [] (const String &payload) {
Serial.print("Set on test_mqtt_blind: "); Serial.print("Set on test_mqtt_blind: ");
Serial.println(payload); Serial.println(payload);
uint16_t shadeID = 0x0400;
if (payload == "OPEN") { if (payload == "OPEN") {
constructUpPacket(); constructUpPacket(shadeID);
printBuffer(); printByteArray(buf);
Serial.println();
sendCommand(buf); sendCommand(buf);
} else if (payload == "CLOSE") { } else if (payload == "CLOSE") {
constructDownPacket(); constructDownPacket(shadeID);
printBuffer(); printByteArray(buf);
Serial.println();
sendCommand(buf); sendCommand(buf);
} else if (payload == "STOP") { } else if (payload == "STOP") {
constructStopPacket(); constructStopPacket(shadeID);
printBuffer(); printByteArray(buf);
Serial.println();
sendCommand(buf); sendCommand(buf);
} }
}); });
@@ -91,7 +96,7 @@ void radioTransmitSetup() {
radio.setAddressWidth(3); radio.setAddressWidth(3);
radio.setRetries(0, 0); radio.setRetries(0, 0);
radio.openWritingPipe(addr); radio.openWritingPipe(rfID);
radio.powerUp(); radio.powerUp();
//radio.printDetails(); //radio.printDetails();
//radio.printPrettyDetails(); //radio.printPrettyDetails();
@@ -100,12 +105,12 @@ void radioTransmitSetup() {
void sendCommand(byte * byteArray) //transmit a command void sendCommand(byte * byteArray) //transmit a command
{ {
uint8_t bytecount = byteArray[0] + 3; uint8_t bytecount = byteArray[1] + 4;
for (int j = 1; j < 5; j++) { for (int j = 1; j < 5; j++) {
radio.setAddressWidth(3); radio.setAddressWidth(3);
radio.disableCRC(); radio.disableCRC();
radio.setAutoAck(false); radio.setAutoAck(false);
radio.openWritingPipe(addr); radio.openWritingPipe(rfID);
for (int i = 1; i < 200; i++) { for (int i = 1; i < 200; i++) {
radio.writeFast(byteArray, bytecount); radio.writeFast(byteArray, bytecount);
} }