0

So, I have an ATTiny85 sending data, awesome. I would like to have it send a message that consists of a few concatenated parts.

I have been reading about strings, buffers, chars, toCharArray and more for about 6 hours now and am completely puzzled by this.

Below, the variable deze will no transmit, but msg will. So I tried to do something like char *deze = "hello" + "world"; but that also fails.

What would be the best way to construct a piece of data that will be send by my 433MHz transmitter. I need to combine INT, String and float before sending it as uint8_t (bytes?).

#include <RH_ASK.h>

#define R_PIN PB4
#define T_PIN PB1
#define L_PIN PB3

RH_ASK driver(2000, R_PIN, T_PIN);

void setup()
{
  if (!driver.init()){
    pinMode(L_PIN, OUTPUT);
    digitalWrite(L_PIN, HIGH);
    delay(500);
    digitalWrite(L_PIN, LOW);
    delay(500);
  }
}

void loop()
{
  char deze[5];
  char *msg = "gewonechar";

  String thijs = "thijs";
  thijs.toCharArray(deze, sizeof(deze)-1);

  driver.send((uint8_t *)deze, strlen(msg));
  driver.waitPacketSent();
  delay(200);
}

[edit]
This is the working code on the receiver side.

struct data {
  uint16_t id;
  uint16_t pin;
  uint16_t value1;
  uint16_t value2;
} myData;

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

    if (driver.recv(buf, &buflen)) {

      memcpy(&myData, buf, buflen);

      Serial.print("ID: "); 
      Serial.println(myData.id);

      Serial.print("Pin: "); 
      Serial.println(myData.pin);

      Serial.print("Value1: "); 
      Serial.println(myData.value1);

      Serial.print("Value2: "); 
      Serial.println(myData.value2);

      Serial.println("");
    }
    server.handleClient();
    yield;
}

And the transmitter

#include <RH_ASK.h>

#define BAUD 2000
#define RX 11
#define TX 12

RH_ASK driver(BAUD, RX, TX);

struct data {
  uint16_t id;
  uint16_t pin;
  uint16_t value1;
  uint16_t value2;
} myData;

void setup() {
    Serial.begin(9600);   // Debugging only
    if (!driver.init())
         Serial.println("init failed");
}

void loop()
{
    struct data myData;

    myData.id = 1001;
    myData.pin = 4;
    myData.value1 = analogRead(4);
    myData.value2 = analogRead(5);

    driver.send((uint8_t *)&myData, sizeof(myData));
    driver.waitPacketSent();
    delay(2000);
}

Library used: http://www.airspayce.com/mikem/arduino/RadioHead/

1 Answer 1

2

First off you never want to use String - especially on a chip with a mere 512 bytes of SRAM.

You should really be learning about struct for sending of data in the most efficient way. A struct is a complex data type that combines multiple bits of data into one single variable - a bit like a record in a database.

You should avoid sending text as it is generally wasteful. The odd character, yes, but not whole strings of text.

For instance, you might want to send temperature, humidity and pressure:

struct data {
    int8_t temperature;
    uint8_t humidity;
    uint16_t pressure;
};

void sendData() {
    struct data myPacket;

    myPacket.temperature = 23;
    myPacket.humidity = 72;
    myPacket.pressure = 1016;

    driver.send((uint8_t *)&myPacket, sizeof(myPacket));
    driver.waitPacketSent();
}

With a struct, if you want to send text, you need to allocate enough space in the struct for your text. For instance, enough room for 9 characters plus NULL:

struct data {
    int8_t temperature;
    uint8_t humidity;
    uint16_t pressure;
    char weather[10];
};

Then, to get data into it, you (for example):

strcpy_P(myData.weather, (PGM_P)F("fog"));
7
  • is myPacket an 'instance' of the data 'class' here?
    – Thijs
    Apr 17, 2017 at 15:48
  • An "instance" of the data "struct", yes.
    – Majenko
    Apr 17, 2017 at 15:49
  • and how would I handle this at the receiver, what I now have is (char*)buf)
    – Thijs
    Apr 17, 2017 at 15:53
  • The same way. Create a struct exactly the same, then an instance of it, and use the pointer to that instance.
    – Majenko
    Apr 17, 2017 at 15:54
  • So I am receiving: driver.recv(buf, &buflen) and I created the struct, and an instance of it. But can I address the buf as a struct? It's not clear how the receiver understand the type so I can save the buff.temperature in the mypacket.temperature
    – Thijs
    Apr 17, 2017 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.