Hey Guys,
I've jumped back into tinkering lately I am trying to create a node to communicate with household sensors and relays.
My first attempt is controlling a ATTiny84+NRF24L01 from an Arduino Nano+NRF24L01, and while I did get the communication happening, it doesn't seem to take my programming changes.
I am using this library: http://tmrh20.github.io/RF24/
My ATTiny84 started with the included example sketch: rf24ping85
My Nano start with the included example sketch: GettingStarted
I set it up so if I enter "L" into the serial monitor it should send the number 14 over to the ATTIny84, and once received should bring pin 2 high and then send back 14 to the Nano. Once the Nano receives 14 it'll resume what it was doing, if not it'll keep trying. Currently I cannot get the ATTIny84 to bring PIN 2 High or send back 14 to the Nano.
Here is my wiring (ignore the colors):
--- NRF24 -----------NANO--ATTINY 84
1- Black (Ground) --- GND -- GND
2 - Red (VCC 3.3v) -- 3.3V -- 3.3V
3 -Green (CE) -- D9 -- 11 (PA2)
4- White (CSN) -- D10 -- 10 (PA3)
5- Blue (SCK) -- D13 -- 9 (PA4)
6 -Orange (MOSI) -- D11 -- 7 (PA6)
7-Yellow (MISO) -- D12 -- 8 (PA5)
8 - Purple (IRQ)
Here is the Attiny84 Sketch currently:
int Lock = 2;
int Press12 = 3;
int Press78 = 4;
bool locked = 0;
// CE and CSN are configurable, specified values for ATtiny85 as connected above
#define CE_PIN 8
#define CSN_PIN 7
//#define CSN_PIN 3 // uncomment for ATtiny85 3 pins solution
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte addresses[][6] = {
"1Node","2Node"};
unsigned long payload = 0;
void setup() {
// Setup and configure rf radio
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(addresses[1]); // Write to device address '2Node'
radio.openReadingPipe(1,addresses[0]); // Read on pipe 1 for device address '1Node'
radio.startListening(); // Start listening
pinMode(Lock, OUTPUT);
pinMode(Press12, OUTPUT);
pinMode(Press78, OUTPUT);
}
void loop(void){
if (locked == 0)
{
radio.stopListening(); // First, stop listening so we can talk.
payload++;
radio.write( &payload, sizeof(unsigned long) );
radio.startListening(); // Now, continue listening
}
else
if (locked == 1)
{
radio.stopListening(); // First, stop listening so we can talk.
radio.write( 14, sizeof(14) );
radio.startListening(); // Now, continue listening
locked = 0;
}
unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
boolean timeout = false; // Set up a variable to indicate if a response was received or not
while ( !radio.available() ){ // While nothing is received
if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
timeout = true;
break;
}
}
if ( !timeout ){ // Describe the results
unsigned long got_time; // Grab the response, compare, and send to debugging spew
radio.read( &got_time, sizeof(unsigned long) );
if (got_time == 14)
{
digitalWrite(Lock,HIGH);
delay(500);
digitalWrite(Lock,LOW);
locked = 1;
}
}
// Try again 1s later
delay(1000);
}
Here is the Nano sketch currently:
#include <SPI.h>
#include "RF24.h"
/*** Set this radio as radio number 0 or 1 ***/
bool radioNumber = 0;
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7,8);
byte addresses[][6] = {"1Node","2Node"};
// Used to control whether this node is sending or receiving
bool role = 0;
bool lock = 0;
bool unlocked = 0;
void setup() {
Serial.begin(115200);
Serial.println(F("RF24/examples/GettingStarted"));
Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
radio.begin();
radio.setPALevel(RF24_PA_LOW);
// Open a writing and reading pipe on each radio, with opposite addresses
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
// Start the radio listening for data
radio.startListening();
}
void loop() {
if (role == 1) {
radio.stopListening(); // First, stop listening so we can talk.
Serial.println(F("Now sending"));
unsigned long start_time = micros(); // Take the time, and send it. This will block until complete
if (lock == 0)
{
if (!radio.write( &start_time, sizeof(unsigned long) )){
Serial.println(F("failed"));
}
}
else
if (lock == 1)
{
if (!radio.write(14, sizeof(14) )){
Serial.println(F("failed"));
}
Serial.println(F("Locking..."));
}
radio.startListening(); // Now, continue listening
unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
boolean timeout = false; // Set up a variable to indicate if a response was received or not
while ( ! radio.available() ){ // While nothing is received
if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
timeout = true;
break;
}
}
if ( timeout ){ // Describe the results
Serial.println(F("Failed, response timed out."));
}else{
unsigned long got_time; // Grab the response, compare, and send to debugging spew
radio.read( &got_time, sizeof(unsigned long) );
unsigned long end_time = micros();
// Spew it
Serial.print(F("Sent "));
if (lock == 0)
{
Serial.print(start_time);
}
else if (lock == 1)
{
Serial.print(14);
if (got_time == 14)
{
lock = 0;
Serial.print(F(", Got response "));
Serial.print(F("Locked"));
Serial.print(got_time);
}
else
if (got_time != 14)
{
Serial.print(F(", Got response "));
Serial.print(F("Not Locked - Will Try Again "));
Serial.print(got_time);
}
}
Serial.print(F(", Round-trip delay "));
Serial.print(end_time-start_time);
Serial.println(F(" microseconds"));
}
// Try again 1s later
delay(1000);
}
if ( role == 0 )
{
unsigned long got_time;
if( radio.available()){
// Variable for the received timestamp
while (radio.available()) { // While there is data ready
radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
}
radio.stopListening(); // First, stop listening so we can talk
radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
radio.startListening(); // Now, resume listening so we catch the next packets.
Serial.print(F("Sent response "));
Serial.println(got_time);
}
}
if ( Serial.available() )
{
char c = toupper(Serial.read());
if ( c == 'T' && role == 0 ){
Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
role = 1; // Become the primary transmitter (ping out)
}else
if ( c == 'R' && role == 1 ){
Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
role = 0; // Become the primary receiver (pong back)
radio.startListening();
}
else
if ( c == 'L' && role == 1 ){
Serial.println(F("*** SENDING LOCK SIGNAL"));
lock = 1; // Send Lock Signal
}
}
} // Loop
Any help is appreciated!
Thanks!