Version 1.1
Added saving device ID to EEPROM.
This commit is contained in:
parent
1d1c2b0dbe
commit
17c5428779
18
README.md
18
README.md
@ -1,6 +1,6 @@
|
|||||||
# RF touch switch
|
# RF touch switch
|
||||||
|
|
||||||
Touch switch on ATmega168/328 + RF24.
|
Touch switch on ATmega168/328 + nRF24.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@ -11,18 +11,6 @@ Touch switch on ATmega168/328 + RF24.
|
|||||||
|
|
||||||
## Note
|
## Note
|
||||||
|
|
||||||
A gateway is required. For details see "RF Gateway".
|
A gateway is required. For details see [RF Gateway](https://github.com/aZholtikov/RF-Gateway).
|
||||||
|
|
||||||
## Full config example for Home Assistant
|
Any feedback via [e-mail](mailto:github@zh.com.ru) would be appreciated. Or... [Buy me a coffee](https://paypal.me/aZholtikov).
|
||||||
|
|
||||||
```yml
|
|
||||||
mqtt:
|
|
||||||
sensor:
|
|
||||||
- name: "NAME"
|
|
||||||
device_class: "voltage"
|
|
||||||
unit_of_measurement: "V"
|
|
||||||
state_topic: "homeassistant/rf_sensor/ID/touch_switch"
|
|
||||||
value_template: "{{ value_json.battery }}"
|
|
||||||
force_update: true
|
|
||||||
qos: 2
|
|
||||||
```
|
|
||||||
|
@ -6,7 +6,6 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
-D TOUCH_SWITCH=4
|
|
||||||
-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,8 +14,9 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
feilipu/FreeRTOS@^10.5.0-0
|
https://github.com/aZholtikov/ZHConfig
|
||||||
nrf24/RF24@^1.4.5
|
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
||||||
|
https://github.com/nRF24/RF24
|
||||||
|
|
||||||
[env:ATmega168P]
|
[env:ATmega168P]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -26,7 +26,6 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
-D TOUCH_SWITCH=4
|
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -35,8 +34,9 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
feilipu/FreeRTOS@^10.5.0-0
|
https://github.com/aZholtikov/ZHConfig
|
||||||
nrf24/RF24@^1.4.5
|
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
||||||
|
https://github.com/nRF24/RF24
|
||||||
|
|
||||||
[env:ATmega168PB]
|
[env:ATmega168PB]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -46,7 +46,6 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
-D TOUCH_SWITCH=4
|
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -55,8 +54,9 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
feilipu/FreeRTOS@^10.5.0-0
|
https://github.com/aZholtikov/ZHConfig
|
||||||
nrf24/RF24@^1.4.5
|
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
||||||
|
https://github.com/nRF24/RF24
|
||||||
|
|
||||||
[env:ATmega328]
|
[env:ATmega328]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -66,7 +66,6 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
-D TOUCH_SWITCH=4
|
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -75,9 +74,9 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
-D TOUCH_SWITCH=4
|
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
|
||||||
@ -87,7 +86,6 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
-D TOUCH_SWITCH=4
|
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -96,8 +94,9 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
feilipu/FreeRTOS@^10.5.0-0
|
https://github.com/aZholtikov/ZHConfig
|
||||||
nrf24/RF24@^1.4.5
|
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
||||||
|
https://github.com/nRF24/RF24
|
||||||
|
|
||||||
[env:ATmega328PB]
|
[env:ATmega328PB]
|
||||||
platform = atmelavr
|
platform = atmelavr
|
||||||
@ -107,7 +106,6 @@ board_build.f_cpu = 8000000L
|
|||||||
framework = arduino
|
framework = arduino
|
||||||
upload_protocol = usbasp
|
upload_protocol = usbasp
|
||||||
build_flags =
|
build_flags =
|
||||||
-D TOUCH_SWITCH=4
|
|
||||||
-D ID=$UNIX_TIME
|
-D ID=$UNIX_TIME
|
||||||
board_fuses.hfuse = 0xDE
|
board_fuses.hfuse = 0xDE
|
||||||
board_fuses.lfuse = 0xE2
|
board_fuses.lfuse = 0xE2
|
||||||
@ -116,5 +114,6 @@ upload_flags =
|
|||||||
-Pusb
|
-Pusb
|
||||||
-e
|
-e
|
||||||
lib_deps =
|
lib_deps =
|
||||||
feilipu/FreeRTOS@^10.5.0-0
|
https://github.com/aZholtikov/ZHConfig
|
||||||
nrf24/RF24@^1.4.5
|
https://github.com/feilipu/Arduino_FreeRTOS_Library
|
||||||
|
https://github.com/nRF24/RF24
|
39
src/main.cpp
39
src/main.cpp
@ -3,26 +3,22 @@
|
|||||||
#include "RF24.h"
|
#include "RF24.h"
|
||||||
#include "avr/interrupt.h"
|
#include "avr/interrupt.h"
|
||||||
#include "semphr.h"
|
#include "semphr.h"
|
||||||
|
#include "EEPROM.h"
|
||||||
typedef struct
|
#include "ZHConfig.h"
|
||||||
{
|
|
||||||
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 sendButtonPushing(void *pvParameters);
|
void sendButtonPushing(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);
|
||||||
SemaphoreHandle_t buttonSemaphore;
|
SemaphoreHandle_t buttonSemaphore;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
loadId();
|
||||||
|
|
||||||
EICRA |= (1 << ISC11) | (1 << ISC10);
|
EICRA |= (1 << ISC11) | (1 << ISC10);
|
||||||
EIMSK |= (1 << INT1);
|
EIMSK |= (1 << INT1);
|
||||||
ADCSRA &= ~(1 << ADEN);
|
ADCSRA &= ~(1 << ADEN);
|
||||||
@ -56,11 +52,11 @@ void sendButtonPushing(void *pvParameters)
|
|||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
xSemaphoreTake(buttonSemaphore, portMAX_DELAY);
|
xSemaphoreTake(buttonSemaphore, portMAX_DELAY);
|
||||||
transmitted_data_t sensor{abs((int16_t)ID), TOUCH_SWITCH};
|
rf_transmitted_data_t sensor{id, RFST_TOUCH_SWITCH};
|
||||||
sensor.value_1 = getBatteryLevelCharge() * 100;
|
sensor.value_1 = getBatteryLevelCharge() * 100;
|
||||||
radio.powerUp();
|
radio.powerUp();
|
||||||
radio.flush_tx();
|
radio.flush_tx();
|
||||||
radio.write(&sensor, sizeof(transmitted_data_t));
|
radio.write(&sensor, sizeof(rf_transmitted_data_t));
|
||||||
radio.powerDown();
|
radio.powerDown();
|
||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
@ -76,8 +72,21 @@ float getBatteryLevelCharge()
|
|||||||
while (bit_is_set(ADCSRA, ADSC))
|
while (bit_is_set(ADCSRA, ADSC))
|
||||||
;
|
;
|
||||||
ADCSRA &= ~(1 << ADEN);
|
ADCSRA &= ~(1 << ADEN);
|
||||||
float value = ((1024 * 1.1) / (ADCL + ADCH * 256));
|
return (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(INT1_vect)
|
ISR(INT1_vect)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user