Skip to content

Commit

Permalink
Minor changes, Add client examples, Update examples
Browse files Browse the repository at this point in the history
- Add code to bring interface up after config
- Modify timing in gw.update()
- Keep track of packets dropped from the network interface due to being
received faster than then radio network can handle
- Add extra info to ncurses example

- Create client examples demonstrating how to automate control or
requesting information from sensor nodes using various scripting methods
(Python,Bash,NodeJS)
  • Loading branch information
TMRh20 committed Apr 15, 2015
1 parent fd3e767 commit 342203e
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 42 deletions.
67 changes: 33 additions & 34 deletions RF24Gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ bool RF24Gateway::begin(bool configTUN, bool meshEnable, uint16_t address, uint8
radio.printDetails();
//#endif


return true;
return true;
}

/***************************************************************************************/
Expand Down Expand Up @@ -200,21 +199,24 @@ int RF24Gateway::setIP( char *ip_addr, char *mask) {
perror(ifr.ifr_name);
return -1;
}

/* Read interface flags */
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
fprintf(stderr, "ifdown: shutdown ");
perror(ifr.ifr_name);
return -1;
}


#ifdef ifr_flags
# define IRFFLAGS ifr_flags
#else /* Present on kFreeBSD */
# define IRFFLAGS ifr_flagshigh
#endif

memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
if (!(ifr.IRFFLAGS & IFF_UP)) {
//fprintf(stdout, "Device is currently down..setting up.-- %u\n",ifr.IRFFLAGS);
ifr.IRFFLAGS |= IFF_UP;
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
fprintf(stderr, "ifup: failed ");
perror(ifr.ifr_name);
return -1;
}
}

memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));

// Set interface address
if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
Expand Down Expand Up @@ -245,7 +247,6 @@ void RF24Gateway::update(){
handleRadio();
//handleTX();
//handleRX();

}

/***************************************************************************************/
Expand All @@ -266,7 +267,7 @@ void RF24Gateway::handleRadio(){
}

RF24NetworkFrame f;
while(network.external_queue.size() > 0){
while(network.external_queue.size() > 0 ){
f = network.external_queue.front();

msgStruct msg;
Expand Down Expand Up @@ -298,7 +299,7 @@ void RF24Gateway::handleRadio(){
}
network.external_queue.pop();

}
}
handleTX();
if(mesh_enabled){

Expand All @@ -310,28 +311,33 @@ void RF24Gateway::handleRadio(){
}else{
while(network.update());
}

handleRX();

if(network.external_queue.size() == 0 ){
if(network.external_queue.size() == 0 || txQueue.empty()){
//sched_yield();
if(dataRate == RF24_2MBPS){
delayMicroseconds(1000);
delayMicroseconds(850);
}else
if(dataRate == RF24_1MBPS){
delayMicroseconds(1500);
delayMicroseconds(1000);
}else
if(dataRate == RF24_250KBPS){
delayMicroseconds(4500);
}
}
// TX section

bool ok = 0;



if(!txQueue.empty() && !radio.available() && network.external_queue.size() == 0) {

while(!txQueue.empty() && !radio.available() && network.external_queue.size() == 0) {
if(dataRate == RF24_2MBPS){
delayMicroseconds(850);
}else
if(dataRate == RF24_1MBPS){
delayMicroseconds(1500);
}else
if(dataRate == RF24_250KBPS){
delayMicroseconds(4500);
}
msgStruct *msgTx = &txQueue.front();

#if (DEBUG >= 1)
Expand Down Expand Up @@ -418,8 +424,6 @@ void RF24Gateway::handleRadio(){

} //End Tx



}

/***************************************************************************************/
Expand Down Expand Up @@ -456,30 +460,26 @@ void RF24Gateway::handleRX(){
memcpy(&msg.message,&buffer,nread);
msg.size = nread;

if(txQueue.size() < 2){ // 150kB max queue size
if(txQueue.size() < 2){
txQueue.push(msg);
}else{
//std::cout << "**** Tun Drop ****" << std::endl;
droppedIncoming++;
}
//return 1;

} else{
#if (DEBUG >= 1)
std::cerr << "Tun: Error while reading from tun/tap interface." << std::endl;
#endif
//return 0;

}
}
}
//return 0;


}

/***************************************************************************************/

void RF24Gateway::handleTX(){


if(rxQueue.size() < 1){
return;
}
Expand Down Expand Up @@ -517,7 +517,6 @@ void RF24Gateway::handleRX(){
rxQueue.pop();
}


}

/***************************************************************************************/
Expand Down
26 changes: 26 additions & 0 deletions RF24Gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ class RF24Gateway {
bool meshEnabled(); /**< Is RF24Mesh enabled? */
bool config_TUN; /**< Using a TAP(false) or TUN(true) interface */

uint32_t ifDropped(){
return droppedIncoming;
}

private:
RF24& radio;
RF24Network& network;
Expand All @@ -116,6 +120,8 @@ class RF24Gateway {
bool begin(bool configTUN, bool meshEnable, uint16_t address, uint8_t mesh_nodeID, rf24_datarate_e data_rate, uint8_t _channel);
bool mesh_enabled;

uint32_t droppedIncoming;

uint8_t channel;
rf24_datarate_e dataRate;
char tunName[IFNAMSIZ];
Expand Down Expand Up @@ -143,6 +149,9 @@ class RF24Gateway {
void printPayload(char *buffer, int nread, std::string debugMsg = "");
};




/**
* @example RF24GatewayNode.cpp
*
Expand All @@ -168,6 +177,23 @@ class RF24Gateway {
* @image html ncurses.JPG
*/

/** @example bClient.sh
*
* Once RF24Gateway and RF24Ethernet are configured, standard tools can be used to interact with
* the sensor nodes. Example of on demand LED/Lighting control using a Bash script.
*/

/** @example nodeClient.js
*
* Once RF24Gateway and RF24Ethernet are configured, standard tools can be used to interact with
* the sensor nodes. Example of on demand LED/Lighting control using a NodeJS script.
*/

/** @example pyClient.py
*
* Once RF24Gateway and RF24Ethernet are configured, standard tools can be used to interact with
* the sensor nodes. Example of scheduled LED/Lighting control using a Python script.
*/

/**
* @mainpage RF24Gateway
Expand Down
3 changes: 2 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ CCFLAGS=-std=c++0x

ifeq "$(RPI)" "1"
# The recommended compiler flags for the Raspberry Pi
CCFLAGS+=-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s
CCFLAGS+=-Ofast -mfpu=vfp -mfloat-abi=hard -mtune=arm1176jzf-s

endif

# define all programs
Expand Down
4 changes: 2 additions & 2 deletions examples/RF24GatewayNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main(int argc, char** argv) {
char subnet[] = "255.255.255.0";

gw.setIP(ip,subnet);

while(1){

// The gateway handles all IP traffic (marked as EXTERNAL_DATA_TYPE) and passes it to the associated network interface
Expand All @@ -47,5 +47,5 @@ int main(int argc, char** argv) {
}
}


return 0;
}
31 changes: 31 additions & 0 deletions examples/clients/BashClient/bClient.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash


if (( $# != 2 ))
then
echo "Usage: ... ./bClient.sh <IP> <ON/OFF>"
exit 1
fi

IP=$1
PAGE=$2

URL=":1000/"
SUFFIX="</td*"

#Build a request ( http://10.10.2.4:1000/ON )
REQUEST="http://"$IP$URL$PAGE

#Make the request, with 1 retry and a timeout of 10 seconds per retry (max 20 seconds per request)
RESULT=$(wget -q --tries=1 --timeout=10 -O- $REQUEST | grep 'LED is')

#Trim the HTML from the result
RESULT=${RESULT%$SUFFIX}

#If succesful, print status, else indicate failure
if [ ${#RESULT} -ne 0 ]
then
echo $RESULT
else
echo "Request Failed"
fi
12 changes: 12 additions & 0 deletions examples/clients/BashClient/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bClient.sh example for use with RF24Ethernet/RF24Gateway

Once RF24Gateway and RF24Ethernet are configured, standard tools can be used to interact with
the sensor nodes.

This is a simple example demonstrating how to connect directly to a sensor node running one of the
included server examples via a bash script and control LEDs/lighting.

Usage:
1. Edit bClient.sh and configure the options
2. Run sudo ./bClient.sh <IP OF NODE> <ON or OFF> ie: ./bClient.sh 10.10.2.4 ON

20 changes: 20 additions & 0 deletions examples/clients/NodeJSClient/nodeClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


var request = require('request')
var url = 'http://10.10.2.4:1000/ON' // input your url here /ON to turn on and /OFF to turn off

// use a timeout value of 10 seconds
var timeoutInMilliseconds = 10*1000
var opts = {
url: url,
timeout: timeoutInMilliseconds
}

request(opts, function (err, res, body) {
if (err) {
console.dir(err)
return
}
var statusCode = res.statusCode
console.log('status code: ' + statusCode)
})
14 changes: 14 additions & 0 deletions examples/clients/NodeJSClient/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
nodeClient.js example for use with RF24Ethernet/RF24Gateway

Once RF24Gateway and RF24Ethernet are configured, standard tools can be used to interact with
the sensor nodes.

This is a simple example demonstrating how to connect directly to a sensor node running one of the
included server examples via a NodeJS script and control LEDs/lighting.

Usage:
1. a: Edit nodeClient.js and configure the URL( Sensor IP, page to request (OFF,ON)
b: From the example directory, run ```npm install request```
2. Run ```node nodeClient.js```
3. Status code: 200 indicates success

Loading

0 comments on commit 342203e

Please # to comment.