HC 12 - too much data??

Hello Community.

I am building a wireless ultrasonic distance sensor with 2x HC 12 and 2x Arduino nano.

I am new to wireless communication and i can't find any solution to my problem.

I can send data from one Arduino to another, but I seem to be getting too much data. If I apply a delay, it simply stops.

I think I need a way to store and send the distance data in a more controlled way. But how??

My current code:

Transmitter:

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

const int pwPin1 = 3;
int triggerPin1 = 4;
long sensor1;

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12'
  pinMode(pwPin1, INPUT);
  pinMode(triggerPin1, OUTPUT);

}

void start_sensor(){
  digitalWrite(triggerPin1,HIGH);
  delay(1);
  digitalWrite(triggerPin1,LOW);

}

  void read_sensors(){
sensor1 = pulseIn(pwPin1, HIGH);
sensor1 = sensor1/10;
}

void send_data(){
while (HC12.available()) {        // If HC-12 has data
HC12.write(sensor1);      // Send that data to HC-12 

}

}

void loop () {
  start_sensor();
  read_sensors();
  send_data();
  delay(100);
  
}

My reciever code:

#include <SoftwareSerial.h>
#include <U8glib.h>

long distance1;

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI

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
  u8g.begin();
}
void loop() {
  while (HC12.available()) {        // If HC-12 has data
  distance1 = HC12.read();
}
u8g.firstPage();
  do {
       draw(distance1);
  }
  while (u8g.nextPage());

  
} 
  void draw(int distance1) {
  u8g.setFont(u8g_font_fur30);
u8g.setPrintPos(0, 63);
  u8g.print(distance1);
  u8g.print("cm");

}

Thank you for all your help!

Your transmitter is sending 4 bytes (size of long on a nano) but your receiver is only reading 1 byte

Inside your transmitter code, you do should not be using the available() command since that just tells you if there is anything in the input buffer (which there won't be since this is the transmitter and never receives anything)

Thank you! How do I change that byte situation?

I have removed the "available" command.

Things will probably be easier if you use HC12.println() rather than HC12.write() in your TX program.

Then you can use the second example in Serial Input Basics to receive the data. The Tutorial also includes a parse example to illustrate how to extract numbers from the received text.

...R

Try these sketches:
Transmitter:

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

const int pwPin1 = 3;
int triggerPin1 = 4;
long sensor1;

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12'
  pinMode(pwPin1, INPUT);
  pinMode(triggerPin1, OUTPUT);

}

void start_sensor() {
  digitalWrite(triggerPin1, HIGH);
  delay(1);
  digitalWrite(triggerPin1, LOW);

}

void read_sensors() {
  sensor1 = pulseIn(pwPin1, HIGH);
  sensor1 = sensor1 / 10;
}

void send_data()
{
  HC12.print(sensor1, HEX);//all 8 hex digit of sensor1 has gone
}

}

void loop ()
{
  start_sensor();
  read_sensors();
  send_data();
  delay(100);
}

Receiver:

#include <SoftwareSerial.h>
#include <U8glib.h>
char myData[9];
long distance1;

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI

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
  u8g.begin();
}

void loop()
{
  while (HC12.available())
  { // If HC-12 has data
    myData[i] = HC12.read();
    i++;
  }
  myData[i] = '\0';
  distance1 = atol(myData);//HC12.read();
  u8g.firstPage();
  do
  {
    draw(distance1);
  }
  while (u8g.nextPage());
}

void draw(int distance1) 
{
  u8g.setFont(u8g_font_fur30);
  u8g.setPrintPos(0, 63);
  u8g.print(distance1);
  u8g.print("cm");

}

Thank you both!

GolamMostafa - I am trying your code, but I get a compelling error: "I" was not declared in this scope. It happens with every myData .
Why is that?
Very excited to try it out! :slight_smile:

Post your compilation error message that has appeared in the IDE. | looks like OR symbol that you have keyed in your code:

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
Arduino: 1.8.10 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

C:\Users\LasseSkaarupFlodgaar\Desktop\Maxbotix_Modtager_Community\Maxbotix_Modtager_Community.ino: In function 'void loop()':

Maxbotix_Modtager_Community:21:12: error: 'i' was not declared in this scope

     myData[i] = HC12.read();

            ^

C:\Users\LasseSkaarupFlodgaar\Desktop\Maxbotix_Modtager_Community\Maxbotix_Modtager_Community.ino: At global scope:

Maxbotix_Modtager_Community:25:3: error: 'myData' does not name a type

   myData[i] = '\0';

   ^~~~~~

Maxbotix_Modtager_Community:26:3: error: 'distance1' does not name a type

   distance1 = atol(myData);//HC12.read();

   ^~~~~~~~~

Maxbotix_Modtager_Community:27:3: error: 'u8g' does not name a type

   u8g.firstPage();

   ^~~

Maxbotix_Modtager_Community:28:3: error: expected unqualified-id before 'do'

   do

   ^~

Maxbotix_Modtager_Community:32:3: error: expected unqualified-id before 'while'

   while (u8g.nextPage());

   ^~~~~

Maxbotix_Modtager_Community:33:1: error: expected declaration before '}' token

 }

 ^

Multiple libraries were found for "SoftwareSerial.h"
 Used: C:\Users\LasseSkaarupFlodgaar\Desktop\arduino-1.8.10-windows\arduino-1.8.10\hardware\arduino\avr\libraries\SoftwareSerial
Multiple libraries were found for "U8glib.h"
 Used: C:\Users\LasseSkaarupFlodgaar\Documents\Arduino\libraries\U8glib
exit status 1
'i' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Declare the following 2 lines in the global area (above setup() function) of the receiver codes:

char myData[[20];
int i = 0;

My current receiver code:

#include <SoftwareSerial.h>
#include <U8glib.h>

char myData[[20];
int i = 0;
long distance1;


U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI

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
  u8g.begin();
}

void loop()
{
  while (HC12.available())
  { // If HC-12 has data
    myData[i] = HC12.read();
    i++;
  }
}
  myData[i] = '\0';
  distance1 = atol(myData);//HC12.read();
  u8g.firstPage();
  do
  {
    draw(distance1);
  }
  while (u8g.nextPage());
}

void draw(int distance1)
{
  u8g.setFont(u8g_font_fur30);
  u8g.setPrintPos(0, 63);
  u8g.print(distance1);
  u8g.print("cm");

}

My current error:

Arduino: 1.8.10 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

Maxbotix_Modtager_Community:4:14: error: expected ']' before numeric constant

 char myData[[20];

              ^~

C:\Users\LasseSkaarupFlodgaar\Desktop\Maxbotix_Modtager_Community\Maxbotix_Modtager_Community.ino: In function 'void loop()':

Maxbotix_Modtager_Community:25:13: error: invalid types 'char[int]' for array subscript

     myData[i] = HC12.read();

             ^

C:\Users\LasseSkaarupFlodgaar\Desktop\Maxbotix_Modtager_Community\Maxbotix_Modtager_Community.ino: At global scope:

Maxbotix_Modtager_Community:29:3: error: 'myData' does not name a type

   myData[i] = '\0';

   ^~~~~~

Maxbotix_Modtager_Community:30:3: error: 'distance1' does not name a type

   distance1 = atol(myData);//HC12.read();

   ^~~~~~~~~

Maxbotix_Modtager_Community:31:3: error: 'u8g' does not name a type

   u8g.firstPage();

   ^~~

Maxbotix_Modtager_Community:32:3: error: expected unqualified-id before 'do'

   do

   ^~

Maxbotix_Modtager_Community:36:3: error: expected unqualified-id before 'while'

   while (u8g.nextPage());

   ^~~~~

Maxbotix_Modtager_Community:37:1: error: expected declaration before '}' token

 }

 ^

Multiple libraries were found for "SoftwareSerial.h"
 Used: C:\Users\LasseSkaarupFlodgaar\Desktop\arduino-1.8.10-windows\arduino-1.8.10\hardware\arduino\avr\libraries\SoftwareSerial
Multiple libraries were found for "U8glib.h"
 Used: C:\Users\LasseSkaarupFlodgaar\Documents\Arduino\libraries\U8glib
exit status 1
expected ']' before numeric constant

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

If you are unable to fix this error, then what should I say -- should I say that you need a ready-made program that works, and I would certainly do it if I would have all the required hardware resources. Anyway:

char myData[[20];  //typo error
==> char myData[20];     //corrected

Thank you for getting back to me!

I assumed it was a typo and tried some variations, but I still get a lot of "does not name a type" errors. Therefore I assumed you had an idea with the double [ that I did not understand.

I appreciate all the help - I am still very new in this Arduino world :slight_smile:

I have corrected the typo and this is my error:

Arduino: 1.8.10 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

Maxbotix_Modtager_Community:29:3: error: 'myData' does not name a type

   myData[i] = '\0';

   ^~~~~~

Maxbotix_Modtager_Community:30:3: error: 'distance1' does not name a type

   distance1 = atol(myData);//HC12.read();

   ^~~~~~~~~

Maxbotix_Modtager_Community:31:3: error: 'u8g' does not name a type

   u8g.firstPage();

   ^~~

Maxbotix_Modtager_Community:32:3: error: expected unqualified-id before 'do'

   do

   ^~

Maxbotix_Modtager_Community:39:3: error: expected unqualified-id before 'while'

   while (u8g.nextPage());

   ^~~~~

Maxbotix_Modtager_Community:40:1: error: expected declaration before '}' token

 }

 ^

Multiple libraries were found for "U8glib.h"
 Used: C:\Users\LasseSkaarupFlodgaar\Documents\Arduino\libraries\U8glib
Multiple libraries were found for "SoftwareSerial.h"
 Used: C:\Users\LasseSkaarupFlodgaar\Desktop\arduino-1.8.10-windows\arduino-1.8.10\hardware\arduino\avr\libraries\SoftwareSerial
exit status 1
'myData' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Is something wrong with any libraries or what could this be?

This is the original Receiver code. Why have you changed it in Post#9?

#include <SoftwareSerial.h>
#include <U8glib.h>
char myData[9];
int i =0;
long distance1;

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI

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
  u8g.begin();
}

void loop()
{
  while (HC12.available())
  { // If HC-12 has data
    myData[i] = HC12.read();
    i++;
  }
  myData[i] = '\0';
  distance1 = atol(myData);//HC12.read();
  u8g.firstPage();
  do
  {
    draw(distance1);
  }
  while (u8g.nextPage());
}

void draw(int distance1)
{
  u8g.setFont(u8g_font_fur30);
  u8g.setPrintPos(0, 63);
  u8g.print(distance1);
  u8g.print("cm");

}

Weird - I am not sure what I changed, but the code you gave me now is verified.

I am out of town but will upload it to my hardware tomorrow. Will get back to you with feedback.

Thank you!