Compare commits
No commits in common. "main" and "v1.0" have entirely different histories.
29
README.md
29
README.md
@ -1,6 +1,6 @@
|
|||||||
# RF water leakage sensor
|
# RF water leakage sensor
|
||||||
|
|
||||||
Water leakage sensor on ATmega168/328 + nRF24.
|
Water leakage sensor on ATmega168/328 + RF24.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@ -11,6 +11,29 @@ Water leakage sensor on ATmega168/328 + nRF24.
|
|||||||
|
|
||||||
## Note
|
## Note
|
||||||
|
|
||||||
A gateway is required. For details see [RF Gateway](https://github.com/aZholtikov/RF-Gateway).
|
A gateway is required. For details see "RF Gateway".
|
||||||
|
|
||||||
Any feedback via [e-mail](mailto:github@zh.com.ru) would be appreciated. Or... [Buy me a coffee](https://paypal.me/aZholtikov).
|
## Full config example for Home Assistant
|
||||||
|
|
||||||
|
```yml
|
||||||
|
mqtt:
|
||||||
|
sensor:
|
||||||
|
- name: "NAME"
|
||||||
|
device_class: "voltage"
|
||||||
|
unit_of_measurement: "V"
|
||||||
|
state_topic: "homeassistant/rf_sensor/ID/water_leakage"
|
||||||
|
value_template: "{{ value_json.battery }}"
|
||||||
|
expire_after: 4500
|
||||||
|
force_update: true
|
||||||
|
qos: 2
|
||||||
|
binary_sensor:
|
||||||
|
- name: "NAME"
|
||||||
|
device_class: "moisture"
|
||||||
|
state_topic: "homeassistant/rf_sensor/ID/water_leakage"
|
||||||
|
value_template: "{{ value_json.state }}"
|
||||||
|
payload_on: "ALARM"
|
||||||
|
payload_off: "DRY"
|
||||||
|
expire_after: 4500
|
||||||
|
force_update: true
|
||||||
|
qos: 2
|
||||||
|
```
|
||||||
|
@ -6,7 +6,10 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
|
-D ALARM=1
|
||||||
|
-D DRY=0
|
||||||
-D portUSE_WDTO=WDTO_1S
|
-D portUSE_WDTO=WDTO_1S
|
||||||
|
-D WATER_LEAKAGE=5
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -15,9 +18,8 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/aZholtikov/ZHConfig
|
feilipu/FreeRTOS@^10.5.0-0
|
||||||
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
nrf24/RF24@^1.4.5
|
||||||
https://github.com/nRF24/RF24
|
|
||||||
|
|
||||||
[env:ATmega168P]
|
[env:ATmega168P]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -27,7 +29,10 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
|
-D ALARM=1
|
||||||
|
-D DRY=0
|
||||||
-D portUSE_WDTO=WDTO_1S
|
-D portUSE_WDTO=WDTO_1S
|
||||||
|
-D WATER_LEAKAGE=5
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -36,9 +41,8 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/aZholtikov/ZHConfig
|
feilipu/FreeRTOS@^10.5.0-0
|
||||||
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
nrf24/RF24@^1.4.5
|
||||||
https://github.com/nRF24/RF24
|
|
||||||
|
|
||||||
[env:ATmega168PB]
|
[env:ATmega168PB]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -48,7 +52,10 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
|
-D ALARM=1
|
||||||
|
-D DRY=0
|
||||||
-D portUSE_WDTO=WDTO_1S
|
-D portUSE_WDTO=WDTO_1S
|
||||||
|
-D WATER_LEAKAGE=5
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -57,9 +64,8 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/aZholtikov/ZHConfig
|
feilipu/FreeRTOS@^10.5.0-0
|
||||||
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
nrf24/RF24@^1.4.5
|
||||||
https://github.com/nRF24/RF24
|
|
||||||
|
|
||||||
[env:ATmega328]
|
[env:ATmega328]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -69,7 +75,10 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
|
-D ALARM=1
|
||||||
|
-D DRY=0
|
||||||
-D portUSE_WDTO=WDTO_1S
|
-D portUSE_WDTO=WDTO_1S
|
||||||
|
-D WATER_LEAKAGE=5
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -78,9 +87,8 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/aZholtikov/ZHConfig
|
feilipu/FreeRTOS@^10.5.0-0
|
||||||
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
nrf24/RF24@^1.4.5
|
||||||
https://github.com/nRF24/RF24
|
|
||||||
|
|
||||||
[env:ATmega328P]
|
[env:ATmega328P]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -90,7 +98,10 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
|
-D ALARM=1
|
||||||
|
-D DRY=0
|
||||||
-D portUSE_WDTO=WDTO_1S
|
-D portUSE_WDTO=WDTO_1S
|
||||||
|
-D WATER_LEAKAGE=5
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -99,9 +110,8 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/aZholtikov/ZHConfig
|
feilipu/FreeRTOS@^10.5.0-0
|
||||||
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
nrf24/RF24@^1.4.5
|
||||||
https://github.com/nRF24/RF24
|
|
||||||
|
|
||||||
[env:ATmega328PB]
|
[env:ATmega328PB]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -111,7 +121,10 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
|
-D ALARM=1
|
||||||
|
-D DRY=0
|
||||||
-D portUSE_WDTO=WDTO_1S
|
-D portUSE_WDTO=WDTO_1S
|
||||||
|
-D WATER_LEAKAGE=5
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -120,6 +133,5 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
https://github.com/aZholtikov/ZHConfig
|
feilipu/FreeRTOS@^10.5.0-0
|
||||||
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
nrf24/RF24@^1.4.5
|
||||||
https://github.com/nRF24/RF24
|
|
43
src/main.cpp
43
src/main.cpp
@ -3,23 +3,27 @@
|
|||||||
#include "RF24.h"
|
#include "RF24.h"
|
||||||
#include "avr/interrupt.h"
|
#include "avr/interrupt.h"
|
||||||
#include "semphr.h"
|
#include "semphr.h"
|
||||||
#include "EEPROM.h"
|
|
||||||
#include "ZHConfig.h"
|
typedef struct
|
||||||
|
{
|
||||||
|
int16_t sensor_id{0};
|
||||||
|
int16_t sensor_type{0};
|
||||||
|
int16_t value_1{0};
|
||||||
|
int16_t value_2{0};
|
||||||
|
int16_t value_3{0};
|
||||||
|
int16_t value_4{0};
|
||||||
|
int16_t value_5{0};
|
||||||
|
} transmitted_data_t;
|
||||||
|
|
||||||
void sendBatteryLevelCharge(void *pvParameters);
|
void sendBatteryLevelCharge(void *pvParameters);
|
||||||
void sendAlarmStatus(void *pvParameters);
|
void sendAlarmStatus(void *pvParameters);
|
||||||
float getBatteryLevelCharge(void);
|
float getBatteryLevelCharge(void);
|
||||||
void loadId(void);
|
|
||||||
|
|
||||||
int16_t id{abs((int16_t)ID)};
|
|
||||||
|
|
||||||
RF24 radio(9, 10);
|
RF24 radio(9, 10);
|
||||||
TaskHandle_t xSendBatteryLevelCharge;
|
TaskHandle_t xSendBatteryLevelCharge;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
loadId();
|
|
||||||
|
|
||||||
EICRA |= (1 << ISC01) | (1 << ISC00);
|
EICRA |= (1 << ISC01) | (1 << ISC00);
|
||||||
EIMSK |= (1 << INT0);
|
EIMSK |= (1 << INT0);
|
||||||
ADCSRA &= ~(1 << ADEN);
|
ADCSRA &= ~(1 << ADEN);
|
||||||
@ -51,12 +55,12 @@ void sendBatteryLevelCharge(void *pvParameters)
|
|||||||
(void)pvParameters;
|
(void)pvParameters;
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
rf_transmitted_data_t sensor{id, RFST_WATER_LEAKAGE};
|
transmitted_data_t sensor{abs((int16_t)ID), WATER_LEAKAGE};
|
||||||
sensor.value_1 = getBatteryLevelCharge() * 100;
|
sensor.value_1 = getBatteryLevelCharge() * 100;
|
||||||
sensor.value_2 = DRY;
|
sensor.value_2 = DRY;
|
||||||
radio.powerUp();
|
radio.powerUp();
|
||||||
radio.flush_tx();
|
radio.flush_tx();
|
||||||
radio.write(&sensor, sizeof(rf_transmitted_data_t));
|
radio.write(&sensor, sizeof(transmitted_data_t));
|
||||||
radio.powerDown();
|
radio.powerDown();
|
||||||
vTaskDelay(3600);
|
vTaskDelay(3600);
|
||||||
}
|
}
|
||||||
@ -68,11 +72,11 @@ void sendAlarmStatus(void *pvParameters)
|
|||||||
(void)pvParameters;
|
(void)pvParameters;
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
rf_transmitted_data_t sensor{id, RFST_WATER_LEAKAGE};
|
transmitted_data_t sensor{abs((int16_t)ID), WATER_LEAKAGE};
|
||||||
sensor.value_2 = ALARM;
|
sensor.value_2 = ALARM;
|
||||||
radio.powerUp();
|
radio.powerUp();
|
||||||
radio.flush_tx();
|
radio.flush_tx();
|
||||||
radio.write(&sensor, sizeof(rf_transmitted_data_t));
|
radio.write(&sensor, sizeof(transmitted_data_t));
|
||||||
radio.powerDown();
|
radio.powerDown();
|
||||||
vTaskDelay(60);
|
vTaskDelay(60);
|
||||||
}
|
}
|
||||||
@ -88,21 +92,8 @@ float getBatteryLevelCharge()
|
|||||||
while (bit_is_set(ADCSRA, ADSC))
|
while (bit_is_set(ADCSRA, ADSC))
|
||||||
;
|
;
|
||||||
ADCSRA &= ~(1 << ADEN);
|
ADCSRA &= ~(1 << ADEN);
|
||||||
return (1024 * 1.1) / (ADCL + ADCH * 256);
|
float value = ((1024 * 1.1) / (ADCL + ADCH * 256));
|
||||||
}
|
return value;
|
||||||
|
|
||||||
void loadId()
|
|
||||||
{
|
|
||||||
cli();
|
|
||||||
if (EEPROM.read(511) == 254)
|
|
||||||
EEPROM.get(0, id);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
EEPROM.write(511, 254);
|
|
||||||
EEPROM.put(0, id);
|
|
||||||
}
|
|
||||||
delay(50);
|
|
||||||
sei();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(INT0_vect)
|
ISR(INT0_vect)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user