Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

sterowanie diodą - particle & iftt

Ostatnio zmodyfikowano 2020-06-03 19:19
Autor Wiadomość
damian00716
Temat założony przez niniejszego użytkownika
sterowanie diodą - particle & iftt
» 2020-06-03 12:19:19
Witam, chciałbym sterować diodami na RPI za pomocą głosu, jednak mam problem z stworzeniem poprawnego kodu.
Potrafię sterować jedną diodą, ale chcę sterować dwiema niezależnie. - prosiłbym o wskazówki. Nie potrafię rozszerzyć tego programu o kolejną.
Używałem tego poradnika by stworzyć projekt: https://medium.com/@bnarasapur/google-assistant-controlled-home-light-with-a-raspberry-pi-with-ifttt-and-particle-io-c2715b90bd11

kod:
 
C/C++
int relay = D1; //pin to which relay is connected

int relay2 = D2;

int boardLed = D7;
bool vin = LOW; //a virtual boolean variable
bool vin2 = LOW; //a virtual boolean variable
// setup() is run only once, it's where we set up GPIO and //initialise peripherals
void setup() {
   
    // Setup GPIO
    pinMode( relay, OUTPUT ); // relay pin is set as output
    digitalWrite( relay, HIGH );
    // Setup GPIO
    pinMode( relay2, OUTPUT ); // relay pin is set as output
    digitalWrite( relay2, HIGH );
   
    // Subscribe to events published by IFTTT using  Particle.subscribe
    Particle.subscribe( "LED_on", myHandler );
    //turning off function declaration
    Particle.subscribe( "LED_off", thisHandler );
    //turning on function declaration
   
    // Subscribe to events published by IFTTT using  Particle.subscribe
    Particle.subscribe( "LED_on_kitchen", myHandler2 );
    //turning off function declaration
    Particle.subscribe( "LED_off_kitchen", thisHandler2 );
    //turning on function declaration     
   
}

// loop() runs continuously, it's our infinite loop.

void loop() {
    if( vin == HIGH ) {
        digitalWrite( relay, LOW );
       
    }
    else if( vin == LOW ) {
        digitalWrite( relay, HIGH );
    }
   
}

void loop2() {
    if( vin2 == HIGH ) {
        digitalWrite( relay2, LOW );
       
    }
    else if( vin2 == LOW ) {
        digitalWrite( relay2, HIGH );
    }
   
}

//our events are called when IFTTT applets are triggered

void myHandler( const char * event, const char * data ) {
    vin = LOW;
}
void thisHandler( const char * event, const char * data ) {
    vin = HIGH;
}

void myHandler2( const char * event, const char * data ) {
    vin2 = LOW;
}
void thisHandler2( const char * event, const char * data ) {
    vin2 = HIGH;
}
.

Obecny kod to moje nieudolne próby.
P-176955
killjoy
» 2020-06-03 13:03:02
Na oko kod w loop2() nie wykona się nigdy, zresztą masz komentarz
C/C++
// loop() runs continuously, it's our infinite loop.
;)
Dorzuć kod z loop2() do loop() i powinno być ok.

@edit coś się posypał parser STC i z uporem maniaka mi dorzuca domknięty znacznik code, tam gdzie go być nie powinno.
@edit2 znacznik code nie działa inline, to by wiele wyjaśniało.
P-176956
damian00716
Temat założony przez niniejszego użytkownika
» 2020-06-03 13:14:32
Super! Dzięki wielkie killjoypl, działa :)
Jeszcze tematu nie zamykajcie proszę bo może jeszcze o coś będę chiał zapytać.

Tak jak myślałem, przy użyciu

 
Particle.subscribe( "LED_on_kitchen", thisHandler2 );
 
Zapalają się obie diody. A przy użyciu
Led_on
 tylko jedna. W czym może być problem?
P-176957
pekfos
» 2020-06-03 16:55:37
Podaj kod.

coś się posypał parser STC i z uporem maniaka mi dorzuca domknięty znacznik code, tam gdzie go być nie powinno.
Znacznik zamykający kod nie może być w komentarzu.
P-176958
damian00716
Temat założony przez niniejszego użytkownika
» 2020-06-03 17:42:06
C/C++
int relay = D1; //pin to which relay is connected

int relay2 = D2;


int boardLed = D7;
bool vin = LOW; //a virtual boolean variable
bool vin2 = LOW; //a virtual boolean variable
// setup() is run only once, it's where we set up GPIO and //initialise peripherals
void setup() {
   
    // Setup GPIO
    pinMode( relay, OUTPUT ); // relay pin is set as output
    digitalWrite( relay, HIGH );
    // Setup GPIO
    pinMode( relay2, OUTPUT ); // relay pin is set as output
    digitalWrite( relay2, HIGH );
   
    // Subscribe to events published by IFTTT using  Particle.subscribe
    Particle.subscribe( "LED_on", myHandler );
    //turning off function declaration
    Particle.subscribe( "LED_off", thisHandler );
    //turning on function declaration
   
   
    // Subscribe to events published by IFTTT using  Particle.subscribe
    Particle.subscribe( "LED_on_kitchen", myHandler2 );
    //turning off function declaration
    Particle.subscribe( "LED_off_kitchen", thisHandler2 );
    //turning on function declaration     
   
}

// loop() runs continuously, it's our infinite loop.

void loop() {
    if( vin == HIGH ) {
        digitalWrite( relay, LOW );
       
    }
    else if( vin == LOW ) {
        digitalWrite( relay, HIGH );
    }
   
    if( vin2 == HIGH ) {
        digitalWrite( relay2, LOW );
       
    }
    else if( vin2 == LOW ) {
        digitalWrite( relay2, HIGH );
    }
   
}


//our events are called when IFTTT applets are triggered

void myHandler( const char * event, const char * data ) {
    vin = LOW;
}
void thisHandler( const char * event, const char * data ) {
    vin = HIGH;
   
}

void myHandler2( const char * event, const char * data ) {
    vin2 = LOW;
}
void thisHandler2( const char * event, const char * data ) {
    vin2 = HIGH;
}

na LED_off_kitchen, również obie diody gasną
P-176959
killjoy
» 2020-06-03 18:24:27
Z dokumentacji:

A subscription works like a prefix filter. If you subscribe to "foo", you will receive any event whose name begins with "foo", including "foo", "fool", "foobar", and "food/indian/sweet-curry-beans". The maximum length of the subscribe prefix is 64 characters.

Zatem dla podanego kodu, zmiana nazwy eventów w taki sposób, żeby nie miały wspólnego początku powinna wystarczyć. Można też sprawdzać argument 'event' w callbacku, który odbierałby wszystkie "LED_on" i "LED_off" i na podstawie jego wartości (pełnej nazwy eventu) decydować co robić.

@pekfos

Znacznik zamykający kod nie może być w komentarzu.
Trochę to nieintuicyjne, zwłaszcza, że wstrzykiwało mi znacznik na koniec posta, ale fair enough.
P-176960
damian00716
Temat założony przez niniejszego użytkownika
» 2020-06-03 18:49:31
I znów znalazłeś rozwiązanie.. killjoypl działa! :)
P-176961
pekfos
» 2020-06-03 19:19:02
Trochę to nieintuicyjne
C/C++
printf( "[/cpp][/code]" ); // wypisuje [/cpp][/code]
Aktualny parser dokłada na koniec brakujące zamknięcia do znaczników.
P-176962
« 1 »
  Strona 1 z 1