433mhz code help

Hi all I found a code recently about sending messages wirelessly but it only accepts text like "HELLO"... But what If I want to send data like sensor values ? For example I have a gps . What should I do so the gps data is being transfered from one arduino to another ?
This is the code that I found for the transmitter :

#include <VirtualWire.h>

void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup"); // Prints "Setup to the serial monitor"
vw_set_tx_pin(12); // Sets pin D12 as the TX pin
vw_setup(4000); // Bits per sec
}

void loop()
{
const char *msg ="HELLO" ; // Message to be sent
vw_send((byte *)msg, strlen(msg)); //Sending the message
vw_wait_tx(); // Wait until the whole message is gone
delay(200); // A short gap.
}

And this is the code for the reciever :

#include <VirtualWire.h>
int count;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup"); //Prints "Setup" to the serial monitor
vw_set_rx_pin(4); //Sets pin D12 as the RX Pin
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(4000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}

void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");

for (i = 0; i < buflen; i++)
{
char c = (buf*);*

  • Serial.print(c);*

  • Serial.print(" ");*

  • }*

  • count++;*

  • // Serial.print(count);*

  • Serial.println("");*

  • digitalWrite(13, false);*

  • }*

}
P.S I am not exactly understanding the code because I am new to the arduino world but I am going to take some lessons the following year , so any explanaition about the code is welcome .Thanks

Looking at VirtualWire.h, it seems to be agnostic to data type. So, I’d pack all the sensor data in a struct and send it as binary. See possible code below (untested, but it does compile) Be sure to keep your stuct size under VW_MAX_PAYLOAD.

#include <VirtualWire.h>
#define SENDING_PERIOD 5000UL  // I don't know, maybe send your data once every 5 seconds?

typedef struct {
  uint32_t sensor1Data;
  int16_t sensor2Data;
  float sensor3Data;
} sensorStruct;

uint32_t timer;

void setup() {
  Serial.begin(9600);     // Debugging only
  Serial.println("setup"); // Prints "Setup to the serial monitor"
  vw_set_tx_pin(12);          // Sets pin D12 as the TX pin
  vw_setup(4000);           // Bits per sec
  timer = millis() - SENDING_PERIOD;
}

void loop() {
  sensorStruct combinedSensorData;

  if ((millis() - timer) >= SENDING_PERIOD) {
    timer += SENDING_PERIOD;

    // Here you would call functions to fill in your sensor data structure

    // Then, send your data
    vw_send((uint8_t *)&combinedSensorData, sizeof(sensorStruct));
    vw_wait_tx();
  }
}

Sooooo it should look something like this? :

#include <VirtualWire.h>
#define SENDING_PERIOD 5000UL // I don't know, maybe send your data once every 5 seconds?
#include "TinyGPS++.h"
#include "SoftwareSerial.h"
SoftwareSerial serial_connection(10, 11); //RX=pin 10, TX=pin 11
TinyGPSPlus gps;
typedef struct {
uint32_t sensor1Data;
int16_t sensor2Data;
float sensor3Data;
} sensorStruct;

uint32_t timer;

void setup() {
Serial.begin(9600); // Debugging only
Serial.println("setup"); // Prints "Setup to the serial monitor"
vw_set_tx_pin(12); // Sets pin D12 as the TX pin
vw_setup(4000); // Bits per sec
timer = millis() - SENDING_PERIOD;
Serial.begin(9600);//This opens up communications to the Serial monitor in the Arduino IDE
serial_connection.begin(9600);//This opens up communications to the GPS
Serial.println("GPS Start");//Just show to the monitor that the sketch has started
Serial.println("Satellite Count:");
Serial.println(gps.satellites.value());
}

void loop() {
sensorStruct combinedSensorData;

if ((millis() - timer) >= SENDING_PERIOD) {
timer += SENDING_PERIOD;

while(serial_connection.available())//While there are characters to come from the GPS
{
gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
}
if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
{
//Get the latest info from the gps object which it derived from the data sent by the GPS unit

const char *msg (gps.location.lat(), 6) ;
(gps.location.lng(), 6);

}
// Here you would call functions to fill in your sensor data structure

// Then, send your data
vw_send((uint8_t *)&combinedSensorData, sizeof(sensorStruct));
vw_wait_tx();
}
}

Please read the Read this before posting a programming question Sticky Topic at the top of this forum and learn how to post your code with code tags.

... and this one too: How to use this forum - please read.

#include <VirtualWire.h>
#define SENDING_PERIOD 5000UL  // I don't know, maybe send your data once every 5 seconds?
#include "TinyGPS++.h"
#include "SoftwareSerial.h"
SoftwareSerial serial_connection(10, 11); //RX=pin 10, TX=pin 11
TinyGPSPlus gps;
typedef struct {
  uint32_t sensor1Data;
  int16_t sensor2Data;
  float sensor3Data;
} sensorStruct;

uint32_t timer;

void setup() {
  Serial.begin(9600);     // Debugging only
  Serial.println("setup"); // Prints "Setup to the serial monitor"
  vw_set_tx_pin(12);          // Sets pin D12 as the TX pin
  vw_setup(4000);           // Bits per sec
  timer = millis() - SENDING_PERIOD;
  Serial.begin(9600);//This opens up communications to the Serial monitor in the Arduino IDE
  serial_connection.begin(9600);//This opens up communications to the GPS
  Serial.println("GPS Start");//Just show to the monitor that the sketch has started
  Serial.println("Satellite Count:");
  Serial.println(gps.satellites.value());
}

void loop() {
  sensorStruct combinedSensorData;

  if ((millis() - timer) >= SENDING_PERIOD) {
    timer += SENDING_PERIOD;

while(serial_connection.available())//While there are characters to come from the GPS
  {
    gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
  }
  if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
  {
    //Get the latest info from the gps object which it derived from the data sent by the GPS unit
    
  
const char    *msg (gps.location.lat(), 6) ;
(gps.location.lng(), 6);
   
  }
    // Here you would call functions to fill in your sensor data structure

    // Then, send your data
    vw_send((uint8_t *)&combinedSensorData, sizeof(sensorStruct));
    vw_wait_tx();
  }
}

The code I posted was only a framework for you to consider for writing your own code. Off the top of my head, the things you’ll need to do are:

  • Customize the ‘struct’ to the particular type of your sensor data.

  • Figure out how often to read the sensors verses how often to send the data. Do you need to do any averaging? Structure your code accordingly. If the sensors take a long time to produce data (or average multiple readings), get that going during the idle time, don’t wait until it’s time to send.

  • Actually read the data from the sensors.

  • Transfer the sensor data into the struct.

  • On the other end, have the VirtualWire library put the received data into an identically-defined struct.

  • Pull the received data out of the struct and do something useful with it.

I’m not at all familiar with the TinyGPSPlus library, but your code looks funny. Does it even compile? Even if that code is right, you don’t seem to be doing anything useful with the GPS data.

You probably want to read up on structures and pointers as well as C programming in general. Don’t try to tackle it all at once. Break it up into small, manageable sub-tasks.

When it comes to coding I 'm total useless ... I dont know how to code complex stuff . I am more keen on the engineering and electronics part of the project . As I said I am about to take some coding lessons this year but till then is there anything that I can do in order to improve my skills ?
By the way the code does compile but it does nothing so I deleted it . And yeah I think my code is funny just because I have no freaking idea what I am doing...I had the same problem when I tried to program an accelerometer ...didnt understand anything from the code so I move on something easier . The gps worked fine but that's it ...The only things that I know how to program are this gps , an ultrasonic sensor , servos , LEDs ,IR , Touch sensor and partly a sound sensor because I can only read 0 and 1 .For example I cant get a 50 , a 16 , a 99 etc so I can say If ( sensorValue>=56) {
digitalWrite(LED , HIGH)
}
else {
digitalWrite(LED, LOW)
}

Expl0siv3Man79:
As I said I am about to take some coding lessons this year but till then is there anything that I can do in order to improve my skills ?

Write more code ?

Go watch some of the many available tutorial sequences instead of waiting for forum people to hold your hand and inch along.

Learn the language before trying to read it.

You wouldn't open a book written in a foreign language and ask people what every single word means as you make your way through the book, you'd learn the language first.

When newbies post copy/pasted code and want them deciphered into plain English, what they sort of learn is a bunch of scattered tidbits with no structure and no context. Analagous to the foreign book example, flipping to random pages, looking at random words, and understanding what those particular words mean. You learn something, but nothing meaningful.