From 6163877950a8689e8e8a8c074dfd9ad374466043 Mon Sep 17 00:00:00 2001 From: wim Date: Sun, 4 Feb 2018 20:24:04 +0100 Subject: [PATCH 1/2] add new remotes --- Master/Itho/CC1101Packet.h | 4 +- Master/Itho/IthoCC1101.cpp | 29 +++- Master/Itho/IthoCC1101.h | 24 +++- Master/Itho/IthoPacket.h | 11 +- Master/IthoEcoFanRFT/IthoEcoFanRFT.ino | 183 +++++++++++++++++++++++-- 5 files changed, 234 insertions(+), 17 deletions(-) diff --git a/Master/Itho/CC1101Packet.h b/Master/Itho/CC1101Packet.h index e79552c..356e31a 100644 --- a/Master/Itho/CC1101Packet.h +++ b/Master/Itho/CC1101Packet.h @@ -7,7 +7,7 @@ #include #ifdef ESP8266 -#include +#include #endif #define CC1101_BUFFER_LEN 64 @@ -22,4 +22,4 @@ class CC1101Packet }; -#endif /* CC1101PACKET_H_ */ \ No newline at end of file +#endif /* CC1101PACKET_H_ */ diff --git a/Master/Itho/IthoCC1101.cpp b/Master/Itho/IthoCC1101.cpp index 2cc636a..d90ed7a 100644 --- a/Master/Itho/IthoCC1101.cpp +++ b/Master/Itho/IthoCC1101.cpp @@ -393,6 +393,11 @@ void IthoCC1101::parseMessageCommand() bool isTimer3Command = true; bool isJoinCommand = true; bool isLeaveCommand = true; + bool isH1Command = true; + bool isH2Command = true; + bool isCookCommand = true; + bool isTimerCommand = true; + //device id memcpy(inIthoPacket.deviceId2, &inMessage2.data[8], sizeof inIthoPacket.deviceId2); @@ -414,6 +419,11 @@ void IthoCC1101::parseMessageCommand() if (inMessage2.data[i+18] != ithoMessage2Timer3CommandBytes[i]) isTimer3Command = false; if (inMessage2.data[i+18] != ithoMessage2JoinCommandBytes[i]) isJoinCommand = false; if (inMessage2.data[i+18] != ithoMessage2LeaveCommandBytes[i]) isLeaveCommand = false; + if (inMessage2.data[i+18] != ithoMessage2_5360146_EcoCommandBytes[i]) isH1Command = false; + if (inMessage2.data[i+18] != ithoMessage2_5360146_ComfortCommandBytes[i]) isH2Command = false; + if (inMessage2.data[i+18] != ithoMessage2_5360146_Cook1CommandBytes[i]) isCookCommand = false; + if (inMessage2.data[i+18] != ithoMessage2_5360146_Timer1CommandBytes[i]) isTimerCommand = false; + } //determine command @@ -428,6 +438,10 @@ void IthoCC1101::parseMessageCommand() if (isTimer3Command) inIthoPacket.command = IthoTimer3; if (isJoinCommand) inIthoPacket.command = IthoJoin; if (isLeaveCommand) inIthoPacket.command = IthoLeave; + if (isH1Command) inIthoPacket.command = IthoHome1; + if (isH2Command) inIthoPacket.command = IthoHome2; + if (isCookCommand) inIthoPacket.command = IthoCook; + if (isTimerCommand) inIthoPacket.command = IthoTimer; } void IthoCC1101::sendCommand(IthoCommand command) @@ -995,9 +1009,16 @@ String IthoCC1101::getLastIDstr(bool ashex) { String IthoCC1101::getLastMessage2str(bool ashex) { String str = "Length="+ String(inMessage2.length) + "."; for (uint8_t i=0; i +#include +#include + #define ITHO_IRQ_PIN D2 IthoCC1101 rf; IthoPacket packet; Ticker ITHOticker; -const uint8_t RFTid[] = {106, 170, 106, 101, 154, 107, 154, 86}; // my ID +//const uint8_t RFTid[] = {106, 170, 106, 101, 154, 107, 154, 86}; // my ID +const uint8_t RFTid1[] = {0x69,0x99,0x96,0x99,0x95,0x6a,0x6a,0x5a}; // my ID +const uint8_t RFTid2[] = {0x6a,0x99,0x66,0xaa,0xa9,0x66,0x6a,0xaa}; // badkamer bool ITHOhasPacket = false; +bool ITHOhasValidPacket = false; +bool ITHOhasValidId = false; + IthoCommand RFTcommand[3] = {IthoUnknown, IthoUnknown, IthoUnknown}; byte RFTRSSI[3] = {0, 0, 0}; byte RFTcommandpos = 0; @@ -46,23 +54,138 @@ IthoCommand RFTstate = IthoUnknown; IthoCommand savedRFTstate = IthoUnknown; bool RFTidChk[3] = {false, false, false}; -void setup(void) { - Serial.begin(115200); - delay(500); - Serial.println("setup begin"); +const char* ssid = ""; +const char* password = ""; +WiFiClient espClient; + +void setupWifi() +{ + Serial.print("Connecting to "); + Serial.println(ssid); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); +} + +void setupRf(void) +{ + Serial.println("setup rf begin"); rf.init(); - Serial.println("setup done"); sendRegister(); Serial.println("join command sent"); pinMode(ITHO_IRQ_PIN, INPUT); attachInterrupt(ITHO_IRQ_PIN, ITHOinterrupt, RISING); } +PubSubClient client(espClient); + +void setupMqtt() +{ + //connect to MQTT server + client.setServer("pi3.lan", 1883); + client.setCallback(callback); +} + + +//print any message received for subscribed topic +void callback(char* topic, byte* payload, unsigned int length) { + Serial.print("Message arrived ["); + Serial.print(topic); + + Serial.print("] (l="); + Serial.print(length); + Serial.print(") "); + + for (int i=0;i 2) RFTcommandpos = 0; // store information in next entry of ringbuffers RFTcommand[RFTcommandpos] = cmd; RFTRSSI[RFTcommandpos] = rf.ReadRSSI(); - bool chk = rf.checkID(RFTid); + + bool chk = rf.checkID(RFTid1) | rf.checkID(RFTid2); + ITHOhasValidId = chk; RFTidChk[RFTcommandpos] = chk; + + ITHOhasPacket = true; if ((cmd != IthoUnknown) && chk) { // only act on good cmd and correct id. - ITHOhasPacket = true; + ITHOhasValidPacket = true; } } } void showPacket() { ITHOhasPacket = false; + String id = rf.getLastIDstr(); + String msg = rf.getLastMessage2str (true); + + Serial.print(id); + Serial.print(" -> "); + Serial.println(msg); + fflush(stdin); + msg = msg.substring(10); + msg.replace(":", ""); + int r = client.publish("itho/ventilatie", msg.c_str()); + if (r == 0) { + Serial.println("Error publishing message"); + } + + if (!ITHOhasValidPacket) { + if (ITHOhasValidId) { + //printf("unknown command: %s\n", rf.getLastCommandStr(false).c_str()); + fflush(stdin); + } + return; + } + //printf("known command: %s\n", rf.getLastCommandStr(false).c_str()); + printf ("\tcounter = %d\n", rf.getLastInCounter()); + fflush(stdin); + + ITHOhasValidPacket = false; uint8_t goodpos = findRFTlastCommand(); if (goodpos != -1) RFTlastCommand = RFTcommand[goodpos]; else RFTlastCommand = IthoUnknown; @@ -147,6 +300,20 @@ void showPacket() { case IthoLeave: Serial.print("leave\n"); break; + case IthoHome1: + Serial.print ("home1\n"); + break; + case IthoHome2: + Serial.print ("home2\n"); + break; + case IthoCook: + Serial.print ("hood\n"); + break; + case IthoTimer: + Serial.print ("timer\n"); + break; + default: + Serial.print ("-\n"); } } From 35c77e12aac06bc2082f8abd0f5a760a0e8ca3f2 Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 11 Feb 2018 14:04:51 +0100 Subject: [PATCH 2/2] work --- Master/Itho/DemandItho.cpp | 83 ++++++++++++++++++++++++++++++++++++ Master/Itho/DemandItho.h | 29 +++++++++++++ Master/Itho/IthoCC1101.h | 1 + Master/platformio.ini | 14 ++++++ Master/src/IthoEcoFanRFT.ino | 1 + Master/test/test_1.cpp | 26 +++++++++++ 6 files changed, 154 insertions(+) create mode 100644 Master/Itho/DemandItho.cpp create mode 100644 Master/Itho/DemandItho.h create mode 100644 Master/platformio.ini create mode 120000 Master/src/IthoEcoFanRFT.ino create mode 100644 Master/test/test_1.cpp diff --git a/Master/Itho/DemandItho.cpp b/Master/Itho/DemandItho.cpp new file mode 100644 index 0000000..3cbf25d --- /dev/null +++ b/Master/Itho/DemandItho.cpp @@ -0,0 +1,83 @@ +#include "stdlib.h" +#include "DemandItho.h" + +// void DemandItho::decodeLastCommand() +// { +// printf("decodeLastCommand\n"); +// CC1101Packet p; +// CC1101Packet received = getLastMessagePacket(); +// uint8_t errorCnt = decode(received, p); +// printf("\terr = %d\n", errorCnt); +// } + +uint8_t DemandItho::decode(CC1101Packet& in, CC1101Packet& decoded) +{ + uint8_t errorCount = 0; + for (int i = 0; i < 8; i++) { + printf("skip %08x\n", in.data[i]); + } + + // init response + const int numberOfLeadByte = 8; + int numberOfNibble = ((in.length - 8)*8) / (2*(8+2)); + + printf("nn = %d\n", numberOfNibble); + + decoded.length = (numberOfNibble - (numberOfLeadByte *2))/2; + for (int i = 0; i < decoded.length; i++) { + decoded.data[i] = 0; + } + + // skip preamble and first 2 bits + int bitIndex = (numberOfLeadByte*8) + 2; + for (int nibbleIndex = 16; nibbleIndex < numberOfNibble; nibbleIndex++) { + for (int idxInNibble = 0; idxInNibble < 4; idxInNibble++) { + bool value = getBit(in, bitIndex); + setBit(decoded, 8*(nibbleIndex / 2) + 4*(1 - (nibbleIndex%2)), value); + bitIndex++; + if (value == getBit(in, bitIndex)) errorCount++; + bitIndex++; // skip a bit + } + // at the end of a nibble, there should be a 0 + if (getBit(in, bitIndex) != false) errorCount++; + } + + //Serial.println(Packet2str(decoded)); + return errorCount; + +} + +bool DemandItho::getBit(CC1101Packet& p, int idx) +{ + int byteIdx = idx / 8; + int bitInByte = 7 - (idx % 8); + return ((p.data[byteIdx] >> bitInByte) & 1); +} + +void DemandItho::setBit(CC1101Packet& p, int idx, bool value) +{ + int byteIdx = idx / 8; + int bitInByte = 7 - (idx % 8); + if (value) { + p.data[byteIdx] = p.data[byteIdx] | (1 << bitInByte); + } else { + p.data[byteIdx] = p.data[byteIdx] & (0xff ^ (1 << bitInByte)); + } +} + +// String DemandItho::Packet2str(CC1101Packet& p , bool ashex) { +// String str; +// for (uint8_t i=0; i +#include "../lib/Itho/DemandItho.h" + +//BOOST_AUTO_TEST_SUITE (enum-test) + +BOOST_AUTO_TEST_CASE( my_test ) +{ + BOOST_CHECK(1); + BOOST_CHECK(1 == 1); + +} + + +BOOST_AUTO_TEST_CASE( decode1 ) +{ + BOOST_CHECK(1); + CC1101Packet p = {10, {0,0,0,0,0,0,0,0, 0b11100001, 0b10000000}}; + CC1101Packet decoded; + DemandItho::decode(p, decoded); + + printf("l = %d\n", decoded.length); + BOOST_CHECK(decoded.length == 1); +} + +//BOOST_AUTO_TEST_SUITE_END( )