From 6aca4fd108ed30e93ed907a6c551df02a1843903 Mon Sep 17 00:00:00 2001 From: Simon Stuerz Date: Tue, 20 Aug 2013 03:15:51 +0200 Subject: [PATCH] RF 433.92 sending works :) sending remote buttons to switches and sending wrong temperature to radio alarmclock --- hive/client/client.pro | 4 - hive/client/hive_client/main.cpp | 3 + .../hive_client/qml/hive_client/main.qml | 23 +-- hive/libhive/jsonparser.cpp | 6 + hive/libhive/jsonparser.h | 18 ++ hive/libhive/jsonserializer.cpp | 6 + hive/libhive/jsonserializer.h | 18 ++ hive/libhive/libhive.pro | 8 +- hive/server/hive_pi/hivecore.cpp | 19 ++- hive/server/hive_pi/radio/radioreciver.cpp | 60 +++++-- hive/server/hive_pi/radio/radioreciver.h | 8 +- hive/server/hive_pi/radio/radiosender.cpp | 155 ++++++++++++++++-- hive/server/hive_pi/radio/radiosender.h | 27 ++- 13 files changed, 291 insertions(+), 64 deletions(-) delete mode 100644 hive/client/client.pro create mode 100644 hive/libhive/jsonparser.cpp create mode 100644 hive/libhive/jsonparser.h create mode 100644 hive/libhive/jsonserializer.cpp create mode 100644 hive/libhive/jsonserializer.h diff --git a/hive/client/client.pro b/hive/client/client.pro deleted file mode 100644 index 82f7e9e3..00000000 --- a/hive/client/client.pro +++ /dev/null @@ -1,4 +0,0 @@ -TEMPLATE = subdirs -CONFIG = ordered - -SUBDIRS += hive_client diff --git a/hive/client/hive_client/main.cpp b/hive/client/hive_client/main.cpp index 8d538bc4..3b4b0abf 100644 --- a/hive/client/hive_client/main.cpp +++ b/hive/client/hive_client/main.cpp @@ -1,9 +1,12 @@ #include #include "qtquick2applicationviewer.h" +#include int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); + //QQmlApplicationEngine engine("qml/hive_client/main.qml"); + QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/hive_client/main.qml")); diff --git a/hive/client/hive_client/qml/hive_client/main.qml b/hive/client/hive_client/qml/hive_client/main.qml index fd7b366f..c8fe10c9 100644 --- a/hive/client/hive_client/qml/hive_client/main.qml +++ b/hive/client/hive_client/qml/hive_client/main.qml @@ -2,22 +2,15 @@ import QtQuick 2.0 import QtQuick.Controls 1.0 import QtQuick.Layouts 1.0 -Rectangle { - id: mainWindow - //title: "Hive Client" - width: 500 - height: 360 - color: "#776262" - RowLayout{ - anchors.centerIn: parent - Button{ - text: "Button 1" - } - Button{ - text: "Button 1" +ApplicationWindow{ + id: window + width: 600 + height: 360 + + statusBar: StatusBar { + RowLayout { + Label { text: "Read Only" } } } - - } diff --git a/hive/libhive/jsonparser.cpp b/hive/libhive/jsonparser.cpp new file mode 100644 index 00000000..cd4669eb --- /dev/null +++ b/hive/libhive/jsonparser.cpp @@ -0,0 +1,6 @@ +#include "jsonparser.h" + +JsonParser::JsonParser(QObject *parent) : + QObject(parent) +{ +} diff --git a/hive/libhive/jsonparser.h b/hive/libhive/jsonparser.h new file mode 100644 index 00000000..e0e90269 --- /dev/null +++ b/hive/libhive/jsonparser.h @@ -0,0 +1,18 @@ +#ifndef JSONPARSER_H +#define JSONPARSER_H + +#include + +class JsonParser : public QObject +{ + Q_OBJECT +public: + explicit JsonParser(QObject *parent = 0); + +signals: + +public slots: + +}; + +#endif // JSONPARSER_H diff --git a/hive/libhive/jsonserializer.cpp b/hive/libhive/jsonserializer.cpp new file mode 100644 index 00000000..738b324b --- /dev/null +++ b/hive/libhive/jsonserializer.cpp @@ -0,0 +1,6 @@ +#include "jsonserializer.h" + +JsonSerializer::JsonSerializer(QObject *parent) : + QObject(parent) +{ +} diff --git a/hive/libhive/jsonserializer.h b/hive/libhive/jsonserializer.h new file mode 100644 index 00000000..8218e137 --- /dev/null +++ b/hive/libhive/jsonserializer.h @@ -0,0 +1,18 @@ +#ifndef JSONSERIALIZER_H +#define JSONSERIALIZER_H + +#include + +class JsonSerializer : public QObject +{ + Q_OBJECT +public: + explicit JsonSerializer(QObject *parent = 0); + +signals: + +public slots: + +}; + +#endif // JSONSERIALIZER_H diff --git a/hive/libhive/libhive.pro b/hive/libhive/libhive.pro index 6480c960..f5df28b4 100644 --- a/hive/libhive/libhive.pro +++ b/hive/libhive/libhive.pro @@ -15,13 +15,17 @@ DEFINES += LIBHIVE_LIBRARY SOURCES += libhive.cpp \ server.cpp \ devicemanager.cpp \ - logwriter.cpp + logwriter.cpp \ + jsonparser.cpp \ + jsonserializer.cpp HEADERS += libhive.h\ libhive_global.h \ server.h \ devicemanager.h \ - logwriter.h + logwriter.h \ + jsonparser.h \ + jsonserializer.h #unix:!symbian { # maemo5 { diff --git a/hive/server/hive_pi/hivecore.cpp b/hive/server/hive_pi/hivecore.cpp index 6877f258..824c09d8 100644 --- a/hive/server/hive_pi/hivecore.cpp +++ b/hive/server/hive_pi/hivecore.cpp @@ -8,19 +8,20 @@ HiveCore::HiveCore(QObject *parent) : m_server = new Server(this); m_server->startServer(); + // create Sender m_sender = new RadioSender(this); + m_sender->setFrequency(RadioSender::RF433MHz); + m_sender->setLineCode(RadioSender::THERMOMETER); + m_sender->setPulseLength(250); + m_sender->sendBin("010100101000101111110110"); + m_sender->sendBin("010100101000101111110110"); - QByteArray data("11101101"); - QDataStream stream(&data, QIODevice::ReadOnly); - qint8 dataInt; - stream >> dataInt; - - qDebug() << data << dataInt << data.toInt(0,2); - + // create 433.92 MHz receiver m_reciver = new RadioReciver(this); - m_reciver->setFrequence(RadioReciver::RC433MHz); + m_reciver->setFrequency(RadioReciver::RF433MHz); m_reciver->enableReceiver(); + m_sender->sendBin("010100101000101111110110"); + - DeviceManager deviceManager; } diff --git a/hive/server/hive_pi/radio/radioreciver.cpp b/hive/server/hive_pi/radio/radioreciver.cpp index 1632b542..919d1a5d 100644 --- a/hive/server/hive_pi/radio/radioreciver.cpp +++ b/hive/server/hive_pi/radio/radioreciver.cpp @@ -4,9 +4,11 @@ #include #include #include +#include static bool m_enable = false; static unsigned int timings[RC_MAX_CHANGES]; +static float lastTemperature = 0; RadioReciver::RadioReciver(QObject *parent) : @@ -58,11 +60,11 @@ void RadioReciver::handleInterrupt() void RadioReciver::detectProtocol(int signalCount) { - qDebug() << "==========================================================================="; if(signalCount < 49){ - qDebug() << "ERROR: got a signal with just" << signalCount << "signals"; + //qDebug() << "ERROR: got a signal with just" << signalCount << "signals"; return; } + qDebug() << "==========================================================================="; QList rawData; // go trough all 49 timings @@ -83,6 +85,11 @@ void RadioReciver::detectProtocol(int signalCount) QByteArray binCode; + // __ + // pulse form: | |________ = 0 1100000000 + // __ + // | |________________ = 1 110000000000000000 + for(int i = 1; i <= 48; i+=2 ){ if(timings[i] < 1000 && timings[i+1] < 3000 && timings[i+1] > 1000){ binCode.append('0'); @@ -101,7 +108,7 @@ void RadioReciver::detectProtocol(int signalCount) } // ######################################################################### - if(delay > 310 && delay < 320){ + if(delay > 310 && delay < 340){ qDebug() << "-----------------------------------------------------------"; qDebug() << " seems to be a REMOTE signal"; @@ -133,9 +140,9 @@ void RadioReciver::detectProtocol(int signalCount) } // _ - // if we have | |___ = 0 -> in 4 delays + // if we have | |___ = 0 -> in 4 delays => 1000 // _ - // if we have ___| | = 1 -> in 4 delays + // if we have ___| | = 1 -> in 4 delays => 0001 if(div == 1 && divNext == 3){ binCode.append("0"); @@ -196,9 +203,11 @@ float RadioReciver::parseTemperature(QByteArray codeBin) qDebug() << byteList; QByteArray temperatureBin(byteList.at(2) + byteList.at(3)); + QByteArray batteryBin(byteList.at(4)); QByteArray temperatureTenthBin(byteList.at(5)); - // get sign of temperature -> if first bit of temperature byte is 1 -> temp is negativ + + // check sign of temperature -> if first bit of temperature byte is 1 -> temp is negativ int sign = 0; if(temperatureBin.left(1).toInt() == 1){ sign = -1; @@ -206,11 +215,36 @@ float RadioReciver::parseTemperature(QByteArray codeBin) sign = 1; } - qDebug() << temperatureBin << "=" << temperatureBin.left(1) << temperatureBin.right(7) << temperatureBin.right(7).toInt(0,2) << "," << temperatureTenthBin.toInt(0,2) ; + //qDebug() << temperatureBin << "=" << temperatureBin.left(1) << temperatureBin.right(7) << temperatureBin.right(7).toInt(0,2) << "," << temperatureTenthBin.toInt(0,2) ; + // calc temperature float temperature = sign*(temperatureBin.right(7).toInt(0,2) + (float)temperatureTenthBin.toInt(0,2)/10); - qDebug() << "Temperature:" << temperature; + // check if the battery is low + QString batteryStatus; + if(batteryBin.toInt(0,2) == 0){ + batteryStatus = "ok"; + }else{ + batteryStatus = "low"; + } + + qDebug() << "Temperature:" << temperature << "Battery: " << batteryStatus; + + if(temperature == lastTemperature){ + return temperature; + }else{ + lastTemperature = temperature; + QString timeStamp = QDateTime::currentDateTime().toString("dd.MM.yyyy, hh:mm");; + qDebug() << timeStamp; + + QFile file("/root/temperature_log.ods"); + file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text); + QTextStream out(&file); + + out << timeStamp << "," << temperature << "," << batteryStatus << "\n"; + file.close(); + } + return temperature; } @@ -224,17 +258,17 @@ void RadioReciver::disableReceiver() m_enable = false; } -void RadioReciver::setFrequence(RadioReciver::Frequenze frequenze) +void RadioReciver::setFrequency(RadioReciver::Frequency frequency) { - if(frequenze == RadioReciver::RC433MHz){ + if(frequency == RadioReciver::RF433MHz){ if(wiringPiSetup() == -1){ - qDebug() << "ERROR: GPIO setup failed."; + qDebug() << "ERROR: GPIO setup for 433.92 MHz receiver failed."; } - qDebug() << "GPIO setup ok."; pinMode(2,INPUT); wiringPiISR(2, INT_EDGE_BOTH, &handleInterrupt); + qDebug() << "GPIO setup for 433.92 MHz receiver ok."; } - if(frequenze == RadioReciver::RC868MHz){ + if(frequency == RadioReciver::RF868MHz){ qDebug() << "ERROR: 868 MHz Module not connected yet"; } diff --git a/hive/server/hive_pi/radio/radioreciver.h b/hive/server/hive_pi/radio/radioreciver.h index e362ef6e..4b55a1c0 100644 --- a/hive/server/hive_pi/radio/radioreciver.h +++ b/hive/server/hive_pi/radio/radioreciver.h @@ -11,9 +11,9 @@ class RadioReciver : public QObject public: explicit RadioReciver(QObject *parent = 0); - enum Frequenze{ - RC433MHz = 0x0, - RC868MHz = 0x1 + enum Frequency{ + RF433MHz = 0x0, + RF868MHz = 0x1 }; private: @@ -26,7 +26,7 @@ signals: public slots: void enableReceiver(); void disableReceiver(); - void setFrequence(Frequenze frequenze); + void setFrequency(Frequency frequency); }; diff --git a/hive/server/hive_pi/radio/radiosender.cpp b/hive/server/hive_pi/radio/radiosender.cpp index d4f82dc1..797ede64 100644 --- a/hive/server/hive_pi/radio/radiosender.cpp +++ b/hive/server/hive_pi/radio/radiosender.cpp @@ -6,34 +6,161 @@ RadioSender::RadioSender(QObject *parent) : QObject(parent) { + // set default pulselenght m_pulseLength = 350; + // set default radio frequency module + m_frequenze = RadioSender::RF433MHz; + } void RadioSender::sendSync() { + // sync for UNIPOLAR: + if(m_lineCode == RadioSender::UNIPOLAR){ -} - -void RadioSender::transmit(int high, int low) -{ - -} - -void RadioSender::sendBin(QString codeBin) -{ - - if(wiringPiSetup() == -1){ - qDebug() << "ERROR: GPIO setup failed."; } - qDebug() << "GPIO setup ok."; - pinMode(0,OUTPUT); + // sync for MANCHESTER: + if(m_lineCode == RadioSender::MANCHESTER){ + + } + // sync for differential MANCHESTER: + if(m_lineCode == RadioSender::DMANCHESTER){ + + } + // sync for REMOTE: 1 high 31 low + if(m_lineCode == RadioSender::REMOTE){ + digitalWrite(m_pin,HIGH); + delayMicroseconds(m_pulseLength); + digitalWrite(m_pin,LOW); + delayMicroseconds(m_pulseLength*31); + } + // sync for THERMOMETER: 1 high 31 low + if(m_lineCode == RadioSender::THERMOMETER){ + digitalWrite(m_pin,HIGH); + delayMicroseconds(m_pulseLength); + digitalWrite(m_pin,LOW); + delayMicroseconds(m_pulseLength*31); + } + // sync for WEATHERSTATION: + if(m_lineCode == RadioSender::WEATHERSTATION){ + + } + +} + +void RadioSender::send0() +{ + // 0 in UNIPOLAR encoding + if(m_lineCode == RadioSender::UNIPOLAR){ + digitalWrite(m_pin,LOW); + delayMicroseconds(m_pulseLength); + } + // 0 in MANCHESTER encoding + if(m_lineCode == RadioSender::MANCHESTER){ + + } + // 0 in differential MANCHESTER encoding + if(m_lineCode == RadioSender::DMANCHESTER){ + + } + // 0 in REMOTE encoding + if(m_lineCode == RadioSender::REMOTE){ + digitalWrite(m_pin,HIGH); + delayMicroseconds(m_pulseLength); + digitalWrite(m_pin,LOW); + delayMicroseconds(m_pulseLength*3); + } + // 0 in THERMOMETER encoding + if(m_lineCode == RadioSender::THERMOMETER){ + digitalWrite(m_pin,HIGH); + delayMicroseconds(m_pulseLength*2); + digitalWrite(m_pin,LOW); + delayMicroseconds(m_pulseLength*8); + } + // 0 in WEATHERSTATION encoding + if(m_lineCode == RadioSender::WEATHERSTATION){ + + } +} + +void RadioSender::send1() +{ + // 1 in UNIPOLAR encoding + if(m_lineCode == RadioSender::UNIPOLAR){ + digitalWrite(m_pin,HIGH); + delayMicroseconds(m_pulseLength); + } + // 1 in MANCHESTER encoding + if(m_lineCode == RadioSender::MANCHESTER){ + + } + // 1 in differential MANCHESTER encoding + if(m_lineCode == RadioSender::DMANCHESTER){ + + } + // 1 in REMOTE encoding + if(m_lineCode == RadioSender::REMOTE){ + digitalWrite(m_pin,HIGH); + delayMicroseconds(m_pulseLength*3); + digitalWrite(m_pin,LOW); + delayMicroseconds(m_pulseLength); + } + // 1 in THERMOMETER encoding + if(m_lineCode == RadioSender::THERMOMETER){ + digitalWrite(m_pin,HIGH); + delayMicroseconds(m_pulseLength*2); + digitalWrite(m_pin,LOW); + delayMicroseconds(m_pulseLength*16); + } + // 1 in WEATHERSTATION encoding + if(m_lineCode == RadioSender::WEATHERSTATION){ + + } +} +void RadioSender::sendBin(QByteArray codeBin) +{ + qDebug() << "send" << codeBin; + for(int i = 0; i < 8; i++){ + for(int i = 0; i < codeBin.length(); i++){ + if(codeBin.at(i) == '0'){ + send0(); + } + if(codeBin.at(i) == '1'){ + send1(); + } + } + sendSync(); + } +} + +void RadioSender::setFrequency(RadioSender::Frequency frequency) +{ + m_frequenze = frequency; + + if(m_frequenze == RadioSender::RF433MHz){ + m_pin = 0; + if(wiringPiSetup() == -1){ + qDebug() << "ERROR: GPIO setup for 433.92 MHz sender failed."; + } + pinMode(m_pin,OUTPUT); + qDebug() << "GPIO setup for 433.92 MHz sender ok."; + } + if(m_frequenze == RadioSender::RF868MHz){ + qDebug() << "ERROR: 868 MHz Module not connected yet"; + } +} + +void RadioSender::setLineCode(RadioSender::LineCode lineCode) +{ + m_lineCode = lineCode; } void RadioSender::setPulseLength(int pulseLength) { m_pulseLength = pulseLength; } + diff --git a/hive/server/hive_pi/radio/radiosender.h b/hive/server/hive_pi/radio/radiosender.h index d012536b..aea7d352 100644 --- a/hive/server/hive_pi/radio/radiosender.h +++ b/hive/server/hive_pi/radio/radiosender.h @@ -9,17 +9,38 @@ class RadioSender : public QObject public: explicit RadioSender(QObject *parent = 0); + enum Frequency{ + RF433MHz = 0x0, + RF868MHz = 0x1 + }; + + enum LineCode{ + UNIPOLAR = 0x0, + MANCHESTER = 0x1, + DMANCHESTER = 0x2, + REMOTE = 0x4, + THERMOMETER = 0x8 + }; + + private: - // [us] + // [us = micro seconds] int m_pulseLength; + Frequency m_frequenze; + LineCode m_lineCode; + int m_pin; + void sendSync(); - void transmit(int high, int low); + void send0(); + void send1(); signals: public slots: - void sendBin(QString codeBin); + void sendBin(QByteArray codeBin); + void setFrequency(Frequency frequency); + void setLineCode(LineCode lineCode); void setPulseLength(int pulseLength); };