Implementing HC-12's into setup

Hello everyone.

First time posting on this forum, and i’m trying to follow all the guidelines.
If you notice anything i could’ve described better or just presented better, let me know!

The project at hand which i’ve had some difficulties with is a setup with standalone MC’s and sending data between them.

Rough idea of how it looks: (Everything is supplied with +5 voltage)

Transmitter side:
DHT22 (Temperature/Humidity Sensor) gathering data, which is connected to a standalone atmega328p
The atmega saves the data into variables then transmits them wirelessly with HC-12.

Receiver side:
HC-12 receives it, it’s connected to another atmega328p where it saves the DHT data in into variables.
After the receiving data process, i’m printing it onto a LCD display (4x20)

It’s supposed to be a wireless thermometer, perhaps for a wine cellar, gathering data inside the room, and then receiving/displaying the data onto a LCD display outside the room.

Sketch:

TX:

// Transmitter TX

// Include needed libraries
#include <DHT.h>
#include <SoftwareSerial.h>

// Defininition
#define dhtPin 4
#define dhtType DHT22

// using the DHT library
DHT dht(dhtPin, dhtType);

//variables
const byte HC12RxDPin = 0;
const byte HC12TxDPin = 1;

// using the SoftwareSerial library
SoftwareSerial HC12(HC12RxDPin, HC12TxDPin); //RX, TX

// Setup function - run one time
void setup() {
  dht.begin();
  HC12.begin(9600);
}

// Loop function - runs forever
void loop () {  

  float tem = dht.readTemperature();
  float hum = dht.readHumidity();
  
  HC12.write("<");
  HC12.write(tem);
  HC12.write(",");
  HC12.write(hum);
  HC12.write(">");
  delay(3000);                          // Wait three seconds and it again
}

RX

// Receiver RX

// Include needed libraries
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

// Definitions for the LCD connections
#define RS 7
#define E 8
#define D4 9
#define D5 10
#define D6 11
#define D7 12

LiquidCrystal lcd(RS, E, D4, D5, D6, D7);

// Variables
int tem = 0;
int hum = 0;

const byte HC12RxDPin = 0;
const byte HC12TxDPin = 1;

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // TEMPORARY ARRAY FOR USE WHEN PARSING

// using the SoftwareSerial library
SoftwareSerial HC12(HC12RxDPin, HC12TxDPin); //RX, TX

boolean newData = false;

// Setup function - run one time
void setup() {
  HC12.begin(9600); 
  lcd.begin(20,4);                  // Defining the LCD        
  lcd.clear();                     // Clear the LCD
}

// Loop function - runs sforever
void loop() {
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    // THIS TEMPORARY COPY IS NECESSARY TO PROTECT THE ORIGINAL DATA
    // BECAUSE STRTOK() USED IN PARSEDATA() REPLACES THE COMMAS WITH \0
    parseData();
    ShowParsedData();
    newData = false;
  }
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (HC12.available() > 0 && newData == false) {
        rc = HC12.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part
    strcpy(tem, strtokIndx); // copy it to tem
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    hum = atof(strtokIndx);     // convert this part to an float - hum

}

//============

void showParsedData() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Temperature: ")
  lcd.setCursor(0,1);
  lcd.print(tem);
  lcd.print(" C");
  lcd.print("Humidity");
  lcd.print(hum);
  lcd.print(" %");
}

I’ve made a smaller experiment without the parsing and start/end markers, resulting in data being received without noticing whether or not it would be correct.

The code for the project is mostly taken from Robin2 in his post:
Serial Input Basics
As you can see with the same code as well.

The goal in this post is to mostly “confirm” the code regarding how it’s presented and perhaps a comment on the code, if you’d think it would work.

Any explanation help would be appreciate and some comments if there’s some errors.

Attached are the Transmitter and receiver code

Transmitter_Exam.ino (744 Bytes)

Receiver_Exam.ino (2.52 KB)

Why are you doing software serial on the hardware serial pins?