Example #1
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();
    }