How to get Int value ?

I am using Mirf example. I am sending distance data as string from Client to Server and Server is sending it back to Client and I am getting the right data back which I sent to Server. What I want to do at Server is… receive data (string like “123”, “45”…) at server which is working and converting it to integer value like 123, 45…

I have seen this also. http://arduino.cc/en/Tutorial/StringToIntExample

In Server code I did like this

iny=t distance = data.toInt();

but it gives error while compiling.

Client Code

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

#include <NewPing.h>

#define TRIGGER_PIN  4  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     5  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 700 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup(){
  Serial.begin(9600);
  /*
   * Setup pins / SPI.
   */
   
  /* To change CE / CSN Pins:
   * 
   * Mirf.csnPin = 9;
   * Mirf.cePin = 7;
   */
  /*
  Mirf.cePin = 7;
  Mirf.csnPin = 8;
  */
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  
  /*
   * Configure reciving address.
   */
   
  Mirf.setRADDR((byte *)"clie1");
  
  /*
   * Set the payload length to sizeof(unsigned long) the
   * return type of millis().
   *
   * NB: payload on client and server must be the same.
   */
   
  Mirf.payload = sizeof(unsigned long);
  
  /*
   * Write channel and payload config then power up reciver.
   */
   
  /*
   * To change channel:
   * 
   * Mirf.channel = 10;
   *
   * NB: Make sure channel is legal in your area.
   */
   
  Mirf.config();
  
  Serial.println("Beginning ... "); 
}

void loop(){
  unsigned long time = millis();
  unsigned long dist, dist1;
  
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  dist = (uS / US_ROUNDTRIP_CM);
  Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  
  Mirf.setTADDR((byte *)"serv1");
  
  Mirf.send((byte *)&dist);
  
  while(Mirf.isSending()){
  }
  Serial.println("Finished sending");
  delay(10);
  while(!Mirf.dataReady()){
    Serial.println("Waiting");
    if ( ( millis() - time ) > 1000 ) {
      Serial.println("Timeout on response from server!");
      return;
    }
  }
  
  Mirf.getData((byte *) &dist1);
  
  Serial.print("Ping: ");
  Serial.println(dist1);
  
  delay(1000);
}

Server Code

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

#include <LED_Bar.h>

LED_Bar bar(2, 3);

void setup(){
  Serial.begin(9600);
  
  /*
   * Set the SPI Driver.
   */

  Mirf.spi = &MirfHardwareSpi;
  
  /*
   * Setup pins / SPI.
   */
   
  Mirf.init();
  
  /*
   * Configure reciving address.
   */
   
  Mirf.setRADDR((byte *)"serv1");
  
  /*
   * Set the payload length to sizeof(unsigned long) the
   * return type of millis().
   *
   * NB: payload on client and server must be the same.
   */
   
  Mirf.payload = sizeof(unsigned long);
  
  /*
   * Write channel and payload config then power up reciver.
   */
   
  Mirf.config();
  
  Serial.println("Listening..."); 
}

void loop(){
  /*
   * A buffer to store the data.
   */
   
  byte data[Mirf.payload];
  
  /*
   * If a packet has been recived.
   *
   * isSending also restores listening mode when it 
   * transitions from true to false.
   */
   
  if(!Mirf.isSending() && Mirf.dataReady()){
    Serial.println("Got packet");
    
    /*
     * Get load the packet into the buffer.
     */
     
    Mirf.getData(data);
    
    /*
     * Set the send address.
     */
     
     
    Mirf.setTADDR((byte *)"clie1");
    
    /*
     * Send the data back to the client.
     */
     
    Mirf.send(data);
    
    
    /*
     * Wait untill sending has finished
     *
     * NB: isSending returns the chip to receving after returning true.
     */
      
    Serial.println("Reply sent.");
        
    delay(100);
  }
  
  
}

In Server code I did like this

Code:
iny=t distance = data.toInt();

but it gives error while compiling.

Yes f course it does. Just look at it.Have a few of these ;;;;;; it looks like you need them.

Mark

It is not working in Arduino 1.0.5

It doesn't matter which version of the IDE, iny=t distance = data.toInt(); will not compile with the code you have shown us.

Sorry, it is int distance = data.toInt();

I found that data.toInt(); is recognized in Arduino 1.5.x but I have a strange problem in Arduino 1.5.x. It says Mirf.xyz is not declared in this scope. I just added Mirf library and SPI library to Arduino 1.5.x and tried to compile my Server code.

I reinstalled Arduino and all libraries required but I am getting this error for my Mirf nRF24L01 Server code.

Arduino: 1.5.7 (Windows 8), Board: "Arduino Nano, ATmega328"

ping_server2.ino: In function 'void loop()':
ping_server2.ino:102:21: error: request for member 'toInt' in 'data', which is of non-class type 'byte [(((sizetype)(((ssizetype)Mirf.Nrf24l::payload) + -1)) + 1)] {aka unsigned char [(((sizetype)(((ssizetype)Mirf.Nrf24l::payload) + -1)) + 1)]}'

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

int distance = data.toInt();

What is "data" here ? data appears to be defined as an array of bytes. data.toInt( ) would be a call to a function/method of a class object, which data is not. If data is supposed to be a String object, then you need to create data as a String object before you try to invoke methods on it.

Also, the following line looks invalid. Even if "mirf.payload" evaluates to a valid value for the length of an array, I don't think you can define an array this way. The array declaration has to either have a length which can be evaluated at compile-time, or else it has to be dynamically allocated.

 byte data[Mirf.payload];

michinyon:
Also, the following line looks invalid. Even if "mirf.payload" evaluates to a valid value for the length of an array, I don't think you can define an array this way. The array declaration has to either have a length which can be evaluated at compile-time, or else it has to be dynamically allocated.

 byte data[Mirf.payload];

In C++ its an extension provided by GCC, its only part of ISO C99