Sending multiple sensor values using HC 12.

Hi,
I am newbie to Arduino.
I want to send multiple sensor vales over HC 12. Module as below.

  1. Pot value
  2. Switch position.

I wrote up below code. I created stuct to store pot value & switch value & trying to send that struc using HC12.write command.

#include <SoftwareSerial.h>

SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

struct MyData {
byte pot_value;
int switch_value;
};
MyData data;

int switch_pin = 5;
int switch_value;

void setup()
{
pinMode (switch_pin, INPUT);
resetData();
}

void loop()
{
switch_value= digitalRead (switch_pin);
data.switch_value = switch_value;
data.pot_value = map (analogRead (A2), 0, 1024, 0, 255);
HC12.write(&data, sizeof(MyData)); // Send that data to HC-12
}

+++++++++++++++++++++++++++++++++++++++++++++

here is the error that I get while compiling. What could be the issue?

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: “Arduino Nano, ATmega328P (Old Bootloader)”

C:\Users\kvpon\Documents\Arduino\HC_12_TX\HC_12_TX.ino: In function ‘void loop()’:

HC_12_TX:83:36: error: no matching function for call to ‘SoftwareSerial::write(MyData*, unsigned int)’

HC12.write (&data, sizeof(MyData)); // Send that data to HC-12

^

In file included from C:\Users\kvpon\Documents\Arduino\HC_12_TX\HC_12_TX.ino:22:0:

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\SoftwareSerial\src/SoftwareSerial.h:102:18: note: candidate: virtual size_t SoftwareSerial::write(uint8_t)

virtual size_t write(uint8_t byte);

^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\SoftwareSerial\src/SoftwareSerial.h:102:18: note: candidate expects 1 argument, 2 provided

In file included from C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Stream.h:26:0,

from C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,

from C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Arduino.h:232,

from sketch\HC_12_TX.ino.cpp:1:

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Print.h:57:12: note: candidate: size_t Print::write(const char*, size_t)

size_t write(const char *buffer, size_t size) {

^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Print.h:57:12: note: no known conversion for argument 1 from ‘MyData*’ to ‘const char*’

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Print.h:56:20: note: candidate: virtual size_t Print::write(const uint8_t*, size_t)

virtual size_t write(const uint8_t *buffer, size_t size);

^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Print.h:56:20: note: no known conversion for argument 1 from ‘MyData*’ to ‘const uint8_t* {aka const unsigned char*}’

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Print.h:52:12: note: candidate: size_t Print::write(const char*)

size_t write(const char *str) {

^

C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.21.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\cores\arduino/Print.h:52:12: note: candidate expects 1 argument, 2 provided

exit status 1
no matching function for call to ‘SoftwareSerial::write(MyData*, unsigned int)’

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

HC_12_TX:83:36: error: no matching function for call to ‘SoftwareSerial::write(MyData*, unsigned int)’

You could probably cast the data variable to a byte array with reinterpret_cast. I guess at some stage the C/C++ folks got themselves so tangled up in their data types that they decided they needed the capability to ride-roughshod over the whole thing. Reinterpret_cast works very well, even if it seems a bit of a kludge.

The other option is to send the data as text using Serial.print() rather than Serial.write(). For example something like this

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

and then you can receive and parse it with code like in the parse example in Serial Input Basics

I find that sending data as text makes debugging much easier.

…R

You could probably cast the data variable to a byte array with reinterpret_cast.

 //HC12.write(&data, sizeof(MyData));      // Send that data to HC-12 
HC12.write((byte*)&data, sizeof(MyData));

You had the syntax wrong for the .write() which sends the array of bytes. .write(buf,len)