Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Bluetooth iPhone & Arduino

Linguaggi e sistemi

Moderatori: Foto UtentePaolino, Foto Utentefairyvilje

0
voti

[1] Bluetooth iPhone & Arduino

Messaggioda Foto UtenteLuca1995 » 23 gen 2016, 1:33

Penso che sto per chiedere qualcosa di oscuro e complicato.....
Qualcuno di voi ha avuto esperienza nell'interfacciare un iPhone con BLE ad Arduino tramite bluetooth?? La documentazione che ho letto mi ha lasciato mezzo morto. Vi scrivo dopo averle provate praticamente tutte.
Ho seguito chili e chili di guide, ora ne sto provando una che da un semplice slider invii la posizione dello slider stesso all'Arduino....
Questa è la parte di codice del cellulare del file RTWViewController.m
Codice: Seleziona tutto
#import "RWTViewController.h"
#import "BTDiscovery.h"
#import "BTService.h"

@interface RWTViewController ()
@property (strong, nonatomic) NSTimer *timerTXDelay;
@property (nonatomic) BOOL allowTX;
@end

@implementation RWTViewController

#pragma mark - Lifecycle

- (void)viewDidLoad {
  [super viewDidLoad];
 
 
  // Rotate slider to vertical position
  UIView *superView = self.positionSlider.superview;
  [self.positionSlider removeFromSuperview];
  [self.positionSlider removeConstraints:self.view.constraints];
  self.positionSlider.translatesAutoresizingMaskIntoConstraints = YES;
  self.positionSlider.transform = CGAffineTransformMakeRotation(M_PI_2);
  [superView addSubview:self.positionSlider];
 
  // Set thumb image on slider
  [self.positionSlider setThumbImage:[UIImage imageNamed:@"Bar"] forState:UIControlStateNormal];
 
  self.allowTX = YES;
 
  // Watch Bluetooth connection
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectionChanged:) name:RWT_BLE_SERVICE_CHANGED_STATUS_NOTIFICATION object:nil];
 
  // Start the Bluetooth discovery process
  [BTDiscovery sharedInstance];
}

- (void)dealloc {
 
  [[NSNotificationCenter defaultCenter] removeObserver:self name:RWT_BLE_SERVICE_CHANGED_STATUS_NOTIFICATION object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
 
  [self stopTimerTXDelay];
}

#pragma mark - IBActions

- (IBAction)positionSliderChanged:(UISlider *)sender {
  // Since the slider value range is from 0 to 180, it can be sent directly to the Arduino board
 
  [self sendPosition:(uint8_t)sender.value];
}

- (IBAction)clickCicca:(UIButton *)sender {
   
}

#pragma mark - Private

- (void)connectionChanged:(NSNotification *)notification {
  // Connection status changed. Indicate on GUI.
  BOOL isConnected = [(NSNumber *) (notification.userInfo)[@"isConnected"] boolValue];
 
  dispatch_async(dispatch_get_main_queue(), ^{
    // Set image based on connection status
    self.imgBluetoothStatus.image = isConnected ? [UIImage imageNamed:@"Bluetooth_Connected"]: [UIImage imageNamed:@"Bluetooth_Disconnected"];
   
    if (isConnected) {
      // Send current slider position
      [self sendPosition:(uint8_t)self.positionSlider.value];
    }
  });
}

- (void)sendPosition:(uint8_t)position {
    // Valid position range: 0 to 180
    static uint8_t lastPosition = 0;
    NSLog(@"Sto per inviare qualcosa");
   
    if (!self.allowTX) { // 1
        NSLog(@"TXnotAllowed\n\n");
        return;
    }
   
    // Validate value
    if (position == lastPosition) { // 2
        return;
    }
    else if ((position < 0) || (position > 180)) { // 3
        return;
    }
   
    // Send position to BLE Shield (if service exists and is connected)
    if ([BTDiscovery sharedInstance].bleService) { // 4
        NSLog(@"Spedisco");
        [[BTDiscovery sharedInstance].bleService writePosition:position];
        lastPosition = position;
       
        // Start delay timer
        self.allowTX = NO;
        if (!self.timerTXDelay) { // 5
            self.timerTXDelay = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerTXDelayElapsed) userInfo:nil repeats:NO];
        }
    }
}

- (void)timerTXDelayElapsed {
  self.allowTX = YES;
  [self stopTimerTXDelay];
 
  // Send current slider position
  [self sendPosition:(uint8_t)self.positionSlider.value];
}

- (void)stopTimerTXDelay {
  if (!self.timerTXDelay) {
    return;
  }
 
  [self.timerTXDelay invalidate];
  self.timerTXDelay = nil;
}

@end


Ora la semplice parte di arduino che dovrebbe ricevere
Codice: Seleziona tutto
// Arduino Bluetooth LE Servo Controlled by iOS

#include <Servo.h>
#include <SoftwareSerial.h>

int LED = 13;     // Most Arduino boards have an onboard LED on pin 13
Servo myservo;    // Create servo object to control the servo

SoftwareSerial BLE_Shield(4,5);  // Configure the Serial port to be on pins D4 and D5. This
                                 // will match the jumpers on the BLE Shield (RX -> D4 & TX -> D5).

void setup()  // Called only once per startup
{
  pinMode(LED, OUTPUT);  // Set pin as an output
  digitalWrite(LED, HIGH);  // Turn on LED (ie set to HIGH voltage)

  myservo.attach(9);        // Attach the servo object to pin 9
  myservo.write(0);         // Initialize servo position to 0
 
  BLE_Shield.begin(9600);   // Setup the serial port at 9600 bps. This is the BLE Shield default baud rate.
}

void loop() // Continuous loop
{
  // See if new position data is available
  if (BLE_Shield.available()) {
    myservo.write(BLE_Shield.read());  // Write position to servo
  }
}

Ho cambiato gli UUID in modo che ora sono compatibili, infatti l'app del cell segna Connected. Ma non riesco ad inviare neanche mezzo byte.
Se qualcuno ha esperienza mi basterebbe che mi dicesse anche solo le operazioni fondamentali per inviare un singolo bit dal cell all'Arduino. Poi il resto intorno lo farei al volo. Questo Core Bluetooth sta diventando il mio incubo!!
Grazie mille per l'attenzione! Notte :shock:
Avatar utente
Foto UtenteLuca1995
790 2 6 12
Frequentatore
Frequentatore
 
Messaggi: 200
Iscritto il: 6 gen 2013, 23:17

Torna a PC e informatica

Chi c’è in linea

Visitano il forum: Nessuno e 16 ospiti