208 lines
6.6 KiB
C++
208 lines
6.6 KiB
C++
|
|
/*
|
|
* Getting Started example sketch for nRF24L01+ radios
|
|
* This is a very basic example of how to send data from one node to another
|
|
* Updated: Dec 2014 by TMRh20
|
|
*/
|
|
|
|
#include <SPI.h>
|
|
#include "RF24.h"
|
|
|
|
/****************** User Config ***************************/
|
|
/*** Set this radio as radio number 0 or 1 ***/
|
|
bool radioNumber = 1;
|
|
|
|
bool TorStatus1;
|
|
bool TorStatus2;
|
|
bool TorStatusChanged;
|
|
|
|
int pinStatusGaragenTor1 = D1;
|
|
int pinStatusGaragenTor2 = D2;
|
|
int pinGaragenTorSchalter = 10;
|
|
int len=0;
|
|
|
|
#define MAXSIZE 100
|
|
#define CHANNEL 108
|
|
char gotmsg[MAXSIZE];
|
|
|
|
char TorOffen[] = "OPENED";
|
|
char TorGeschlossen[] = "CLOSED";
|
|
char TorInBewegung[] = "IN TRANSIT";
|
|
|
|
char TorAuf[] = "Oeffne Garagentor";
|
|
char TorZu[] = "Schliesse Garagentor";
|
|
char TorStatus[] = "Schicke Status";
|
|
|
|
|
|
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
|
|
RF24 radio(D4,D8);
|
|
/**********************************************************/
|
|
|
|
byte addresses[][6] = {"1Node","2Node"};
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
radio.begin();
|
|
|
|
// Set the PA Level low to prevent power supply related issues since this is a
|
|
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
|
|
radio.setPALevel(RF24_PA_MAX);
|
|
radio.setDataRate(RF24_250KBPS);
|
|
radio.setAutoAck(false);
|
|
radio.setRetries(15, 15);
|
|
//radio.setChannel(CHANNEL);
|
|
|
|
// 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();
|
|
|
|
pinMode(pinStatusGaragenTor1, INPUT);
|
|
pinMode(pinStatusGaragenTor2, INPUT);
|
|
pinMode(pinGaragenTorSchalter, OUTPUT);
|
|
|
|
TorStatus1 = digitalRead(pinStatusGaragenTor1);
|
|
TorStatus2 = digitalRead(pinStatusGaragenTor2);
|
|
digitalWrite(pinGaragenTorSchalter, HIGH);
|
|
|
|
TorStatusChanged = true;
|
|
}
|
|
|
|
|
|
|
|
|
|
// MAIN CONTROL LOOP //
|
|
void loop() {
|
|
|
|
|
|
/****************** Ping Out Role ***************************/
|
|
if (TorStatusChanged == true) {
|
|
|
|
radio.stopListening(); // First, stop listening so we can talk.
|
|
Serial.print(F("Now sending: "));
|
|
|
|
unsigned long start_time = micros();
|
|
if ((TorStatus1 == true) && (TorStatus2 == false)) {
|
|
Serial.println(TorOffen);
|
|
if (!radio.write( &TorOffen, strlen(TorOffen) )){
|
|
Serial.println(F("failed"));
|
|
}
|
|
} else if ((TorStatus1 == false) && (TorStatus2 == false)) {
|
|
Serial.println(TorInBewegung);
|
|
if (!radio.write( &TorInBewegung, strlen(TorInBewegung) )){
|
|
Serial.println(F("failed"));
|
|
}
|
|
} else if ((TorStatus1 == false) && (TorStatus2 == true)) {
|
|
Serial.println(TorGeschlossen);
|
|
if (!radio.write( &TorGeschlossen, strlen(TorGeschlossen) )){
|
|
Serial.println(F("failed"));
|
|
}
|
|
}
|
|
|
|
radio.startListening(); // Now, continue listening
|
|
|
|
// Set up a timeout period, get the current microseconds
|
|
unsigned long started_waiting_at = micros();
|
|
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 {
|
|
// Grab the response, compare, and send to debugging spew
|
|
len = radio.getDynamicPayloadSize();
|
|
radio.read( &gotmsg, len );
|
|
unsigned long end_time = micros();
|
|
|
|
// Spew it
|
|
Serial.print(F("Sent "));
|
|
Serial.print(start_time);
|
|
Serial.print(F(", Got response "));
|
|
Serial.print(gotmsg);
|
|
Serial.print(F(", Round-trip delay "));
|
|
Serial.print(end_time-start_time);
|
|
Serial.println(F(" microseconds"));
|
|
}
|
|
|
|
//go back to listening
|
|
TorStatusChanged = false;
|
|
}
|
|
|
|
|
|
|
|
/****************** Pong Back Role ***************************/
|
|
|
|
if ( TorStatusChanged == false )
|
|
{
|
|
unsigned long got_time;
|
|
|
|
if( radio.available()){
|
|
// Variable for the received timestamp
|
|
while (radio.available()) { // While there is data ready
|
|
len = radio.getDynamicPayloadSize();
|
|
radio.read( &gotmsg, len ); // Get the payload
|
|
}
|
|
|
|
radio.stopListening(); // First, stop listening so we can talk
|
|
radio.write( &gotmsg, strlen(gotmsg) ); // Send the final one back.
|
|
radio.startListening(); // Now, resume listening so we catch the next packets.
|
|
Serial.print(F("Sent response ACK: "));
|
|
Serial.println(gotmsg);
|
|
|
|
if ((strcmp(gotmsg,TorAuf)==0) && (!TorStatus1) && (TorStatus2)) {
|
|
Serial.println("Oeffne das Tor");
|
|
digitalWrite(pinGaragenTorSchalter, LOW);
|
|
delay(1500);
|
|
digitalWrite(pinGaragenTorSchalter, HIGH);
|
|
}
|
|
if ((strcmp(gotmsg,TorAuf)==0) && (TorStatus1)) {
|
|
Serial.println("Tor bereits offen!");
|
|
}
|
|
|
|
if ((strcmp(gotmsg,TorZu)==0) && (TorStatus1) && (!TorStatus2)) {
|
|
Serial.println("Schliesse das Tor");
|
|
digitalWrite(pinGaragenTorSchalter, LOW);
|
|
delay(1500);
|
|
digitalWrite(pinGaragenTorSchalter, HIGH);
|
|
}
|
|
if ((strcmp(gotmsg,TorZu)==0) && (!TorStatus1)) {
|
|
Serial.println("Tor bereits geschlossen!");
|
|
}
|
|
|
|
if (strcmp(gotmsg,TorStatus)==0) {
|
|
Serial.println("Tor status requested!");
|
|
TorStatusChanged = true; //forciere das Sendes des Status
|
|
delay(1500);
|
|
}
|
|
}
|
|
}
|
|
|
|
/****************** Control Logic ***************************/
|
|
if ((digitalRead(pinStatusGaragenTor1) != TorStatus1)||(digitalRead(pinStatusGaragenTor2) != TorStatus2)) {
|
|
TorStatus1 = digitalRead(pinStatusGaragenTor1);
|
|
TorStatus2 = digitalRead(pinStatusGaragenTor2);
|
|
TorStatusChanged = true; //Tor status was changed!: Transmitting...
|
|
}
|
|
|
|
/*Serial.print("Sensor 1: ");
|
|
Serial.print(TorStatus1);
|
|
Serial.print(" Sensor 2: ");
|
|
Serial.println(TorStatus2);
|
|
delay(200);*/
|
|
|
|
} // Loop
|