Go Down

Topic: virtual wire multiple values (Read 1 time) previous topic - next topic

jmnijsse

With tips & trucks from all of you at least i have a code which is working (completed with a DHT11 sensor):

Sender:
Code: [Select]

#include <VirtualWire.h>
#include <DHT.h>

#define DHTPIN 2

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
  Serial.begin(9600);
  Serial.println("setup");
  vw_set_tx_pin(12);
  vw_setup(2000);
  pinMode(13,OUTPUT);
  dht.begin();
}
void loop()
{
  int h = dht.readHumidity();
  int t = dht.readTemperature();

  String messageString = String();
  for(int i=0;i<6;i++){
    messageString.concat(analogRead(i));
    messageString.concat(",");
  }
  messageString.concat(t);
  messageString.concat(",");
  messageString.concat(h);
  messageString.concat(",");

  char msg[messageString.length()+1];
  messageString.toCharArray(msg,messageString.length()+1);
  digitalWrite(13, true);
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx();
  digitalWrite(13, false);
  delay(1000);
}


Reciever:

Code: [Select]
#include <VirtualWire.h>
void setup()
{
     Serial.begin(9600);
    Serial.println("setup");
    Serial.println("CLEARDATA");
    vw_setup(2000);
    vw_set_rx_pin(11);
    vw_rx_start();
    pinMode(13,OUTPUT);
}
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(13, true);
//Serial.print("Got:   ");
Serial.println("CLEARDATA");
Serial.print("DATA, ,");
for (i = 0; i < buflen; i++)
{
 
    Serial.write(buf[i]);
    Serial.print(" ");
}
Serial.println(" ");

     digitalWrite(13, false);
    }
}


The concat-function works fine for me.
The CLEARDATA and DATA prints are necessary for the transport to Excel.

Thanks so far...

Gr.
Johan

PaulS

A char array, large enough to hold what you are sending, and sprintf() will use far fewer resources on the sender.

On the receiver, you can use strtok() to extract the two values from the string, and atoi() to convert them to numbers, if you want to.

Go Up