HC12 with multiple variables

This come mostly off the web, but I had to correct the Receiver side , this will allow you to transfer any number of numeric variables,.

//Transmitter
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial HC12(10, 11);//tx,rx

type or paste code here

int variable1;//variables that will get sent
int variable2;
int variable3;
int variable4;
int variable5;

void setup() {
  HC12.begin(9600);//Sets the hc 12 communication to 9600 baud, which is their factory default
  Serial.begin(9600);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
}
void loop() {
  variable1 = 10; //assigns the variables that will be sent
  variable2 = 50;
  variable3 = 80; 
  variable4 = analogRead(A3);
  variable5 = 215; 
  //Uncomment the following lines if you want the remote to also send the analog input
  //data to the serial monitor, to check if the data is being read properly
  //If you do, It will look the same as it should on the other end's serial monitor
  //Serial.print(variable1);
  //Serial.print(",");
  //Serial.print(variable2);
  //Serial.print(",");
  //Serial.print(variable3); 
  //Serial.print(","); 
  //Serial.println(variable4);
  //Serial.print(",");
  //Serial.print(variable5);
  //Serial.println("");
  HC12.print(variable1);//sends the variables
  HC12.print(",");
  HC12.print(variable2);
  HC12.print(",");
  HC12.print(variable3);
  HC12.print(",");
  HC12.print(variable4);
  HC12.print(",");
  HC12.print(variable5);
  HC12.println("");
  delay(100);
}

//Receiver
#include <SoftwareSerial.h>
#include <Wire.h>

SoftwareSerial HC12(10, 11);//tx,rx

int variable1=0;
int variable2=0;
int variable3=0;
int variable4=0;
int variable5;
String input;
int d1; //setup to reference delimiter locations so its easier to follow
int d2;
int d3;
int d4;
const char delimiter = ',';

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

void loop() {
  if(HC12.available())
  {
  input = HC12.readStringUntil('\n');
  if (input.length() > 0)
      {
        Serial.println(input);
       
       d1 = input.indexOf(delimiter);
       //Serial.println(d1);
        variable1 = input.substring(0, d1).toInt();
    
        d2 = input.indexOf(delimiter, d1+1);
        //Serial.println(d2);
        variable2 = input.substring(d1+1, d2).toInt();

        d3 = input.indexOf(delimiter,d2+1);          
        //Serial.println(d3);
        variable3 = input.substring(d2+1, d3).toInt();
    
        d4 = input.indexOf(delimiter, d3+1);
        //Serial.println(d4);
        variable4 = input.substring(d3+1, d4).toInt();
    
        variable5 = input.substring(d4+1).toInt();
        Serial.println(variable1);
        Serial.println(variable2);
        Serial.println(variable3);
        Serial.println(variable4);
        Serial.println(variable5);
  
delay(10);
      }
  }
}

Results
10,50,80,239,215

10
50
80
239
215

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.