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. toInt() - Arduino Reference
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);
}
}