void loop(void) { // Read the current command from the user std::string command; std::getline(std::cin, command); std::cerr << "Got '" << command << "'.\n"; // Split the command on spaces std::stringstream ss("0 "); ss << command; ss << " 0"; unsigned char bytes[PAYLOAD]; int buf = 0; // Read each space-separated value into an integer and put it into bytes int used = 0; for (int i = 0; i < PAYLOAD && ss >> buf; i++) { bytes[i] = (unsigned char)buf; used = i; } std::cerr << "Read " << used << " bytes from stdin." << std::endl; // Stop listening so we can talk. radio.stopListening(); // Send the bytes std::cerr << "Sending bytes: "; for (int i = 0; i < used; i++) { std::cerr << (int)bytes[i] << " "; } std::cerr << std::endl; bool ok = radio.write( bytes, used ); if (ok) { std::cerr << "ok..." << std::endl; } else { std::cerr << "failed..." << std::endl; // TODO: retry! } // Now, continue listening radio.startListening(); // Wait here until we get a response, or timeout (500ms) unsigned long started_waiting_at = __millis(); bool timeout = false; while ( !radio.available() && ! timeout ) { __msleep(5); //add a small delay to let radio.available to check payload timeout = ((__millis() - started_waiting_at) > TIMEOUT); } // Describe the results if (timeout) { std::cerr << "response timed out..." << std::endl; // TODO: retry! } else { // Grab the response, compare, and send to debugging spew radio.read( bytes, PAYLOAD ); // TODO: replace PAYLOAD // Print the bytes for (int i = 0; i < PAYLOAD; i++) { std::cout << (unsigned int)bytes[i] << " "; } std::cout << std::endl; } }
/** * * @return */ int left_ms() { return interval_end_ms - __millis(); }
/** * * @return */ bool expired() { return (interval_end_ms > 0L) && (__millis() >= interval_end_ms); }
/** * * @param ms */ void countdown_ms(unsigned long ms) { interval_end_ms = __millis() + ms; }
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(); }