Comunicazione APC220

Salve, ho acquistato i moduli APC220 per avere una comunicazione seriale wireless con arduino leonardo.

Vi riporto i collegamenti che ho fatto:

APC220     A. Leonardo

GND    -->  GND
VCC    -->  VCC (5v)
TX     -->  RX (pin 0)
RX     -->  TX (pin 1)

lo sketch che sto usando è il seguente:

boolean stato = false;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
if(Serial.available())
  {
    char s = Serial.read();
    
    switch(s)
    {
        case 'a': stato = !stato;
                  digitalWrite(13, stato);
                  break;                   
    }
  }
}

Purtroppo non funziona nulla. Preciso che entrambi i moduli sono settati sulla stessa frequanza e netID tramite il software RF-Magic.

Ho anche provato ad utilizzare la Software Serial ma niente..

Sapete dirmi dove sbaglio?

Nessuno che sa aiutarmi? ::slight_smile:

ote that on the Leonardo, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class

Brunello:
ote that on the Leonardo, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class

I've tried with Serial1 class, but nothing happens

EDIT:

Now it works perfectly. The error was that the transmitters were working to 57600bps, while in the sketch I had initialized communication at 9600bps. :P

Thanks so much for the help! ;)

This sketch is correct:

boolean stato = false;

void setup() {
  Serial1.begin(57600);
  pinMode(13, OUTPUT);
}

void loop() {
if(Serial1.available())
  {
    char s = Serial1.read();
    
    switch(s)
    {
        case 'a':   stato = !stato;
                    digitalWrite(13, stato);
                    break;                   
    }
  }
}

PS Because if i use the class Serial1, I can not send/receive commands via the serial monitor? :-\

I am forced to use the command prompt windows ( echo "command" > comN )

Qualcuno che mi chiarisca quest'ultimo dubbio? :grinning:

La Leonardo è un pò strana e non ricordo come è messa a seriali, però se è uguale alla UNO, la seriale che comunica con Il PC tramite Il chip FTDI232 o l'atmeghino minuscolo è la 0 (zero), non la uno.

Quindi se vuoi vedere i dati seriali acquisiti dal APC220 sulla Serial1, devi fare uno switch dei dati verso la seriale zero, cioè la Serial e basta (senza 0 o 1 dopo Serial).

La Leonardo usa una porta seriale virtuale per comunicare con il PC, simulata via software.

La porta "tradizionale" UART sui pin 0 ed 1 è invece la Serial1.

Per inviare comandi alla porta Serial1, devi intercettare a livello software la comunicazione su Serial(0) ed inviarla alla Serial1: in gergo informatico si dice fare un "bridge".

Perfetto, grazie mille per i chiarimenti! :wink: