Nano & HC-05 help

I am trying to get some existing code to work over BT using the HC-05. I am able to send & receive through software.serial just fine. The issue I am having is that when sending commands over the BT serial connection (using TeraTerm) the command shows up in serial monitor of the USB connection on the Nano but it doesn't actually execute that command on the nano and return the data. If I run the command in serial monitor it runs just fine and everything that shows up in the serial monitor shows up in the serial connection of the HC-05. I can see data going both ways so I know it isn't a hardware or cabling issue. I also have a 2.2k & 4.7k resistor as a voltage divider on the HC-05 so I get 3.3v. Using pins 11, 9 for rx/tx

The nano has a BME280 sensor on it that is used as part of a dew heater for a telescope. Sending oz# should return a string of temp, humidity, etc data from the BME along with some data from 2 DS18b20 sensors.

This is the project on Github:

Will upload the code that has the software.serial next

[code]
/*
 * Q-Astro Dew Monitor
 *
 * Q-Astro Dew Monitor Code.
 * Version: 3.0.2
 * 
 * Copyright (c)2022 Quidne IT Ltd.
 * 
 */

#include <Arduino.h>
#include <DallasTemperature.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Timer.h"
#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(11, 9); // RX | TX


#define DEVICE_RESPONSE "Q-Astro Dew Monitor"
#define VERSION "v3.0.2"

#define LCDPresent 1    //Change this to 0 if you do not use the LCD display. 

#define qastroId 'i'
#define observingconditionsId 'o'

String ASCOMcmd;
bool ASCOMcmdComplete;

bool ShowData = false;
int DisplayAlwaysOn = 1;

void setup() 
{
  BTserial.begin(9600);
  InitSerial();
  if (LCDPresent==1) {
    Serial.println("Init OLED");
    InitOLEDLCD();
  }
  Serial.println("Init Dew Monitor");
  InitObservingConditions();
  Serial.println("Ready..");
}

void loop() {

    if(BTserial.available()){
    Serial.write(BTserial.read());
   }
    if (Serial.available()) {
    BTserial.write(Serial.read());
   }

  if (ASCOMcmdComplete) {

    switch((char)ASCOMcmd[0]) {
      case qastroId:
          SendSerialCommand((String(DEVICE_RESPONSE) + " " + String(VERSION)));
        break;

      case observingconditionsId: //Case fhe function is for the Environmentals
        DoObservingConditionsAction(ASCOMcmd.substring(1)); //Remove first char from string as this is the function type.
      break;
    }
    
    ASCOMcmdComplete = false;
    ASCOMcmd = "";
  }

  if (LCDPresent==1) {CheckShowDataButton();}

  UpdateData();
}
[/code]

serial.ino tab:

/* ---------------------------------------------------------------------------------------------------------------------------- */
/* Start of Serial Commands */

void InitSerial()
{
    Serial.flush();
    Serial.begin(9600);  // Baud rate, make sure this is the same as ASCOM driver
    ASCOMcmd = "";
    ASCOMcmdComplete = false;  
}

void serialEvent() 
{
  while (Serial.available()) 
  {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the ASCOMcmd:
    ASCOMcmd += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '#') {
      ASCOMcmdComplete = true;
    }
  }
}

void SendSerialCommand(String sendCmd)
{
  Serial.print(sendCmd); 
  BTserial.print(sendCmd);
  Serial.println("#");  // Similarly, so ASCOM knows
  BTserial.println("#");
}

void SendSerialCommand(char function, String sendCmd)
{
  Serial.print(function);
  BTserial.print(function);
  Serial.print(sendCmd); 
  BTserial.print(sendCmd);
  Serial.println("#");  // Similarly, so ASCOM knows
  BTserial.println("#");
}

/* ---------------------------------------------------------------------------------------------------------------------------- */
/* End of Serial Commands */

type or paste code here


Screenshot showing data going both directions & ouput when entering in oz# command
Last of oz# was sent through TeraTerm on the HC-05 serial connection, which is received on the Nano, but does not do anything with it.

Got this working. The tab that was sending the temp data was only sending it via the serial port, added lines to send via the BTserial and it is now working as expected.

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