Author Topic: Arduino Plugin  (Read 22176 times)

0 Members and 1 Guest are viewing this topic.

laksmanx

  • Jr. Member
  • **
  • Posts: 3
  • Karma: 0
    • View Profile
Re: Arduino Plugin
« Reply #30 on: June 21, 2015, 04:12:04 AM »
Hi James

Thank you for the advice on VC responding to inputs on the Arduino.

It all makes a great deal of sense now.

By triggering events with digital inputs on Arduino, one can literally do anything. I went from the idea of VC letting me know when the front door or garage door is opening, to a multimedia interface where i can play, pause and skip music from my entertainment area with buttons or voice.

Here is the code I used to trigger multiple inputs.

Code: [Select]
String data="";  //used to receive serial messages
const int buttonPin1 =2;
const int buttonPin2 =3;
const int buttonPin3 =4;
const int buttonPin4 =5;
bool flag_1 = 0;
bool flag_2 = 0;
bool flag_3 = 0;
bool flag_4 = 0;
bool buttonState_1 = 0;
bool buttonState_2 = 0;
bool buttonState_3 = 0;
bool buttonState_4 = 0;
void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  Serial.begin(9600);
}

void loop() {
  delay(100);
  int oneButtonState = digitalRead(buttonPin1);
 
  if (oneButtonState != buttonState_1) {
    if(oneButtonState)
    {
       if(flag_1 == 0)
       {
       Serial.println("VC.TriggerEvent&&ButtonOne.Pressed");
       flag_1 = 1;
       }
    }
    else
    {
       if(flag_1 == 1)
          {
            Serial.println("VC.TriggerEvent&&ButtonOne.State.Released");
            flag_1 = 0;
          }
    }
    buttonState_1 = oneButtonState;
  }
  delay(100);
  int twoButtonState = digitalRead(buttonPin2);
  if (twoButtonState != buttonState_2) {
    if(twoButtonState)
    {
      if(flag_2 == 0)
        {
         Serial.println("VC.TriggerEvent&&ButtonTwo.Pressed");
         flag_2 = 1;
        }
    }
    else
    {
      if(flag_2 == 1)
        {
         Serial.println("VC.TriggerEvent&&ButtonTwo.Released");
         flag_2 = 0;
        } 
    }
    buttonState_2 = twoButtonState;
  }
 
  delay(100);
  int threeButtonState = digitalRead(buttonPin3);
  if (threeButtonState != buttonState_3) {
    if(threeButtonState)
    {
      if(flag_3 == 0)
        {
         Serial.println("VC.TriggerEvent&&ButtonThree.Pressed");
         flag_3 = 1;
        }
    }
    else
    {
      if(flag_3 == 1)
        {
         Serial.println("VC.TriggerEvent&&ButtonThree.Released");
         flag_3 = 0;
        } 
    }
    buttonState_3 = threeButtonState;
  }
delay(100);
  int fourButtonState = digitalRead(buttonPin4);
  if (fourButtonState != buttonState_4) {
    if(fourButtonState)
    {
      if(flag_4 == 0)
        {
         Serial.println("VC.TriggerEvent&&ButtonFour.Pressed");
         flag_4 = 1;
        }
    }
    else
    {
      if(flag_4 == 1)
        {
         Serial.println("VC.TriggerEvent&&ButtonFour.Released");
         flag_4 = 0;
        } 
    }
    buttonState_4 = fourButtonState;
  }
}

void serialEvent() {
  data="";
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the data:
    if (inChar == (char)10)
    {
      break; //break message on newline
    }
    data += inChar;
  }
  //data should be ready now.  What should we do with it?

  if (data=="hello")
  {
    Serial.println("tts.speak&&hi there");
  }

  //Analog Read - Pin #
  else if (data.startsWith("ar."))
  {
    String strPin = data.substring(3);
    int whatPin = atoi(strPin.c_str());
    int v = analogRead (whatPin);
    Serial.println("VC.TriggerEvent&&Analog.Pin&&"+strPin+"&&"+v);
  }

  //DigitalWrite HIGH - Pin #
  else if (data.startsWith("dw.high."))
  {
    String strPin = data.substring(8);
    int whatPin = atoi(strPin.c_str());
    pinMode(whatPin, OUTPUT);
    digitalWrite (whatPin,HIGH);
  }

  //DigitalWrite LOW - Pin #
  else if (data.startsWith("dw.low."))
  {
    String strPin = data.substring(7);
    int whatPin = atoi(strPin.c_str());
    pinMode(whatPin, OUTPUT);
    digitalWrite (whatPin,LOW);
  }

  //unknown command
  else
  {
    Serial.println("VC.TriggerEvent&&Arduino.error&&"+data);
  }

  //clear data for next time
  data="";
}










mexicanto

  • Contributor
  • ***
  • Posts: 55
  • Karma: 1
    • View Profile
Re: Arduino Plugin
« Reply #31 on: December 19, 2015, 04:06:45 AM »
Hi James,

I already did the 2 examples for arduino (relays and led/analog), and both worked perfectly,
but in both VC is triggering the accions

Is there a chance for you to post a example for trigger a event or two for buttons in arduino as a
starting point?
my main goal is, for example, if somebody ring the bell on the main door, the arduino can
"speak" a message: "the door bell is ringing", and a osd.text display in the screen
and have another button in the same sketch, for example a motion sensor in the back patio
that trigger a speak and a message on the screen

in this example, can you include the PY code, the arduino code and the XML code?


but, if is this not a big problem, the best could be have a complete example, with a couple of buttons, a couple
of LEDs, and a couple of potentiometres, with the PY, Arduino and XML code

Thank you very much in advance

Daniel

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Arduino Plugin
« Reply #32 on: December 19, 2015, 11:09:49 AM »
Sounds like you would benefit from using "MySensors".  Try doing a search for it on this forum.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Arduino Plugin
« Reply #33 on: December 19, 2015, 11:11:16 AM »

Is there a chance for you to post a example for trigger a event or two for buttons in arduino as a
starting point?

That is exactly what the post immediately before your question shows how to do!

http://voxcommando.com/forum/index.php?topic=1452.msg19237#msg19237

mexicanto

  • Contributor
  • ***
  • Posts: 55
  • Karma: 1
    • View Profile
Re: Arduino Plugin
« Reply #34 on: December 19, 2015, 07:09:57 PM »
Thanks for the quick response, James

I am just scratching the surface of VC, and i am very happy with it

I checked mysensors before, and is very interesting too
when i asked for an example of two buttons, i was thinking in a whole example
like what you did with the relays, LED and analog input reading, with
the arduino sketch, the PY source and the XML, and from there, modify as my needs
as i said, i just start digging in VC, can you somebody give me a hand with that ?

so far, what i was doing is modify the examples of the XML to suit my needs,
for me is easiest in that way, than start from scratch.

I also have the amazon echo, and this device is superb on voice recognition, BUT...
it is lacking the functions to "ask" values of sensors, triggers events (you can do it
with IFTT, but takes forever to have a response), and in this field, VC is perfect

greetings from (not so) sunny California

Daniel



mexicanto

  • Contributor
  • ***
  • Posts: 55
  • Karma: 1
    • View Profile
Re: Arduino Plugin
« Reply #35 on: December 24, 2015, 12:33:39 AM »
any help with this ??
an example with:

a PY code, arduino code and xml code for a two button in arduino triggering action in VC ?

thank you

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: Arduino Plugin
« Reply #36 on: December 24, 2015, 01:49:50 AM »
Hi mexicanto, the example is already given here: http://voxcommando.com/forum/index.php?topic=1452.msg12742#msg12742
***********  get excited and make things  **********

mexicanto

  • Contributor
  • ***
  • Posts: 55
  • Karma: 1
    • View Profile
Re: Arduino Plugin
« Reply #37 on: December 24, 2015, 05:50:24 AM »
Thank you Kalle

I did both examples already (relay control and reading the state of a pin)
but both examples are triggered from VC to the arduino
i am looking for the opposite,
That is, arduino trigger an event in VC

lets say that i have two buttons in arduino, when i press one, i expect VC to trigger an event, lets say
an osd.showtext "button 1 have been pressed", the same for button 2

I learn quickly just modifying examples, for that, if somebody can help me
with the arduno code, the PY code and the XML code, i will appreciate that

thank you again

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: Arduino Plugin
« Reply #38 on: December 24, 2015, 08:18:41 AM »
It looks like you did not read also the next post in the link above  ;)


The python script listen to the Arduino port, so you can put in your Arduino code something like this: Serial.println("VC.TriggerEvent&&I am Event&&and am I a payload").


It is better you read all the posts in this thread, because there are some helpful information and also the same question as yours:
http://voxcommando.com/forum/index.php?topic=1452.msg19182#msg19182


I have no example script here, but if you take a look in the "SerialTestJames.ino" you will find it at line 36. We have a "ready" to use python script for the MySensors API, with this script VC can communicate with the MySensors stuff (MySensors-Gateway is needed)
« Last Edit: December 24, 2015, 08:29:27 AM by Kalle »
***********  get excited and make things  **********

mexicanto

  • Contributor
  • ***
  • Posts: 55
  • Karma: 1
    • View Profile
Re: Arduino Plugin
« Reply #39 on: December 24, 2015, 10:51:12 AM »
Hi Kalle,

I read all the post at least twice, the PY code is there, the arduino sketch is there, if only
somebody help me with the XML code as an example, it will help me a lot

I am pretty new to VC, less than a month using it, 2 weeks as a licensed user

after finish the button testing, i will jump into mysensors to implement it on home automation
i have a little dog as a roommate, some times he is alone most of the day, i have surveillance cameras
to keep an eye on her, but my first goal is to have a monitoring system, to alert me (via email, i have that cover
with VC) in case of fire or flood, i can attach the sensors to arduino, and the arduino can trigger the VC

thanks again

mexicanto

  • Contributor
  • ***
  • Posts: 55
  • Karma: 1
    • View Profile
Re: Arduino Plugin
« Reply #40 on: December 24, 2015, 10:59:50 AM »
I forgot to ask you... when is the mysensors-gateway will be ready for purchase?

thanks again

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: Arduino Plugin
« Reply #41 on: December 24, 2015, 11:14:06 AM »
Yes, I have here some MySensors-Gateways, but without any housing - write me a PM with your address.  ;)


When you ask for a xml, you mean a command which contain trigger event?
I will look into or maybe James or Dave can help, because they has even more experience  ::)
***********  get excited and make things  **********

mexicanto

  • Contributor
  • ***
  • Posts: 55
  • Karma: 1
    • View Profile
Re: Arduino Plugin
« Reply #42 on: December 24, 2015, 11:24:18 AM »
Yes, the XML that goes into the command tree, with the instructions for, lest say:

osd.showtext "button one has been pressed"

osd.showtext "button two has been pressed"

I will pm you with my info

thank you

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Arduino Plugin
« Reply #43 on: December 24, 2015, 12:05:12 PM »
The sample above generates events in VC when a button is pressed on your Arduino.  An event can trigger any command in VC.

It sounds like maybe you need to learn how to use events.  Here is the information you need to use events, which by the way is a core concept that you will need to understand in order to make full use of VoxCommando's potential:
http://voxcommando.com/mediawiki/index.php?title=Events
(Note that there are video tutorials on that wiki page.)

If you do not learn how to use events in VoxCommando, then you should not even consider using MySensors because they generate events.
« Last Edit: December 24, 2015, 12:09:18 PM by nime5ter »

mexicanto

  • Contributor
  • ***
  • Posts: 55
  • Karma: 1
    • View Profile
Re: Arduino Plugin
« Reply #44 on: December 24, 2015, 12:20:29 PM »
Thanks James & Kalle

I think i finally doing some progress, i did some test with vc.triggerevent
i can now fire one event based in other command

i will probably post the XML for the buttons in a few days (hours).

have all of you a happy holidays