I want to send a String written through Serial.read() to the arduino via RF virtual wire to another arduino but i get the error message:
invalid cast from type 'String' to type 'uint8_t* {aka unsigned char*}'
so what do i need to change in order to be able to send variable strings which i send to the arduino via Serial?
TRANSMITTER
#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
String msg;
void setup() {
Serial.begin(9600);
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RF Link module
vw_setup(2000); // Bits per sec
vw_set_tx_pin(7); // pin 3 is used as the transmit data out into the TX Link module, change this as per your needs
}
void loop() {
delay(1000);
if (Serial.available() > 0) {
msg = "";
while (Serial.available()) {
char received = Serial.read();
msg += received;
}
vw_send((uint8_t *)msg, strlen(msg) );
vw_wait_tx();
delay(200);
}
}
RECEIVER (working fine)
#include <VirtualWire.h> // you must download and install the VirtualWire.h to your hardware/libraries folder
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
Serial.begin(9600);
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for RX Link Module
vw_setup(2000); // Bits per sec
vw_set_rx_pin(6); // We will be receiving on pin 4 i.e the RX pin from the module connects to this pin.
vw_rx_start(); // Start the receiver
}
void loop() {
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)){
for (int i = 0; i < buflen; i++) {
Serial.print((char)buf[i]);
}
Serial.println("");
}
}
gives me following error:
cannot convert 'String' to 'const char*' for argument '1' to 'size_t strlen(const char*)'
cannot convert 'String' to 'const char*' for argument '1' to 'size_t strlen(const char*)'
If you do it right, you won't. Look at your call to toCharArray() and my call to toCharArray() in reply #11. The first argument to toCharArray() is NOT the first element beyond the array (which is a char). It is the array.