Need help in programming, to make sure my serial comm. reciever receives my transmitter signals alone

I am using a serial wireless transfer of temperature data,
Components used
1.lm35 temperature sensor ( signal pin connected to A0 of arduino uno)
2. hc12 serial wireless communicator( rx(hc12) to tx (uno) and tx(hc12) to rx(uno)
3. arduino uno

My arduino tx code goes like this

// Define the analog pin, the LM35's Vout pin is connected to
#define sensorPin A0

void setup() {
  // Begin serial communication at 9600 baud rate
  Serial.begin(9600);
}

void loop() {
  // Get the voltage reading from the LM35
  int reading = analogRead(sensorPin);

  // Convert that reading into voltage
  float voltage = reading * (5.0 / 1024.0);

  // Convert the voltage into the temperature in Celsius
  float temperatureC = voltage * 100;

  // Print the temperature in Celsius
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
    Serial.println("C  |  ");
  
  delay(1000); // wait a second between readings
}

and on the receiver side i have

  1. arduino uno
  2. hc 12 alone

my receiver code is

// Example 4 - Receive a number as text and convert it to an int

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

int dataNumber = 0;             // new for this version

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewNumber();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    if (Serial.available() > 0) {
        rc = Serial.read();

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

void showNewNumber() {
    if (newData == true) {
        dataNumber = 0;             // new for this version
        dataNumber = atoi(receivedChars);   // new for this version
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        Serial.print("Data as Number ... ");    // new for this version
        Serial.println(dataNumber);     // new for this version
        newData = false;
    }
}


This works really well, my temperature is sent wirelessly without any problem, My next aim is to make this secured, like for example for my project if temperature goes high beyond 45 deg celsius i need to switch on the dc motor fan (just digitalWrite(x, HIGH); AND many serial communications are present in my test area, I need help in the receiver side that the data received is only from my transmitter (first check) and if first check is fine, then it has to check for temperature >45 deg, if yes switch on tiny dc fan, if not do nothing.

I tried creating a string, tried to send constant , but not able to put in the form of code, any help would be greatly appreciated, Let God reward you guys

Your best solution is to encode the data so you can check on the receiving end to be sure it is ok. Posting a schematic as it is wired would help but it is not necessary.

Can you please do this thing in the code format, I am not able to apply your description in the code format,

If possible, please

Hello
Add a byte to the payload transmitted to identify the device address.

This would be an improvement on the sender; it will send an ID followed by the temperature and a simple checksum.

https://forum.arduino.cc/t/need-help-in-programming-to-make-sure-my-serial-comm-reciever-receives-my-transmitter-signals-alone/1010074

const char *id = "1234";
const byte sensorPin = A0;

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

void loop() {
  // Get the voltage reading from the LM35
  int reading = analogRead(sensorPin);

  // Convert that reading into voltage
  float voltage = reading * (5.0 / 1024.0);

  // Convert the voltage into the temperature in Celsius
  float temperatureC = voltage * 100;

  // convert float to character array
  char t[32];
  dtostrf(temperatureC, 5, 2, t);

  // create buffer and place id and temperature in it
  char txBuf[64];
  sprintf(txBuf, "%s,%s,", id, t);

  // send start marker
  Serial.print("<"):
  // send data
  Serial.print(txBuf);

  // calculate a simple checksum
  uint8_t cs = 0;
  for (uint16_t cnt = 0; cnt < strlen(txBuf); cnt++)
  {
    cs += txBuf[cnt];
  }
  // send checksum
  Serial.print(cs);
  // send end marker
  Serial.print(">"):
  
  delay(1000); // wait a second between readings
}

Use Robin's example with the start- and endmarker at the receiver. No time for more (load shedding :frowning: ).
After receiving, check the checksume first, next the ID.

Will my same receiver code which I have posted already work??

How do I check checksum then temperature?m

No, it will not work with your existing code, you need to implement the receive with start and end marker.

Robin also has an example that splits the string on the comma using strtok.

To calculate the checksum, you need to split on the last comma, calculate over the first part (including the last comma) and compare.

I will try to spend some time on it tomorrow.

tried with different endmarker and checksum, it works with password now, thanks to everyone

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