send multiple floats over Serial

I need to get the data from dataToSend to moduleOne over the HC12 software serial but i’m not sure how to do that.

SENDER

//set module name here
char moduleName [] = "module1";
float dataToSend [] = {13.367, 12.22, 2.12, 0.01};//SV, BV, SBC, SLC         //I need this

byte incomingByte;
String readBuffer = "";

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
}
void loop() {
  while (HC12.available()) {             // If HC-12 has data
    incomingByte = HC12.read();          // Store each incoming byte from HC-12
    readBuffer += char(incomingByte);    // Add each byte to ReadBuffer string variable
  }


if (readBuffer == moduleName){
    Serial.println("sendingData");
    readBuffer = "";
    for (int i = 0; i<4; i++){
      HC12.println(dataToSend [i]);
    }
    
  }
}

Receiver

char moduleOneName   [] = "module1";
char moduleTwoName   [] = "module2";
char moduleThreeName [] = "module3";
char moduleFourName  [] = "module4";
char moduleFiveName  [] = "module5";
char moduleSixName   [] = "module6";
char moduleSevenName [] = "module7";
char moduleEightName [] = "module8";
char moduleNineName  [] = "module9";
char moduleTenName   [] = "module10";

float moduleOne   [] = {0, 0, 0, 0};//SV, BV, SBC, SLC           //   to go here
float moduletwo   [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulethree [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulefour  [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulefive  [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulesix   [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float moduleseven [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float moduleeight [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulenine  [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float moduleten   [] = {0, 0, 0, 0};//SV, BV, SBC, SLC



#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); 
void setup() {
  Serial.begin(9600);             
  HC12.begin(9600);               
}

void loop() {
  HC12.print(moduleOneName);//ask module1 for data
  
 
  
  
  delay(500);
  for (int i = 0; i<4; i++){
      Serial.println("module one has");
      Serial.println("SV is ");
      Serial.println(moduleOne[0]);
      Serial.println("BV is ");
      Serial.println(moduleOne[1]);
      Serial.println("SBC is ");
      Serial.println(moduleOne[2]);
      Serial.println("SLC is ");
      Serial.println(moduleOne[3]);
    }
  
}

If you like to figure this out by yourself, there is a Serial Input Basics tutorial from Robin2.

I got it working using easytransfer but with only 2 arduinos at a time i can't get all three to work

photo

You can easily transfer floats (ints, doubles, structs, etc…) between Arduinos using SerialTransfer.h:

Example TX code:

#include "SerialTransfer.h"
#include <SoftwareSerial.h>


SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;


struct STRUCT {
  char z;
  float y;
} testStruct;

uint16_t arr[3] = {0, 1, 2};


void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
  myTransfer.begin(mySerial);

  testStruct.z = '|';
  testStruct.y = 4.5;
}


void loop()
{
  // use this variable to keep track of how many
  // bytes we're stuffing in the transmit buffer
  uint16_t sendSize = 0;

  ///////////////////////////////////////// Stuff buffer with individual bytes
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 200;
  sendSize += 2;

  ///////////////////////////////////////// Stuff buffer with struct
  myTransfer.txObj(testStruct, sizeof(testStruct), sendSize);
  sendSize += sizeof(testStruct);

  ///////////////////////////////////////// Stuff buffer with array
  myTransfer.txObj(arr, sizeof(arr), sendSize);
  sendSize += sizeof(arr);

  ///////////////////////////////////////// Send buffer
  myTransfer.sendData(sendSize);
  delay(100);
}

Example RX code:

#include "SerialTransfer.h"
#include <SoftwareSerial.h>


SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;


struct STRUCT {
  char z;
  float y;
} testStruct;

uint16_t arr[3] = {};


void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
  myTransfer.begin(mySerial);
}


void loop()
{
  if(myTransfer.available())
  {
    // use this variable to keep track of how many
    // bytes we've processed from the receive buffer
    uint16_t recSize = 0;

    Serial.print(myTransfer.rxBuff[0]);
    Serial.print(' ');
    Serial.print(myTransfer.rxBuff[1]);
    Serial.print(" | ");
    recSize += 2;

    myTransfer.rxObj(testStruct, sizeof(testStruct), recSize);
    Serial.print(testStruct.z);
    Serial.print(' ');
    Serial.print(testStruct.y);
    Serial.print(" | ");
    recSize += sizeof(testStruct);

    myTransfer.rxObj(arr, sizeof(arr), recSize);
    Serial.print(arr[0]);
    Serial.print(' ');
    Serial.print(arr[1]);
    Serial.print(' ');
    Serial.println(arr[2]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

This should work with multiple Arduinos (assuming you’re wiring them together correctly). Can you post a schematic?

You can try the master and slave code here. Based on your original code and untested (do compile.)

Keep in mind that these aren’t terribly robust (no frame alignment mechanism, use of String, no checksum, no timeout etc) but it should at least illustrate a simple way to send “floats” over serial.

//////////////////////////////////////////////////////////////////////////////////////////////////
//SLAVE CODE
#include <SoftwareSerial.h>

//set module name here
char moduleName [] = "module1";
float dataToSend [] = {13.367, 12.22, 2.12, 0.01};//SV, BV, SBC, SLC         //I need this

unsigned char *puchData;

byte incomingByte;
String readBuffer = "";

SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
void setup() 
{
    Serial.begin(9600);             // Serial port to computer
    HC12.begin(9600);               // Serial port to HC12
    
}//setup

void loop() 
{
    while( HC12.available() ) 
    {             
        // If HC-12 has data
        incomingByte = HC12.read();          // Store each incoming byte from HC-12
        readBuffer += char(incomingByte);    // Add each byte to ReadBuffer string variable
        
    }//while

    if( readBuffer == moduleName )
    {
        Serial.println("sendingData");
        readBuffer = "";

        puchData = (unsigned char *)dataToSend;
        for( int i=0; i<sizeof( dataToSend ); i++ )
           HC12.write( *puchData++ );
   
    }//if
    
}//loop

//////////////////////////////////////////////////////////////////////////////////////////////////
//MASTER CODE
#include <SoftwareSerial.h>

char moduleOneName   [] = "module1";
char moduleTwoName   [] = "module2";
char moduleThreeName [] = "module3";
char moduleFourName  [] = "module4";
char moduleFiveName  [] = "module5";
char moduleSixName   [] = "module6";
char moduleSevenName [] = "module7";
char moduleEightName [] = "module8";
char moduleNineName  [] = "module9";
char moduleTenName   [] = "module10";

float moduleOne   [] = {0, 0, 0, 0};//SV, BV, SBC, SLC           //   to go here
float moduletwo   [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulethree [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulefour  [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulefive  [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulesix   [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float moduleseven [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float moduleeight [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float modulenine  [] = {0, 0, 0, 0};//SV, BV, SBC, SLC
float moduleten   [] = {0, 0, 0, 0};//SV, BV, SBC, SLC

SoftwareSerial HC12(10, 11);
unsigned char *puchData;
int numDataRxd;

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

#define SND_REQUEST     0
#define RX_DATA         1
#define SHOW_DATA       2
//
void loop() 
{
    static byte
        stateRequests = SND_REQUEST;
        
    switch( stateRequests )
    {
        case    SND_REQUEST:
            HC12.print(moduleOneName);//ask module1 for data
            puchData = (unsigned char *)moduleOne;            
            numDataRxd = 0;
            stateRequests = RX_DATA;
            
        break;

        case    RX_DATA:
            if( HC12.available() > 0 )
            {
                do
                {
                    unsigned char val = HC12.read();
                    if( numDataRxd < sizeof( moduleOne ) )
                    {
                        *puchData++ = HC12.read();
                        numDataRxd++;
                        
                    }//if
                    else
                    {
                        stateRequests = SHOW_DATA;
                        
                    }//else
                    
                }while( HC12.available() > 0 );
                
            }//if
            
        break;

        case    SHOW_DATA:
            Serial.println("module one has");
            Serial.println("SV is ");
            Serial.println(moduleOne[0]);
            Serial.println("BV is ");
            Serial.println(moduleOne[1]);
            Serial.println("SBC is ");
            Serial.println(moduleOne[2]);
            Serial.println("SLC is ");
            Serial.println(moduleOne[3]);

            stateRequests = SND_REQUEST;
        
        break;
                
    }//switch
     
}//loop