HC12 Sending multiple variables?

Hi,

I would like to send 2 analog variables with my HC12 module to another arduino. I spend hours on the internet searching a code but i found nothing that works for me. Can anybody help me please?

Kind regards, Simon van den Berg

Hello Simon, Welcome.

Am I correct in thinking that you want someone to provide working code for you? If that's the case please click 'report to moderator' and ask for this to be moved to 'gigs and collaboration' and indicate how much you are willing to pay.

Thank you.

@SimonVanDenBerg

Other post/duplicate DELETED Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future. Other general help and troubleshooting advice can be found here. It will help you get the best out of the forum in the future.

I believe a HC12 sends data as if it was a serial connection.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

Hi,

Thanks for your reply.
I found a code on a older post by Cattledog: https://forum.arduino.cc/index.php?topic=630040.0
But i can’t get it to work. I tried this with my NRF24l01 Modules with wire library and it works fine but i can’t get it to work with my HC12 module. It prints all the values x,y,z randomly. Can somebody help me please?

//Sender Arduino
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin


struct MyData {
byte x = 10;
byte y = 20;
byte z = 30;
};
MyData data;



void setup()
{
  HC12.begin(9600);
}

void loop()
{
HC12.write((byte*)&data, sizeof(MyData));
}







//Receiver Arduino
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin


struct MyData {
byte x;
byte y;
byte z;
};
MyData data;



void setup()
{
  Serial.begin(9600);
  HC12.begin(9600);
}

void loop()
{
  if (HC12.available()) { 
HC12.readBytes((byte*)&data, sizeof(MyData));
  }
  
Serial.println(data.x); // Test of it works
}

SimonVanDenBerg:
I found a code on a older post by Cattledog: https://forum.arduino.cc/index.php?topic=630040.0
But i can’t get it to work. I tried this with my NRF24l01 Modules with wire library and it works fine but i can’t get it to work with my HC12 module.

The nRF24 and the Wire library are very different because both of them invisibly encapsulate the message with start- and end-markers so that it is sent as a package.

As I mentioned in Reply #3 I believe the HC12 sends data like a serial connection and if so then the data is not encapsulated automatically. The code I suggested in Reply #3 does encapsulate the data between start- and end-markers. However it assumes you are sending data as human readable text.

If you need to data in binary format then I posted some simple code to send a struct between Arduinos using Serial. But I suspect that my system for sending textual data is more reliable.

…R

The sending routine in the code posted in relpy #4 is free running and will soon overwhelm output and input buffers.

Place a delay() in the sending routine to confirm this, or better use a millis() timer.

In most applications, the sending routine is timed.