Ejemplo n.º 1
0
void ping_client(void)
{
	util_init();

	// Start up
	nRF905_init();

	// Set address of this device
	uint8_t addrRx[] = RXADDR;
	nRF905_setRXAddress(addrRx);

	// Interrupts on
	sei();

	// LED indicator
	DDRC |= _BV(DDC5);
	PORTC |= _BV(PORTC5);

	// Put into receive mode
	nRF905_receive();
	
	uint8_t counter = 0;

	while(1)
	{
		// Make data
		char data[NRF905_MAX_PAYLOAD] = {0};
		sprintf_P(data, PSTR("test %hhu"), counter);
		counter++;

		unsigned long startTime = millis();

		// Set address of device to send to
		uint8_t addr[] = TXADDR;
		nRF905_setTXAddress(addr);

		// Set payload data
		nRF905_setData(data, sizeof(data));

		// Send payload (send fails if other transmissions are going on, keep trying until success)
		while(!nRF905_send());

		// Put into receive mode
		nRF905_receive();

		// Make buffer for reply
		uint8_t buffer[NRF905_MAX_PAYLOAD];
		bool success;

		// Wait for reply with timeout
		unsigned long sendStartTime = millis();
		while(1)
		{
			success = nRF905_getData(buffer, sizeof(buffer));
			if(success)// Got data
				break;

			// Timeout
			if(millis() - sendStartTime > TIMEOUT)
				break;
		}

		// If success toggle LED and send ping time over UART
		if(success)
		{
			unsigned int totalTime = millis() - startTime;
			PORTC ^= _BV(PORTC5);

			printf_P(PSTR("Ping time: %ums\n"), totalTime);

			// Print out ping contents
			printf_P(PSTR("Data from server: "));
			for(uint8_t i=0;i<sizeof(buffer);i++)
				uart_put_nb(buffer[i]);
			puts_P(PSTR(""));
		}
		else
			puts_P(PSTR("Ping timed out"));

		_delay_ms(100);					
	}
}
Ejemplo n.º 2
0
void main(void)
{
	clock_prescale_set(clock_div_1);

	// Timer 0 settings for approx. millisecond tracking
	TCCR0A = _BV(WGM01);
	TCCR0B = CLOCKSEL;
	TIMSK0 = _BV(OCIE0A);
	OCR0A = TICKS;

	// UART
	//PORTD |= _BV(PORTD0);
	//DDRD |= _BV(DDD1);
	UBRR0 = UBRR_VALUE;
#if USE_2X
	UCSR0A = _BV(U2X0);
#endif
	UCSR0B = _BV(TXEN0);
	
	// LED indicator
	DDRC |= _BV(DDC5);
	PORTC |= _BV(PORTC5);

	stdout = &uart_io;

	// Start up
	nRF905_init();

	// Set address of this device
	nRF905_setListenAddress(RXADDR);

	// Interrupts on
	sei();

	uint8_t counter = 0;
	uint32_t sent = 0;
	uint32_t replies = 0;
	uint32_t timeouts = 0;
	uint32_t invalids = 0;

	while(1)
	{
		// Make data
		char data[NRF905_MAX_PAYLOAD] = {0};
		sprintf_P(data, PSTR("test %hhu"), counter);
		counter++;
		
		packetStatus = PACKET_NONE;
		
		printf_P(PSTR("Sending data: %s\n"), data);

		uint32_t startTime = millis();

		// Send the data (send fails if other transmissions are going on, keep trying until success) and enter RX mode on completion
		while(!nRF905_TX(TXADDR, data, sizeof(data), NRF905_NEXTMODE_RX));
		sent++;

		puts_P(PSTR("Data sent, waiting for reply..."));

		uint8_t success;

		// Wait for reply with timeout
		uint32_t sendStartTime = millis();
		while(1)
		{
			success = packetStatus;
			if(success != PACKET_NONE)
				break;
			else if(millis() - sendStartTime > TIMEOUT)
				break;
		}

		if(success == PACKET_NONE)
		{
			puts_P(PSTR("Ping timed out"));
			timeouts++;
		}
		else if(success == PACKET_INVALID)
		{
			// Got a corrupted packet
			puts_P(PSTR("Invalid packet!"));
			invalids++;
		}
		else
		{
			// If success toggle LED and send ping time over UART
			uint16_t totalTime = millis() - startTime;
			PORTC ^= _BV(PORTC5);

			replies++;

			printf_P(PSTR("Ping time: %ums\n"), totalTime);

			// Get the ping data
			uint8_t replyData[NRF905_MAX_PAYLOAD];
			nRF905_read(replyData, sizeof(replyData));

			// Print out ping contents
			printf_P(PSTR("Data from server: "));
			for(uint8_t i=0;i<sizeof(replyData);i++)
				printf_P(PSTR("%c"), replyData[i]);
			puts_P(PSTR(""));
		}

		printf_P(PSTR("Totals: %lu Sent, %lu Replies, %lu Timeouts, %lu Invalid\n------\n"), sent, replies, timeouts, invalids);

		_delay_ms(1000);			
	}
}