I have the following situation:
One Arduino Uno with a 433mhz transmitter wired to the laptop to have serial communication. Another Arduino Uno with a 9V battery wired with a 433mhz receiver and an LCD screen (16x2, I2C).
I have the following code to transmit:
#include <VirtualWire.h>
void setup() {
//Force low when enabled
vw_set_ptt_inverted(true);
//Set pin 12 for transmitter
vw_set_tx_pin(12);
//Setup with speed of the data transfer.
vw_setup(4000);
//Set serial communication at 9600 baud
Serial.begin(9600);//set Serial
//Set pin 13 to light up when transmitting
pinMode(13,OUTPUT);
}
void loop() {
//If message send via serial monitor
if (Serial.available()) {
//wait a bit for the entire message to arrive
delay(100);
while (Serial.available() > 0) {
//Read serial into string
String message = Serial.readString();
//Initialize char array
char msg[message.length()];
//Write string to char array
message.toCharArray(msg, message.length());
//send char array as unsigned int (bits)
vw_send((uint8_t *)msg, strlen(msg));
//Wait until the complete message has been send
vw_wait_tx();
//Turn on light when finished
digitalWrite(13,HIGH);
delay(200);
}
}else{
//Turn off light
digitalWrite(13, LOW);
}
}
And the following code to receive a message and print on a LCD screen:
#include <LiquidCrystal_I2C.h>
#include <VirtualWire.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup() {
//start up LCD with 16 chars with 2 lines
lcd.begin(16,2);
//Turn on backlight
lcd.backlight();
//Write initialising message
lcd.setCursor(0,0);
lcd.print("Initialisation..");
//Force low when enabled
vw_set_ptt_inverted(true);
//Set pin 12 to receive
vw_set_rx_pin(12);
//Setup with speed of the data transfer.
vw_setup(4000);
//set pin 13 for light
pinMode(13, OUTPUT);
//Start receiver
vw_rx_start();
//Set message on LCD that setup has completed
lcd.setCursor(0,1);
lcd.print("Completed!");
}
void loop() {
//initialize array for message
uint8_t msg[VW_MAX_MESSAGE_LEN];
uint8_t msgLength = VW_MAX_MESSAGE_LEN;
//If message received
if (vw_get_message(msg, &msgLength)){
//Turn on light
digitalWrite(13, HIGH);
//Print on lcd
lcd.clear();
lcd.setCursor(0,0);
lcd.write(msg, msgLength);
}else{
//Else turn of light
digitalWrite(13, LOW);
}
}
My problem is that the message that is being transmitted and received by the other arduino always is short 1 character. For example if I send "test", it receives and displays "tes". What am I missing?
(My short solution was to just add a space to the message, then the space got cut off. But I want to understand what is going wrong.)