Example #1
0
void wifinetworkconfig(){
	uint64_t addr=1;
	uint64_t mask=0xFFFFFFFFFF000000LL;
	uint64_t next_addr;
	boolean ok;
	boolean addr_load;
	uint8_t last_dev_count=NULL;
	uint8_t dev_count=NULL;
	uint8_t total_devs=NULL;
	uint8_t zone=1;
	uint8_t	addr_count=NULL;
	
	my_pipe=(uint8_t)random(1,0xFF);
	my_pipe=(my_pipe<<32);
	eeprom_write_block(&my_pipe,&my_pipe_addr,sizeof(my_pipe));
	radio.openReadingPipe(1,my_pipe);
	printf("Server address set to:%lX%08lX\r\n",(uint32_t)(my_pipe>>32),(uint32_t)my_pipe);
	
	do{		
		printf("starting configuration of zone:%d\r\n",zone);
		delay(1000);
		do{
			dev_count=0;
			if (zone>1)	{
				addr_load=load_from_file("addr.lst",(uint8_t *)&next_addr,sizeof(next_addr),addr_count);
				printf("extending with addr:%lX%08lX\r\n",(uint32_t)(next_addr>>32),(uint32_t)next_addr);
				addr_count++;
			}
			addr=1;
			while(addr<0xFFLL){
				blink_led();
				if (zone>1){
					RFbuffer.destino=next_addr;
					RFbuffer.dados[1]=addr;
					RFbuffer.dados[0]=zone;
					RFbuffer.comando=scan_zone;
					radio.stopListening();
					radio.openWritingPipe((next_addr & mask));
					ok=radio.write(&RFbuffer,sizeof(RFbuffer));
					radio.startListening();
					if(ok){
						while(!radio.available());
						radio.read(&RFbuffer,sizeof(RFbuffer));
						if (RFbuffer.comando!=not_found){
							save_new_address(&RFbuffer.destino);
							dev_count++;
						}
					}
					addr++;
				}
				if(zone==1){
					RFbuffer.destino=my_pipe;
					RFbuffer.dados[0]=zone;
					radio.stopListening();
					radio.openWritingPipe(addr);
					ok=radio.write(&RFbuffer,sizeof(RFbuffer));
					radio.startListening();
					if (ok){
						while(!radio.available());
						radio.read(&RFbuffer,sizeof(RFbuffer));
						save_new_address(&RFbuffer.destino);
						dev_count++;
					}
					addr++;
				}
				delay(20);
			}
			if (last_dev_count!=0)
				last_dev_count--;
		}while(last_dev_count>0);
		last_dev_count=dev_count;
		total_devs+=dev_count;
		zone++;
	}while(zone<4 && last_dev_count>0);
int main(int argc, char** argv) 
{
    int fr = atoi(argv[1]);
    int to = atoi(argv[2]);
    int ac = atoi(argv[3]);
    
    printf("vendor/TMRh20/RF24_RPi/Task\n");
    
    // Refer to RF24.h or nRF24L01 DS for settings

    radio.begin();

    delay(5);
    
    radio.setPayloadSize(10);
    radio.setRetries(15,15); // optionally, increase the delay between retries & # of retries
    //radio.setAutoAck(1); // Ensure autoACK is enabled
    radio.setAutoAck(0); // Ensure autoACK is disabled
    radio.setPALevel(RF24_PA_HIGH);
    radio.setDataRate(RF24_250KBPS);
    //radio.setCRCLength(RF24_CRC_8);
    radio.setChannel(114);
    
    radio.openWritingPipe(pipes[(to-1)]); // atoi() change a char to a int
    radio.openReadingPipe(1,pipes[(fr-1)]);
    
    radio.startListening();
    
    radio.printDetails();
    
    // First, stop listening so we can talk.
    radio.stopListening();
    radio.powerUp();
    
    // Take the time, and send it.  This will block until complete

    char payload_send[10] = "";
    snprintf(payload_send, 10, "to:%d,ac:%d", to, ac);
    
    char payload_send_size[10] = "";
    snprintf(payload_send_size, 10, "%d", sizeof(payload_send));
    
    printf("Sending ..\n");
    printf("Payload size: ");
    printf(payload_send_size);
    printf("\n");
    printf("Payload: ");
    printf(payload_send);
    printf("\n");

    //bool ok = radio.write( &payload_send, 10 );
    radio.write( &payload_send, 10 );

    /*if (!ok){
        printf("failed.\n");
        exit(1);
    }*/
    
    //radio.powerDown();
    
    // Now, continue listening
    radio.startListening();

    // Wait here until we get a response, or timeout (250ms)
    unsigned long started_waiting_at = millis();
    bool timeout = false;
    
    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    while ( ! radio.available() && ! timeout ) {
        if (millis() - started_waiting_at > 3000 ) {
            timeout = true;
        }
    }


    // Describe the results
    if ( timeout ) {
        printf("err: response timed out.\n");
        exit(1);
        
    } else {
        // Grab the response, compare, and send to debugging spew
        char payload_receive[10] = "";
        radio.read( &payload_receive, 10 );

        char payload_receive_size[10] = "";
        snprintf(payload_receive_size, 32, "%d", 10);
        // Spew it
        printf("Received ..\n");
        printf("Payload size: ");
        printf(payload_receive_size);
        printf("\n");
        printf("Payload: ");
        printf(payload_receive);
        printf("\n");
        
        printf(payload_receive);
        exit(0);
    }
    exit(1);
}
Example #3
0
int main (){


	HardwareInit();

	RF24 radio = RF24();
	// Radio pipe addresses for the 2 nodes to communicate.
	const uint64_t pipes[2] = { 0xF0F0F0F0BELL, 0xF0F0F0F0EFLL };

	setup_watchdog(wdt_64ms);

	//
	// Setup and configure rf radio
	//
	radio.begin();

	radio.setChannel(100);

	// optionally, increase the delay between retries & # of retries
	radio.setRetries(15,15);

	// optionally, reduce the payload size.  seems to
	// improve reliability
	radio.setPayloadSize(sizeof(report_t));

	radio.setDataRate(RF24_250KBPS);
	radio.openWritingPipe(pipes[0]);
	radio.openReadingPipe(1,pipes[1]);
	radio.startListening();
	radio.stopListening();

	//unsigned long  pa = 0;
	uint8_t counter = 0;


	//check eeprom
	if (eeprom_read_byte(0)==EEPROM_MAGIC_NUMBER){
		minx=(char)eeprom_read_byte((uint8_t*)1);
		maxx=(char)eeprom_read_byte((uint8_t*)2);
		miny=(char)eeprom_read_byte((uint8_t*)3);
		maxy=(char)eeprom_read_byte((uint8_t*)4);
		minrx=(char)eeprom_read_byte((uint8_t*)5);
		maxrx=(char)eeprom_read_byte((uint8_t*)6);
		minry=(char)eeprom_read_byte((uint8_t*)7);
		maxry=(char)eeprom_read_byte((uint8_t*)8);
		calibrated=true;

#ifdef DEBUG
		print_string("read eeprom magic number");
		char temp[100];
		sprintf(temp,"minx:%d maxx:%d minry:%d maxry:%d \n",minx,maxx,minry,maxry);
		print_string(temp);
		_delay_ms(2000);
#endif

	}


	while(1){

		ReadController();
#ifdef DEBUG
		char temp[100];
		print_string("before convert\n");
		sprintf(temp,"hat:%u x:%d y:%d rx:%d ry:%d b1:%u b2:%u\n",reportBuffer.hat,(int8_t)reportBuffer.x,(int8_t)reportBuffer.y,(int8_t)reportBuffer.rx,(int8_t)reportBuffer.ry,reportBuffer.b1,reportBuffer.b2);
		print_string(temp);
#endif

		convertAxes();

#ifdef DEBUG
		sprintf(temp,"hat:%u x:%d y:%d rx:%d ry:%d b1:%u b2:%u\n",reportBuffer.hat,(int8_t)reportBuffer.x,(int8_t)reportBuffer.y,(int8_t)reportBuffer.rx,(int8_t)reportBuffer.ry,reportBuffer.b1,reportBuffer.b2);
		print_string(temp);
#endif

		//Calibration
		if(A_PRESSED && B_PRESSED && Z_PRESSED && L_PRESSED && R_PRESSED){
			calibrate();
		}

		counter=(counter+1)%20;
		sendData(counter==0,radio);


		do_sleep();
	}
	//never reached
	return 0;
}
Example #4
0
inline void sendData(bool askforStatus,RF24 & radio){
	reportBuffer.reportid=askforStatus?1:0;
	bool ok = radio.write( &reportBuffer, sizeof(report_t) );

	if (ok){
		//status led off
		PORTD	&= ~(1<<LED1);
	}
	else{
		//status led on
		PORTD	|= (1<<LED1);
#ifdef DEBUG
		print_string("pa error\n");
#endif
		radio.startListening();
		radio.stopListening();
		return;
	}

	radio.startListening();

	if (askforStatus){

		// Wait here until we get a response, or timeout (250ms)
		bool timeout = false;
		timer2_start();
		while ( ! radio.available() && ! timeout ){
			if (timer2_gettick() > 200 )
				timeout = true;
		}

		// Describe the results
		if ( timeout )
		{
			//status led on
			PORTD	|= (1<<LED1);
#ifdef DEBUG
			print_string("Failed, response timed out.\n\r");
#endif
		}
		else
		{
			uint8_t response;
			radio.read( &response, sizeof(uint8_t) );
			//status led off
			PORTD	&= ~(1<<LED1);

			if (response){
				//battery led on
#ifdef DEBUG
				print_string("led on\n");
#endif
				PORTD	|= (1<<LED2);
			}else{
				//battery led off
				PORTD	&= ~(1<<LED2);
#ifdef DEBUG
				print_string("led off\n");
#endif
			}
		}

		timer2_stop();
	}

	radio.stopListening();
}
Example #5
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);
    
}
Example #6
0
bool loop()
{
  const char msg[10] = "";
  //
  // Ping out role.  Repeatedly send the current time
  //

  if (role == role_ping_out)
  {
    // First, stop listening so we can talk.
    radio.stopListening();

    // Take the time, and send it.  This will block until complete
//    unsigned long time = __millis();
 //   char msg[9] = "lamp1_on";
 //   printf("Now sending %s...",msg);
    bool ok = radio.write( msg, 10 );
    
    if (ok)
      printf("ok...");
    else
      printf("failed.\n\r");

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

    // Wait here until we get a response, or timeout (250ms)
    unsigned long started_waiting_at = __millis();
    bool timeout = false;
    while ( ! radio.available() && ! timeout ) {
	// by bcatalin ยป Thu Feb 14, 2013 11:26 am 
	__msleep(5); //add a small delay to let radio.available to check payload
      if (__millis() - started_waiting_at > 200 )
        timeout = true;
    }

    // Describe the results
    if ( timeout )
    {
      printf("Failed, response timed out.\n\r");
      return false;
    }
    else
    {
      // Grab the response, compare, and send to debugging spew
      char got[10];
      radio.read( &got, 10 );

      // Spew it
      printf("Got response %s\n",got);
      return true;
    }

    // Try again 1s later
//    delay(1000);
//sleep(1);
  }

  //
  // 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
      unsigned long got_time = 0;
      bool done = false;
      while (!done)
      {
        // Fetch the payload, and see if this was the last one.
//        done = radio.read( &got_time, sizeof(unsigned long) );

	float celsius;
//	done = radio.read( &celsius, sizeof(float));
	char tempC[100];
	char len[2];
	std::string recievedStr;
//	int i = 0;
//	while(i<16)
//	{
//		done = radio.read( &len, 2);
//		cout << len << endl;
//		i++;
//	}
//	printf("Payload length: %s... ",len);

	int payloadSize = radio.getPayloadSize();
//	printf("Payload size: %d", payloadSize);
	done = radio.read( &tempC, payloadSize);
        cout << "Received from radio: " << tempC << endl;
        // Spew it
        printf("Got payload %s... ",tempC);
	stringstream ss;
	string received;
	ss << tempC;
	ss >> received;


//	char mark[]  = "mark,a,b";
	char *test[sizeof(strtok(tempC, ","))];
	test[0] = strtok(tempC, ",");
	for(int i = 1; i < sizeof(strtok(tempC, ",")); i++)
        {
            test[i] = strtok (NULL, ",");
//            printf ("%s\n",test[i]); // Writes "is"
        }


	cout << "App:" << test[0] << "append" << strcmp(test[0],"temp") << endl;
	if(strcmp(test[0],"temp") == 0)
	{
		printf("Got data from temperature sensor, sending to temp server\n");
		try
		{
		
			ClientSocket client_socket ( "localhost", 5656 );
			
			std::string reply;
			
			try
			{
				cout << "received: " << received << endl;
				client_socket << received;
				client_socket >> reply;
			}
			catch ( SocketException& ) {}
			
			std::cout << "We received this response from the server:\n\"" << reply << "\"\n";;
			
		}
		catch ( SocketException& e )
		{
			std::cout << "Exception was caught:" << e.description() << "\n";
		}
	}

        // Delay just a little bit to let the other unit
        // make the transition to receiver
        delay(20);
      }

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

      // Send the final one back.
      printf("Sent response.\n\r");
      radio.write( &got_time, sizeof(unsigned long) );

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

  bool role_ping_out = true, role_pong_back = false;
  bool role = role_pong_back;

  cout << "RF24/examples/GettingStarted/\n";

  // Setup and configure rf radio
  radio.begin();

  // optionally, increase the delay between retries & # of retries
  radio.setRetries(15,15);
  // Dump the configuration of the rf unit for debugging
  radio.printDetails();


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

  printf("\n ************ Role Setup ***********\n");
  string input = "";
  char myChar = {0};
  cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (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;
	}
  }
/***********************************/
  // This simple sketch opens two pipes for these two nodes to communicate
  // back and forth.

    if ( !radioNumber )    {
      radio.openWritingPipe(pipes[0]);
      radio.openReadingPipe(1,pipes[1]);
    } else {
      radio.openWritingPipe(pipes[1]);
      radio.openReadingPipe(1,pipes[0]);
    }
	
	radio.startListening();
	
	// forever loop
	while (1)
	{
		if (role == role_ping_out)
		{
			// First, stop listening so we can talk.
			radio.stopListening();

			// Take the time, and send it.  This will block until complete

			printf("Now sending...\n");
			unsigned long time = millis();

			bool ok = radio.write( &time, sizeof(unsigned long) );

			if (!ok){
				printf("failed.\n");
			}
			// Now, continue listening
			radio.startListening();

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


			// Describe the results
			if ( timeout )
			{
				printf("Failed, response timed out.\n");
			}
			else
			{
				// Grab the response, compare, and send to debugging spew
				unsigned long got_time;
				radio.read( &got_time, sizeof(unsigned long) );

				// Spew it
				printf("Got response %lu, round-trip delay: %lu\n",got_time,millis()-got_time);
			}
			sleep(1);
		}

		//
		// 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
				unsigned long got_time;

				// Fetch the payload, and see if this was the last one.
				while(radio.available()){
					radio.read( &got_time, sizeof(unsigned long) );
				}
				radio.stopListening();
				
				radio.write( &got_time, sizeof(unsigned long) );

				// Now, resume listening so we catch the next packets.
				radio.startListening();

				// Spew it
				printf("Got payload(%d) %lu...\n",sizeof(unsigned long), got_time);
				
				delay(925); //Delay after payload responded to, minimize RPi CPU time
				
			}
		
		}

	} // forever loop

  return 0;
}