Example #1
0
void setup(void)
{
	//
	// Refer to RF24.h or nRF24L01 DS for settings
	radio.begin();
	radio.enableDynamicPayloads();
	radio.setAutoAck(1);
	radio.setRetries(15,15);
	radio.setDataRate(RF24_1MBPS);
	radio.setPALevel(RF24_PA_MAX);
	radio.setChannel(76);
	radio.setCRCLength(RF24_CRC_16);

	// Open 6 pipes for readings ( 5 plus pipe0, also can be used for reading )
	radio.openWritingPipe(pipes[0]);
	radio.openReadingPipe(1,pipes[1]);
	radio.openReadingPipe(2,pipes[2]);
	radio.openReadingPipe(3,pipes[3]);
	radio.openReadingPipe(4,pipes[4]);
	radio.openReadingPipe(5,pipes[5]);

	//
	// Dump the configuration of the rf unit for debugging
	//

	// Start Listening
	radio.startListening();

	radio.printDetails();
	printf("\n\rOutput below : \n\r");
	usleep(1000);
        printf("\n\rDinamycPayloadSize: %i",radio.getDynamicPayloadSize());
}
Example #2
0
void loop(void)
{
	//
	// Ping out.
	//
	// The payload will always be the same, what will change is how much of it we send.
	static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";

	// First, stop listening so we can talk.
	radio.stopListening();

	// Take the time, and send it.  This will block until complete
	printf("Now sending length %i...",next_payload_size);
	radio.write( send_payload, next_payload_size );

	// Now, continue listening
	radio.startListening();

	// Wait here until we get a response, or timeout
	long started_waiting_at = __millis();

	bool timeout = false;
	while ( ! radio.available() && ! timeout )
		if (__millis() - started_waiting_at > 500 )
			timeout = true;

	// Describe the results
	if ( timeout )
	{
		printf("Failed, response timed out.\n\r");
	}
	else
	{
		// Grab the response, compare, and send to debugging spew
		uint8_t len = radio.getDynamicPayloadSize();
		radio.read( receive_payload, len );

		// Put a zero at the end for easy printing
		receive_payload[len] = 0;

		// Spew it
		printf("Got response size=%i value=%s\n\r",len,receive_payload);
	}

	// Update size for next time.
	next_payload_size += payload_size_increments_by;
	if ( next_payload_size > max_payload_size )
		next_payload_size = min_payload_size;


	sleep(1);
}
void gotData(void){
	bool rx = 0, blnTXOK = 0, blnTXFail = 0;
	intResult = 0;
	uint8_t pipe_num = 0;
    radio.whatHappened(blnTXOK,blnTXFail,rx, &pipe_num);

	if(blnTXFail){
		intResult = 2;
	}else if(blnTXOK){
		intResult = 1;
	}else if ( rx ){
		intResult = 3;
		uint8_t len = radio.getDynamicPayloadSize();

		bool more_available = true;
		while (more_available)
		{
			// Fetch the payload, and see if this was the last one.
			more_available = radio.read( receive_payload, len );

			// Put a zero at the end for easy printing
			receive_payload[len] = 0;
			
			// Print received packet
			printf("[%d] Data size=%i value=%s\n\r",pipe_num, len,receive_payload);
			
			// next payload can be of different size
			if (more_available){
				len = radio.getDynamicPayloadSize();
			}
		}
	
	}
//	printf("intResult %d %d %d %d \n\r",blnTXOK,blnTXFail,rx,intResult);
	fflush (stdout) ;
//	radio.clearInterrupt();
}
Example #4
0
unsigned long loop(void)
{
  unsigned long sender_id = 0;
  ArPiMessage data;

  char blank[21]="FFFFFFFFFFFFFFFFFFFF";

  int max_sleep = 10000;
  while (!radio.available()) {
    usleep(1000);
  }

  if (max_sleep > 0) {
    uint8_t len = radio.getDynamicPayloadSize();
    radio.read( receivePayload, len );

    TRI_LOG(len);

    memcpy(&data, receivePayload, sizeof(data));

    if (data.parity != parity(data.data)) {
     TRI_LOG_STR("parity doesn't match, rejecting...\n");
   } else {

     unsigned long timer = data.data;
     sender_id = data.sender_id;

		// Send back payload to sender
     radio.stopListening();

		// Match for blank and do not re-send it out
     TRI_LOG_STR("All good");
     
     //     if ( strcmp(receivePayload,blank)  ) {
     //       radio.write(receivePayload,len);
     //       TRI_LOG_STR("Sending back");
     // } else {
     //   TRI_LOG_STR("Not sending anything back");
     // }
   }
 }

  TRI_LOG_STR("radio.startListening()");
  radio.startListening();

  // radio.startListening();

 return sender_id;
}
Example #5
0
void loop(void)
{
    // 32 byte character array is max payload
    char receivePayload[32];
 
    while (radio.available())
    {
        // read from radio until payload size is reached
        uint8_t len = radio.getDynamicPayloadSize();
        radio.read(receivePayload, len);
 
        // display payload
        cout << receivePayload << endl;
    }
}
Example #6
0
void loop(void)
{
	char receivePayload[32];
	uint8_t pipe = 0;
	

	 while ( radio.available( &pipe ) ) {

		uint8_t len = radio.getDynamicPayloadSize();
		radio.read( receivePayload, len );

		// Display it on screen
		printf("Recv: size=%i payload=%s pipe=%i",len,receivePayload,pipe);

		// Send back payload to sender
		radio.stopListening();


		// if pipe is 7, do not send it back
		if ( pipe != 7 ) {
			// Send back using the same pipe
			// radio.openWritingPipe(pipes[pipe]);
			time_t now = time(0);
			tm * gmtm = gmtime(&now);		
			char * dt = asctime(gmtm);	
			//sprintf(dt, "%d", time_t);
			strftime(dt, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
			radio.write(dt,strlen(dt));

			receivePayload[len]=0;
			printf("\t Send: size=%i payload=%s pipe:%i\n\r",strlen(dt),dt,pipe);
  		} else {
			printf("\n\r");
                }

		// Enable start listening again
		radio.startListening();

	// Increase the pipe outside the while loop
	pipe++;
	// reset pipe to 0
	if ( pipe > 5 ) pipe = 0;
	}

	usleep(20);
}
Example #7
0
void loop(void) {
   // 32 byte character array is max payload
   // char receivePayload[32];
   char str[80];
 
   while (radio.available()) {
      // read from radio until payload size is reached
      uint8_t len = radio.getDynamicPayloadSize();      
      // radio.read(receivePayload, len);
      // display payload
      // cout << receivePayload << endl;

      radio.read(&mpu_data, len);
      Quat quat = mpu_data.Q;
      sprintf(str, "Quat: w=%f, x=%f, y=%f, z=%f", quat.w, quat.x, quat.y, quat.z);
      cout << str << endl;
   }
}
Example #8
0
boolean pcmRX::txSel(byte sel, int timeoutDelay){
  radi.write(&sel,sizeof(byte));
  int result = radi.getDynamicPayloadSize();
  if( result == 2 ){  return(1);}
  else{
	  Serial.println(result);
      if(result > 32){

		  SPI.setBitOrder(MSBFIRST);
		  SPI.setDataMode(SPI_MODE0);
		  SPI.setClockDivider(SPI_CLOCK_DIV4);
		  digitalWrite(csPin,LOW);
		  SPI.transfer( FLUSH_RX );
		  digitalWrite(csPin,HIGH);

      }
      if(timeoutDelay > 0){ delay(timeoutDelay);}  return(0);
  }
}
Example #9
0
void loop(void) {
    radio.startListening();
    delay(10);
    if ( radio.available() ) {
      while ( radio.available() ) {
        len = radio.getDynamicPayloadSize();
        radio.read(receive_payload, len);
        receive_payload[len] = 0;
        printf("Payload size=%i value=%s\n\r", len, receive_payload);
        lcd.setCursor(0, 0);
        delay(1);
        lcd.print(receive_payload);
      }
    }
    radio.stopListening();
    delay(1);
    radio.write( send_payload, 16 );
    delay(10);
}
/*
************** LOOP Procedure **************
*/
void loop(void) {
	if (radio.available(&pipeNo)) {
		int len = radio.getDynamicPayloadSize();  // Size of th epayload to read
		radio.read(&payload, len);	// Read the payload
		printf("%d:",pipeNo); 
		sprintf(SQLstring,";");	// Initialise SQLString to a default value "nothing"
		switch(payload.type) {    
		case SENSOR_STATION:
			printf("SENSOR_STATION:");
			printf("packet %d:%d ID:%u PowerVoltage:%u status:%u \n", len, sizeof(payload), payload.data.SENSOR_STATION.Station_id, payload.data.SENSOR_STATION.Power_Voltage, payload.data.SENSOR_STATION.status);
			break;	
		case CC_SENSOR_DYN:
			printf("CC_SENSOR_DYN:");
			printf("packet %d:%d Sensor:%d Temp:%f Watts:%d status:%u \n", len, sizeof(payload), payload.data.CC_SENSOR_DYN.sensor_id, 
			payload.data.CC_SENSOR_DYN.temperature, payload.data.CC_SENSOR_DYN.watts, payload.data.CC_SENSOR_DYN.status);
			sprintf(SQLstring,"INSERT INTO CC_SENSOR_DYN VALUES(NOW(),%d,%5.1f,%d)",payload.data.CC_SENSOR_DYN.sensor_id, payload.data.CC_SENSOR_DYN.temperature, payload.data.CC_SENSOR_DYN.watts);
			break;		
		case CC_SENSOR_HIST:
			printf("CC_SENSOR_HIST:");
			printf("packet %d:%d Sensor:%d Period:%lc:%i Watts:%3.3f status:%u \n", len, sizeof(payload), payload.data.CC_SENSOR_HIST.sensor_id, 
			payload.data.CC_SENSOR_HIST.hist_type, payload.data.CC_SENSOR_HIST.hist_period, payload.data.CC_SENSOR_HIST.watts, payload.data.CC_SENSOR_HIST.status);
			sprintf(SQLstring,"INSERT INTO CC_SENSOR_HIST VALUES(NOW(),%d,'%lc',%i, %3.3f)",payload.data.CC_SENSOR_HIST.sensor_id, payload.data.CC_SENSOR_HIST.hist_type, payload.data.CC_SENSOR_HIST.hist_period, payload.data.CC_SENSOR_HIST.watts);
			break;		

		default:
			printf("Unknown message.\n");
			break;
		}

		// Executing the SQL instruction. There is one payload per loop, therefore one SQL instructions per loop.
		if ((mysql1 != NULL) &&  (strcmp(SQLstring,";") != 0)) { // Check mySql connection availability and Sql Instructions validity
			if (!mysql_query(mysql1, SQLstring)) { 	
				//printf(" >SQL Ok: %s\n",SQLstring); 
			}  
			else { 
				printf("  >SQL NOk: %s\n",SQLstring); 	// Error Occured
				printf("%s\n", mysql_error(mysql1)); 	// Print mysql Error
			}
		}
	}
	delay(20);
}
Example #11
0
void loop(void)
{
	char receivePayload[32];
	uint8_t pipe = 0;
	

	 while ( radio.available( &pipe ) ) {

		uint8_t len = radio.getDynamicPayloadSize();
		radio.read( receivePayload, len );

		// Display it on screen
		printf("Recv: size=%i payload=%s pipe=%i",len,receivePayload,pipe);

		// Send back payload to sender
		radio.stopListening();


		// if pipe is 7, do not send it back
		if ( pipe != 7 ) {
			// Send back using the same pipe
			// radio.openWritingPipe(pipes[pipe]);
			radio.write(receivePayload,len);

			receivePayload[len]=0;
			printf("\t Send: size=%i payload=%s pipe:%i\n\r",len,receivePayload,pipe);
  		} else {
			printf("\n\r");
                }

		// Enable start listening again
		radio.startListening();

	// Increase the pipe outside the while loop
	pipe++;
	// reset pipe to 0
	if ( pipe > 5 ) pipe = 0;
	}

	usleep(20);
}
Example #12
0
bool COM_24g::receiveFrame(RF24 _radioCom)	// Decode the available frame on the pipe
{

int   len 	= _radioCom.getDynamicPayloadSize();  	// Size of the payload to read
bool  done 	= _radioCom.read(&_payload, len);        // Read the payload

if(done){
//_dataType        = _payload.type;
_dataVersion     = _payload.version;

  switch (_payload.type ) {
    /*case CMD_WHO:
    _data.CMD_WHO.partnum   = _payload.data.CMD_WHO.partnum;
    _data.CMD_WHO.revision  = _payload.data.CMD_WHO.revision;
    _data.CMD_WHO.parttype  = _payload.data.CMD_WHO.parttype;
    return true;
    break;
    */
    case YOUARE:
    _data.YOUARE.partid        = _payload.data.YOUARE.partid;
    _data.YOUARE.baseCmdAdd    = _payload.data.YOUARE.baseCmdAdd;
    _data.YOUARE.base_encode   = _payload.data.YOUARE.base_encode;
    _dataType                  = YOUARE;
    return true;
    break;
   
    
    default:
        //serial.println("Unknown message.");
    break;
    
  }

} else {
 //serial.println("Error of read on the Paypload"); 
 return false;
}


}
void intHandler(){
  //
  // Pong back role.  Receive each packet, dump it out, and send it back
  //

  if ( role == role_pong_back )
  {
    // if there is data ready
    if ( radio.available() )
    {
      // Dump the payloads until we've gotten everything
      uint8_t len=0;

      while (radio.available())
      {
        // Fetch the payload, and see if this was the last one.
	len = radio.getDynamicPayloadSize();
	radio.read( receive_payload, len );

	// Put a zero at the end for easy printing
	receive_payload[len] = 0;

	// Spew it
	printf("Got payload size=%i value=%s\n\r",len,receive_payload);
      }

      // First, stop listening so we can talk
      radio.stopListening();

      // Send the final one back.
      radio.write( receive_payload, len );
      printf("Sent response.\n\r");

      // Now, resume listening so we catch the next packets.
      radio.startListening();
    }
  }
}
int main(int argc, char** argv){


  // Print preamble:
  cout << "RF24/examples/pingpair_dyn/\n";

  // Setup and configure rf radio
  radio.begin();
  radio.enableDynamicPayloads();
  radio.setRetries(5,15);
  radio.printDetails();


/********* Role chooser ***********/

  printf("\n ************ Role Setup ***********\n");
  string input = "";
  char myChar = {0};
  cout << "Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) \n>";
  getline(cin,input);

  if(input.length() == 1) {
	myChar = input[0];
	if(myChar == '0'){
		cout << "Role: Pong Back, awaiting transmission " << endl << endl;
	}else{  cout << "Role: Ping Out, starting transmission " << endl << endl;
		role = role_ping_out;
	}
  }
/***********************************/

    if ( role == role_ping_out )    {
      radio.openWritingPipe(pipes[0]);
      radio.openReadingPipe(1,pipes[1]);
    } else {
      radio.openWritingPipe(pipes[1]);
      radio.openReadingPipe(1,pipes[0]);
      radio.startListening();
    }
    attachInterrupt(interruptPin, INT_EDGE_FALLING, intHandler); //Attach interrupt to bcm pin 23

// forever loop
	while (1)
	{

if (role == role_ping_out)
  {
    // The payload will always be the same, what will change is how much of it we send.
    static char send_payload[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ789012";

    // First, stop listening so we can talk.
    radio.stopListening();

    // Take the time, and send it.  This will block until complete
    printf("Now sending length %i...",next_payload_size);
    radio.write( send_payload, next_payload_size );

    // Now, continue listening
    radio.startListening();

    // Wait here until we get a response, or timeout
    unsigned long started_waiting_at = millis();
    bool timeout = false;
    while ( ! radio.available() && ! timeout )
      if (millis() - started_waiting_at > 500 )
        timeout = true;

    // Describe the results
    if ( timeout )
    {
      printf("Failed, response timed out.\n\r");
    }
    else
    {
      // Grab the response, compare, and send to debugging spew
      uint8_t len = radio.getDynamicPayloadSize();
      radio.read( receive_payload, len );

      // Put a zero at the end for easy printing
      receive_payload[len] = 0;

      // Spew it
      printf("Got response size=%i value=%s\n\r",len,receive_payload);
    }

    // Update size for next time.
    next_payload_size += payload_size_increments_by;
    if ( next_payload_size > max_payload_size )
      next_payload_size = min_payload_size;

    // Try again 1s later
    delay(100);
  }


}
}
Example #15
0
void loop()
{
        char receivePayload[32];
        uint8_t pipe = 0;


         while ( radio.available( &pipe ) ) {

                uint8_t len = radio.getDynamicPayloadSize();
                radio.read( &p, sizeof(p) );

                char temp[5];

                char outBuffer[1024]="";
                strcat(outBuffer, "");
                strcat(outBuffer, "key=");
                strcat(outBuffer, p.id);
                int val = 0;
                if ((p.type & TEMPERATURE) == TEMPERATURE) { 
                        strcat(outBuffer,"&1=");
                        sprintf(temp, "%3.2f", p.val[val++]/100.0);
                        strcat(outBuffer, temp);
                }
                if ((p.type & HUMIDITY) == HUMIDITY) { 
                        strcat(outBuffer,"&2=");
                        sprintf(temp, "%3.2f", p.val[val++]/100.0);
                        strcat(outBuffer, temp);
                }
                if ((p.type & CURRENT) == CURRENT) { 
                        strcat(outBuffer,"&3=");
                        sprintf(temp, "%3.2f", p.val[val++]/100.0);
                        strcat(outBuffer, temp);
                }
                if ((p.type & LUMINOSITY) == LUMINOSITY) { 
                        strcat(outBuffer,"&4=");
                        sprintf(temp, "%3.2f", p.val[val++]/100.0);
                        strcat(outBuffer, temp);
                }
                if ((p.type & PRESSURE) == PRESSURE) { 
                        strcat(outBuffer,"&6=");
                        sprintf(temp, "%3.2f", p.val[val++]/100.0);
                        strcat(outBuffer, temp);
                }

                // Display it on screen
                printf("\n\rRecv: size:%i pipe:%i type:%d data:%s\n\r",len,pipe,p.type,outBuffer);
                sendToServer(outBuffer);

                // Send back payload to sender
                radio.stopListening();


                // if pipe is 7, do not send it back
                if ( pipe != 7 ) {
                        // Send back using the same pipe
                        // radio.openWritingPipe(pipes[pipe]);
                        radio.write(receivePayload,len);

                        receivePayload[len]=0;
                        printf("\t Send: size=%i payload=%s pipe:%i\n\r",len,receivePayload,pipe);
                } else {
                        printf("\n\r");
                }

                // Enable start listening again
                radio.startListening();
        }
        //read on all pipes
        pipe++;
        if ( pipe > 5 ) pipe = 0;
        
        usleep(20);
}
Example #16
0
void loop(void)
{

  int Data1,Data2,Data3,Data4 = 0;
  char temp[5];
  bool timeout=0;

  // Get the last two Bytes as node-id
  uint16_t nodeID = pipes[0] & 0xff;

  // Use the last 2 pipes address as nodeID  
  // sprintf(nodeID,"%X",pipes[0]);
  
  char outBuffer[32]=""; // Clear the outBuffer before every loop
  unsigned long send_time;
  uint8_t rtt = 0;
    
    // Get readings from sensors, change codes below to read sensors
    Data1 = counter++;
    Data2 = rand() % 1000;
    Data3 = rand() % 1000;
    Data4 = rand() % 1000;
    
    if ( counter > 999 ) counter = 0;

    // Append the hex nodeID to the beginning of the payload    
    sprintf(outBuffer,"%2X",nodeID);
    
    strcat(outBuffer,",");
    
    // Convert int to strings and append with zeros if number smaller than 3 digits
    // 000 to 999
    
    sprintf(temp,"%03d",Data1);  
    strcat(outBuffer,temp);
    
    strcat(outBuffer,",");
    
    sprintf(temp,"%03d",Data2);
    strcat(outBuffer,temp);
    
    strcat(outBuffer,",");

    sprintf(temp,"%03d",Data3);
    strcat(outBuffer,temp);
   
    strcat(outBuffer,",");
   
    sprintf(temp,"%03d",Data4);
    strcat(outBuffer,temp); 

    // Test for max payload size
    //strcat(outBuffer,"012345678901");

    
    // End string with 0
    // strcat(outBuffer,0);
            
    printf("outBuffer: %s len: %d\n\r",outBuffer, strlen(outBuffer));
    
    send_time = __millis();
    
    // Stop listening and write to radio 
    radio.stopListening();
    
    // Send to hub
    if ( radio.write( outBuffer, strlen(outBuffer)) ) {
       printf("Send successful\n\r"); 
    }
    else {
       printf("Send failed\n\r");
    }
  
    radio.startListening();
    delay(20);  

  while ( radio.available() && !timeout ) {

         uint8_t len = radio.getDynamicPayloadSize();
         radio.read( receivePayload, len); 
         
         receivePayload[len] = 0;
         printf("inBuffer:  %s\n\r",receivePayload);
         
         // Compare receive payload with outBuffer        
         if ( ! strcmp(outBuffer, receivePayload) ) {
             rtt = __millis() - send_time;
             printf("inBuffer --> rtt: %i \n\r",rtt);       

         }       
    
    // Check for timeout and exit the while loop
    if ( __millis() - send_time > 500 ) {
         printf("Timeout!!!\n\r");
         timeout = 1;
     }          
      
     delay(10);
   } // End while  
     
    delay(250);
    
}