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
#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
#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);
}
}