Version 1.1

Added saving device ID to EEPROM.
This commit is contained in:
Alexey Zholtikov 2023-03-10 11:58:19 +03:00
parent 1d1c2b0dbe
commit 17c5428779
3 changed files with 45 additions and 49 deletions

View File

@ -1,6 +1,6 @@
# RF touch switch
Touch switch on ATmega168/328 + RF24.
Touch switch on ATmega168/328 + nRF24.
## Features
@ -11,18 +11,6 @@ Touch switch on ATmega168/328 + RF24.
## 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
```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
```
Any feedback via [e-mail](mailto:github@zh.com.ru) would be appreciated. Or... [Buy me a coffee](https://paypal.me/aZholtikov).

View File

@ -6,7 +6,6 @@ board_build.f_cpu = 8000000L
framework = arduino
upload_protocol = usbasp
build_flags =
-D TOUCH_SWITCH=4
-D ID=$UNIX_TIME
board_fuses.hfuse = 0xDE
board_fuses.lfuse = 0xE2
@ -15,8 +14,9 @@ upload_flags =
-Pusb
-e
lib_deps =
feilipu/FreeRTOS@^10.5.0-0
nrf24/RF24@^1.4.5
https://github.com/aZholtikov/ZHConfig
https://github.com/feilipu/Arduino_FreeRTOS_Library
https://github.com/nRF24/RF24
[env:ATmega168P]
platform = atmelavr
@ -26,7 +26,6 @@ board_build.f_cpu = 8000000L
framework = arduino
upload_protocol = usbasp
build_flags =
-D TOUCH_SWITCH=4
-D ID=$UNIX_TIME
board_fuses.hfuse = 0xDE
board_fuses.lfuse = 0xE2
@ -35,8 +34,9 @@ upload_flags =
-Pusb
-e
lib_deps =
feilipu/FreeRTOS@^10.5.0-0
nrf24/RF24@^1.4.5
https://github.com/aZholtikov/ZHConfig
https://github.com/feilipu/Arduino_FreeRTOS_Library
https://github.com/nRF24/RF24
[env:ATmega168PB]
platform = atmelavr
@ -46,7 +46,6 @@ board_build.f_cpu = 8000000L
framework = arduino
upload_protocol = usbasp
build_flags =
-D TOUCH_SWITCH=4
-D ID=$UNIX_TIME
board_fuses.hfuse = 0xDE
board_fuses.lfuse = 0xE2
@ -55,8 +54,9 @@ upload_flags =
-Pusb
-e
lib_deps =
feilipu/FreeRTOS@^10.5.0-0
nrf24/RF24@^1.4.5
https://github.com/aZholtikov/ZHConfig
https://github.com/feilipu/Arduino_FreeRTOS_Library
https://github.com/nRF24/RF24
[env:ATmega328]
platform = atmelavr
@ -66,7 +66,6 @@ board_build.f_cpu = 8000000L
framework = arduino
upload_protocol = usbasp
build_flags =
-D TOUCH_SWITCH=4
-D ID=$UNIX_TIME
board_fuses.hfuse = 0xDE
board_fuses.lfuse = 0xE2
@ -75,9 +74,9 @@ upload_flags =
-Pusb
-e
lib_deps =
-D TOUCH_SWITCH=4
feilipu/FreeRTOS@^10.5.0-0
nrf24/RF24@^1.4.5
https://github.com/aZholtikov/ZHConfig
https://github.com/feilipu/Arduino_FreeRTOS_Library
https://github.com/nRF24/RF24
[env:ATmega328P]
platform = atmelavr
@ -87,7 +86,6 @@ board_build.f_cpu = 8000000L
framework = arduino
upload_protocol = usbasp
build_flags =
-D TOUCH_SWITCH=4
-D ID=$UNIX_TIME
board_fuses.hfuse = 0xDE
board_fuses.lfuse = 0xE2
@ -96,8 +94,9 @@ upload_flags =
-Pusb
-e
lib_deps =
feilipu/FreeRTOS@^10.5.0-0
nrf24/RF24@^1.4.5
https://github.com/aZholtikov/ZHConfig
https://github.com/feilipu/Arduino_FreeRTOS_Library
https://github.com/nRF24/RF24
[env:ATmega328PB]
platform = atmelavr
@ -107,7 +106,6 @@ board_build.f_cpu = 8000000L
framework = arduino
upload_protocol = usbasp
build_flags =
-D TOUCH_SWITCH=4
-D ID=$UNIX_TIME
board_fuses.hfuse = 0xDE
board_fuses.lfuse = 0xE2
@ -116,5 +114,6 @@ upload_flags =
-Pusb
-e
lib_deps =
feilipu/FreeRTOS@^10.5.0-0
nrf24/RF24@^1.4.5
https://github.com/aZholtikov/ZHConfig
https://github.com/feilipu/Arduino_FreeRTOS_Library
https://github.com/nRF24/RF24

View File

@ -3,26 +3,22 @@
#include "RF24.h"
#include "avr/interrupt.h"
#include "semphr.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;
#include "EEPROM.h"
#include "ZHConfig.h"
void sendButtonPushing(void *pvParameters);
float getBatteryLevelCharge(void);
void loadId(void);
int16_t id{abs((int16_t)ID)};
RF24 radio(9, 10);
SemaphoreHandle_t buttonSemaphore;
void setup()
{
loadId();
EICRA |= (1 << ISC11) | (1 << ISC10);
EIMSK |= (1 << INT1);
ADCSRA &= ~(1 << ADEN);
@ -56,11 +52,11 @@ void sendButtonPushing(void *pvParameters)
for (;;)
{
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;
radio.powerUp();
radio.flush_tx();
radio.write(&sensor, sizeof(transmitted_data_t));
radio.write(&sensor, sizeof(rf_transmitted_data_t));
radio.powerDown();
sei();
}
@ -76,8 +72,21 @@ float getBatteryLevelCharge()
while (bit_is_set(ADCSRA, ADSC))
;
ADCSRA &= ~(1 << ADEN);
float value = ((1024 * 1.1) / (ADCL + ADCH * 256));
return value;
return (1024 * 1.1) / (ADCL + ADCH * 256);
}
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)