Convert Hotdog sketch to PlatformIO
This commit is contained in:
158
src/main.cpp
Normal file
158
src/main.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
// sketch to sniff Hunter Douglas packets from a pebble remote or hub
|
||||
|
||||
#define CIRCULAR_BUFFER_INT_SAFE
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <RF24.h> // library by TMRh20
|
||||
#include <EspMQTTClient.h>
|
||||
#include <PacketReceiver.h>
|
||||
#include <PacketBuilder.h>
|
||||
#include "secrets.h"
|
||||
|
||||
#define SER_BAUDRATE (115200)
|
||||
|
||||
#define DEFAULT_RF_CHANNEL (7) //this is the channel HD shades use
|
||||
#define DEFAULT_RF_DATARATE (RF24_1MBPS) //this is the speed HD shades use
|
||||
|
||||
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 uint8_t rfID[2] = { 0xC8, 0x2E }; // 0x2EC8
|
||||
|
||||
PacketReceiver packetReceiver(&radio);
|
||||
PacketBuilder remotePacketBuilder(0x369E, 0xAC, 0x82);
|
||||
|
||||
EspMQTTClient client(
|
||||
SECRET_WIFI_SSID, // Wifi SSID
|
||||
SECRET_WIFI_PASSWORD, // Wifi Password
|
||||
SECRET_MQTT_SERVER_IP, // MQTT Broker server IP
|
||||
SECRET_MQTT_USERNAME, // MQTT Username
|
||||
SECRET_MQTT_PASSWORD, // MQTT Password
|
||||
"hotdog", // MQTT Client ID
|
||||
1883
|
||||
);
|
||||
|
||||
uint8_t buffer[32];
|
||||
|
||||
// put function declarations here:
|
||||
|
||||
static void
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
IRAM_ATTR
|
||||
#endif
|
||||
handleNrfIrq();
|
||||
|
||||
void radioListenSetup();
|
||||
void radioTransmitSetup();
|
||||
void processPacket(const uint8_t*);
|
||||
void sendCommand(const uint8_t*);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(SER_BAUDRATE);
|
||||
|
||||
Serial.println("Starting up");
|
||||
|
||||
packetReceiver.setReceivedCallback(processPacket);
|
||||
|
||||
if (!radio.begin()) {
|
||||
Serial.println("Failed to communicate with radio");
|
||||
}
|
||||
Serial.println("Connected to radio");
|
||||
|
||||
radio.setChannel(DEFAULT_RF_CHANNEL);
|
||||
radio.setDataRate(DEFAULT_RF_DATARATE);
|
||||
|
||||
radio.setAutoAck(false);
|
||||
radio.disableCRC();
|
||||
radio.setRetries(0, 0);
|
||||
radio.setPayloadSize(32);
|
||||
radio.setAddressWidth(2);
|
||||
radio.powerUp();
|
||||
|
||||
pinMode(RF_IRQ_PIN, INPUT);
|
||||
|
||||
attachInterrupt(digitalPinToInterrupt(RF_IRQ_PIN), handleNrfIrq, FALLING);
|
||||
|
||||
radioListenSetup();
|
||||
|
||||
client.setKeepAlive(10);
|
||||
client.enableLastWillMessage("hotdog/availability", "offline", true);
|
||||
client.enableDebuggingMessages();
|
||||
|
||||
delay(100);
|
||||
Serial.println("Ready");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
packetReceiver.loop();
|
||||
client.loop();
|
||||
}
|
||||
|
||||
// put function definitions here:
|
||||
static void handleNrfIrq() {
|
||||
packetReceiver.read();
|
||||
}
|
||||
|
||||
void radioListenSetup() {
|
||||
radio.openReadingPipe(0, rfID);
|
||||
|
||||
radio.startListening();
|
||||
Serial.println("Listening");
|
||||
|
||||
interrupts();
|
||||
}
|
||||
|
||||
void radioTransmitSetup() {
|
||||
radio.stopListening();
|
||||
|
||||
radio.setPALevel(RF24_PA_HIGH, true);
|
||||
|
||||
radio.openWritingPipe(rfID);
|
||||
radio.powerUp();
|
||||
|
||||
Serial.println("Ready to Transmit");
|
||||
}
|
||||
|
||||
void processPacket(const uint8_t *buffer) {
|
||||
Serial.println("Got packet");
|
||||
}
|
||||
|
||||
void sendCommand(uint8_t *buffer) //transmit a command
|
||||
{
|
||||
radioTransmitSetup();
|
||||
uint8_t bytecount = buffer[1] + 4;
|
||||
for (int j = 1; j < 5; j++) {
|
||||
radio.openWritingPipe(rfID);
|
||||
for (int i = 1; i < 200; i++) {
|
||||
radio.writeFast(buffer, bytecount);
|
||||
}
|
||||
delay(500);
|
||||
radio.flush_tx();
|
||||
radio.txStandBy();
|
||||
}
|
||||
radioListenSetup();
|
||||
}
|
||||
|
||||
void onConnectionEstablished() {
|
||||
Serial.println("Connection established");
|
||||
|
||||
client.subscribe("hotdog/test_mqtt_blind/set", [] (const String &payload) {
|
||||
uint16_t shadeID = 0x0400;
|
||||
if (payload == "OPEN") {
|
||||
remotePacketBuilder.buildUpPacket(buffer, shadeID);
|
||||
sendCommand(buffer);
|
||||
} else if (payload == "CLOSE") {
|
||||
remotePacketBuilder.buildDownPacket(buffer, shadeID);
|
||||
sendCommand(buffer);
|
||||
} else if (payload == "STOP") {
|
||||
remotePacketBuilder.buildStopPacket(buffer, shadeID);
|
||||
sendCommand(buffer);
|
||||
}
|
||||
});
|
||||
|
||||
client.subscribe("hotdog/test_mqtt_blind/set_position", [] (const String &payload) {
|
||||
|
||||
});
|
||||
|
||||
client.publish("hotdog/availability", "online", true);
|
||||
}
|
||||
Reference in New Issue
Block a user