HC 12 just receive numbers such as 0, 128 and 255

Hi guys, I have a some problem using the HC-12. One of it sends a binary dataset and another ones just receives numbers 0 or 128 or 255. My question is why? Yet converted to binary, char, string and so much another which I tryied? To sender the sketch used was:

void loop()
{
float temp = bmp.readTemperature();
float pressao = bmp.readPressure();
pressao = pressao / 101325; // convertendo de Pa para ATM
float umidade = dht.readHumidity();
umidade = umidade - 100;
float batt = readVcc();
float alti = bmp.readAltitude();

t = (int)(temp1e2);
p = (int)(pressao
1e3);
u = (int)(umidade1e2);
b = (int)(batt);
l = (int)(alti
1e2);

TX[0] = t/256;
TX[1] = t%256;
TX[2] = p/256;
TX[3] = p%256;
TX[4] = u/256;
TX[5] = u%256;
TX[6] = b/256;
TX[7] = b%256;
TX[8] = l/256;
TX[9] = l%256;

for(int i=0; i<n; i++)
{
HC12.write(TX[i]);
}

The receiver code is:

while(HC12.available()>0)
{
for(int i=0; i<n; i++)
{
RX[i] = HC12.read();
}
}

    temp = ((RX[0]*256) + RX[1]) / 1e2;                 
    pres = ((RX[2]*256) + RX[3]) / 1e3;
    umi  = ((RX[4]*256) + RX[5]) / 1e2;
    bat  = ((RX[6]*256) + RX[7]) / 1e0;
    alt  = ((RX[8]*256) + RX[9]) / 1e2;

// }

Maybe: the receiver checks if there is one or more bytes available to read; but you read N bytes regardless if there is one or two or N bytes available.

Can you please edit your post, select the code and click the < / > button just above the text box; this will add the code tags. Next save the post.

We also prefer complete code, not snippets; there might be other things wrong in your code. In the IDE, use edit → copy for forum; this will copy the code (including code tags) to the clipboard. Next paste the code in a reply.

@nazlob2019, your topic has been moved to a more suitable location on the forum.

You probably mean:
if (HC12.available() >= n)

Ok, that’s fine. Could you give me the link? Thank you for your attention.
I’m new one here.

[THIS IS THE TRANSMITTER CODE]
#include <Adafruit_Sensor.h> //INCLUSÃO DE BIBLIOTECA
#include <Adafruit_BMP280.h> //INCLUSÃO DE BIBLIOTECA
#include <SPI.h>
#include <Wire.h>
#include <SoftwareSerial.h>;
#include <DHT.h>                  // Biblioteca para DHT temp e umidade
#define dht_dpin A0
#define DHTTYPE DHT11

SoftwareSerial HC12(2, 3);
DHT dht(dht_dpin, DHTTYPE);       // Inicializa o sensor de temperatura e umidade
Adafruit_BMP280 bmp; //OBJETO DO TIPO Adafruit_BMP280 (I2C)

int n = 10;
byte TX[10];
unsigned long time = millis();
unsigned long data, hora;
float temp = 0, pressao=0;
float  umidade=0, batt=0;
float  lati, longi, alti;
int p=0, t=0, b=0, u=0;
int l=0, ll=0;
double al, vel;
float te, pr, um, ba;


void setup()
{
  Serial.begin(9600);
  HC12.begin(9600);
  pinMode(8,OUTPUT);
  digitalWrite(8,HIGH);
  Serial.println("Iniciando..."); //IMPRIME O TEXTO NO MONITOR SERIAL    
  
  if(!bmp.begin(0x76))
  { //SE O SENSOR NÃO FOR INICIALIZADO NO ENDEREÇO I2C 0x76, FAZ
    Serial.println(F("Sensor BMP280 não foi identificado! Verifique as conexões.")); //IMPRIME O TEXTO NO MONITOR SERIAL
    while(1); //SEMPRE ENTRE NO LOOP
  }
  else
  {
    Serial.println("BMP OK!"); //IMPRIME O TEXTO NO MONITOR SERIAL    
  }
}


long readVcc() 
{
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1125300L / result; // Back-calculate AVcc in mV
  return result;
}




void loop()
{
  float temp = bmp.readTemperature();
  float pressao = bmp.readPressure();
  pressao = pressao / 101325; // convertendo de Pa para ATM
  float umidade = dht.readHumidity();
  umidade = umidade - 100;
  float batt = readVcc();
  float alti = bmp.readAltitude();
      
  t = (int)(temp*1e2);
  p = (int)(pressao*1e3);
  u = (int)(umidade*1e2);
  b = (int)(batt);
  l = (int)(alti*1e2);

  String msg = String(temp) + ", " + String(pressao) + ", " + String(umidade) + ", " + String(batt) + ", " + String(alti);
  
  TX[0] = t/256;
  TX[1] = t%256;
  TX[2] = p/256;
  TX[3] = p%256;
  TX[4] = u/256;
  TX[5] = u%256;
  TX[6] = b/256;
  TX[7] = b%256;
  TX[8] = l/256;
  TX[9] = l%256;

//  Serial.println(TX[0]);
//  Serial.println(TX[1]);
//  Serial.println(TX[2]);
//  Serial.println(TX[3]);
//  Serial.println(TX[4]);
//  Serial.println(TX[5]);
//  Serial.println(TX[6]);
//  Serial.println(TX[7]);
//  Serial.println(TX[8]);
//  Serial.println(TX[9]);   
//       
//  for(int i=0; i<n; i++)
//  {
//     HC12.write(TX[i]);
//  }

  HC12.print(temp);
  HC12.println(" ");
  HC12.print(pressao);
  HC12.print(" " );
  HC12.print(umidade);
  HC12.print(" ");
  HC12.print(alti);
  HC12.println(" ");
  HC12.print(batt);
  HC12.println(" ");
          
  Serial.print("Temperatura : " );
  Serial.print(temp );
  Serial.println(" *C");
  Serial.print("Pressao     : " );
  Serial.print( pressao );
  Serial.println(" atm");
  Serial.print("Umidade     : " );
  Serial.print( umidade );
  Serial.println(" %");
  Serial.print("Alt.        : ");
  Serial.print(alti);
  Serial.println(" m");
  Serial.print("Bat.        : ");
  Serial.print(batt);
  Serial.println(" mV");
  Serial.println("");
  delay(3000);
}
[/code]
[THIS IS THE RECEIVER CODE]
/***CONFIGURATION ****/
const long interval = 5000;  // ms
const int slaves = 1; //Number of slaves (max 16) (go to line 95 and complete the if statement for every sensor)
/*********************/
//Libraries
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27,16,2);
SoftwareSerial HC12(5, 6); // RX, TX

float temp = 0, bat = 0, alt = 0;
float umi = 0, pres = 0;
float lati = 0, longi = 0;
char incomingByte, msg; 
String command;

int n = 10;
byte RX[10];


void setup()
{
  Serial.begin(9600);
  HC12.begin(9600);
  pinMode(7,OUTPUT);
  digitalWrite(7,HIGH);
  lcd.begin();
  lcd.setCursor(5,0);
  lcd.print("LAFEJ");
  lcd.setCursor(2,1);
  lcd.print("Aguardando...");
}



void loop()
{
  lcd.setCursor(5,0);
  lcd.print("LAFEJ");
  lcd.setCursor(2,1);
  lcd.print("Aguardando...");
  delay(500);
  lcd.clear();  
  
  for(int i=0; i<n; i++)
  {
    RX[i] = 0;        // Zerar todas as posições para renovar valores
  }


  while(HC12.available()>=n)
  {
    for(int i=0; i<n; i++)
    {
      RX[i] = HC12.read();
      float v = map(RX[i], 0, 255, 0, 1023);
      Serial.print(v);
      Serial.print("  --> ");
      Serial.print("RX[i] =  ");
      Serial.print(RX[i]);
      Serial.print("  i = ");
      Serial.println(i);
    }
//  }
        Serial.println("FORA DO LOOP");
        Serial.println(RX[0]);
        Serial.println(RX[1]);
        Serial.println(RX[2]);
        Serial.println(RX[3]);
        Serial.println(RX[4]);
        Serial.println(RX[5]);
        Serial.println(RX[6]);
        Serial.println(RX[7]);
        Serial.println(RX[8]);
        Serial.println(RX[9]);
        
        temp = ((RX[0]*256) + RX[1]) / 1e2;                  // LSB
        pres = ((RX[2]*256) + RX[3]) / 1e3;
        umi  = ((RX[4]*256) + RX[5]) / 1e2;
        bat  = ((RX[6]*256) + RX[7]) / 1e0;
        alt  = ((RX[8]*256) + RX[9]) / 1e2;
//  }
  
    Serial.print("Temperatura : ");
    Serial.print( temp );
    Serial.println(" *C");
    Serial.print("Pressao     : ");
    Serial.print( pres );
    Serial.println(" atm");
    Serial.print("Umidade     : ");
    Serial.print( umi );
    Serial.println(" %");
    Serial.print("Bateria     : ");
    Serial.print( bat );
    Serial.println(" V");
    Serial.print("Altitude    : ");
    Serial.print( alt );
    Serial.println(" m");
    Serial.println("");
    
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("TEMP : ");
    lcd.setCursor(7,0);
    lcd.print(temp);
    lcd.setCursor(12,0);
    lcd.print(" C");
    lcd.setCursor(0,1);
    lcd.print("UMID : ");
    lcd.setCursor(7,1);
    lcd.print(umi);
    lcd.setCursor(12,1);
    lcd.print(" %  ");
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("BAT  : ");
    lcd.setCursor(7,0);
    lcd.print(bat);
    lcd.setCursor(11,0);
    lcd.print(" V  ");
    lcd.setCursor(0,1);
    lcd.print("PRES : ");
    lcd.setCursor(7,1);
    lcd.print(pres);
    lcd.setCursor(11,1);
    lcd.print(" atm");   
    delay(1000); 
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("ALT. : ");
    lcd.setCursor(7,0);
    lcd.print(alt);
    lcd.setCursor(13,0);
    lcd.print(" m ");
    lcd.setCursor(0,1);
    lcd.print("");
    lcd.setCursor(7,1);
    lcd.print("");
    lcd.setCursor(13,1);
    lcd.print("");  
    delay(1000); 
    lcd.clear();
  }
}
[/code]

Hi John, thank you for your reply. So, I modified according to you but didn't work yet.

// [THIS IS THE TRANSMITTER CODE]
#include <Adafruit_Sensor.h> //INCLUSÃO DE BIBLIOTECA
#include <Adafruit_BMP280.h> //INCLUSÃO DE BIBLIOTECA
#include <SPI.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <DHT.h>                  // Biblioteca para DHT temp e umidade
#define dht_dpin A0
#define DHTTYPE DHT11

SoftwareSerial HC12(2, 3);

DHT dht(dht_dpin, DHTTYPE);       // Inicializa o sensor de temperatura e umidade
Adafruit_BMP280 bmp; //OBJETO DO TIPO Adafruit_BMP280 (I2C)

const int n = 10;
byte TX[n];


void setup()
{
  Serial.begin(9600);
  HC12.begin(9600);
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
  Serial.println("Iniciando..."); //IMPRIME O TEXTO NO MONITOR SERIAL

  if (!bmp.begin(0x76))
  {
    //SE O SENSOR NÃO FOR INICIALIZADO NO ENDEREÇO I2C 0x76, FAZ
    Serial.println(F("Sensor BMP280 não foi identificado! Verifique as conexões.")); //IMPRIME O TEXTO NO MONITOR SERIAL
    while (1); //SEMPRE ENTRE NO LOOP
  }
  else
  {
    Serial.println("BMP OK!"); //IMPRIME O TEXTO NO MONITOR SERIAL
  }
}


long readVcc()
{
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA, ADSC));
  result = ADCL;
  result |= ADCH << 8;
  result = 1125300L / result; // Back-calculate AVcc in mV
  return result;
}




void loop()
{
  float temp = bmp.readTemperature();
  float pressao = bmp.readPressure();
  pressao = pressao / 101325; // convertendo de Pa para ATM
  float umidade = dht.readHumidity();
  umidade = umidade - 100;
  float batt = readVcc();
  float alti = bmp.readAltitude();

  uint16_t t = temp * 1e2;
  uint16_t p = pressao * 1e3;
  uint16_t u = umidade * 1e2;
  uint16_t b = batt;
  uint16_t l = alti * 1e2;

  TX[0] = highByte(t);
  TX[1] = lowByte(t);
  TX[2] = highByte(p);
  TX[3] = lowByte(p);
  TX[4] = highByte(u);
  TX[5] = lowByte(u);
  TX[6] = highByte(b);
  TX[7] = lowByte(b);
  TX[8] = highByte(l);
  TX[9] = lowByte(l);

  for (int i = 0; i < n; i++)
  {
    HC12.write(TX[i]);
  }

  Serial.print("Temperatura : " );
  Serial.print(temp );
  Serial.println(" *C");
  Serial.print("Pressao     : " );
  Serial.print( pressao );
  Serial.println(" atm");
  Serial.print("Umidade     : " );
  Serial.print( umidade );
  Serial.println(" %");
  Serial.print("Alt.        : ");
  Serial.print(alti);
  Serial.println(" m");
  Serial.print("Bat.        : ");
  Serial.print(batt);
  Serial.println(" mV");
  Serial.println("");
  delay(3000);
}
//[THIS IS THE RECEIVER CODE]
/***CONFIGURATION ****/
const long interval = 5000;  // ms
const int slaves = 1; //Number of slaves (max 16) (go to line 95 and complete the if statement for every sensor)
/*********************/
//Libraries
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial HC12(5, 6); // RX, TX

float temp = 0, bat = 0, alt = 0;
float umi = 0, pres = 0;
float lati = 0, longi = 0;
char incomingByte, msg;
String command;

const int n = 10;
byte RX[n];


void setup()
{
  Serial.begin(9600);
  HC12.begin(9600);
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
  lcd.begin();

  lcd.setCursor(5, 0);
  lcd.print("LAFEJ");
  lcd.setCursor(2, 1);
  lcd.print("Aguardando...");
}

void loop()
{
  lcd.setCursor(5, 0);
  lcd.print("LAFEJ");
  lcd.setCursor(2, 1);
  lcd.print("Aguardando...");
  delay(500);
  lcd.clear();

  if (HC12.available() >= n)
  {
    Serial.println("FORA DO LOOP");

    for (int i = 0; i < n; i++)
    {
      RX[i] = HC12.read();
      Serial.print("RX[");
      Serial.print(i);
      Serial.print("]: ");
      Serial.println(RX[i]);
    }

    float temp = ((RX[0] * 256) + RX[1]) / 1.0e2;
    float pres = ((RX[2] * 256) + RX[3]) / 1.0e3;
    float umi  = ((RX[4] * 256) + RX[5]) / 1.0e2;
    float bat  = ((RX[6] * 256) + RX[7]);
    float alt  = ((RX[8] * 256) + RX[9]) / 1.0e2;

    Serial.print("Temperatura : ");
    Serial.print( temp );
    Serial.println(" *C");
    Serial.print("Pressao     : ");
    Serial.print( pres );
    Serial.println(" atm");
    Serial.print("Umidade     : ");
    Serial.print( umi );
    Serial.println(" %");
    Serial.print("Bateria     : ");
    Serial.print( bat );
    Serial.println(" V");
    Serial.print("Altitude    : ");
    Serial.print( alt );
    Serial.println(" m");
    Serial.println("");

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("TEMP : ");
    lcd.setCursor(7, 0);
    lcd.print(temp);
    lcd.setCursor(12, 0);
    lcd.print(" C");
    lcd.setCursor(0, 1);
    lcd.print("UMID : ");
    lcd.setCursor(7, 1);
    lcd.print(umi);
    lcd.setCursor(12, 1);
    lcd.print(" %  ");
    delay(1000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("BAT  : ");
    lcd.setCursor(7, 0);
    lcd.print(bat);
    lcd.setCursor(11, 0);
    lcd.print(" V  ");
    lcd.setCursor(0, 1);
    lcd.print("PRES : ");
    lcd.setCursor(7, 1);
    lcd.print(pres);
    lcd.setCursor(11, 1);
    lcd.print(" atm");
    delay(1000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("ALT. : ");
    lcd.setCursor(7, 0);
    lcd.print(alt);
    lcd.setCursor(13, 0);
    lcd.print(" m ");
    lcd.setCursor(0, 1);
    lcd.print("");
    lcd.setCursor(7, 1);
    lcd.print("");
    lcd.setCursor(13, 1);
    lcd.print("");
    delay(1000);
    lcd.clear();
  }
}

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