Bluetooth communication via app ,,Serial Bluetooth Terminal"

Hello guys,
I am doing a project, that is a senzor connected on Arduino Uno and there is also a bluetooth module HM-10 and i am trying to send informations from the Arduino via bluetooth to the mobile phone. And I am having a trouble, because i have a code, that can send data from Arduino to app called Serial Bluetooth Terminal, but when I write something that is defined in switch, to the app, my Arduino do following things:
a) when a sensor is 1 and I write defined letter into the app, my Arduino doesn´t do anything.
b) when a sensor is 0 and the arduino is in sleep mode and i write the defined letter into the app, Arduino wake up, write ,,Value on sensor is 0" and again fall asleep.
My code that i am having trouble with: Bluetooth doors - Pastebin.com
Can anyone help me solve this problem?

external links like the one you have posted above are a potential dangerness as they could be
maliciuos. I'm pretty sure you are a user with good intentions. If you really are then

You should post code by using code-tags
There is an automatic function for doing this inthe Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy to forum"
  3. paste clipboard into write-window of a posting

If your code exceeds 9000 characters you can attach an *.INO-file to a posting

best regards Stefan

By copying the code there you ment this right?
code:

#include <avr/sleep.h>
#include <avr/power.h>
#include <SoftwareSerial.h>


#define pin2 2
#define LED 13
SoftwareSerial bluetooth(0, 1);

void pin2Interrupt(void)
{

  detachInterrupt(0);

}

void enterSleep()
{

  sleep_enable();

  attachInterrupt(digitalPinToInterrupt(pin2), WakeUp, CHANGE);


  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  digitalWrite(LED, LOW);
  delay(100);

  sleep_cpu();
  Serial.println("Arduino just woke up.");
  digitalWrite(LED, HIGH);
}

void setup()
{
  bluetooth.begin(9600);
  bluetooth.println("test Bluetooth.");

  Serial.begin(9600);


  pinMode(pin2, INPUT_PULLUP);
  pinMode(LED, OUTPUT);

  digitalWrite(pin2, HIGH);
  digitalWrite(LED, HIGH);
}
void WakeUp() {

  sleep_disable();

}

void commands() {
  byte BluetoothData;

  if (bluetooth.available() > 0) {

    BluetoothData = bluetooth.read();

    switch (BluetoothData) {
  
      case '0':
      
        ;
        bluetooth.println("Written value is 0.");
        break;
      case '1':

        bluetooth.println("Written value is 1.");
        break;
      case 'a':
       
        bluetooth.print("Time since the Arduino program started:  ");
        bluetooth.print(millis() / 1000);
        bluetooth.println(" seconds");
        break;

      case '\r':
    
        break;
      case '\n':
  
        break;
      default:
  
        bluetooth.println("Unknown command.");
    }
  }
 
  delay(100);


}
void loop()
{



 

  Serial.println(digitalRead(pin2));
  if (digitalRead(pin2) == 0)
  {
    Serial.println("Value on sensor is 0");
    Serial.println("doors are closed");
    commands();
    delay(1000);

    enterSleep();

  }
  else
  {
    Serial.println("Value on sensor is 1");
    Serial.println("doors are open");
    commands();
    delay(1000);

    WakeUp();
  }



}

For making an arduino sleep there must be some example-code online.
Did you ever look into such an example?

You have to explain in more detail:

Did you test each single part of your code on its own.

I mean a simple testprogram that does nothing more than go sleeping
on pressing a button or switch wake up
stay awake for some seconds go sleeping again?

Did you test serial receiving via bluetooth stand alone with echoing the characters you have received to the
"debug"-serial ?

here is a codeversion with more serial debug-output and better formatted. Still readable but more compact.

#include <avr/sleep.h>
#include <avr/power.h>
#include <SoftwareSerial.h>

#define pin2 2
#define LED 13
SoftwareSerial bluetooth(0, 1);

void pin2Interrupt(void) {
  detachInterrupt(0);
}

void enterSleep() {

  sleep_enable();

  attachInterrupt(digitalPinToInterrupt(pin2), WakeUp, CHANGE);

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  digitalWrite(LED, LOW);
  delay(100);

  sleep_cpu();
  Serial.println("Arduino just woke up.");
  digitalWrite(LED, HIGH);
}

void setup() {
  bluetooth.begin(9600);
  bluetooth.println("test Bluetooth.");

  Serial.begin(9600);

  pinMode(pin2, INPUT_PULLUP);
  pinMode(LED, OUTPUT);

  digitalWrite(pin2, HIGH);
  digitalWrite(LED, HIGH);
}

void WakeUp() {
  sleep_disable();
}


void commands() {
  Serial.println("entering commands()");
  
  byte BluetoothData;

  if (bluetooth.available() > 0) {

    BluetoothData = bluetooth.read();
    Serial.print("received Data #");
    Serial.print(BluetoothData);
    Serial.println("#");

    switch (BluetoothData) {
 
      case '0':
     
        ;
        bluetooth.println("Written value is 0.");
        break;
      case '1':

        bluetooth.println("Written value is 1.");
        break;
      case 'a':
       
        bluetooth.print("Time since the Arduino program started:  ");
        bluetooth.print(millis() / 1000);
        bluetooth.println(" seconds");
        break;

      case '\r':
   
        break;
      case '\n':
 
        break;
      default:
 
        bluetooth.println("Unknown command.");
    }
  }
  delay(100);
  Serial.println("leaving commands()");

}

void loop() {

  Serial.println(digitalRead(pin2));
  
  if (digitalRead(pin2) == 0) {
    Serial.println("Value on sensor is 0");
    Serial.println("doors are closed");
    commands();
    delay(1000);

    enterSleep();

  }
  else {
    Serial.println("Value on sensor is 1");
    Serial.println("doors are open");
    commands();
    delay(1000);

    WakeUp();
  }
}

best regards Stefan

htooteulb:
By copying the code there you ment this right?
code:

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(0, 1);

You are using software serial on hardware serial pins 0,1. This is a sure-fire recipe for disaster. You need to make up your mind whether to

  1. leave Bluetooth where it is but dispense with software serial OR

  2. Retain software serial but use another pair of pins for Bluetooth.

Since the real job is to communicate with Android, the former may suffice.

So first of all, I tried to run program for sleeping separately and it worked normally and I did the same thing with the part of the program, that send and recieve data from android phone and it worked again.
Now i did some changes and my program now work as i wanted it to work, but there is still one little thing.
When my Arduino is in sleep mode (value on sensor is 0) and i write something into the mobile app Arduino wake up write the value, that should be stored in ,,BluetoothData", but there is stored a different number (166, 83, 13, 10 and more), which I dont understand and then it write ,,unknown command" into the app and then it fall asleep again.
How can i do it to be able to write something in the app on phone when Arduino is in sleep mode and recieve the message in the app?

my new modified code:

#include <avr/sleep.h>
#include <avr/power.h>
#include <SoftwareSerial.h>

#define RX 11
#define TX 10
#define pin2 2
#define LED 13
SoftwareSerial bluetooth(11, 10);

void pin2Interrupt(void) {
  detachInterrupt(0);
}

void enterSleep() {

  sleep_enable();

  attachInterrupt(digitalPinToInterrupt(pin2), WakeUp, CHANGE);

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  digitalWrite(LED, LOW);
  delay(100);

  sleep_cpu();
  Serial.println("Arduino just woke up.");
  digitalWrite(LED, HIGH);
}

void setup() {
  bluetooth.begin(9600);
  bluetooth.println("test Bluetooth.");

  Serial.begin(9600);

  pinMode(pin2, INPUT_PULLUP);
  pinMode(LED, OUTPUT);

  digitalWrite(pin2, HIGH);
  digitalWrite(LED, HIGH);
}

void WakeUp() {
  sleep_disable();
}


void commands() {
  WakeUp();


  byte BluetoothData;

  if (bluetooth.available() > 0 ) {

    BluetoothData = bluetooth.read();



    switch (BluetoothData) {

      case '0':

        bluetooth.println("Written value is 0.");
        break;
      case '1':

        bluetooth.println("Written value is 1.");
        break;
      case 'a':

        bluetooth.print("Time since the Arduino program started:  ");
        bluetooth.print(millis() / 1000);
        bluetooth.println(" seconds");
        break;

      case '\r':

        break;
      case '\n':

        break;
      default:

        bluetooth.println("Unknown command.");
    }
  }
  delay(300);
  Serial.println(BluetoothData);

}

void loop() {
  commands();

  Serial.println(digitalRead(pin2));

  if (digitalRead(pin2) == 0) {
    Serial.println("Value on sensor is 0");
    Serial.println("doors are closed");
    bluetooth.println("doors are closed");
    delay(1000);

    enterSleep();

  }
  else {
    Serial.println("Value on sensor is 1");
    Serial.println("doors are open");
    bluetooth.println("doors are open");
    delay(5000);
    , ,
    WakeUp();
  }


}

I don't want to be rude or something, but i would really use any help. I am getting desperate, because I couldn't find anything on google.

I haven't worked with the sleepmode yet.

My guess is that as long as the arduino is in sleep-mode the serial-receive does not work properly. Serial-receive needs that the microcontroller is awake and running (instead of sleeping) fortunately the arduino wakes up on a serial receive at all.

So without having your hardware I can just suggest that you need to send a first byte and send a message like "awake know. Send command"

as a standrad-search for online-ressources I use

"Arduino keyword" where "keyword is the specific part what information I'm looking for. This is always worth a 5 minute-search. often successful but not 100%.
So here i used
www.google.de/search?as_q=Arduino+sleepmode+wake+up+through+serial+receive

and this came up with this hit
https://forum.arduino.cc/index.php?topic=66042.0

The HM-10-module has the CTS and the DTS-signal
They mean request to send clear to send
so if you use activate hardware flow-control on the bluetooth-moduls this might work

just another gearch
www.google.de/search?as_q=serial+cts+dts
which came up with this

http://www.brainboxes.com/faq/items/what-is-rts--cts-hardware-flow-control-

best regards Stefan

I have spent a lot of time searching something, that would be useful for me, but unfortunately I am searching wrong or It just can't be done as I wanted it. I have just fixed a lot of problems, that i had at the start of this post, but now Arduino still can't read values, that i write into the app. It always return me unknown command.
If anyone know how to fix it, I would be glad.
Thanks

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