SparkFun Forums 

Where electronics enthusiasts find answers.

All things pertaining to wireless and RF links
By dpward
#168911
HI All,
Please help, I am really struggling.... :x
I am new to Arduino and am working on a project using two Arduino Uno's, a 433Mhz Transmitter and reciever and an Ultrasonic Sensor HC-SRO4. I want to send the distance data from the untrasonic sensor from one Arduino to another. The project is to monitor the Kerosene levels in a home heating oil tank using the sensor and wirelessly transmit the data to an arduino board inside the house with an LCD display on it.

Using code that I found Online I can get the Ultrasonic sensor to measure distance on one arduino. I also found code online that transmits a text message("Hello") from one arduino to another. I am struggling to get the code to transmit the distance data from one UNO to the other. This is the code I am working with. I think I need to packet the dat to transmit...Anyone know the code for this?



//code for testing Ultrasonic Sensor
#include <VirtualWire.h>

#define ECHOPIN 3 // pin to recieve echo pulse
#define TRIGPIN 2 // Pin to recieve Trigger Pulse


void setup ()
{
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}

void loop ()
{
// Start Ranging - Generating a trigger of 10us burst
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN,LOW);


//Distance Calculation


float distance = pulseIn(ECHOPIN, HIGH);
distance= distance/58;
Serial.print(distance);
Serial.println(" cm");
{
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
}
send (distance);
}
void send (char *distance)
{
vw_send((uint8_t *)distance, strlen(distance));
vw_wait_tx(); // Wait until the whole message is gone
delay(200);
}
By Valen
#168919
It looks like he is allready aware of that library. Atleast there are references in the code to it.

@ dpward: What are you receiving on the endpoint?

[EDIT] Disregard the following. I just noticed, 2000 bits/sec. seems ok. Also, shouldn't vw_setup be in "void Setup()" instead of in void loop()?

[disregard]The baudrate might be a bit high for those kind of transmitter/receivers. Have you tried lowering it? I guess it depends on the specific model that you are using, so better tell us that too so we can check it's specs. (if those can be found)[/disregard]
By dpward
#168958
Thanks for the responses. Yes I am aware of Virtaul Wire library and have been using it. As I said I can transmit a text message using it however when I try to transmit the distance measurement (eg. "8.0cm") I get nothing back. I am using the folouwing:
2 x Arduino Uno's
One Arduino connected to HC-SR04 Ultrasonic Sensor and a Transmitter
One arduino connected to Reciever

Receiver module parameters
1.Product Model: MX-05V
2.Operating voltage: DC5V
3.Quiescent Current: 4MA
4.Receiving frequency: 433.92MHZ
5.Receiver sensitivity:-105DB
6.Size: 30 * 14 * 7mm
7.External antenna: 32CM single core wire, wound into a spiral


Technical parameters of the transmitter head
1.Product Model: MX-FS-03V
2.Launch distance :20-200 meters (different voltage, different results)
3.Operating voltage :3.5-12V
4.Dimensions: 19 * 19mm
5.Operating mode: AM
6.Transfer rate: 4KB / S
7.Transmitting power: 10mW
8.Transmitting frequency: 433M
9.An external antenna: 25cm ordinary multi-core or single-core line
10.Pinout from left ? right: (DATA; VCC; GND)
By dpward
#168959
Had a breakthrough...Just need some advide on the data being sent. I am currently recieving the following on the serial monitor.Can you help in getting the proper data shown....Is this Hex data?
Distance: ý { Ë
Distance: ý { Ë
Distance: ý { Ë
Distance: ý { Ë
Distance: ý { Ë
Distance: ý { Ë
The Sending Code is:
#include<VirtualWire.h>
#define trigPin 2
#define echoPin 3
const int transmit_pin = 12;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
vw_set_tx_pin(transmit_pin);
vw_setup(2000); // Transmission rate
}

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
vw_send((uint8_t *)distance, 3);
vw_wait_tx(); // Wait until the whole message is gone delay(1000);
}
}


The Recieving code is:
// Include VirtualWire library
#include <VirtualWire.h>

// Pins definition
const int led_pin = 13;
const int transmit_pin = 12;
const int receive_pin = 11;

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

// Initialise the IO and ISR
vw_set_rx_pin(receive_pin);
vw_setup(2000); // Transmission rate

// Start the receiver PLL
vw_rx_start();

// Set LED pin
pinMode(led_pin, OUTPUT);
}

void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

// Check if a message was received
if (vw_get_message(buf, &buflen))
{
// Flash a light to show received good message
digitalWrite(led_pin, HIGH);
Serial.print("Distance: ");

// Print message
for (int i = 0; i < buflen; i++)
{
Serial.print(char(buf));
Serial.print(' ');
}
Serial.println();
digitalWrite(led_pin, LOW);
}
}
By Valen
#168962
Well, what looks like spaces may infact represent a binary number for which no ascii character is assigned. Or atleast no image bitmap. So it is a bit hard to decipher "ý { Ë", not knowing what to expect. Maybe you could use a different Terminal program that can show the received characters in Hexadecimal format. And make sure you send the same distance value (as ascii character string) over the normal serial port to the terminal program to check what the virtual wire is supposed to be transmitting.

Your sending void loop:
void loop() {
int duration, distance; // defined as integer value
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1; // minor objection: integer divided by float constant and stored as integer, .. are you sure this leads to the desired rounding or truncation of the fraction part?
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm"); // Sent as ascii characters over serial UART
vw_send((uint8_t *)distance, 3); // distance is sent as a 8-bit unsigned binary value, not represented ina string of ascii characters, ... and over virtual wire, not to serial UART for debugging output
vw_wait_tx(); // Wait until the whole message is gone delay(1000);
}
}
By jremington
#168968
To continue Valen's analyis, your are sending the 2 byte distance as a 3 binary values (why 3?). You then use this code to print those values to the monitor:
Code: Select all
for (int i = 0; i < buflen; i++)
{
Serial.print(char(buf[i]));
Serial.print(' ');
}
In the special case that the argument to Serial.print() is character, it sends that character to the screen. This will usually be uninterpretable.

What you should be doing is sending 2 bytes, not 3, for distance and interpreting those bytes on the receiving end as an integer. One way to do that is to read in both bytes, then depending on the byte order of the integer, convert them to a 16 bit value:

int distance = 256*buf[1] + buf[0];