Not Able to Use Bluetooth COM

Hi All!

I am trying to make a sensor node for my bedroom. I have this sensor from Amazon for my Arduino. I have followed this tutorial. To get a base for how to connect a bluetooth device to my Arduino.

However, after I pair (using the default paring code of 1234), upload the sketch via usb serial, then unplug it and use the usb cable to supply power from a battery bank, I get the option to use two different COM ports to upload my sketch.

However, both give me the “An error occurred while uploading the sketch”.

I’m also trying to rename the device and give it a new PIN. (Its currently connected to my PC as HC-06, but I want to change it to 455Sensor_Node, and the PIN to 1218).

I have attached my sketch below in case if it helps. I used ta tutorial for the AT commands but can’t find them at the moment!

//Bluetooth Libraries
/*
 * Arduino treats bluetooth like a Serial conection, so the standard "Serial" library can be used.
 * No additional libraties go here
 * DO NOT plug in Bluetooth when uploading code, or it will override this serial connection.
 */

//Bluetooth Statements
/*Rx = 0;
 * Tx = 1;
 */

//Bluetooth Variables
char incoming, sending;

//DHT11 Libraries
#include <dht.h>

//DHT11 Statements
dht DHT;
#define DHT11_Pin A0

//Storage Variables for DHT
float cels1, cels2, cels3, fahrs;
float hum1, hum2, hum3, avghum;

//TE215 Statements
int TE_Pin = A1;

//Storage Variables for TE215
float readt1, readt2, readt3, avgreadt;

//Photocell Statements
int PhC_Pin = A2;

//Storage Variables for Photocell
float readp1, readp2, readp3, avgreadp;

//Pins
int datain = 12;
int rTE = 11;
int rDHTt= 10;
int rDHTh = 9;
int rPhC = 8;
int dataout = 7;
int error = 6;
int DHTerror = 5;

void setup() {
  //Serial Statements
  Serial.begin(9600);
  Serial.println("AT+NAME455Sensor_Node");
  Serial.println("AT+PIN1218");
  digitalWrite(datain, HIGH);
  delay(5000)

  //LED Statements
  pinMode(datain, OUTPUT);
  pinMode(rTE, OUTPUT);
  pinMode(rDHTt, OUTPUT);
  pinMode(rDHTh, OUTPUT);
  pinMode(rPhC, OUTPUT);
  pinMode(dataout, OUTPUT);
  pinMode(error, OUTPUT);
  pinMode(DHTerror, OUTPUT);

  digitalWrite(datain, LOW);
  digitalWrite(rTE, LOW);
  digitalWrite(rDHTt, LOW);
  digitalWrite(rDHTh, LOW);
  digitalWrite(rPhC, LOW);
  digitalWrite(dataout, LOW);
  digitalWrite(error, LOW);
  digitalWrite(DHTerror, LOW);
}


void loop() {
  if (Serial.available()>0){
    incoming = Serial.read();
    digitalWrite(error, LOW);
    digitalWrite(rTE, LOW);
    digitalWrite(rDHTt, LOW);
    digitalWrite(rDHTh, LOW);
    digitalWrite(rPhC, LOW);
    digitalWrite(dataout, LOW);
    digitalWrite(datain, HIGH);

    delay(1000);
    
    if (incoming == "TE215"){
      digitalWrite(rTE, HIGH);
      digitalWrite(DHTerror, LOW);
      digitalWrite(datain, LOW);
      TE215_Reading();
    }
    
    else if (incoming == "DHTt"){
      digitalWrite(rDHTt, HIGH);
      digitalWrite(DHTerror, LOW);
      digitalWrite(datain, LOW);
      DHT11_Temp();
    }
    
    else if (incoming == "DHTh"){
      digitalWrite(rDHTh, HIGH);
      digitalWrite(DHTerror, LOW);
      digitalWrite(datain, LOW);
      DHT11_Hum();
    }
    
    else if (incoming == "PhC"){
      digitalWrite(rPhC, HIGH);
      digitalWrite(DHTerror, LOW);
      digitalWrite(datain, LOW);
      PhC_Reading();
    }
    
    else{
      digitalWrite(error, HIGH);
      digitalWrite(datain, LOW);
      digitalWrite(DHTerror, LOW);
    }
  }
}


void TE215_Reading() {
  readt1 = analogRead(TE_Pin);
  readt2 = analogRead(TE_Pin);
  readt3 = analogRead(TE_Pin);
  avgreadt = (readt1 + readt2 + readt3)/3;
  
  digitalWrite(dataout, HIGH);
  sending = avgreadt;
  Serial.println(sending);
  delay(1000);
  digitalWrite(dataout, LOW);
}


void DHT11_Temp(){
  int chk = DHT.read11(DHT11_Pin);
  switch (chk)
  {
    case DHTLIB_OK:  
    digitalWrite(DHTerror, LOW);
    break;
    case DHTLIB_ERROR_CHECKSUM: 
    digitalWrite(DHTerror, HIGH);
    break;
    case DHTLIB_ERROR_TIMEOUT: 
    digitalWrite(DHTerror, HIGH);
    break;
    default: 
    digitalWrite(DHTerror, HIGH);
    break;
  }

  cels1 = DHT.temperature;
  cels2 = DHT.temperature;
  cels3 = DHT.temperature;
  fahrs = ((cels1 + cels2 + cels3)/3 * 9/5) + 32;
  
  digitalWrite(dataout, HIGH);
  sending = fahrs;
  Serial.println(sending);
  delay(1000);
  digitalWrite(dataout, LOW);
}


void DHT11_Hum(){
  int chk = DHT.read11(DHT11_Pin);
  switch (chk)
  {
    case DHTLIB_OK:  
    digitalWrite(DHTerror, LOW);
    break;
    case DHTLIB_ERROR_CHECKSUM: 
    digitalWrite(DHTerror, HIGH);
    break;
    case DHTLIB_ERROR_TIMEOUT: 
    digitalWrite(DHTerror, HIGH);
    break;
    default: 
    digitalWrite(DHTerror, HIGH);
    break;
  }

  hum1 = DHT.temperature;
  hum2 = DHT.temperature;
  hum3 = DHT.temperature;
  avghum = ((cels1 + cels2 + cels3)/3);

  digitalWrite(dataout, HIGH);
  sending = avghum;
  Serial.println(sending);
  delay(1000);
  digitalWrite(dataout, LOW);
}


void PhC_Reading() {
  readp1 = analogRead(TE_Pin);
  readp2 = analogRead(TE_Pin);
  readp3 = analogRead(TE_Pin);
  avgreadp = (readp1 + readp2 + readp3)/3;
  
  digitalWrite(dataout, HIGH);
  sending = avgreadp;
  Serial.println(sending);
  delay(1000);
  digitalWrite(dataout, LOW);
}

Any and all help is super appreciated, but it might take me a while to get back since I just started Uni!

Bluetooth works in two modes - AT mode, and communication mode. You are apparently trying to do both at the same time, which is unnecessary, and not even a good idea at this juncture.

It is good practice to use a 1k/2k voltage divider in ther Arduino Tx line.

Get rid of all the Serial.print (AT .... stuff, and then ensure your programme does what you want on the serial monitor.

Disconnect monitor, install bluetooth and check all is well, preferably on your Android phone.

Reconfigure bluetooth as you need, using a separate programme, probably with BT on software serial. This exercise should be once-only

Put BT back on pins 0,1 and reload your working programme, secure in the knowledge that your last problem, if there is one, is sorting out the Windows COM port.