Info su moduli bluethoot e Arduino (che poi sarà un Attiny)

Buona sera a tutti (e ancora tanti auguri per l'anno nuovo), sono qua con un piccolo dilemma riguardante dei moduli Bluethoot. Nel particolare ho 2 moduli RN-42 e li vorrei collegare uno ad un arduino e l'atro (momentaneamente) ad un'altro arduino ma che poi sarà un attiny85. Vorrei inviare i dati letti dagli ingressi del tiny via bluethoot all'altro arduino.

Ho trovato questo documento http://www.fut-electronics.com/wp-content/plugins/fe_downloads/Uploads/Communication%20Bet%202%20Arduino%20using%20Bluetooth.pdf e questo codice http://www.seeedstudio.com/wiki/images/3/30/BluetoothShieldDemoCode.zip

documenti e sketch li trovo molto interessanti ma non ho ancora capito se posso collegare direttamente i moduli ad arduino o devo inserire qualcosa tra i due per i livelli logoci. Chiedo scusa ma sono un po' a secco con le comunicazioni seriali.... è la prima volta che provo a giocarci

Fabio

Prima nota: allo stato attuale la NewSoftSerial non funziona correttamente sugli Attiny85. In particolare non funziona la ricezione. Puoi usare in alternativa la SoftwareSerial ma blocchi l'esecuzione del codice in ricezione.

Detto questo, vedo che nel datasheet dello shield c'è scritto che quel modulo usa anche i pin D8/D9 e A4/A5 (I2C) oltre alla comunicazione seriale. Non vorrei che non fosse possibile pilotarlo con l'Attiny85.

Il problema della NewSoftSerial e della SoftwareSerial li conoscevo già, nella tua guida ormai diventata una bibbia dell'attiny, lo ripeti più di una volta. Grazie proprio alla tua guida sono riuscito a trasmettere dati tra attiny e aduino tramite la seriale e adesso volevo sostituire i due cavi (RX e TX) con due moduli bluetooth

Mi sono dimenticato di specificare che l'attiny funzionerebbe soltanto in trasmissione, in quanto deve inviare i dati letti dalle porte analogche, e l'arduino deve solo riceve.

Il datasheet che ho postato, l'ho messo solo per far capire su che documento mi sono basato, l'unico che sono riuscito a trovare nella rete e soprattutto che mi spiegasse come funzionano i comandi AT di comunicazione

Il mio modulo bluetooth è un RN42.

Fabio

Posso aiutarti a livello teorico, non pratico non avendo mai usato moduli bluetooth con l'Arduino.
Di quanti pin necessiti per gestire un modulo bluetooth?
Uno sketch "tipo" compilato, quanta memoria occupa?

Ovviamente ti devi procurare due moduli bluetooth, ma uno deve essere master e l'altro slave, di solito tutti i moduli sono slave, solo alcuni se impostati correttamente diventano master.
Prima di acquistarli controlla che sia possibile attivare la funzione master tramite i comandi at.

I due moduli sono sono configurabili tramite il pin 3 (PIO6) come master o slave a questo punto devo sperare di poterlo fare anche con i comandi at.
Attualmente nel tiny è caricato lo sketch (4422 bytes):

#include <NewSoftSerial.h>  //importiamo la libreria per la seriale sw 

#define temp 2
#define RX 0
#define TX 1

NewSoftSerial mySerial(RX, TX);

void setup()  
{ 
  delay(5000);  //aspetto 5 secondi e poi si parte :) 
  pinMode(temp, INPUT);  //predisponiamo il pin 7 dell'attiny85 come input dell'adc 
  mySerial.begin(9600);
} 

void loop()                      
{ 
  int adc = analogRead(temp) ;
  float adcV = (adc * 5.0 / 1023) ;
  float tempC = adcV * 100;
  
  mySerial.print("name=AT01&level=");  
  mySerial.print(adcV);
  mySerial.print("&temp=");
  mySerial.print(tempC);
  delay(10000) ;
}

e nell'arduino (2334 bytes):

char myCmd[128];
int inputSize=0;

void setup()
{
 Serial.begin(9600);
 Serial.println("Ready:");
}

void loop()
{
 inputSize=0;
 if (Serial.available() > 0) 
{
   delay(300);
   inputSize = Serial.available();
   Serial.print("inputSize=");
   Serial.println(inputSize);
   for (int i = 0; i < inputSize; i++)
   {
      myCmd[i] = Serial.read();
   }
   Serial.println(myCmd);
 }
}

questo però collegando tramite cavo i due componenti, cosa che mi piacerebbe poter sostituire con i moduli.

Posto gli sketch trovati in rete per i moduli Master/slave
slave:

#include <NewSoftSerial.h>   //Software Serial Port
#define RxD 0
#define TxD 1
 
#define DEBUG_ENABLED  1
 
NewSoftSerial blueToothSerial(RxD,TxD);
 
void setup() 
{ 
  Serial.begin(9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
 
} 
 
void loop() 
{ 
  char recvChar;
  while(1){
    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
      recvChar = blueToothSerial.read();
      Serial.print(recvChar);
    }
    if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
      recvChar  = Serial.read();
      blueToothSerial.print(recvChar);
    }
  }
} 
 
void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=RN42-4928\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable 
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

Master:

#include <NewSoftSerial.h>   //Software Serial Port
#define RxD 6
#define TxD 7
 
#define DEBUG_ENABLED  1

String retSymb = "+RTINQ=";//start symble when there's any return
String slaveName = ";RN42-4928";// caution that ';'must be included, and make sure the slave name is right.
int nameIndex = 0;
int addrIndex = 0;

String recvBuf;
String slaveAddr;

String connectCmd = "\r\n+CONN=";

NewSoftSerial blueToothSerial(RxD,TxD);
 
void setup() 
{ 
  Serial.begin(9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  //wait 1s and flush the serial buffer
  delay(1000);
  Serial.flush();
  blueToothSerial.flush();
} 
 
void loop() 
{
  char recvChar;
  while(1){
    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
      recvChar = blueToothSerial.read();
      Serial.print(recvChar);
    }
    if(Serial.available()){//check if there's any data sent from the local serial terminal, you can add the other applications here
      recvChar  = Serial.read();
      blueToothSerial.print(recvChar);
    }	
 } 
} 
 
void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=1\r\n");//set the bluetooth work in master mode
  blueToothSerial.print("\r\n+STNA=RN42-4924\r\n");//set the bluetooth name as "SeeedBTMaster"
  blueToothSerial.print("\r\n+STAUTO=0\r\n");// Auto-connection is forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.flush();
  blueToothSerial.print("\r\n+INQ=1\r\n");//make the master inquire
  Serial.println("Master is inquiring!");
  delay(2000); // This delay is required.
    
  //find the target slave
  char recvChar;
  while(1){
    if(blueToothSerial.available()){
      recvChar = blueToothSerial.read();
      recvBuf += recvChar;
      nameIndex = recvBuf.indexOf(slaveName);//get the position of slave name
      //nameIndex -= 1;//decrease the ';' in front of the slave name, to get the position of the end of the slave address
      if ( nameIndex != -1 ){
        //Serial.print(recvBuf);
 	addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()- 18) ) + retSymb.length());//get the start position of slave address	 		
 	slaveAddr = recvBuf.substring(addrIndex, nameIndex);//get the string of slave address 			
 	break;
      }
    }
  }
  //form the full connection command
  connectCmd += slaveAddr;
  connectCmd += "\r\n";
  int connectOK = 0;
  Serial.print("Connecting to slave:");
  Serial.print(slaveAddr);
  Serial.println(slaveName);
  //connecting the slave till they are connected
  do{
    blueToothSerial.print(connectCmd);//send connection command
    recvBuf = "";
    while(1){
      if(blueToothSerial.available()){
        recvChar = blueToothSerial.read();
 	recvBuf += recvChar;
 	if(recvBuf.indexOf("CONNECT:OK") != -1){
          connectOK = 1;
 	  Serial.println("Connected!");
 	  blueToothSerial.print("Connected!");
 	  break;
 	}else if(recvBuf.indexOf("CONNECT:FAIL") != -1){
 	  Serial.println("Connect again!");
 	  break;
 	}
      }
    }
  }while(0 == connectOK);
}

volevo allegare il datasheet del modulo rn-42 ma "l'upload folder is full", posto il link:
http://www.rovingnetworks.com/files/resources/Bluetooth-RN-42-DS.pdf

UPUP, mi spiace fare UP, ma non ho più ricevuto consigli o dritte su come procedere...

Anche dopo la modifica di Leo sulla libreria NewSoftSerial per permettere agli attiny di ricevere dati dalla seriale.

Riposto il dilemma: Ho 2 moduli bluetooth (RN-42) e 2 arduino, posso collegarli direttamente RX-TX e TX-RX o devo mettere qualcosa in mezzo per ridimensionare i livelli logici???

Il dubbio mi è venuto vedendo in rete le schede BlueSmirf

Grazie

Mi pareva che BigJohnson ti avesse risposto. Io non so darti aiuto sui mod. bluetooth: mai usati.