Two arduinos comunicating via HC-05 data dont show the right data

Hello to everybody

I have a small issue,
On my project two arduinos are comunicating via HC-05 modules so one arduino send the command and the other is waiting for the command and then send the temperature.

As i can see the first arduino recive the data but not in a way that i can use. so im asking you guys if you can look at the code and see where i did the mistake.

The temperature sensor is a DHT 22

THE ARDUINO SENDING THE COMAND CODE

#include <Wire.h>  
byte degreeChr[8] = { B00111, B00101, B00111, B00000, B00000, B00000, B00000,};
#include <AltSoftSerial.h>
AltSoftSerial BTserial; 

// Set DEBUG to true to output debug information to the serial monitor
boolean DEBUG = true;

// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;
 
 
// Variables used for the timer
unsigned long startTime = 0;
unsigned long waitTime = 1000;
 
const byte TEMP_PIN = 7;
 
 
void setup()  
{
  
  if (DEBUG)
  {
       // open serial communication for debugging
       Serial.begin(9600);
       Serial.println(__FILE__);
       Serial.println(" ");
  }
 
    BTserial.begin(9600); 
    if (DEBUG)  {  Serial.println("AltSoftSerial started at 9600");     }
 
    newData = false; 
    startTime = millis();
 
} // void setup()
void loop()  
{
    if (  millis()-startTime > waitTime ) 
    {
       BTserial.print("<sendTemp>");  
       if (DEBUG) { Serial.println("Request sent"); }
       startTime = millis();
    }
 
    recvWithStartEndMarkers(); 
    if (newData)  
    {    
         if (DEBUG) { Serial.println("Data received"); }
         Serial.println(receivedChars);
          newData = false;  
          receivedChars[0]='\0';  
    }    
}
 
 
 
 
// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See  http://forum.arduino.cc/index.php?topic=288234.0
/*
****************************************
* Function recvWithStartEndMarkers
* reads serial data and returns the content between a start marker and an end marker.
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       newData
*       receivedChars
*
*/
void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (BTserial.available() > 0) 
     {
          rc = BTserial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
}

ARDUINO SENDING THE DATA CODE

/*
* Sketch: Arduino2Arduino_example2_RemoteTemp_Slave
* By Martyn Currey
* 11.05.2016
* Written in Arduino IDE 1.6.3
*
* Send a temperature reading by Bluetooth
* Uses the following pins
*
* D8 - software serial RX
* D9 - software serial TX
* A0 - single wire temperature sensor
*
*
* AltSoftSerial uses D9 for TX and D8 for RX. While using AltSoftSerial D10 cannot be used for PWM.
* Remember to use a voltage divider on the Arduino TX pin / Bluetooth RX pin
* Download from https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
*/
#include <Wire.h>
#include <DHT.h>
#include <AltSoftSerial.h>
AltSoftSerial BTserial; 
#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
int chk;
float hum;  //Stores humidity value

 
// Set DEBUG to true to output debug information to the serial monitor
boolean DEBUG = true;
 
 
// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;
 
const byte TEMP_PIN = 7;
 
 
void setup()  
{
    dht.begin();   
    if (DEBUG)
    {
        // open serial communication for debugging
        Serial.begin(9600);
        Serial.println(__FILE__);
        Serial.println(" ");
        
    }
 
    //  open a software serial connection to the Bluetooth module.
    BTserial.begin(9600); 
    if (DEBUG)  {   Serial.println(F("AltSoftSerial started at 9600"));     }
    newData = false;
 
} // void setup()
 
 
 
 
void loop()  
{
    recvWithStartEndMarkers(); 
    if (newData)  {   processCommand();  }    
}
 
 
 
 
/*
****************************************
* Function getTemp
* read a analogue pin and converts the value to a temperature
* based on the adafruit thermistor guide https://learn.adafruit.com/thermistor/testing-a-thermistor 
* 
* passed:
*  
* global: 
*
* Returns:
*        float temp  
* Sets:
*
*/

/*
****************************************
* Function processCommand
* parses data commands contained in receivedChars[]
* receivedChars[] has not been checked for errors
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       receivedChars[]
*       newData
*
*/
void processCommand()
{
 
     Serial.println(receivedChars);
 
     if (strcmp ("sendTemp",receivedChars) == 0) 
     { 
         float temp = dht.readTemperature();
         BTserial.print("<");  BTserial.print( temp ); BTserial.print(">");
         if (DEBUG) { Serial.print("Temp is ");   Serial.print(temp);  Serial.println("");   }
     }
 
     newData = false;
     receivedChars[0]='\0'; 
 
}
 
 
 
 
 
// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See  http://forum.arduino.cc/index.php?topic=288234.0
/*
****************************************
* Function recvWithStartEndMarkers
* reads serial data and returns the content between a start marker and an end marker.
* 
* passed:
*  
* global: 
*       receivedChars[]
*       newData
*
* Returns:
*          
* Sets:
*       newData
*       receivedChars
*
*/
void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (BTserial.available() > 0) 
     {
          rc = BTserial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
 
}

You are calling both programs “sending” which is a bit confusing. I can’t figure which one is supposed to have the problem.

Also, can you show what is being received and what should be received?

It is VERY much easier to makes sense of code if there is just one statement on each line.

…R

What is the value returned by temp = dht.readTemperature();

What are you actually receiving on the master device (printed by Serial.println(receivedChars);)?

Hello Guys,

Sory for the late reply

The first code is the arduino that is sending the command, and the second code is the arduino that get the data from the temperature sensor and send it back to the first arduino.

Here in the attachment you can finde what i see on the serial monitor of the arduino that is reciving back the temperature data.

I can see that the comunication betwen the two arduinos is open because on both arduinos the TX LED is blinking

Thank you guy for the support

Image from Reply #3 so we don’t have to download it. See Image Guide

b157df4cf9c2b7622993bc796a641cf6614ab310.png

…R

Please post the output as text (rather than a picture) so we can read it.

...R

Since you are displaying "Data received" we know that the newData flag is being set which means you are receiving the start and end markers. What is not clear is what, if anything, is between the markers. It may be nothing or it may be non printable characters.

It may be worth while taking a step back; double check that the temperature sensor is working without Bluetooth, get a basic connection working using basic serial prints, and then add put the 2 together.

I did that and the temperature sensor is working its write on the serial monitor the right number,

Its possible that the modul with the temperature is sending the data but somehow not the right type of data.

Because when i connect the modul that have to recive back the temperature,(i will call him the master modul),
when i print the newData its show 1 because its boolean so my preposition is that the modul is reciving something but not the right data :slight_smile:

RokiRokus:
Because when i connect the modul that have to recive back the temperature,(i will call him the master modul),
when i print the newData its show 1 because its boolean so my preposition is that the modul is reciving something but not the right data :slight_smile:

If you want to get communication working between two Arduinos don't bother with any sensors. Just get one of them to send "" and the other one to send "" at (say) 1 second intervals.

When that simple program works you can extend it to do other stuff.

...R

Did you try the first example (flash an LED) from the website you are using and did you get it working?

Hello,

Yes i tried to blink an LED but it worked, but then i figured out that maybe i damaged a port when i play with the AT command, and there were some problem with the HC-05 and quess what the port gives the logic 1 up to 2V and what is to small to the arduino to understand as positive logic, i changed the master HC 05 and now is working properli.

Thank you guys again for your help