Friday, November 20, 2015

How to make Arduino remotely controlled by Bluetooth or Bluetooth LE using phone - Robomart

Ever wanted to remotely control your Arduino ? Or read sensor values​​, or to make it something ? Maybe even create a remotely controlled robot ? Well now you can. The most basic example will cover; Switch off the LED on  and off .





Step -1: what you will need

  • 1 x 220 ohm (or similar) resistor
  • Assorted wire
  • 1 x smart phone (currently the app we will use only support
  • Android, but we are working on getting it accepted for iOS AppStore).
  • To use with a Bluetooth LE unit, the phone must support Bluetooth LE.
  • 1 x LED
  • 1 x Bluetooth or Bluetooth LE unit. The ones we have tested and works:
  • Bluetooth: HC-05 and HC-06 (will not be supported for iOS)
  • Bluetooth LE: HM-10

Step -2 : Hooking Everything Up 

Only now called BT ) Bluetooth or Bluetooth to connect the unit to :

Step-3: Code to the Arduino

5  ArduinoMobileIntegrationExamples/README.md
@@ -67,6 +67,11 @@ To run in app, copy the following files to the Internet:
An example that shows how a push button may be implemented in the app, and
state reported to an Arduino through a Bluetooth device.

+Two versions of Arduino code:
+* One using `PLabBTSerial.h` located in our `PLabBT` library.
+* One using only `SoftwareSerial.h` and a simple state machine to parse
+incomming text
+
To run in app, copy the following files to the Internet:
* `SimpleButtonExample.pde`


View
114  ...onExample/Arduino/SimpleButtonExampleSoftwareSerial/SimpleButtonExampleSoftwareSerial.ino
@@ -0,0 1,114 @@
/*
 * SimpleButtonExample SoftwareSerial
 *
 * Fjernstyr en LED på Arduine ved hjelp av SoftwareSerial og en tilstandsmaskin.
 *
 * Remotely controlled LED on Arduino using SoftwareSerial and a simple state machine.
 */
#include <SoftwareSerial.h>

// Enum definisjon. Brukt for å parse innkommende tekst
// Enum definition. Used to parse incomming text.
enum ReadState { INIT, O_ACCEPTED, F_ACCEPTED, END };

// State of parser
ReadState myState = INIT;

// Definer inn/utgangspinnene som brukes for send (TX) og motta (RX) for bluetooth
// Define I/O ports used for transmit (TX) and receive (RX)
const int BT_RX = 10;
const int BT_TX = 11;


// Hvilken utgang vi har lysene paa
// Which output we have the lights connected to
const int LIGHT_OUT = 4;


// Definer serieporten for kommunikasjon med bluetooth
// Define the serial port for communication with bluetooth
SoftwareSerial btSerial (BT_RX, BT_TX);

void setup() {
  // Start kommunikasjon med konsoll
  // Start communication throuhg console
  Serial.begin (9600);
  // Linja under trengs kun for Leonardo (kan slettes om du bruker Arduino UNO)
  // The following line is only needed for Leonardo (can be deleted if you are using Arduino UNO)
  while (!Serial);
  Serial.println ("Enkel knapp demonstrasjon med BLUETOOTH");
 
  // Setter utgangen for lyset
  // Set the output for our light
  pinMode (LIGHT_OUT, OUTPUT);
  digitalWrite (LIGHT_OUT, LOW);
 
  // Start kommunikasjonen med bluetooth enhet
  // Start communication with bluetooth unit
  btSerial.begin (9600);
}

void loop() {
  // Sjekker om vi har mottatt noe fra bluetooth enhet
  // See if anything has been received by Bluetooth unit
  if (btSerial.available()) {
    // Les mottatt bokstav
    // Read received character
    char recv = btSerial.read();

    // Gjør valg avhengig av tilstand
    // Do choises dependent on state
    switch (myState)
    {
      // INIT og END tilstand er egentlig like
      // INIT and END state are actually the same
      case INIT:  // Fallthrough
      case END:
        // Første bokstav vi aksepterer er kun en 'O'
        // First accepted is only an 'O'
        if (recv == 'O')
          myState = O_ACCEPTED;
        else
          myState = END;
        break;
      case O_ACCEPTED:
        // Andre bokstav kan enten være en ny 'O', en 'N' eller 'F'
        // Second character can either be an 'O', 'N' or 'F'
        switch(recv) {
          case 'O':
            myState = O_ACCEPTED;
            break;
          case 'N':
            myState = END;
            digitalWrite(LIGHT_OUT, HIGH);
            break;
          case 'F':
            myState = F_ACCEPTED;
            break;
          default:
            myState = END;
        }
        break;
      case F_ACCEPTED:
        // 'O' og 'F' er akseptable bokstaver
        // 'O' and 'F' are acceptable characters
        switch(recv) {
          case 'O':
            myState = O_ACCEPTED;
            break;
          case 'F':
            myState = END;
            digitalWrite(LIGHT_OUT, LOW);
            break;
          default:
            myState = END;
        }
        break;
    }
  }
  // Hvis vi har lyst til aa kunne skrive kommandoer i konsollvinduet, tar vi med dette
  // If we want to be able to write commands in the console window, we include this
  if (Serial.available () > 0) {
    btSerial.write (Serial.read ());
  }
}

When you have selected a version of the code.

  • Connect the Arduino to your computer.
  • Open the source file.
  • Press upload.

Step-4: Download the PLab app




Step-5: Running the sketch

First , sketch app can find it , a place where there should be . This means it must be uploaded to the Internet .
InterfacesInc.pde only need to upload the app so SimpleButtonExample.pde , internally covers
Sketch run , first connect the device to your Bluetooth or Bluetooth
  • Select the device from the list
  • Select wheter you want to connect to a Bluetooth or a Bluetooth LE device. To connect to a Bluetooth unit, this unit has to be paired with your phone. This is not neccessary when working with Bluetooth LE.
  • It is already uploaded here , it should be possible to directly use the upload . Be aware , and feeds into the app . This can be done by :
  • Leave settings andpainstakingly write the entire address into the "Sketch ID" field.
  • Go to settings and delete "URL address base" and "URL address end" 
If you have access to a web area , then use this to store your samples will be very easy to do .
After successfully loading the sketch , a white button should be visible on the brown background . Pressing the button to go dark a

1 comment: