136 lines
3.3 KiB
C++
136 lines
3.3 KiB
C++
#include "arduino_secrets.h"
|
|
#include "HDPacketBuilder.h"
|
|
#include <EspMQTTClient.h>
|
|
#include <RF24.h> // library by TMRh20
|
|
|
|
#define SER_BAUDRATE (115200)
|
|
|
|
// pins for: esp8266 NodeMCU v3 dev board
|
|
#define RF_CE_PIN (5) // 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
|
|
|
|
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(
|
|
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
|
|
);
|
|
|
|
void setup() {
|
|
for (int i = 0; i < 32; i++) {
|
|
buf[i] = 0x00;
|
|
}
|
|
|
|
Serial.begin(SER_BAUDRATE);
|
|
|
|
client.setKeepAlive(10);
|
|
client.enableLastWillMessage("hotdog/availability", "offline", true);
|
|
client.enableDebuggingMessages();
|
|
|
|
radio.begin();
|
|
radioTransmitSetup();
|
|
|
|
Serial.println("Ready");
|
|
}
|
|
|
|
void onConnectionEstablished() {
|
|
|
|
client.subscribe("hotdog/test_mqtt_blind/set", [] (const String &payload) {
|
|
uint16_t shadeID = 0x0400;
|
|
if (payload == "OPEN") {
|
|
constructUpPacket(shadeID);
|
|
printPacket(buf);
|
|
Serial.println();
|
|
sendCommand(buf);
|
|
} else if (payload == "CLOSE") {
|
|
constructDownPacket(shadeID);
|
|
printPacket(buf);
|
|
Serial.println();
|
|
sendCommand(buf);
|
|
} else if (payload == "STOP") {
|
|
constructStopPacket(shadeID);
|
|
printPacket(buf);
|
|
Serial.println();
|
|
sendCommand(buf);
|
|
}
|
|
});
|
|
|
|
client.subscribe("hotdog/test_mqtt_blind/set_position", [] (const String &payload) {
|
|
|
|
});
|
|
|
|
client.publish("hotdog/availability", "online", true);
|
|
}
|
|
|
|
void loop() {
|
|
client.loop();
|
|
}
|
|
|
|
void radioTransmitSetup() {
|
|
radio.stopListening();
|
|
radio.powerDown();
|
|
radio.begin();
|
|
delay(100);
|
|
Serial.println();
|
|
radio.setChannel(0x07); //shooting for 2.407 GHz
|
|
radio.setDataRate(RF24_1MBPS); //can go at 250kbs, 1Mbs, 2Mbs
|
|
radio.setPALevel(RF24_PA_HIGH); //transmit power
|
|
radio.disableCRC();
|
|
radio.setAutoAck(false);
|
|
radio.setAddressWidth(3);
|
|
radio.setRetries(0, 0);
|
|
|
|
radio.openWritingPipe(rfID);
|
|
radio.powerUp();
|
|
//radio.printDetails();
|
|
//radio.printPrettyDetails();
|
|
Serial.println("Ready to Transmit");
|
|
}
|
|
|
|
void sendCommand(byte * byteArray) //transmit a command
|
|
{
|
|
uint8_t bytecount = byteArray[1] + 4;
|
|
for (int j = 1; j < 5; j++) {
|
|
radio.setAddressWidth(3);
|
|
radio.disableCRC();
|
|
radio.setAutoAck(false);
|
|
radio.openWritingPipe(rfID);
|
|
for (int i = 1; i < 200; i++) {
|
|
radio.writeFast(byteArray, bytecount);
|
|
}
|
|
delay(500);
|
|
radio.flush_tx();
|
|
radio.txStandBy();
|
|
}
|
|
|
|
rollingCode1++;
|
|
rollingCode2++;
|
|
}
|
|
|
|
void printHex(const uint8_t num) { //print a byte as two hex chars
|
|
if (num < 0x10) {
|
|
Serial.print("0");
|
|
}
|
|
Serial.print(num, HEX);
|
|
}
|
|
|
|
void printByteArray(const uint8_t * byteArray, const int arraySize) { //print a byte array as hex chars
|
|
for (int i = 0; i < arraySize; i++) {
|
|
printHex(byteArray[i]);
|
|
}
|
|
}
|
|
|
|
void printPacket(const uint8_t *buffer) {
|
|
uint8_t length = buffer[1] + 4;
|
|
printByteArray(buffer, length);
|
|
}
|