SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Harsha_P922
#192968
I am trying convert a string to char array and send it over 433Mhz transmitter using virtual wire. But on the receiver end I am receiving something else.
I've tried examples of virtual wire transmitter and receiver are working fine.
Why isn't the data I am transmitting and receiving same?
This is the code for transmitter
Code: Select all
#include <VirtualWire.h>
String test; 
int i; 
String testval[55]; 
int l; 
String strappend; 
char charbuf[10];
void setup() {
  vw_setup(2000);
  vw_set_tx_pin(12);
  Serial.begin(9600);
}

int ascii() {
  strappend = ""; String db[10] = {"51", "52", "53", "54"};
  String cyph = db[random(0, 3)];
  Serial.println(cyph);
  delay(100);
  for (i = 0; i <= test.length(); i++) {
    if (testval[i] == "h") {
      strappend = strappend + "10" + cyph;
    }
    else if (testval[i] == "e") {
      strappend = strappend + "11" + cyph;
    }
    else if (testval[i] == "l") {
      strappend = strappend + "12" + cyph;
    }
    else if (testval[i] == "o") {
      strappend = strappend + "13" + cyph;
    }
    else if (testval[i] == " ") {
      strappend = strappend + "14" + cyph;
    }
    else if (testval[i] == "w") {
      strappend = strappend + "15" + cyph;
    }
    else if (testval[i] == "r") {
      strappend = strappend + "16" + cyph;
    }
    else if (testval[i] == "d") {
      strappend = strappend + "17" + cyph;
    }
    else {
      strappend += "99";
    }
  } 
  Serial.println(strappend);
}
void loop() {
  delay(1000); 
  ascii();
  if (Serial.available() > 0) {
    test = Serial.readString();
    l = test.length();
    delay(500);
    strappend.toCharArray(charbuf, 10);
    Serial.println(charbuf);
    for (i = 0; i <= l; i) {
      testval[i] = test.substring(i, i++);
      Serial.println(testval[i]);
    }
  }
  Serial.println(charbuf[2]);
  vw_send((uint8_t *)charbuf, 10);
  vw_wait_tx();
}
This is the code for receiver
Code: Select all
#include <VirtualWire.h>
int led_pin = 13;
const int transmit_pin = 12;
const int receive_pin = 11;

void setup()
{
  delay(1000);
  Serial.begin(9600);  // Debugging only
  Serial.println("setup");

  // Initialise the IO and ISR
  vw_set_tx_pin(transmit_pin);
  vw_set_rx_pin(receive_pin);

  vw_setup(2000);

  vw_rx_start();
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen))
  {
    int i;
    digitalWrite(led_pin, HIGH);
    Serial.print("Got: ");

    for (i = 0; i < buflen; i++)
    {
      Serial.print(buf[i], HEX);
      Serial.print(' ');
    }
    Serial.println();
    digitalWrite(led_pin, LOW);
  }
}
By jremington
#192971
It is hard to see what you are trying to do, just to send a few characters. Make sure simple tests work before you try something complicated.

In any case it is a bad idea to use Strings with Arduino. They cause memory problems and are completely unnecessary.

Use simple character arrays instead, and string functions like sprintf(), itoa() etc. to format data. Or just send data.
By Harsha_P922
#192972
jremington wrote:It is hard to see what you are trying to do, just to send a few characters. Make sure simple tests work before you try something complicated.
I am masking a string with another string so only intended receivers can decode it.

Could you suggest where should i change the code?
By Valen
#192984
Have you got any examples of what you sent and what was received?

Also do note that anyone (or any "thing" like weather sensor/wireless doorbell) in your neighborhood could be transmitting in that band and disrupt your stream of data. Test your code with a secure electrical link (real wire) between devices first before you try it out on the dirty ether.