Sending serial data via batch file triggered by a stream deck to be received by Arduino Uno Rev3

Hello everybody,

as I am wanting to interact with an arduino uno REV3 using a stream deck and there does not seem to be a plugin to do serial communication for the stream deck I found a possible workaround by using a batch file converted to an exe which I can start from the streamdeck. That converted batch file is then meant to send an integer to the comport to which the Arduino is connected.

The "Serial Input Basics - updated" thread Serial Input Basics - updated gave me some insights like sending single character messages via serial is advised due to the rather slow speed of this connection.

Anyhow I have tried to send either
echo 1 > COM5
echo 1 >\.\COM5
set /p x="1" \.\COM5
Via a batch file, but also via the command prompt directly as user and as Admin. At least I found out that you have to close the serial monitor to at least get access to the COM port :smiley: but thats it .

None of the sent commands seem to be received by the arduino uno REV3 using the following sketch in the Arduino IDE 2.0.3:

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

int light_group_1_active = 0;
int light_group_2_active = 0;

void setup() {
  Serial.begin(9600);
  pinMode(10, INPUT);          // for button at pin 10
  pinMode(11, INPUT);          // for button at pin 11
  mySwitch.enableTransmit(3);  // transmitter at digital pin 3 of arduino uno
  mySwitch.enableReceive(0);   // receiver at digital pin 2 of arduino uno
  mySwitch.setProtocol(2);
  Serial.println("");
  Serial.println("ready");
}


void loop() {

  /////// SEND 433MHZ
  if (digitalRead(10) == HIGH) {
    button_1_toggle();
  }
  if (digitalRead(11) == HIGH) {
    button_2_toggle();
  }

  //////// RECEIVE 433MHZ
  if (mySwitch.available()) {
    Serial.print("Received ");
    Serial.print(mySwitch.getReceivedValue());
    Serial.print(" / ");
    Serial.print(mySwitch.getReceivedBitlength());
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println(mySwitch.getReceivedProtocol());
    mySwitch.resetAvailable();
  }

  //////// RECEIVE string from COM port
  while (Serial.available() > 0) {
    int received_data = Serial.read();
    if (received_data == "1") {
      button_1_toggle();
    } else if (received_data == "2") {
      button_2_toggle();
    } else {
      Serial.println(Serial.read());
    }
  }
}



void button_1_toggle() {
  if (light_group_1_active == 0) {
    Serial.println("activate light group 1");
    mySwitch.send(907438080, 32);
    light_group_1_active = 1;
  } else {
    Serial.println("de-activate light group 1");
    mySwitch.send(1041655808, 32);
    light_group_1_active = 0;
  }
  delay(500);
}



void button_2_toggle() {
  if (light_group_2_active == 0) {
    Serial.println("activate light group 2");
    mySwitch.send(236349440, 32);
    light_group_2_active = 1;
  } else {
    Serial.println("de-activate light group 2");
    mySwitch.send(18245632, 32);
    light_group_2_active = 0;
  }
  delay(500);
}

Hope I have included all the needed info, and wonder what I am missing or if there is another easy way to call the functions button_1_toggle and button_2_toggle from the stream deck ...

Oh by the way: Using the physical buttons connected directly to the arduino to call the functions and trigger the lights works perfectly fine :wink:

Cheers
Ralf

That is likely because you do NOT wait a second or so while the Arduino is being reset by initiation of the serial connection.

Hello Paul,

and thanks for your suggestion, although I am not quite sure what to do. Following your post I tried "delay(2000);" before, at the start and at the end of the "while" loop, which to my understanding is only true when there has something been received and stored in the serial receive buffer.

Am I missing something? Is the buffer maybe cleared before the next loop and therefore Serial.available() > 0 can never be true?

NO! the delay MUST be in the SENDING program!!!

By the "sending" program you are referring to the exe on my PC which I am using to send the int and not something in the Arduino sketch, right?
If so, the exe is only executed ones and only contains one line of code like
"echo 1 > COM5" the other variants I mentioned I tried each separately and manually with their own exes. So there is at least several seconds of no communication to or from the serial port.

Any other ideas?

It seems like I have found a solution myself :stuck_out_tongue:

1. Create a batch file, convert it to an exe
Seemingly stream decks cannot execute batch files directly, therefore you need an exe created like so: How to convert .BAT file to .EXE file in Windows Computer ? - YouTube

The batch file should contain the COMPORT your arduino is connected to (in my case "COM5") and in the second line the int (in my case "1") you want to send. The rest can be left as is.

mode COM5 BAUD=9600 PARITY=n DATA=8
echo 1 > COM5

2. Create a button to trigger the exe via Streamdeck
The streamdeck app contains a button under "Custom Action" called "launch" which can be used to start applicatons

3. Create an Arduino sketch
Here is a shortened version of my arduino sketch only containing the serial communication part:

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    switch (Serial.parseInt()) {
      case 1 :
        button_1_toggle();
        break;
      case 2 :
        button_2_toggle();
        break;
      default: break;
    }
  }
}

void button_1_toggle() {
    Serial.println("You have pressed button 1!");
}

void button_2_toggle() {
    Serial.println("You have pressed button 2!");
}

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