Go Down

Topic: Send digit from PC to Nano via bluetooth with a script? (Read 210 times) previous topic - next topic

majhi

Hello all.  I have a project that controls my lights using a Nano and bluetooth module (HC-05).  I use my tablet to send a single digit to the Arduino via BT, which converts that digit into ASCII, then performs an action accordingly (see code below).  My question is if I can write a script on my PC (either a .bat or a hotkey) that can do the same thing: send a single digit to the BT module.  What would be the simplest way to go about doing this?  Thanks in advance.

Code: [Select]
int data = 0;
#define lights 6

void setup()
{
  Serial.begin(9600);
  pinMode(lights, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}

void flash()
{
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);
  digitalWrite(LED_BUILTIN, LOW);
}

void loop()
{
  if(Serial.available() > 0)      // Send data only when you receive data:
  {
    data = Serial.read();
    Serial.println(data);
   
    // LIGHTS
    if (data == 49)
    {
      analogWrite(lights,255);
      Serial.println("Lights 100");
      flash();
    }
    if (data == 50)
    {
      analogWrite(lights,255*.6);
      Serial.println("Lights 60");
      flash();
    }
    if (data == 51)
    {
      analogWrite(lights,255*.3);
      Serial.println("Lights 30");
      flash();
    }
    if (data == 52)
    {
      analogWrite(lights,255*.1);
      Serial.println("Lights 10");
      flash();
    }
    if (data == 53)
    {
      analogWrite(lights,0);
      Serial.println("Lights 0");
      flash();
    }
    if (data == 54)
    {
      for(int fadeValue = 0 ; fadeValue <= 150; fadeValue +=5) {
        analogWrite(lights, fadeValue);
        delay(70);
      }
      Serial.println("Morning ritual");
      flash();
    }
  }
}

Robin2

A problem with a PC script or batch file is that it will probably open the serial port, send the message and then close the serial port. But when the PC opens the serial port that normally causes the Arduino to reset, which takes a  few seconds with the result that it won't hear the message from the script.

A better idea is to write a PC program that opens the serial port and allows time for the Arduino to reset before sending the first message. Then it should keep the serial port open until it is completely finished with the Arduino.

This Simple Python - Arduino demo should give you the idea. The Python code should work on Windows if you edit it to use the Windows style of COM ports. And you can do the same sort of thing with any PC programming language.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Nick_Pyner

I use my tablet to send a single digit to the Arduino via BT, which converts that digit into ASCII, then performs an action accordingly (see code below).  My question is if I can write a script on my PC (either a .bat or a hotkey) that can do the same thing: send a single digit to the BT module.  
I guess you can simply send the data using a PC terminal programme like RealTerm. It beats the hell out of re-inventing the wheel.

groundFungus

I use the Bluetooth Terminal app from the Play Store on my tablet to communicate with my Arduino projects via Bluetooth.  You can, easily, define buttons to do actions (macros).
You will save everyone's time if you read and follow the forum guidelines.  :)          
https://forum.arduino.cc/index.php/topic,148850.0.html
and
https://forum.arduino.cc/index.php?topic=97455.0

majhi

I guess you can simply send the data using a PC terminal programme like RealTerm. It beats the hell out of re-inventing the wheel.
And that can work over BT?

I use the Bluetooth Terminal app from the Play Store on my tablet to communicate with my Arduino projects via Bluetooth.  You can, easily, define buttons to do actions (macros).
I already have an app on my tablet, I'm looking to send from PC.

Nick_Pyner

#5
Nov 21, 2020, 11:36 pm Last Edit: Nov 21, 2020, 11:38 pm by Nick_Pyner
And that can work over BT?
Yes.
Since you already have an app on the tablet, and assuming you are OK with that, I fail to see why you would not do the same thing on the PC. I think you are simply drowning in your own technobabble.

majhi

Yes.
Since you already have an app on the tablet, and assuming you are OK with that, I fail to see why you would not do the same thing on the PC. I think you are simply drowning in your own technobabble.
That's the whole point of my first question, haha.  I don't know how to send single digits over BT from PC.  I'll take a look at the link you posted, thanks for the tip.

Nick_Pyner

I don't know how to send single digits over BT from PC. 
This probably has something to do with pressing the key with the appropriate digit printed thereon.  There is a swag of terminals available - all free. There may be minor differences that better suit your needs.

Go Up