Alexis Santiago Allende
Published © GPL3+

Arduino 101 Connects with Raspberry Pi Zero W

Connecting devices is the way to make IoT devices. Here I show the how to connect my Arduino 101 board with the recent Raspberry Pi.

IntermediateFull instructions provided2 hours12,316
Arduino 101 Connects with Raspberry Pi Zero W

Things used in this project

Hardware components

Arduino 101 & Genuino 101
Arduino 101 & Genuino 101
×1
Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
USB-A to B Cable
USB-A to B Cable
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

My circuit

Code

Arduino code

Arduino
#include <CurieBLE.h>

BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService ledService("19b10000-e8f2-537e4f6c-d104768a1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic switchCharacteristic("19b10001-e8f2-537e4f6c-d104768a1214",  // standard 16-bit characteristic UUID
    BLERead | BLENotify); 

const int ledPin = 13; // pin to use for the LED
int oldValue = 0;  // last battery level reading from analog input
long previousMillis = 0;  // last time the battery level was checked, in ms


void setup() {
  Serial.begin(9600);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // set advertised local name and service UUID:
  blePeripheral.setLocalName("LED");
  blePeripheral.setAdvertisedServiceUuid(ledService.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(ledService);
  blePeripheral.addAttribute(switchCharacteristic);

  // set the initial value for the characeristic:
  switchCharacteristic.setValue(0);

  // begin advertising BLE service:
  blePeripheral.begin();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
         long currentMillis = millis();
      // if 200ms have passed, check the battery level:
      if (currentMillis - previousMillis >= 1000) {
        previousMillis = currentMillis;
        updateSensor();
      }

     
      }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}

void updateSensor(){

  int sensorValue = analogRead(A0);
  int sensorLevel = map(sensorValue, 0, 1023, 0, 255);
  switchCharacteristic.setValue(sensorLevel);
  }

Node.js Code

JavaScript
var noble = require('noble');

// Search only for the Service UUID of the device (remove dashes)
var serviceUuids = ['19b10000e8f2537e4f6cd104768a1214'];

// Search only for the led charateristic
var characteristicUuids = ['19b10001e8f2537e4f6cd104768a1214'];

// start scanning when bluetooth is powered on
noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    noble.startScanning(serviceUuids);
  } else {
    noble.stopScanning();
  }
});

// Search for BLE peripherals
noble.on('discover', function(peripheral) {
  peripheral.connect(function(error) {
    console.log('connected to peripheral: ' + peripheral.uuid);
    // Only discover the service we specified above
    peripheral.discoverServices(serviceUuids, function(error, services) {
      var service = services[0];
      console.log('discovered service');

      service.discoverCharacteristics(characteristicUuids, function(error, characteristics) {
        console.log('discovered characteristics');
        // Assign Characteristic
        var ledCharacteristic = characteristics[0];
        
        setInterval(function() {
           
            ledCharacteristic.read(function(error, data) {
            // data is a buffer
            console.log('sensor value is: ' + data.readUInt8(0));
                });
            
        }, 1000);

      });
    });
  });
});

Credits

Alexis Santiago Allende

Alexis Santiago Allende

0 projects • 59 followers
Im a person who since young feel a passion for electronics, I also like to cook pizza and travel. Now Im working on the internet of things

Comments