From e9b781c3902a7ec7c58356e682484604d6ea1a3c Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 01:54:35 +0300 Subject: [PATCH 01/10] builds --- .../TransmissionModule/TransmissionModule.cpp | 47 ++++++++++++++++++- .../TransmissionModule/TransmissionModule.h | 28 +++++++++-- cybergarden-sensor/platformio.ini | 8 ++-- cybergarden-sensor/src/main.cpp | 13 ++--- 4 files changed, 82 insertions(+), 14 deletions(-) diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp index 2403d97..2d9b959 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp @@ -8,7 +8,52 @@ #define ECC_LENGTH 4 #define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH) -void TransmissionModule::transmit(MeasureData &data) { +void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t* output_data) +{ + size_t posn, len, inc = 5; + cipher->clear(); + cipher->setKey(encryptionKey, key_size); + cipher->setIV(iv, iv_size); + memset(buffer, 0xBA, sizeof(buffer)); + + for (posn = 0; posn < auth_size; posn += inc) { + len = auth_size - posn; + if (len > inc) + len = inc; + cipher->addAuthData(authdata + posn, len); + } + + for (posn = 0; posn < data_size; posn += inc) { + len = data_size - posn; + if (len > inc) + len = inc; + cipher->encrypt(buffer + posn, input_data + posn, len); + } + + byte buffer2[MAX_PLAINTEXT_LEN]; + memset(buffer2, 0xBA, sizeof(buffer)); + + cipher->setKey(encryptionKey, 16); + cipher->setIV(iv, iv_size); + + for (posn = 0; posn < data_size; posn += inc) { + len = data_size - posn; + if (len > inc) + len = inc; + cipher->decrypt(buffer2 + posn, buffer + posn, len); + } + + if (memcmp(buffer2, input_data, data_size) != 0) { + // Serial.println("doesn't work"); + } else{ + // Serial.println("works"); + } + + memcpy(output_data, buffer, data_size); +} + +void TransmissionModule::transmit(MeasureData &data) +{ uint8_t msg[MSG_LENGTH]; msg[0] = data.sensor_id; memcpy(msg + 1, (void *) &data, 4); diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h index d3a81fa..247b77d 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h @@ -1,23 +1,43 @@ +#ifndef KEMPT_KINKAJOU_TRANSMISSIONMODULE_H +#define KEMPT_KINKAJOU_TRANSMISSIONMODULE_H + #include +#include +#include #include "MeasureData.h" #include "TransmitterModule.h" #include "ReedSolomonModule.h" -#ifndef KEMPT_KINKAJOU_TRANSMISSIONMODULE_H -#define KEMPT_KINKAJOU_TRANSMISSIONMODULE_H +#define MAX_PLAINTEXT_LEN 265 class TransmissionModule { private: TransmitterModule * transmitterModule; ReedSolomonModule * reedSolomonModule; + ChaChaPoly* cipher; + uint8_t encryptionKey[16] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f}; + uint8_t authdata[16] = {0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, + 0xc4, 0xc5, 0xc6, 0xc7}; + + uint8_t iv[16] = {0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, + 0x44, 0x45, 0x46, 0x47}; + uint8_t buffer[MAX_PLAINTEXT_LEN]; + size_t key_size = 16; + size_t auth_size = 12; + // size_t data_size = 4; + size_t iv_size = 12; public: - unsigned char * encryptionKey = 0; + TransmissionModule( TransmitterModule &transmitterModule, ReedSolomonModule &reedSolomonModule ) : transmitterModule(&transmitterModule), - reedSolomonModule(&reedSolomonModule) {} + reedSolomonModule(&reedSolomonModule) { + cipher = new ChaChaPoly(); + } + void encrypt(uint8_t * data, size_t data_size, uint8_t* output_data); void transmit(MeasureData &data); }; diff --git a/cybergarden-sensor/platformio.ini b/cybergarden-sensor/platformio.ini index 9f204c0..203b30b 100644 --- a/cybergarden-sensor/platformio.ini +++ b/cybergarden-sensor/platformio.ini @@ -9,8 +9,10 @@ ; https://docs.platformio.org/page/projectconf.html [env:esp32doit-devkit-v1] -platform = atmelavr -board = megaatmega2560 +platform = espressif32 +board = esp32dev framework = arduino +monitor_speed = 115200 lib_deps = - mikem/RadioHead@^1.120 \ No newline at end of file + mikem/RadioHead@^1.120 + rweather/Crypto@^0.4.0 diff --git a/cybergarden-sensor/src/main.cpp b/cybergarden-sensor/src/main.cpp index 8eb0ba6..63c7234 100644 --- a/cybergarden-sensor/src/main.cpp +++ b/cybergarden-sensor/src/main.cpp @@ -8,9 +8,9 @@ ReedSolomonModule reedSolomonModule; TransmissionModule transmissionModule(transmitterModule, reedSolomonModule); void setup() { - Serial.begin(9600); + Serial.begin(115200); transmitterModule.init(); - pinMode(LED_BUILTIN, OUTPUT); + // pinMode(LED_BUILTIN, OUTPUT); } uint16_t getMockSensorData() { @@ -22,10 +22,11 @@ void loop() { measureData.sensor_id = 0xA5; measureData.sensor_type = 0x01; measureData.payload = getMockSensorData(); + // transmissionModule.encrypt((uint8_t*)&measureData+1, 4, (uint8_t*)&measureData+1); transmissionModule.transmit(measureData); - analogWrite(LED_BUILTIN, 255); - delay(200); - analogWrite(LED_BUILTIN, 0); - delay(10000); + // analogWrite(LED_BUILTIN, 255); + // delay(200); + // analogWrite(LED_BUILTIN, 0); + // delay(10000); } \ No newline at end of file From 214617355993b8330fd61e83505150dac2ac3c6d Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 02:18:20 +0300 Subject: [PATCH 02/10] fixed spi-flash-reboot on esp32, encryption kinda works --- .gitignore | 1 + .../TransmissionModule/TransmissionModule.cpp | 36 ++++++++++--------- .../lib/TransmitterModule/TransmitterModule.h | 6 ++-- cybergarden-sensor/src/main.cpp | 9 ++--- 4 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..02288c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cybergarden-sensor/.vscode diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp index 2d9b959..2e4e09f 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp @@ -30,24 +30,28 @@ void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t cipher->encrypt(buffer + posn, input_data + posn, len); } - byte buffer2[MAX_PLAINTEXT_LEN]; - memset(buffer2, 0xBA, sizeof(buffer)); + //************************* + //Encryption Testing + //************************* - cipher->setKey(encryptionKey, 16); - cipher->setIV(iv, iv_size); + // byte buffer2[MAX_PLAINTEXT_LEN]; + // memset(buffer2, 0xBA, sizeof(buffer)); - for (posn = 0; posn < data_size; posn += inc) { - len = data_size - posn; - if (len > inc) - len = inc; - cipher->decrypt(buffer2 + posn, buffer + posn, len); - } + // cipher->setKey(encryptionKey, 16); + // cipher->setIV(iv, iv_size); - if (memcmp(buffer2, input_data, data_size) != 0) { - // Serial.println("doesn't work"); - } else{ - // Serial.println("works"); - } + // for (posn = 0; posn < data_size; posn += inc) { + // len = data_size - posn; + // if (len > inc) + // len = inc; + // cipher->decrypt(buffer2 + posn, buffer + posn, len); + // } + + // if (memcmp(buffer2, input_data, data_size) != 0) { + // Serial.println("doesn't work"); + // } else{ + // Serial.println("works"); + // } memcpy(output_data, buffer, data_size); } @@ -57,8 +61,6 @@ void TransmissionModule::transmit(MeasureData &data) uint8_t msg[MSG_LENGTH]; msg[0] = data.sensor_id; memcpy(msg + 1, (void *) &data, 4); - - uint8_t encoded[MSG_LENGTH + ECC_LENGTH]; reedSolomonModule->encode(msg, encoded); transmitterModule->send(encoded, MSG_LENGTH + ECC_LENGTH); diff --git a/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h b/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h index b8f6964..94bdae9 100644 --- a/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h +++ b/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h @@ -8,15 +8,15 @@ #ifndef KEMPT_KINKAJOU_TRANSMITTERMODULE_H #define KEMPT_KINKAJOU_TRANSMITTERMODULE_H -#define RH_TRANSMIT_PIN 12 -#define RH_RECEIVE_PIN 11 +#define RH_TRANSMIT_PIN 5 +#define RH_RECEIVE_PIN 4 class TransmitterModule { private: RH_ASK *rhAsk; public: TransmitterModule() { - rhAsk = new RH_ASK(); + rhAsk = new RH_ASK(2000, RH_RECEIVE_PIN, RH_TRANSMIT_PIN); } bool init() { return rhAsk->init(); diff --git a/cybergarden-sensor/src/main.cpp b/cybergarden-sensor/src/main.cpp index 63c7234..508530b 100644 --- a/cybergarden-sensor/src/main.cpp +++ b/cybergarden-sensor/src/main.cpp @@ -10,7 +10,6 @@ TransmissionModule transmissionModule(transmitterModule, reedSolomonModule); void setup() { Serial.begin(115200); transmitterModule.init(); - // pinMode(LED_BUILTIN, OUTPUT); } uint16_t getMockSensorData() { @@ -22,11 +21,7 @@ void loop() { measureData.sensor_id = 0xA5; measureData.sensor_type = 0x01; measureData.payload = getMockSensorData(); - // transmissionModule.encrypt((uint8_t*)&measureData+1, 4, (uint8_t*)&measureData+1); + transmissionModule.encrypt((uint8_t*)&measureData+1, 4, (uint8_t*)&measureData+1); transmissionModule.transmit(measureData); - - // analogWrite(LED_BUILTIN, 255); - // delay(200); - // analogWrite(LED_BUILTIN, 0); - // delay(10000); + delay(1000); } \ No newline at end of file From f10ac55b925f14d948dbc111767b698a430d5be9 Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 02:19:32 +0300 Subject: [PATCH 03/10] cleaned a little --- cybergarden-sensor/.vscode/extensions.json | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 cybergarden-sensor/.vscode/extensions.json diff --git a/cybergarden-sensor/.vscode/extensions.json b/cybergarden-sensor/.vscode/extensions.json deleted file mode 100644 index 080e70d..0000000 --- a/cybergarden-sensor/.vscode/extensions.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ], - "unwantedRecommendations": [ - "ms-vscode.cpptools-extension-pack" - ] -} From ca8d66c5216940ee12b8ab8d0846dabea45b9afe Mon Sep 17 00:00:00 2001 From: "o.likhogub" Date: Sun, 8 Oct 2023 02:29:14 +0300 Subject: [PATCH 04/10] Added aggregator code --- cybergarden-aggregator/.gitignore | 5 + .../lib/UuidModule/Uuid.cpp | 180 ++++++++++++++++++ cybergarden-aggregator/lib/UuidModule/Uuid.h | 75 ++++++++ cybergarden-aggregator/platformio.ini | 18 ++ cybergarden-aggregator/src/main.cpp | 107 +++++++++++ 5 files changed, 385 insertions(+) create mode 100644 cybergarden-aggregator/.gitignore create mode 100644 cybergarden-aggregator/lib/UuidModule/Uuid.cpp create mode 100644 cybergarden-aggregator/lib/UuidModule/Uuid.h create mode 100644 cybergarden-aggregator/platformio.ini create mode 100644 cybergarden-aggregator/src/main.cpp diff --git a/cybergarden-aggregator/.gitignore b/cybergarden-aggregator/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/cybergarden-aggregator/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/cybergarden-aggregator/lib/UuidModule/Uuid.cpp b/cybergarden-aggregator/lib/UuidModule/Uuid.cpp new file mode 100644 index 0000000..9dd81a9 --- /dev/null +++ b/cybergarden-aggregator/lib/UuidModule/Uuid.cpp @@ -0,0 +1,180 @@ +// +// FILE: UUID.cpp +// AUTHOR: Rob Tillaart +// VERSION: 0.1.5 +// DATE: 2022-06-14 +// PURPOSE: Arduino Library for generating UUID's +// URL: https://github.com/RobTillaart/UUID +// https://en.wikipedia.org/wiki/Universally_unique_identifier + + +#include "UUID.h" + + +UUID::UUID() +{ + seed(1, 2); + setVariant4Mode(); + generate(); +} + + +void UUID::seed(uint32_t s1, uint32_t s2) +{ + // set Marsaglia constants, prevent 0 as value + if (s1 == 0) s1 = 1; + if (s2 == 0) s2 = 2; + _m_w = s1; + _m_z = s2; +} + + +// check version 0.1.1 for more readable code +void UUID::generate() +{ + uint32_t ar[4]; + for (uint8_t i = 0; i < 4; i++) + { + ar[i] = _random(); + // store binary version globally ? + // _ar[i] = ar[i]; + } + // Conforming to RFC 4122 Specification + // - byte 7: four most significant bits ==> 0100 --> always 4 + // - byte 9: two most significant bits ==> 10 --> always {8, 9, A, B}. + // + // patch bits for version 1 and variant 4 here + if (_mode == UUID_MODE_VARIANT4) + { + ar[1] &= 0xFFF0FFFF; // remove 4 bits. + ar[1] |= 0x00040000; // variant 4 + ar[2] &= 0xFFFFFFF3; // remove 2 bits + ar[2] |= 0x00000008; // version 1 + } + + // process 16 bytes build up the char array. + for (uint8_t i = 0, j = 0; i < 16; i++) + { + // multiples of 4 between 8 and 20 get a -. + // note we are processing 2 digits in one loop. + if ((i & 0x1) == 0) + { + if ((4 <= i) && (i <= 10)) + { + _buffer[j++] = '-'; + } + } + + // process one byte at the time instead of a nibble + uint8_t nr = i / 4; + uint8_t xx = ar[nr]; + uint8_t ch = xx & 0x0F; + _buffer[j++] = (ch < 10) ? '0' + ch : ('a' - 10) + ch; + + ch = (xx >> 4) & 0x0F; + ar[nr] >>= 8; + _buffer[j++] = (ch < 10) ? '0' + ch : ('a' - 10) + ch; + } + + // if (_upperCase) + // { + // for (int i = 0; i < 37; i++) + // { + // _buffer[i] = toUpper(_buffer[i]); + // } + // } + _buffer[36] = 0; +} + + +char * UUID::toCharArray() +{ + // if (_upperCase) + // { + // for (int i = 0; i < 37; i++) + // { + // _buffer[i] = toLower(_buffer[i]); + // } + // } + // else + // { + // for (int i = 0; i < 37; i++) + // { + // _buffer[i] = toLower(_buffer[i]); + // } + // } + return _buffer; +} + + +////////////////////////////////////////////////// +// +// MODE +// +void UUID::setVariant4Mode() +{ + _mode = UUID_MODE_VARIANT4; +} + + +void UUID::setRandomMode() +{ + _mode = UUID_MODE_RANDOM; +} + + +uint8_t UUID::getMode() +{ + return _mode; +} + + +////////////////////////////////////////////////// +// +// PRINTING +// +size_t UUID::printTo(Print& p) const +{ + return p.print(_buffer); +} + + +////////////////////////////////////////////////// +// +// CASE +// +// void UUID::setLowerCase() +// { +// _upperCase = false; +// } +// +// +// void UUID::setUpperCase() +// { +// _upperCase = true; +// } + + +////////////////////////////////////////////////// +// +// PRIVATE +// + + + +////////////////////////////////////////////////// +// +// RANDOM GENERATOR MARSAGLIA +// +// An example of a simple pseudo-random number generator is the +// Multiply-with-carry method invented by George Marsaglia. +// two initializers (not null) +uint32_t UUID::_random() +{ + _m_z = 36969L * (_m_z & 65535L) + (_m_z >> 16); + _m_w = 18000L * (_m_w & 65535L) + (_m_w >> 16); + return (_m_z << 16) + _m_w; // 32-bit result +} + + +// -- END OF FILE -- diff --git a/cybergarden-aggregator/lib/UuidModule/Uuid.h b/cybergarden-aggregator/lib/UuidModule/Uuid.h new file mode 100644 index 0000000..ea77a1b --- /dev/null +++ b/cybergarden-aggregator/lib/UuidModule/Uuid.h @@ -0,0 +1,75 @@ +#pragma once +// +// FILE: UUID.h +// AUTHOR: Rob Tillaart +// VERSION: 0.1.5 +// DATE: 2022-06-14 +// PURPOSE: Arduino Library for generating UUID's +// URL: https://github.com/RobTillaart/UUID +// https://en.wikipedia.org/wiki/Universally_unique_identifier +// +// e.g. 20d24650-d900-e34f-de49-8964ab3eb46d + + +#include "Arduino.h" +#include "Printable.h" + + +#define UUID_LIB_VERSION (F("0.1.5")) + +// TODO an enum? +const uint8_t UUID_MODE_VARIANT4 = 0; +const uint8_t UUID_MODE_RANDOM = 1; + + +///////////////////////////////////////////////// +// +// CLASS VERSION +// +class UUID : public Printable +{ +public: + UUID(); + + // at least one seed value is mandatory, two is better. + void seed(uint32_t s1, uint32_t s2 = 0); + // generate a new UUID + void generate(); + // make a UUID string + char * toCharArray(); + + // MODE + void setVariant4Mode(); + void setRandomMode(); + uint8_t getMode(); + + // Printable interface + size_t printTo(Print& p) const; + + + // CASE + // (experimental code, not tested yet) + // lower case is default and according to spec. + // upper case is an additional feature. + // must it be done at generation or at toCharArray() ? + // fast or flex + // + // void setLowerCase(); + // void setUpperCase(); + + +private: + // Marsaglia 'constants' + function + uint32_t _m_w = 1; + uint32_t _m_z = 2; + uint32_t _random(); + + // UUID in string format + char _buffer[37]; + uint8_t _mode = UUID_MODE_VARIANT4; + + // bool _upperCase = false; +}; + + +// -- END OF FILE -- diff --git a/cybergarden-aggregator/platformio.ini b/cybergarden-aggregator/platformio.ini new file mode 100644 index 0000000..9f9afbf --- /dev/null +++ b/cybergarden-aggregator/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32doit-devkit-v1] +platform = espressif32 +board = esp32doit-devkit-v1 +framework = arduino +monitor_speed = 115200 +lib_deps = + mikem/RadioHead@^1.120 + bblanchon/ArduinoJson@^6.21.3 diff --git a/cybergarden-aggregator/src/main.cpp b/cybergarden-aggregator/src/main.cpp new file mode 100644 index 0000000..dee7707 --- /dev/null +++ b/cybergarden-aggregator/src/main.cpp @@ -0,0 +1,107 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#ifdef RH_HAVE_HARDWARE_SPI +#include // Not actually used but needed to compile +#endif + +#include "Uuid.h" +#include + +String serverName = "http://gw.cg.k-lab.su/api/v1/measures/register"; + +ReedSolomonModule reedSolomonModule; +TransmitterModule transmitterModule; +TransmissionModule transmissionModule(transmitterModule, reedSolomonModule); + +UUID uuid; + +const char *ntpServer = "pool.ntp.org"; +unsigned long epochTime; + +void setup() +{ + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin("Darius wifi", "alstroemeria"); + Serial.println("\nConnecting"); + while (WiFi.status() != WL_CONNECTED) + { + Serial.print("."); + delay(100); + } + pinMode(LED_BUILTIN, OUTPUT); + transmitterModule.init(); + configTime(0, 0, ntpServer); +} + +uint8_t sensor_id; +MeasureData measureData; + +unsigned long getTime() +{ + time_t now; + struct tm timeinfo; + if (!getLocalTime(&timeinfo)) + { + Serial.println("Failed to obtain time"); + return (0); + } + time(&now); + return now; +} + +String getPayload(MeasureData &data) +{ + String serverPath = serverName; + serverPath += String("/") + getTime(); + serverPath += String("/") + data.sensor_id; + serverPath += String("/") + WiFi.macAddress(); + serverPath += String("/") + getTime(); + if (data.sensor_type == 1) { + serverPath += String("/") + "temperature"; + } else { + serverPath += String("/") + data.sensor_type; + } + serverPath += String("/") + data.payload; + return serverPath; +} + +bool sendPayload(MeasureData &data) { + if (WiFi.status() == WL_CONNECTED) { + HTTPClient http; + String serverPath = getPayload(data); + + http.begin(serverPath.c_str()); + int httpResponseCode = http.GET(); + if (httpResponseCode > 0) { + Serial.print("HTTP Response code: "); + Serial.println(httpResponseCode); + String payload = http.getString(); + Serial.println(payload); + return true; + } + else { + Serial.print("Error code: "); + Serial.println(httpResponseCode); + } + http.end(); + } + return false; +} + +MeasureData data; + +void loop() { + data.sensor_id = 0; + data.sensor_type = 1; + data.payload = rand() & 0xffff; + sendPayload(data); + delay(5000); +} \ No newline at end of file From 2efc7889a555408a07bd5aa22f15061a0dc3bb10 Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 02:47:11 +0300 Subject: [PATCH 05/10] aggregator recieved messages decryption --- .gitignore | 1 + .../TransmissionModule/TransmissionModule.cpp | 28 ++++++++++++++++++- .../TransmissionModule/TransmissionModule.h | 28 +++++++++++++++++-- .../lib/TransmitterModule/TransmitterModule.h | 5 ++-- .../lib/UuidModule/Uuid.cpp | 2 +- cybergarden-aggregator/platformio.ini | 1 + 6 files changed, 58 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 02288c4..41cab91 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ cybergarden-sensor/.vscode +cybergarden-aggregator/.vscode diff --git a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp index 8c10285..5e6818f 100644 --- a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp @@ -8,7 +8,33 @@ #define ECC_LENGTH 4 #define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH) -bool TransmissionModule::receive(uint8_t &sensor_id, MeasureData &data) { +void TransmissionModule::decrypt(uint8_t *data, size_t data_size, uint8_t *output_data) +{ + size_t posn, len, inc = 5; + cipher->clear(); + cipher->setKey(encryptionKey, key_size); + cipher->setIV(iv, iv_size); + memset(buffer, 0xBA, sizeof(buffer)); + + for (posn = 0; posn < auth_size; posn += inc) { + len = auth_size - posn; + if (len > inc) + len = inc; + cipher->addAuthData(authdata + posn, len); + } + + for (posn = 0; posn < data_size; posn += inc) { + len = data_size - posn; + if (len > inc) + len = inc; + cipher->decrypt(buffer + posn, data + posn, len); + } + + memcpy(output_data, buffer, data_size); +} + +bool TransmissionModule::receive(uint8_t &sensor_id, MeasureData &data) +{ uint8_t buf[TOTAL_LENGTH]; if (!transmitterModule->receive(buf, TOTAL_LENGTH)) { return false; diff --git a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h index a782453..92efcf5 100644 --- a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h +++ b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h @@ -1,17 +1,39 @@ +#ifndef KEMPT_KINKAJOU_TRANSMISSIONMODULE_H +#define KEMPT_KINKAJOU_TRANSMISSIONMODULE_H + #include +#include +#include #include "MeasureData.h" #include "TransmitterModule.h" #include "ReedSolomonModule.h" -#ifndef KEMPT_KINKAJOU_TRANSMISSIONMODULE_H -#define KEMPT_KINKAJOU_TRANSMISSIONMODULE_H +#define MAX_PLAINTEXT_LEN 265 class TransmissionModule { private: TransmitterModule * transmitterModule; ReedSolomonModule * reedSolomonModule; + ChaChaPoly* cipher; + uint8_t encryptionKey[16] = {0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f}; + uint8_t authdata[16] = {0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, + 0xc4, 0xc5, 0xc6, 0xc7}; + + uint8_t iv[16] = {0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, + 0x44, 0x45, 0x46, 0x47}; + uint8_t buffer[MAX_PLAINTEXT_LEN]; + size_t key_size = 16; + size_t auth_size = 12; + // size_t data_size = 4; + size_t iv_size = 12; public: - TransmissionModule(TransmitterModule &transmitterModule, ReedSolomonModule &reedSolomonModule) : transmitterModule(&transmitterModule), reedSolomonModule(&reedSolomonModule) {} + TransmissionModule(TransmitterModule &transmitterModule, ReedSolomonModule &reedSolomonModule) : + transmitterModule(&transmitterModule), + reedSolomonModule(&reedSolomonModule) { + cipher = new ChaChaPoly(); + } + void decrypt(uint8_t * data, size_t data_size, uint8_t* output_data); bool receive(uint8_t &sendor_id, MeasureData &data); }; diff --git a/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h b/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h index 6f61ef1..b57f4b6 100644 --- a/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h +++ b/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h @@ -1,12 +1,13 @@ // // Created by o.likhogub on 10/7/2023. // +#ifndef KEMPT_KINKAJOU_TRANSMITTERMODULE_H +#define KEMPT_KINKAJOU_TRANSMITTERMODULE_H #include "Arduino.h" #include #include -#ifndef KEMPT_KINKAJOU_TRANSMITTERMODULE_H -#define KEMPT_KINKAJOU_TRANSMITTERMODULE_H + class TransmitterModule { private: diff --git a/cybergarden-aggregator/lib/UuidModule/Uuid.cpp b/cybergarden-aggregator/lib/UuidModule/Uuid.cpp index 9dd81a9..f25836a 100644 --- a/cybergarden-aggregator/lib/UuidModule/Uuid.cpp +++ b/cybergarden-aggregator/lib/UuidModule/Uuid.cpp @@ -8,7 +8,7 @@ // https://en.wikipedia.org/wiki/Universally_unique_identifier -#include "UUID.h" +#include "Uuid.h" UUID::UUID() diff --git a/cybergarden-aggregator/platformio.ini b/cybergarden-aggregator/platformio.ini index 9f9afbf..9b58c2e 100644 --- a/cybergarden-aggregator/platformio.ini +++ b/cybergarden-aggregator/platformio.ini @@ -16,3 +16,4 @@ monitor_speed = 115200 lib_deps = mikem/RadioHead@^1.120 bblanchon/ArduinoJson@^6.21.3 + rweather/Crypto@^0.4.0 From f918be204c0c9ba9100753ab111223bb95e3fc04 Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 03:07:13 +0300 Subject: [PATCH 06/10] refactor --- .../TransmissionModule/TransmissionModule.cpp | 38 +++++++++---------- .../TransmissionModule/TransmissionModule.h | 1 - 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp index 5e6818f..7ee88f4 100644 --- a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.cpp @@ -8,8 +8,22 @@ #define ECC_LENGTH 4 #define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH) -void TransmissionModule::decrypt(uint8_t *data, size_t data_size, uint8_t *output_data) + + +bool TransmissionModule::receive(uint8_t &sensor_id, MeasureData &data) { + uint8_t buf[TOTAL_LENGTH]; + if (!transmitterModule->receive(buf, TOTAL_LENGTH)) { + return false; + } + + uint8_t msg[MSG_LENGTH]; + if (!reedSolomonModule->decode(buf, msg)) { + return false; + } + + sensor_id = msg[0]; + size_t posn, len, inc = 5; cipher->clear(); cipher->setKey(encryptionKey, key_size); @@ -23,30 +37,16 @@ void TransmissionModule::decrypt(uint8_t *data, size_t data_size, uint8_t *outpu cipher->addAuthData(authdata + posn, len); } + size_t data_size = 4; + for (posn = 0; posn < data_size; posn += inc) { len = data_size - posn; if (len > inc) len = inc; - cipher->decrypt(buffer + posn, data + posn, len); + cipher->decrypt(buffer + posn, msg + 1 + posn, len); } - memcpy(output_data, buffer, data_size); -} - -bool TransmissionModule::receive(uint8_t &sensor_id, MeasureData &data) -{ - uint8_t buf[TOTAL_LENGTH]; - if (!transmitterModule->receive(buf, TOTAL_LENGTH)) { - return false; - } - - uint8_t msg[MSG_LENGTH]; - if (!reedSolomonModule->decode(buf, msg)) { - return false; - } - - sensor_id = msg[0]; - memcpy((void *) &data, msg+1, 4); + memcpy((void *) &data, buffer, 4); return true; } diff --git a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h index 92efcf5..8fb9a43 100644 --- a/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h +++ b/cybergarden-aggregator/lib/TransmissionModule/TransmissionModule.h @@ -33,7 +33,6 @@ public: reedSolomonModule(&reedSolomonModule) { cipher = new ChaChaPoly(); } - void decrypt(uint8_t * data, size_t data_size, uint8_t* output_data); bool receive(uint8_t &sendor_id, MeasureData &data); }; From cd336cab1a82044138eb0ba22ed1013e0ae5b9ba Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 03:16:38 +0300 Subject: [PATCH 07/10] sensor refactor --- .../lib/TransmissionModule/TransmissionModule.cpp | 13 +++++-------- .../lib/TransmissionModule/TransmissionModule.h | 1 - cybergarden-sensor/src/main.cpp | 1 - 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp index 2e4e09f..96191b5 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp @@ -8,9 +8,11 @@ #define ECC_LENGTH 4 #define TOTAL_LENGTH (MSG_LENGTH + ECC_LENGTH) -void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t* output_data) +void TransmissionModule::transmit(MeasureData &data) { + size_t posn, len, inc = 5; + size_t data_size = 4; cipher->clear(); cipher->setKey(encryptionKey, key_size); cipher->setIV(iv, iv_size); @@ -27,7 +29,7 @@ void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t len = data_size - posn; if (len > inc) len = inc; - cipher->encrypt(buffer + posn, input_data + posn, len); + cipher->encrypt(buffer + posn, (uint8_t*)&data + 1 + posn, len); } //************************* @@ -47,17 +49,12 @@ void TransmissionModule::encrypt(uint8_t * input_data, size_t data_size, uint8_t // cipher->decrypt(buffer2 + posn, buffer + posn, len); // } - // if (memcmp(buffer2, input_data, data_size) != 0) { + // if (memcmp(buffer2, (uint8_t*)&data + 1, data_size) != 0) { // Serial.println("doesn't work"); // } else{ // Serial.println("works"); // } - memcpy(output_data, buffer, data_size); -} - -void TransmissionModule::transmit(MeasureData &data) -{ uint8_t msg[MSG_LENGTH]; msg[0] = data.sensor_id; memcpy(msg + 1, (void *) &data, 4); diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h index 247b77d..2ac2156 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.h @@ -37,7 +37,6 @@ public: reedSolomonModule(&reedSolomonModule) { cipher = new ChaChaPoly(); } - void encrypt(uint8_t * data, size_t data_size, uint8_t* output_data); void transmit(MeasureData &data); }; diff --git a/cybergarden-sensor/src/main.cpp b/cybergarden-sensor/src/main.cpp index 508530b..d2c64f8 100644 --- a/cybergarden-sensor/src/main.cpp +++ b/cybergarden-sensor/src/main.cpp @@ -21,7 +21,6 @@ void loop() { measureData.sensor_id = 0xA5; measureData.sensor_type = 0x01; measureData.payload = getMockSensorData(); - transmissionModule.encrypt((uint8_t*)&measureData+1, 4, (uint8_t*)&measureData+1); transmissionModule.transmit(measureData); delay(1000); } \ No newline at end of file From 97528622db85b49e72e1ac997aebc70d2637ff10 Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 05:36:42 +0300 Subject: [PATCH 08/10] no temp, others work --- cybergarden-sensor/lib/Sensors/Sensors.cpp | 31 +++++++++++++++++ cybergarden-sensor/lib/Sensors/Sensors.h | 39 ++++++++++++++++++++++ cybergarden-sensor/platformio.ini | 4 +++ cybergarden-sensor/src/main.cpp | 11 +++++- 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 cybergarden-sensor/lib/Sensors/Sensors.cpp create mode 100644 cybergarden-sensor/lib/Sensors/Sensors.h diff --git a/cybergarden-sensor/lib/Sensors/Sensors.cpp b/cybergarden-sensor/lib/Sensors/Sensors.cpp new file mode 100644 index 0000000..904a4b1 --- /dev/null +++ b/cybergarden-sensor/lib/Sensors/Sensors.cpp @@ -0,0 +1,31 @@ +#include + + +float Sensor::temperatureSensor(){ + tempSens->requestTemperatures(); + while (!tempSens->isConversionComplete()); + float temp = tempSens->getTempCByIndex(1); + return temp; +} + +float Sensor::humiditySensor(){ + float h = dht->readHumidity(); + if (isnan(h)) { + return -1; + } + else{ + return h; + } +} + +int Sensor::photoSensor(){ + int value = analogRead(PHOTO_SENS); + return value; +} + +int Sensor::vibroSensor(){ + int value = analogRead(VIBRO_SENS); + return value; +} + + diff --git a/cybergarden-sensor/lib/Sensors/Sensors.h b/cybergarden-sensor/lib/Sensors/Sensors.h new file mode 100644 index 0000000..6faeec6 --- /dev/null +++ b/cybergarden-sensor/lib/Sensors/Sensors.h @@ -0,0 +1,39 @@ +#pragma once +#include "Arduino.h" +#include "DallasTemperature.h" +#include "DHT.h" + +/////////////////////////////////////////////////////// +// Pins Definitions +/////////////////////////////////////////////////////// + +#define HUM_SENS 33 +#define TEMP_SENS 32 +#define PHOTO_SENS 25 +#define VIBRO_SENS 26 +class Sensor{ + + private: + // KY-001 + OneWire *oneWire; + DallasTemperature *tempSens; + //KY-015 + DHT *dht; + + public: + float temperatureSensor(); + float humiditySensor(); + int photoSensor(); + int vibroSensor(); + +Sensor() + { + oneWire = new OneWire(TEMP_SENS); + tempSens = new DallasTemperature(oneWire); + dht = new DHT(HUM_SENS, DHT11); + tempSens->begin(); + dht->begin(); + } + + +}; diff --git a/cybergarden-sensor/platformio.ini b/cybergarden-sensor/platformio.ini index 203b30b..1084f9e 100644 --- a/cybergarden-sensor/platformio.ini +++ b/cybergarden-sensor/platformio.ini @@ -16,3 +16,7 @@ monitor_speed = 115200 lib_deps = mikem/RadioHead@^1.120 rweather/Crypto@^0.4.0 + paulstoffregen/OneWire@^2.3.7 + milesburton/DallasTemperature@^3.11.0 + adafruit/DHT sensor library@^1.4.4 + adafruit/Adafruit Unified Sensor@^1.1.13 diff --git a/cybergarden-sensor/src/main.cpp b/cybergarden-sensor/src/main.cpp index d2c64f8..e23b2c1 100644 --- a/cybergarden-sensor/src/main.cpp +++ b/cybergarden-sensor/src/main.cpp @@ -2,10 +2,12 @@ #include "TransmitterModule.h" #include "TransmissionModule.h" #include "ReedSolomonModule.h" +#include "Sensors.h" TransmitterModule transmitterModule; ReedSolomonModule reedSolomonModule; TransmissionModule transmissionModule(transmitterModule, reedSolomonModule); +Sensor sensors; void setup() { Serial.begin(115200); @@ -21,6 +23,13 @@ void loop() { measureData.sensor_id = 0xA5; measureData.sensor_type = 0x01; measureData.payload = getMockSensorData(); + Serial.print(sensors.humiditySensor(), DEC); + Serial.print(" "); + Serial.print(sensors.temperatureSensor(), DEC); + Serial.print(" "); + Serial.print(sensors.photoSensor(), DEC); + Serial.print(" "); + Serial.println(sensors.vibroSensor(), DEC); transmissionModule.transmit(measureData); - delay(1000); + delay(100); } \ No newline at end of file From 7820f4fb5ee95ad1f345dc67555ddc946a790171 Mon Sep 17 00:00:00 2001 From: "o.likhogub" Date: Sun, 8 Oct 2023 08:16:16 +0300 Subject: [PATCH 09/10] Fixed encryption/decryption --- .../lib/TransmitterModule/TransmitterModule.h | 2 +- cybergarden-aggregator/platformio.ini | 1 - cybergarden-aggregator/src/main.cpp | 15 +++++++++------ .../lib/TransmissionModule/TransmissionModule.cpp | 13 +++---------- .../lib/TransmitterModule/TransmitterModule.h | 4 ++-- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h b/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h index b57f4b6..79ece7c 100644 --- a/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h +++ b/cybergarden-aggregator/lib/TransmitterModule/TransmitterModule.h @@ -14,7 +14,7 @@ private: RH_ASK *rhAsk; public: TransmitterModule() { - rhAsk = new RH_ASK(2000, 4, 5, 0); + rhAsk = new RH_ASK(2000, 5, 4, 0); } bool init() { return rhAsk->init(); diff --git a/cybergarden-aggregator/platformio.ini b/cybergarden-aggregator/platformio.ini index 9b58c2e..24bbb6d 100644 --- a/cybergarden-aggregator/platformio.ini +++ b/cybergarden-aggregator/platformio.ini @@ -15,5 +15,4 @@ framework = arduino monitor_speed = 115200 lib_deps = mikem/RadioHead@^1.120 - bblanchon/ArduinoJson@^6.21.3 rweather/Crypto@^0.4.0 diff --git a/cybergarden-aggregator/src/main.cpp b/cybergarden-aggregator/src/main.cpp index dee7707..fbdf17e 100644 --- a/cybergarden-aggregator/src/main.cpp +++ b/cybergarden-aggregator/src/main.cpp @@ -12,7 +12,6 @@ #endif #include "Uuid.h" -#include String serverName = "http://gw.cg.k-lab.su/api/v1/measures/register"; @@ -99,9 +98,13 @@ bool sendPayload(MeasureData &data) { MeasureData data; void loop() { - data.sensor_id = 0; - data.sensor_type = 1; - data.payload = rand() & 0xffff; - sendPayload(data); - delay(5000); + if (transmissionModule.receive(sensor_id, data)) { + data.sensor_id = sensor_id; + data.sensor_type = data.sensor_type; + data.payload = data.payload; + analogWrite(LED_BUILTIN, 255); + sendPayload(data); + delay(100); + analogWrite(LED_BUILTIN, 0); + } } \ No newline at end of file diff --git a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp index 96191b5..061a01b 100644 --- a/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp +++ b/cybergarden-sensor/lib/TransmissionModule/TransmissionModule.cpp @@ -25,12 +25,8 @@ void TransmissionModule::transmit(MeasureData &data) cipher->addAuthData(authdata + posn, len); } - for (posn = 0; posn < data_size; posn += inc) { - len = data_size - posn; - if (len > inc) - len = inc; - cipher->encrypt(buffer + posn, (uint8_t*)&data + 1 + posn, len); - } + cipher->encrypt(buffer + 1, (uint8_t*)&data, data_size); + buffer[0] = data.sensor_id; //************************* //Encryption Testing @@ -55,10 +51,7 @@ void TransmissionModule::transmit(MeasureData &data) // Serial.println("works"); // } - uint8_t msg[MSG_LENGTH]; - msg[0] = data.sensor_id; - memcpy(msg + 1, (void *) &data, 4); uint8_t encoded[MSG_LENGTH + ECC_LENGTH]; - reedSolomonModule->encode(msg, encoded); + reedSolomonModule->encode(buffer, encoded); transmitterModule->send(encoded, MSG_LENGTH + ECC_LENGTH); } diff --git a/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h b/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h index 94bdae9..6fae96f 100644 --- a/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h +++ b/cybergarden-sensor/lib/TransmitterModule/TransmitterModule.h @@ -8,8 +8,8 @@ #ifndef KEMPT_KINKAJOU_TRANSMITTERMODULE_H #define KEMPT_KINKAJOU_TRANSMITTERMODULE_H -#define RH_TRANSMIT_PIN 5 -#define RH_RECEIVE_PIN 4 +#define RH_TRANSMIT_PIN 4 +#define RH_RECEIVE_PIN 5 class TransmitterModule { private: From 8624f652f23f2b9975eed7a6b1ae43b7e2304fee Mon Sep 17 00:00:00 2001 From: 033zhek <033zhek@gmail.com> Date: Sun, 8 Oct 2023 08:55:50 +0300 Subject: [PATCH 10/10] changed temperature sensor --- cybergarden-sensor/lib/Sensors/Sensors.cpp | 9 ++--- cybergarden-sensor/lib/Sensors/Sensors.h | 10 +----- cybergarden-sensor/src/main.cpp | 38 +++++++++++++++++++--- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/cybergarden-sensor/lib/Sensors/Sensors.cpp b/cybergarden-sensor/lib/Sensors/Sensors.cpp index 904a4b1..9fd7591 100644 --- a/cybergarden-sensor/lib/Sensors/Sensors.cpp +++ b/cybergarden-sensor/lib/Sensors/Sensors.cpp @@ -1,15 +1,12 @@ #include -float Sensor::temperatureSensor(){ - tempSens->requestTemperatures(); - while (!tempSens->isConversionComplete()); - float temp = tempSens->getTempCByIndex(1); - return temp; +float Sensor::temperatureSensor(){ + return analogRead(TEMP_SENS); } float Sensor::humiditySensor(){ - float h = dht->readHumidity(); + float h = dht->readHumidity(); if (isnan(h)) { return -1; } diff --git a/cybergarden-sensor/lib/Sensors/Sensors.h b/cybergarden-sensor/lib/Sensors/Sensors.h index 6faeec6..446d320 100644 --- a/cybergarden-sensor/lib/Sensors/Sensors.h +++ b/cybergarden-sensor/lib/Sensors/Sensors.h @@ -1,7 +1,7 @@ #pragma once #include "Arduino.h" -#include "DallasTemperature.h" #include "DHT.h" +#include "math.h" /////////////////////////////////////////////////////// // Pins Definitions @@ -14,9 +14,6 @@ class Sensor{ private: - // KY-001 - OneWire *oneWire; - DallasTemperature *tempSens; //KY-015 DHT *dht; @@ -28,12 +25,7 @@ class Sensor{ Sensor() { - oneWire = new OneWire(TEMP_SENS); - tempSens = new DallasTemperature(oneWire); dht = new DHT(HUM_SENS, DHT11); - tempSens->begin(); dht->begin(); } - - }; diff --git a/cybergarden-sensor/src/main.cpp b/cybergarden-sensor/src/main.cpp index e23b2c1..d3d579f 100644 --- a/cybergarden-sensor/src/main.cpp +++ b/cybergarden-sensor/src/main.cpp @@ -19,10 +19,38 @@ uint16_t getMockSensorData() { } void loop() { - MeasureData measureData; - measureData.sensor_id = 0xA5; - measureData.sensor_type = 0x01; - measureData.payload = getMockSensorData(); + MeasureData measureData_temp; + measureData_temp.sensor_id = 0xA5; + measureData_temp.sensor_type = 0x01; + measureData_temp.payload = sensors.temperatureSensor(); + transmissionModule.transmit(measureData_temp); + + delay(100); + + MeasureData measureData_hum; + measureData_hum.sensor_id = 0xA5; + measureData_hum.sensor_type = 0x02; + measureData_hum.payload = sensors.humiditySensor(); + transmissionModule.transmit(measureData_hum); + + delay(100); + + MeasureData measureData_photo; + measureData_photo.sensor_id = 0xA5; + measureData_photo.sensor_type = 0x03; + measureData_photo.payload = sensors.photoSensor(); + transmissionModule.transmit(measureData_photo); + + delay(100); + + MeasureData measureData_vibro; + measureData_vibro.sensor_id = 0xA5; + measureData_vibro.sensor_type = 0x04; + measureData_vibro.payload = sensors.photoSensor(); + transmissionModule.transmit(measureData_vibro); + + delay(100); + Serial.print(sensors.humiditySensor(), DEC); Serial.print(" "); Serial.print(sensors.temperatureSensor(), DEC); @@ -30,6 +58,6 @@ void loop() { Serial.print(sensors.photoSensor(), DEC); Serial.print(" "); Serial.println(sensors.vibroSensor(), DEC); - transmissionModule.transmit(measureData); + delay(100); } \ No newline at end of file