Skip to content

Instantly share code, notes, and snippets.

@3xtr3m3d
Created February 20, 2020 15:01
Show Gist options
  • Save 3xtr3m3d/faef13acf206c3d45d7a49d39e2eb3a1 to your computer and use it in GitHub Desktop.
Save 3xtr3m3d/faef13acf206c3d45d7a49d39e2eb3a1 to your computer and use it in GitHub Desktop.
attiny84 - rf24
#define CE_PIN 2
#define CSN_PIN 3
#define LED_PIN 8
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
const byte myAddress[6] = "CELL1";
const byte destAddress[6] = "CELL0";
bool newData = false;
struct dataStruct{
int type;
float value1;
float value2;
float value3;
float value4;
float value5;
float value6;
float value7;
}dataSend;
dataStruct dataReceived;
void setup() {
pinMode(LED_PIN, OUTPUT);
radio.begin();
radio.setPALevel(RF24_PA_MIN);
radio.openWritingPipe(destAddress);
radio.openReadingPipe(1, myAddress);
radio.setRetries(3,5);
radio.startListening();
}
void loop() {
if(radio.available()){
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
if(newData == true){
if(dataReceived.type == 1){
dataSend.value1 = 1;
dataSend.value2 = 1;
dataSend.value3 = 1;
dataSend.value4 = 1;
dataSend.value5 = 1;
dataSend.value6 = 1;
dataSend.value7 = 1;
}else if(dataReceived.type == 2){
dataSend.value1 = 2;
dataSend.value2 = 2;
dataSend.value3 = 2;
dataSend.value4 = 2;
dataSend.value5 = 2;
dataSend.value6 = 2;
dataSend.value7 = 2;
}
dataSend.type = dataReceived.type;
radio.stopListening();
if(radio.write(&dataSend, sizeof(dataSend))){
doubleBlink();
}else{
blink();
}
radio.startListening();
newData = false;
}
delay(1000);
}
void doubleBlink(){
blink();
delay(50);
blink();
}
void blink(){
digitalWrite(LED_PIN, HIGH);
delay(50);
digitalWrite(LED_PIN, LOW);
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define LED_PIN 8
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
const byte myAddress[6] = "CELL0";
const byte destAddress[6] = "CELL1";
bool newData = false;
int req_type = 1;
struct dataStruct{
int type;
float value1;
float value2;
float value3;
float value4;
float value5;
float value6;
float value7;
}dataReceived;
dataStruct configData;
struct REQ_CONFIG{
uint8_t req;
};
REQ_CONFIG req_config;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(destAddress);
radio.openReadingPipe(1, myAddress);
radio.setRetries(3,5);
}
void loop() {
if(newData == false){
radio.stopListening();
configData.type = req_type;
radio.openWritingPipe(destAddress);
if (radio.write( &configData, sizeof(configData) )){
doubleBlink();
req_type = req_type + 1;
}else{
Serial.println(F(" write failed"));
blink();
}
radio.startListening();
if(req_type > 2){
req_type = 1;
}
}
if (radio.available()){
newData = true;
radio.read( &dataReceived, sizeof(dataReceived) );
Serial.println(dataReceived.type);
newData = false;
}
delay(1000);
}
void doubleBlink(){
digitalWrite(LED_PIN, HIGH);
delay(50);
digitalWrite(LED_PIN, LOW);
delay(50);
digitalWrite(LED_PIN, HIGH);
delay(50);
digitalWrite(LED_PIN, LOW);
}
void blink(){
digitalWrite(LED_PIN, HIGH);
delay(50);
digitalWrite(LED_PIN, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment