Last active
December 31, 2015 09:29
-
-
Save glisha/7967490 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include "nRF24L01.h" | |
#include "RF24.h" | |
#include "printf.h" | |
String txtMsg = ""; | |
char cmd[10]; //"state address", state=0|1, address=0..7 | |
char s; | |
byte state; | |
byte address; | |
RF24 radio(9,10); | |
void setup() { | |
Serial.begin(9600); | |
printf_begin(); | |
radio.begin(); | |
radio.setRetries(15,15); | |
radio.openReadingPipe(1,(uint64_t)17); | |
radio.printDetails(); | |
} | |
byte prati_nrf(byte state, byte address) { | |
//Masterot prakja komanda. | |
//Slave-ot ja izvrshuva i ja vrakja istata | |
//Masterot cheka da vidi shto napravil slave-ot. | |
//Se dodeka ne go napravi toa shto treba mu prakja pak. | |
bool sfatil = false; | |
while (!sfatil) { | |
uint64_t pipe = (uint64_t)address; | |
radio.openWritingPipe(pipe); | |
printf("Prakjam %d na %d",state,address); | |
radio.write( &state, sizeof(byte) ); | |
radio.startListening(); | |
//chekaj go timeout ili odgovor | |
unsigned long started_waiting_at = millis(); | |
bool timeout = false; | |
while ( ! radio.available() && ! timeout ) | |
if (millis() - started_waiting_at > 200 ) | |
timeout = true; | |
if ( timeout ) { | |
printf("Timeout projde"); | |
return 0; | |
} else { | |
byte recv_state; | |
radio.read( &recv_state, sizeof(byte) ); | |
if ( recv_state==state ) { | |
sfatil = true; | |
} | |
} | |
} | |
radio.stopListening(); | |
return 1; | |
} | |
void loop() { | |
//prochitaj shto i komu treba da pratish od seriska | |
while (Serial.available()) { | |
s=Serial.read(); | |
if (s == '\r') { | |
txtMsg.toCharArray(cmd,10); | |
byte ok = sscanf(cmd, "%d %d",state,&address); | |
if ( ok == 2 ) { | |
//prakjaj na nrf | |
if ( ! prati_nrf(state,address) ) { | |
Serial.println("not ok"); | |
} | |
} | |
txtMsg = ""; | |
} else { | |
txtMsg +=s; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (C) 2011 J. Coliz <[email protected]> | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
version 2 as published by the Free Software Foundation. | |
*/ | |
/** | |
* @file printf.h | |
* | |
* Setup necessary to direct stdout to the Arduino Serial library, which | |
* enables 'printf' | |
*/ | |
#ifndef __PRINTF_H__ | |
#define __PRINTF_H__ | |
#ifdef ARDUINO | |
int serial_putc( char c, FILE * ) | |
{ | |
Serial.write( c ); | |
return c; | |
} | |
void printf_begin(void) | |
{ | |
fdevopen( &serial_putc, 0 ); | |
} | |
#else | |
#error This example is only for use on Arduino. | |
#endif // ARDUINO | |
#endif // __PRINTF_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include "nRF24L01.h" | |
#include "RF24.h" | |
#include "printf.h" | |
byte state; | |
RF24 radio(9,10); | |
void setup() { | |
Serial.begin(9600); | |
printf_begin(); | |
radio.begin(); | |
radio.setRetries(15,15); | |
radio.openWritingPipe((uint64_t)17); | |
radio.openReadingPipe(1,(uint64_t)1); | |
radio.startListening(); | |
radio.printDetails(); | |
} | |
void loop() { | |
if ( radio.available() ) { | |
radio.read(&state,sizeof(state)); | |
printf("Dobiv komanda: %d", state); | |
delay(20); | |
radio.stopListening(); | |
radio.write( &state, sizeof(state) ); | |
radio.startListening(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment