From 61374b584aab0c0c0742c3578c258bd133600ba2 Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Sun, 2 May 2021 01:20:47 +0200 Subject: [PATCH] restructuring, testing --- sensor433/platformio.ini | 9 +- sensor433/src/config.h.example | 12 +++ sensor433/src/main.cpp | 111 ++++++-------------- sensor433/src/sensor433.cpp | 19 ++++ sensor433/src/sensor433.h | 71 +++++++++++++ sensor433/test/test_desktop/test_sensor.cpp | 36 +++++++ 6 files changed, 178 insertions(+), 80 deletions(-) create mode 100644 sensor433/src/sensor433.cpp create mode 100644 sensor433/src/sensor433.h create mode 100644 sensor433/test/test_desktop/test_sensor.cpp diff --git a/sensor433/platformio.ini b/sensor433/platformio.ini index 01e5dce..935d53f 100644 --- a/sensor433/platformio.ini +++ b/sensor433/platformio.ini @@ -34,4 +34,11 @@ force_verbose = True ; Platformio Project Tasks -> Set Fuses! board_fuses.lfuse = 0xE2 -board_fuses.hfuse = 0xDF \ No newline at end of file +board_fuses.hfuse = 0xDF + +; for unit testing. In Platformio -> Project Tasks open native->Advanced click test. +[env:native] +platform = native +build_flags = -std=gnu++11 +lib_deps = + ArduinoFake \ No newline at end of file diff --git a/sensor433/src/config.h.example b/sensor433/src/config.h.example index fbf4a5f..0ac775a 100644 --- a/sensor433/src/config.h.example +++ b/sensor433/src/config.h.example @@ -1,5 +1,17 @@ #ifndef __CONFIG_H__ #define __CONFIG_H__ +// Device ID, from 0-15: +#define DEVICE_ID 0x01 + +/* + define when to wake up + 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms + 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec +*/ +#define WATCHDOG_WAKEUP 9 + +// Sleep this many again before waging up. Sleep time in s = WATCHDOG_WAKEUP s * WATCHDOG_SLEEP_FURTHER, e.g. 8*8s = 64s +#define WATCHDOG_SLEEP_FURTHER 8 #endif \ No newline at end of file diff --git a/sensor433/src/main.cpp b/sensor433/src/main.cpp index e8c00f2..5785ca6 100644 --- a/sensor433/src/main.cpp +++ b/sensor433/src/main.cpp @@ -7,78 +7,29 @@ https://git.cryhost.de/crycode/attiny85-radiohead-dht22-weather-sensor/-/tree/master */ -// pin with the transistor base (And LED) is connected -#define TRANSISTOR_BASE_PIN 4 -// blink time for the LED -#define LED_TIME 200 - -// pin of the DHT22 sensor -#define DHT_PIN 1 - -// pin of the soil sensor -#define SOIL_PIN A1 - -// the own RadioHead address -#define RH_OWN_ADDR 0xca // 202 - -// RadioHead bitrate in bit/s -#define RH_SPEED 1000 - -/* - define when to wake up - 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms - 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec -*/ -#define WATCHDOG_WAKEUP 9 - -// Sleep this many again before waging up. Sleep time in s = WATCHDOG_WAKEUP s * WATCHDOG_SLEEP_FURTHER, e.g. 8*8s = 64s -#define WATCHDOG_SLEEP_FURTHER 8 - -// pins for the radio hardware -#define RH_RX_PIN 6 // not used, set to a non-existent pin -#define RH_TX_PIN 3 // Transmit Pin -#define RH_PTT_PIN 6 // not used, set to a non-existent pin #include #include #include #include -// for watchdog wakeup -// https://gist.github.com/dwhacks/8055287 -#ifndef cbi -#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) -#endif -#ifndef sbi -#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) -#endif +// RadioHead +#include + // DHT22 lib // not using the current adafruit dht library, because it's too memory intensive #include "dht22.h" -dht22 dht; - // Configuration, see file config.h.example and rename to config.h. Project specific, do not add to git! #include "config.h" -#define RH_BUF_LEN 11 -uint8_t rh_buf[RH_BUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +#include "sensor433.h" -// reduce the RadioHead max message length to save memory -#define RH_ASK_MAX_MESSAGE_LEN RH_BUF_LEN -// RadioHead -#include -// Message Headers: -#define MSG_START 0x00 -#define MSG_BAT 0x03 -#define MSG_HEADER 0x05 -#define MSG_ERR 0xee - -RH_ASK rh_driver(RH_SPEED, RH_RX_PIN, RH_TX_PIN, RH_PTT_PIN); +// global variables: // watchdog flag volatile boolean f_wdt = 1; @@ -86,18 +37,28 @@ volatile boolean f_wdt = 1; // temp / humid toggle volatile uint8_t counter = 0; +dht22 dht; + +uint8_t rh_buf[RH_BUF_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +RH_ASK rh_driver(RH_SPEED, RH_RX_PIN, RH_TX_PIN, RH_PTT_PIN); +// end global variables. + + + // prototypes: -void gotoSleep(void); - -int getVcc(); - -void rh_send(uint8_t len); +void rh_send(uint8_t rh_buf[], uint8_t len); void setup_watchdog(int ii); void system_sleep(); -void blink(uint8_t num); +void gotoSleep(void); + +int getVcc(); + + + void setup() { @@ -120,7 +81,7 @@ void setup() // send start message rh_buf[0] = MSG_START; - rh_send(1); + rh_send(rh_buf,1); setup_watchdog(WATCHDOG_WAKEUP); } @@ -149,17 +110,17 @@ void loop() rh_buf[0] = MSG_BAT; battery = getVcc(); memcpy(&rh_buf[1], &battery, 4); - rh_send(RH_BUF_LEN); + rh_send(rh_buf,RH_BUF_LEN); } else if (dht_read_data(&dht, &t, &h) == 1) { - rh_buf[0] = MSG_HEADER; + rh_buf[0] = MSG_TEMPHUM; memcpy(&rh_buf[1], &t, 4); memcpy(&rh_buf[5], &h, 4); soil = analogRead(SOIL_PIN); memcpy(&rh_buf[9], &soil, 2); - rh_send(RH_BUF_LEN); + rh_send(rh_buf,RH_BUF_LEN); } else { @@ -168,7 +129,7 @@ void loop() digitalWrite(TRANSISTOR_BASE_PIN, HIGH); delay(5); rh_buf[0] = MSG_ERR; - rh_send(1); + rh_send(rh_buf,1); } counter++; @@ -186,21 +147,13 @@ void loop() } } -void blink(uint8_t num) -{ - // blink n times - for (uint8_t i = 0; i < num; i++) - { - digitalWrite(TRANSISTOR_BASE_PIN, HIGH); - _delay_ms(LED_TIME); - digitalWrite(TRANSISTOR_BASE_PIN, LOW); - _delay_ms(LED_TIME); - } -} - // function to send RadioHead messages from the buffer -void rh_send(uint8_t len) +void rh_send(uint8_t rh_buf[],uint8_t len) { +#ifdef DEVICE_ID +rh_buf[0] = add_device_id(rh_buf[0], DEVICE_ID); +#endif + rh_driver.send((uint8_t *)rh_buf, len); rh_driver.waitPacketSent(); } @@ -276,4 +229,4 @@ int getVcc() val = (ADCH << 8) | low; return ((long)1024 * 1100) / val; -} +} \ No newline at end of file diff --git a/sensor433/src/sensor433.cpp b/sensor433/src/sensor433.cpp new file mode 100644 index 0000000..3a758d9 --- /dev/null +++ b/sensor433/src/sensor433.cpp @@ -0,0 +1,19 @@ +#include "sensor433.h" + + +void blink(uint8_t num) +{ + // blink n times + for (uint8_t i = 0; i < num; i++) + { + digitalWrite(TRANSISTOR_BASE_PIN, HIGH); + delay(LED_TIME); + digitalWrite(TRANSISTOR_BASE_PIN, LOW); + delay(LED_TIME); + } +} + +uint8_t add_device_id(uint8_t value, uint8_t device_id) { + return value |= device_id; +} + diff --git a/sensor433/src/sensor433.h b/sensor433/src/sensor433.h new file mode 100644 index 0000000..558a6c6 --- /dev/null +++ b/sensor433/src/sensor433.h @@ -0,0 +1,71 @@ +/** + * Sensor Header lib. + * Definitions and testable functions. + * + **/ + +#ifndef __SENSOR433_H__ +#define __SENSOR433_H__ + +#include + + + +// DHT22 lib +// not using the current adafruit dht library, because it's too memory intensive +#include "dht22.h" + + +#include "config.h" + +// pin with the transistor base (And LED) is connected +#define TRANSISTOR_BASE_PIN 4 + +// blink time for the LED +#define LED_TIME 200 + +// pin of the DHT22 sensor +#define DHT_PIN 1 + +// pin of the soil sensor +#define SOIL_PIN A1 + + +#define RH_BUF_LEN 11 + +// RadioHead bitrate in bit/s +#define RH_SPEED 1000 + +// pins for the radio hardware +#define RH_RX_PIN 6 // not used, set to a non-existent pin +#define RH_TX_PIN 3 // Transmit Pin +#define RH_PTT_PIN 6 // not used, set to a non-existent pin + + +// Message Headers: +#define MSG_START 0x10 +#define MSG_BAT 0x20 +#define MSG_TEMPHUM 0x30 +#define MSG_ERR 0x40 + +// for watchdog wakeup +// https://gist.github.com/dwhacks/8055287 +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + + + + + +// function prototypes: + +void blink(uint8_t num); + +uint8_t add_device_id(uint8_t value, uint8_t device_id); + + +#endif \ No newline at end of file diff --git a/sensor433/test/test_desktop/test_sensor.cpp b/sensor433/test/test_desktop/test_sensor.cpp new file mode 100644 index 0000000..7dc39f1 --- /dev/null +++ b/sensor433/test/test_desktop/test_sensor.cpp @@ -0,0 +1,36 @@ + +#include +#include +#include "config.h" +#include "sensor433.h" + + +// somehow not possible to use method from sensor433.h/cpp, linking fails: +uint8_t add_device_id(uint8_t value, uint8_t device_id) { + return value |= device_id; +} + + + +void test_message_header() { + uint8_t header = MSG_START; + header = add_device_id(header, DEVICE_ID); + + // 0x10 | 0x01 = 0x11 + TEST_ASSERT_EQUAL(0x11, header); + + // header + TEST_ASSERT_EQUAL(0x10, header & 0x10); + + // id + TEST_ASSERT_EQUAL(0x01, header & 0x01); + +} + +int main(int argc, char **argv) { + UNITY_BEGIN(); + RUN_TEST(test_message_header); + UNITY_END(); + + return 0; +} \ No newline at end of file