Problems trasmitting light sensor data wirelessly using HC12

Hi all, I am facing major challenges getting this to work. My end goal is to transmit my light sensor data wirelessly from one arduino to another connecting to my PC.

This setup is to be integrated with PLX-DAQ. I’ve got it to work when I type into the serial monitor but it does not work when I let the sensor do the writing. I’m really really stuck.

I don’t have much knowledge in Arduino & electronics generally. Please help me :disappointed_relieved:

This my transmitter code:

   #include <SoftwareSerial.h>
    SoftwareSerial HC12(2, 3); 
    int tempLED = 8;


    
    void setup() {
      Serial.begin(9600);             // Serial port to computer
      HC12.begin(9600);               // Serial port to HC12
      pinMode (tempLED,INPUT);
   Serial.println("Ready to transmit data wirelessly...");
    }




    
    void loop() {
    
    int sensor = digitalRead(tempLED);

    Serial.println(sensor);
    HC12.write(Serial.read());

    delay(1000);

    }

and this is the receiving codes:

   #include <SoftwareSerial.h>
    SoftwareSerial HC12(2, 3); // HC-12 TX Pin, HC-12 RX Pin
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;



    
    void setup() {
      Serial.begin(9600);             // Serial port to computer
      HC12.begin(9600);               // Serial port to HC12
      Serial.println("LABEL,Remark");

      
    }
    void loop() {

       recvWithEndMarker();
       showNewData();

    }

    void recvWithEndMarker() {
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;
 
 // if (Serial.available() > 0) {
           while (HC12.available() > 0 && newData == false) {
 Serial.write(rc = HC12.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 showNewData() {
 if (newData == true) {
 Serial.print("DATA,");
 Serial.println(receivedChars);
 newData = false;
 }
}

I don’t know if you’ve noticed. Much of this codes is taken off the net and put together according to my limited understanding. I can transmit data with no problem when typing in the serial monitor but have issue when letting the sensor send it self. It shows 1 & 0 at the transmitter serial monitor but boxes at the receiving end. :’(

In the end I want to see in the serial monitor of the receiving end:

DATA (for the PLX-DAQ),Day(day of the log),Time (time of the log),Status (of the light sensor in 1 or 0)

Please also find attached a picture of my arduino setup & the 2 serial monitors

Thank you very much.

 Serial.println(sensor);
    HC12.write(Serial.read());

What do you think that the Serial.read() is returning. I doubt that it is the value of sensor.

Isn't it returning the value of the sensor?

I serial.println the sensor value into the serial monitor & then the .writes the serial monitor value into the HC12?

Or I should have

HC12.write(sensor);

I think i tried this and it din't work :frowning:

I serial.println the sensor value into the serial monitor & then the .writes the serial monitor value into the HC12?

No, the Serial.read() returns -1 because the receive buffer is empty. The write method sends a byte. In your case it sends 0x00 or 0x01 which are not printable in serial monitor. Try using HC12.print(sensor) that will send to '0' or '1' characters.

Try this to see the difference

// change to 0 or 1 to see what is printed in serial monitor
boolean sensor = 0;  // boolean data type cause digitalRead(sensor) returns only 0 0r 1

void setup() 
{
  Serial.begin(115200);
  Serial.print("write   ");
  Serial.write(sensor);
  Serial.println();
  Serial.print("print   ");
  Serial.println(sensor);
}

void loop()
{
  // put your main code here, to run repeatedly:

}

OMG THAT WORKED!!! :smiley:

TQTQ I now need to understand what boolean means. That solves one major roadblock. Shall continue on…

Really tq so much groundFungus!!!

groundFungus:
No, the Serial.read() returns -1 because the receive buffer is empty. The write method sends a byte. In your case it sends 0x00 or 0x01 which are not printable in serial monitor. Try using HC12.print(sensor) that will send to '0' or '1' characters.

Try this to see the difference

// change to 0 or 1 to see what is printed in serial monitor

boolean sensor = 0;  // boolean data type cause digitalRead(sensor) returns only 0 0r 1

void setup()
{
  Serial.begin(115200);
  Serial.print("write  ");
  Serial.write(sensor);
  Serial.println();
  Serial.print("print  ");
  Serial.println(sensor);
}

void loop()
{
  // put your main code here, to run repeatedly:

}

I just realised I didn't quote you and thus maybe you didn't get notified. Thanks again. I did somewhat understand that write sends a byte and print sends the entire character. But I thought that my HC12.write was limited to just that. Didn't know I could HC12.print.

A couple of links that might be of interest.

https://www.arduino.cc/en/Reference/SoftwareSerial

https://www.tutorialspoint.com/arduino/arduino_data_types.htm